blob: 0d06d730f5887717245328e1f8756b43e2725fc6 [file] [log] [blame]
weijun73ed4182010-06-24 14:26:22 +08001/*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6958026
27 * @summary Problem with PKCS12 keystore
28 */
29
30import java.io.File;
31import java.io.FileInputStream;
32import java.io.FileOutputStream;
33import java.security.AlgorithmParameters;
34import java.security.KeyStore;
35import java.security.cert.Certificate;
36import java.security.cert.X509Certificate;
37import javax.crypto.Cipher;
38import javax.crypto.SecretKey;
39import javax.crypto.SecretKeyFactory;
40import javax.crypto.spec.PBEKeySpec;
41import javax.crypto.spec.PBEParameterSpec;
42import sun.security.pkcs.EncryptedPrivateKeyInfo;
43import sun.security.tools.KeyTool;
44import sun.security.util.ObjectIdentifier;
45import sun.security.x509.AlgorithmId;
46import sun.security.x509.X500Name;
47
48public class PKCS12SameKeyId {
49
50 private static final String JKSFILE = "PKCS12SameKeyId.jks";
51 private static final String P12FILE = "PKCS12SameKeyId.p12";
52 private static final char[] PASSWORD = "changeit".toCharArray();
53 private static final int SIZE = 10;
54
55 public static void main(String[] args) throws Exception {
56
57 // Prepare a JKS keystore with many entries
58 new File(JKSFILE).delete();
59 for (int i=0; i<SIZE; i++) {
60 System.err.print(".");
61 String cmd = "-keystore " + JKSFILE
62 + " -storepass changeit -keypass changeit "
63 + "-genkeypair -alias p" + i + " -dname CN=" + i;
64 KeyTool.main(cmd.split(" "));
65 }
66
67 // Prepare EncryptedPrivateKeyInfo parameters, copied from various
68 // places in PKCS12KeyStore.java
69 AlgorithmParameters algParams =
70 AlgorithmParameters.getInstance("PBEWithSHA1AndDESede");
71 algParams.init(new PBEParameterSpec("12345678".getBytes(), 1024));
72 AlgorithmId algid = new AlgorithmId(
73 new ObjectIdentifier("1.2.840.113549.1.12.1.3"), algParams);
74
75 PBEKeySpec keySpec = new PBEKeySpec(PASSWORD);
76 SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
77 SecretKey skey = skFac.generateSecret(keySpec);
78
79 Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
80 cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
81
82 // Pre-calculated keys and certs and aliases
83 byte[][] keys = new byte[SIZE][];
84 Certificate[][] certChains = new Certificate[SIZE][];
85 String[] aliases = new String[SIZE];
86
87 // Reads from JKS keystore and pre-calculate
88 KeyStore ks = KeyStore.getInstance("jks");
weijundff52e62011-08-12 12:26:31 +080089 try (FileInputStream fis = new FileInputStream(JKSFILE)) {
90 ks.load(fis, PASSWORD);
91 }
weijun73ed4182010-06-24 14:26:22 +080092 for (int i=0; i<SIZE; i++) {
93 aliases[i] = "p" + i;
94 byte[] enckey = cipher.doFinal(
95 ks.getKey(aliases[i], PASSWORD).getEncoded());
96 keys[i] = new EncryptedPrivateKeyInfo(algid, enckey).getEncoded();
97 certChains[i] = ks.getCertificateChain(aliases[i]);
98 }
99
100 // Write into PKCS12 keystore. Use this overloaded version of
101 // setKeyEntry() to be as fast as possible, so that they would
102 // have same localKeyId.
103 KeyStore p12 = KeyStore.getInstance("pkcs12");
104 p12.load(null, PASSWORD);
105 for (int i=0; i<SIZE; i++) {
106 p12.setKeyEntry(aliases[i], keys[i], certChains[i]);
107 }
weijundff52e62011-08-12 12:26:31 +0800108 try (FileOutputStream fos = new FileOutputStream(P12FILE)) {
109 p12.store(fos, PASSWORD);
110 }
weijun73ed4182010-06-24 14:26:22 +0800111
112 // Check private keys still match certs
113 p12 = KeyStore.getInstance("pkcs12");
weijundff52e62011-08-12 12:26:31 +0800114 try (FileInputStream fis = new FileInputStream(P12FILE)) {
115 p12.load(fis, PASSWORD);
116 }
weijun73ed4182010-06-24 14:26:22 +0800117 for (int i=0; i<SIZE; i++) {
118 String a = "p" + i;
119 X509Certificate x = (X509Certificate)p12.getCertificate(a);
120 X500Name name = (X500Name)x.getSubjectDN();
121 if (!name.getCommonName().equals(""+i)) {
122 throw new Exception(a + "'s cert is " + name);
123 }
124 }
125 }
126}