Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/ConvertP12Test.java
41153 views
/*1* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import static java.lang.System.out;2425import java.io.ByteArrayInputStream;26import java.io.File;27import java.io.FileOutputStream;28import java.nio.file.Files;29import java.nio.file.Paths;30import java.security.Key;31import java.security.KeyStore;32import java.security.KeyStoreException;33import java.security.NoSuchAlgorithmException;34import java.security.UnrecoverableKeyException;35import java.security.cert.Certificate;36import java.util.Arrays;37import java.util.Base64;38import java.util.Enumeration;3940/*41* @test42* @bug 804861943* @author Bill Situ44* @summary Test converting keystore from jceks to P12 and from P12 to other45* (jceks,jks). including following test cases:46* Read jceks key store and convert to the p12 key store, then compare entries47* in the two key stores.48* Read p12 key store and convert to the jceks key store, then compare entries49* in the two key stores.50* Read p12 key store (contains only private key and a self-signed certificate)51* and convert to the jceks key store, then compare entries of two key stores.52* Read p12 key store (contains 2 entries) and convert to the jceks key store,53* then compare entries in the two key stores.54* Read p12 key store (entry password and key store password are different) and55* convert to the jceks key store, then compare entries in the two key stores.56* Read p12 key store and convert to the jks key store, then compare entries57* in the two key stores.58* Read p12 key store (contains only private key and a self-signed certificate)59* and convert to the jks key store, then compare entries in the two key stores.60* Read p12 key store (contains 2 entries) and convert to the jks key store,61* then compare entries in the two key stores.62* Read p12 key store (entry password and key store password are different) and63* convert to the jks key store, then compare entries in the two key stores.64*/6566public class ConvertP12Test {6768private static final String SUN_JSSE = "SunJSSE";69private static final String SUN_JCE = "SunJCE";70private static final String SUN = "SUN";71private static final String PKCS12 = "pkcs12";72private static final String JCE_KS = "JceKS";73private static final String JKS = "JKS";7475public static void main(String args[]) throws Exception {7677ConvertP12Test jstest = new ConvertP12Test();7879jstest.driver("JceksToP12", "keystoreCA.jceks.data", JCE_KS, SUN_JCE,80"storepass", "keypass", PKCS12, SUN_JSSE);8182jstest.driver("P12ToJceks_Chain", "ie_jceks_chain.pfx.data", PKCS12,83SUN_JSSE, "pass", "pass", JCE_KS, SUN_JCE);8485jstest.driver("P12ToJceks_SelfSigned", "jdk_jceks_selfsigned.p12.data",86PKCS12, SUN_JSSE, "pass", "pass", JCE_KS, SUN_JCE);8788jstest.driver("P12ToJceks_TwoEntry", "jdk_jceks_twoentry.p12.data",89PKCS12, SUN_JSSE, "pass", "pass", JCE_KS, SUN_JCE);9091jstest.driver("P12ToJceks_TwoPass", "jdk_jceks_twopass.p12.data",92PKCS12, SUN_JSSE, "storepass", "keypass", JCE_KS, SUN_JCE);9394jstest.driver("P12ToJks_Chain", "ie_jks_chain.pfx.data", PKCS12,95SUN_JSSE, "pass", "pass", JKS, SUN);9697jstest.driver("P12ToJks_SelfSigned", "jdk_jks_selfsigned.p12.data",98PKCS12, SUN_JSSE, "pass", "pass", JKS, SUN);99100jstest.driver("P12ToJks_TwoEntry", "jdk_jks_twoentry.p12.data", PKCS12,101SUN_JSSE, "pass", "pass", JKS, SUN);102103jstest.driver("P12ToJks_TwoPass", "jdk_jks_twopass.p12.data", PKCS12,104SUN_JSSE, "storepass", "keypass", JKS, SUN);105106}107108private void driver(String testCase, String inKeyStore,109String inKeyStoreType, String inKeyStoreTypePrv,110String inStorePass, String inKeyPass, String outKeyStoreType,111String outKeyStorePrv) throws Exception {112113String outStorePass = "pass";114String outKeyPass = "pass";115KeyStore inputKeyStore, outputKeyStore;116117out.println("Testing " + testCase);118String keystorePath = System.getProperty("test.src", ".")119+ File.separator + "certs" + File.separator + "convertP12";120out.println("Output KeyStore : " + inKeyStore + ".out");121String outKeyStoreName = inKeyStore + ".out";122try (FileOutputStream fout = new FileOutputStream(outKeyStoreName);) {123inputKeyStore = KeyStore.getInstance(inKeyStoreType,124inKeyStoreTypePrv);125126// KeyStore have encoded by Base64.getMimeEncoder().encode(),need127// decode first.128byte[] input = Files.readAllBytes(Paths.get(keystorePath,129inKeyStore));130ByteArrayInputStream arrayIn = new ByteArrayInputStream(Base64131.getMimeDecoder().decode(input));132133out.println("Input KeyStore : " + inKeyStore);134135inputKeyStore.load(arrayIn, inStorePass.toCharArray());136137outputKeyStore = KeyStore.getInstance(outKeyStoreType,138outKeyStorePrv);139outputKeyStore.load(null, null);140141run(inputKeyStore, outputKeyStore, inKeyPass, outKeyPass);142143outputKeyStore.store(fout, outStorePass.toCharArray());144145// for P12ToJks_TwoEntry test case will test includes each other,146// others just test compareKeystore147if (testCase.contains("TwoEntry")) {148149compareKeyStore(inputKeyStore, outputKeyStore, inKeyPass,150outKeyPass, 2);151compareKeyStore(outputKeyStore, inputKeyStore, outKeyPass,152inKeyPass, 2);153} else {154compareKeyStore(inputKeyStore, outputKeyStore, inKeyPass,155outKeyPass, 1);156}157out.println("Test " + testCase + " STATUS: Pass!!");158} catch (Exception ex) {159out.println("Test " + testCase + " STATUS: failed with exception: "160+ ex.getMessage());161throw ex;162}163}164165private void run(KeyStore inputKeyStore, KeyStore outputKeyStore,166String inKeyPass, String outKeyPass) throws Exception {167Enumeration<String> e = inputKeyStore.aliases();168String alias;169while (e.hasMoreElements()) {170alias = e.nextElement();171Certificate[] certs = inputKeyStore.getCertificateChain(alias);172173boolean isCertEntry = inputKeyStore.isCertificateEntry(alias);174// Test KeyStore only contain key pair entries.175if (isCertEntry == true) {176throw new RuntimeException(177"inputKeystore should not be certEntry because test"178+ " keystore only contain key pair entries"179+ " for alias:" + alias);180}181182boolean isKeyEntry = inputKeyStore.isKeyEntry(alias);183Key key = null;184if (isKeyEntry) {185key = inputKeyStore.getKey(alias, inKeyPass.toCharArray());186} else {187throw new RuntimeException("Entry type unknown for alias:"188+ alias);189}190outputKeyStore.setKeyEntry(alias, key, outKeyPass.toCharArray(),191certs);192}193}194195private void compareKeyStore(KeyStore a, KeyStore b, String inKeyPass,196String outKeyPass, int keyStoreSize) throws Exception {197if (a.size() != keyStoreSize || b.size() != keyStoreSize) {198throw new RuntimeException("size not match or size not equal to "199+ keyStoreSize);200}201202Enumeration<String> eA = a.aliases();203while (eA.hasMoreElements()) {204String aliasA = eA.nextElement();205206if (!b.containsAlias(aliasA)) {207throw new RuntimeException("alias not match for alias:"208+ aliasA);209}210211compareKeyEntry(a, b, inKeyPass, outKeyPass, aliasA);212}213}214215private void compareKeyEntry(KeyStore a, KeyStore b, String aPass,216String bPass, String alias) throws KeyStoreException,217UnrecoverableKeyException, NoSuchAlgorithmException {218Certificate[] certsA = a.getCertificateChain(alias);219Certificate[] certsB = b.getCertificateChain(alias);220221if (!Arrays.equals(certsA, certsB)) {222throw new RuntimeException("Certs don't match for alias:" + alias);223}224225Key keyA = a.getKey(alias, aPass.toCharArray());226Key keyB = b.getKey(alias, bPass.toCharArray());227228if (!keyA.equals(keyB)) {229throw new RuntimeException(230"Key don't match for alias:" + alias);231}232}233}234235236