test/jdk/java/security/KeyStore/PKCS12/ConvertP12Test.java
author igerasim
Wed, 16 Oct 2019 18:47:11 -0700
changeset 58659 4113f16d5109
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231859: Extra dash after the exception name in @throws clause of javadoc Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27802
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     1
/*
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     2
 * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     4
 *
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     8
 *
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    13
 * accompanied this code).
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    14
 *
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    18
 *
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    21
 * questions.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    22
 */
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    23
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    24
import static java.lang.System.out;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    25
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    26
import java.io.ByteArrayInputStream;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    27
import java.io.File;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    28
import java.io.FileOutputStream;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    29
import java.nio.file.Files;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    30
import java.nio.file.Paths;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    31
import java.security.Key;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    32
import java.security.KeyStore;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    33
import java.security.KeyStoreException;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    35
import java.security.UnrecoverableKeyException;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    36
import java.security.cert.Certificate;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    37
import java.util.Arrays;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    38
import java.util.Base64;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    39
import java.util.Enumeration;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    40
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    41
/*
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    42
 * @test
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    43
 * @bug 8048619
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    44
 * @author  Bill Situ
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    45
 * @summary Test converting keystore from jceks to P12 and from P12 to other
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    46
 *  (jceks,jks). including following test cases:
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    47
 * Read jceks key store and convert to the p12 key store, then compare entries
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    48
 *  in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    49
 * Read p12 key store and convert to the jceks key store, then compare entries
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    50
 *  in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    51
 * Read p12 key store (contains only private key and a self-signed certificate)
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    52
 *  and convert to the jceks key store, then compare entries of two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    53
 * Read p12 key store (contains 2 entries) and convert to the jceks key store,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    54
 *  then compare entries in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    55
 * Read p12 key store (entry password and key store password are different) and
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    56
 *  convert to the jceks key store, then compare entries in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    57
 * Read p12 key store and convert to the jks key store, then compare entries
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    58
 *  in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    59
 * Read p12 key store (contains only private key and a self-signed certificate)
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    60
 *  and convert to the jks key store, then compare entries in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    61
 * Read p12 key store (contains 2 entries) and convert to the jks key store,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    62
 *  then compare entries in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    63
 * Read p12 key store (entry password and key store password are different) and
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    64
 * convert to the jks key store, then compare entries in the two key stores.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    65
 */
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    66
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    67
public class ConvertP12Test {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    68
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    69
    private static final String SUN_JSSE = "SunJSSE";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    70
    private static final String SUN_JCE = "SunJCE";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    71
    private static final String SUN = "SUN";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    72
    private static final String PKCS12 = "pkcs12";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    73
    private static final String JCE_KS = "JceKS";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    74
    private static final String JKS = "JKS";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    75
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    76
    public static void main(String args[]) throws Exception {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    77
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    78
        ConvertP12Test jstest = new ConvertP12Test();
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    79
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    80
        jstest.driver("JceksToP12", "keystoreCA.jceks.data", JCE_KS, SUN_JCE,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    81
                "storepass", "keypass", PKCS12, SUN_JSSE);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    82
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    83
        jstest.driver("P12ToJceks_Chain", "ie_jceks_chain.pfx.data", PKCS12,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    84
                SUN_JSSE, "pass", "pass", JCE_KS, SUN_JCE);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    85
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    86
        jstest.driver("P12ToJceks_SelfSigned", "jdk_jceks_selfsigned.p12.data",
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    87
                PKCS12, SUN_JSSE, "pass", "pass", JCE_KS, SUN_JCE);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    88
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    89
        jstest.driver("P12ToJceks_TwoEntry", "jdk_jceks_twoentry.p12.data",
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    90
                PKCS12, SUN_JSSE, "pass", "pass", JCE_KS, SUN_JCE);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    91
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    92
        jstest.driver("P12ToJceks_TwoPass", "jdk_jceks_twopass.p12.data",
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    93
                PKCS12, SUN_JSSE, "storepass", "keypass", JCE_KS, SUN_JCE);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    94
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    95
        jstest.driver("P12ToJks_Chain", "ie_jks_chain.pfx.data", PKCS12,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    96
                SUN_JSSE, "pass", "pass", JKS, SUN);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    97
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    98
        jstest.driver("P12ToJks_SelfSigned", "jdk_jks_selfsigned.p12.data",
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
    99
                PKCS12, SUN_JSSE, "pass", "pass", JKS, SUN);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   100
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   101
        jstest.driver("P12ToJks_TwoEntry", "jdk_jks_twoentry.p12.data", PKCS12,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   102
                SUN_JSSE, "pass", "pass", JKS, SUN);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   103
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   104
        jstest.driver("P12ToJks_TwoPass", "jdk_jks_twopass.p12.data", PKCS12,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   105
                SUN_JSSE, "storepass", "keypass", JKS, SUN);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   106
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   107
    }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   108
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   109
    private void driver(String testCase, String inKeyStore,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   110
            String inKeyStoreType, String inKeyStoreTypePrv,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   111
            String inStorePass, String inKeyPass, String outKeyStoreType,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   112
            String outKeyStorePrv) throws Exception {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   113
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   114
        String outStorePass = "pass";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   115
        String outKeyPass = "pass";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   116
        KeyStore inputKeyStore, outputKeyStore;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   117
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   118
        out.println("Testing " + testCase);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   119
        String keystorePath = System.getProperty("test.src", ".")
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   120
                + File.separator + "certs" + File.separator + "convertP12";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   121
        out.println("Output KeyStore : " + inKeyStore + ".out");
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   122
        String outKeyStoreName = inKeyStore + ".out";
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   123
        try (FileOutputStream fout = new FileOutputStream(outKeyStoreName);) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   124
            inputKeyStore = KeyStore.getInstance(inKeyStoreType,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   125
                    inKeyStoreTypePrv);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   126
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   127
            // KeyStore have encoded by Base64.getMimeEncoder().encode(),need
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   128
            // decode first.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   129
            byte[] input = Files.readAllBytes(Paths.get(keystorePath,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   130
                    inKeyStore));
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   131
            ByteArrayInputStream arrayIn = new ByteArrayInputStream(Base64
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   132
                    .getMimeDecoder().decode(input));
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   133
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   134
            out.println("Input KeyStore : " + inKeyStore);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   135
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   136
            inputKeyStore.load(arrayIn, inStorePass.toCharArray());
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   137
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   138
            outputKeyStore = KeyStore.getInstance(outKeyStoreType,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   139
                    outKeyStorePrv);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   140
            outputKeyStore.load(null, null);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   141
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   142
            run(inputKeyStore, outputKeyStore, inKeyPass, outKeyPass);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   143
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   144
            outputKeyStore.store(fout, outStorePass.toCharArray());
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   145
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   146
            // for P12ToJks_TwoEntry test case will test includes each other,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   147
            // others just test compareKeystore
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   148
            if (testCase.contains("TwoEntry")) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   149
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   150
                compareKeyStore(inputKeyStore, outputKeyStore, inKeyPass,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   151
                        outKeyPass, 2);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   152
                compareKeyStore(outputKeyStore, inputKeyStore, outKeyPass,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   153
                        inKeyPass, 2);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   154
            } else {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   155
                compareKeyStore(inputKeyStore, outputKeyStore, inKeyPass,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   156
                        outKeyPass, 1);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   157
            }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   158
            out.println("Test " + testCase + " STATUS: Pass!!");
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   159
        } catch (Exception ex) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   160
            out.println("Test " + testCase + " STATUS: failed with exception: "
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   161
                    + ex.getMessage());
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   162
            throw ex;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   163
        }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   164
    }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   165
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   166
    private void run(KeyStore inputKeyStore, KeyStore outputKeyStore,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   167
            String inKeyPass, String outKeyPass) throws Exception {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   168
        Enumeration<String> e = inputKeyStore.aliases();
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   169
        String alias;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   170
        while (e.hasMoreElements()) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   171
            alias = e.nextElement();
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   172
            Certificate[] certs = inputKeyStore.getCertificateChain(alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   173
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   174
            boolean isCertEntry = inputKeyStore.isCertificateEntry(alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   175
            // Test KeyStore only contain key pair entries.
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   176
            if (isCertEntry == true) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   177
                throw new RuntimeException(
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   178
                        "inputKeystore should not be certEntry because test"
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   179
                                + " keystore only contain key pair entries"
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   180
                                + " for alias:" + alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   181
            }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   182
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   183
            boolean isKeyEntry = inputKeyStore.isKeyEntry(alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   184
            Key key = null;
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   185
            if (isKeyEntry) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   186
                key = inputKeyStore.getKey(alias, inKeyPass.toCharArray());
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   187
            } else {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   188
                throw new RuntimeException("Entry type unknown for alias:"
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   189
                        + alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   190
            }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   191
            outputKeyStore.setKeyEntry(alias, key, outKeyPass.toCharArray(),
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   192
                    certs);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   193
        }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   194
    }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   195
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   196
    private void compareKeyStore(KeyStore a, KeyStore b, String inKeyPass,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   197
            String outKeyPass, int keyStoreSize) throws Exception {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   198
        if (a.size() != keyStoreSize || b.size() != keyStoreSize) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   199
            throw new RuntimeException("size not match or size not equal to "
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   200
                    + keyStoreSize);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   201
        }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   202
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   203
        Enumeration<String> eA = a.aliases();
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   204
        while (eA.hasMoreElements()) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   205
            String aliasA = eA.nextElement();
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   206
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   207
            if (!b.containsAlias(aliasA)) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   208
                throw new RuntimeException("alias not match for alias:"
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   209
                        + aliasA);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   210
            }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   211
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   212
            compareKeyEntry(a, b, inKeyPass, outKeyPass, aliasA);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   213
        }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   214
    }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   215
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   216
    private void compareKeyEntry(KeyStore a, KeyStore b, String aPass,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   217
            String bPass, String alias) throws KeyStoreException,
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   218
            UnrecoverableKeyException, NoSuchAlgorithmException {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   219
        Certificate[] certsA = a.getCertificateChain(alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   220
        Certificate[] certsB = b.getCertificateChain(alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   221
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   222
        if (!Arrays.equals(certsA, certsB)) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   223
            throw new RuntimeException("Certs don't match for alias:" + alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   224
        }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   225
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   226
        Key keyA = a.getKey(alias, aPass.toCharArray());
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   227
        Key keyB = b.getKey(alias, bPass.toCharArray());
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   228
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   229
        if (!keyA.equals(keyB)) {
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   230
            throw new RuntimeException(
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   231
                    "Key don't match for alias:" + alias);
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   232
        }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   233
    }
c6d453fa55bb 8048619: Implement tests for converting PKCS12 keystores
weijun
parents:
diff changeset
   234
}