Path: blob/master/test/jdk/sun/security/pkcs12/PKCS12SameKeyId.java
41152 views
/*1* Copyright (c) 2010, 2020, 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*/2223/*24* @test25* @bug 6958026 824215126* @summary Problem with PKCS12 keystore27* @modules java.base/sun.security.pkcs28* java.base/sun.security.tools.keytool29* java.base/sun.security.util30* java.base/sun.security.x50931* @compile -XDignore.symbol.file PKCS12SameKeyId.java32* @run main PKCS12SameKeyId33*/3435import java.io.File;36import java.io.FileInputStream;37import java.io.FileOutputStream;38import java.security.AlgorithmParameters;39import java.security.KeyStore;40import java.security.cert.Certificate;41import java.security.cert.X509Certificate;42import javax.crypto.Cipher;43import javax.crypto.SecretKey;44import javax.crypto.SecretKeyFactory;45import javax.crypto.spec.PBEKeySpec;46import javax.crypto.spec.PBEParameterSpec;47import sun.security.pkcs.EncryptedPrivateKeyInfo;48import sun.security.util.ObjectIdentifier;49import sun.security.x509.AlgorithmId;50import sun.security.x509.X500Name;5152public class PKCS12SameKeyId {5354private static final String JKSFILE = "PKCS12SameKeyId.jks";55private static final String P12FILE = "PKCS12SameKeyId.p12";56private static final char[] PASSWORD = "changeit".toCharArray();57private static final int SIZE = 10;5859public static void main(String[] args) throws Exception {6061// Prepare a JKS keystore with many entries62new File(JKSFILE).delete();63for (int i=0; i<SIZE; i++) {64System.err.print(".");65String cmd = "-keystore " + JKSFILE66+ " -storepass changeit -keypass changeit -keyalg rsa "67+ "-genkeypair -alias p" + i + " -dname CN=" + i;68sun.security.tools.keytool.Main.main(cmd.split(" "));69}7071// Prepare EncryptedPrivateKeyInfo parameters, copied from various72// places in PKCS12KeyStore.java73AlgorithmParameters algParams =74AlgorithmParameters.getInstance("PBEWithSHA1AndDESede");75algParams.init(new PBEParameterSpec("12345678".getBytes(), 1024));76AlgorithmId algid = new AlgorithmId(77ObjectIdentifier.of("1.2.840.113549.1.12.1.3"), algParams);7879PBEKeySpec keySpec = new PBEKeySpec(PASSWORD);80SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");81SecretKey skey = skFac.generateSecret(keySpec);8283Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");84cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);8586// Pre-calculated keys and certs and aliases87byte[][] keys = new byte[SIZE][];88Certificate[][] certChains = new Certificate[SIZE][];89String[] aliases = new String[SIZE];9091// Reads from JKS keystore and pre-calculate92KeyStore ks = KeyStore.getInstance("jks");93try (FileInputStream fis = new FileInputStream(JKSFILE)) {94ks.load(fis, PASSWORD);95}96for (int i=0; i<SIZE; i++) {97aliases[i] = "p" + i;98byte[] enckey = cipher.doFinal(99ks.getKey(aliases[i], PASSWORD).getEncoded());100keys[i] = new EncryptedPrivateKeyInfo(algid, enckey).getEncoded();101certChains[i] = ks.getCertificateChain(aliases[i]);102}103104// Write into PKCS12 keystore. Use this overloaded version of105// setKeyEntry() to be as fast as possible, so that they would106// have same localKeyId.107KeyStore p12 = KeyStore.getInstance("pkcs12");108p12.load(null, PASSWORD);109for (int i=0; i<SIZE; i++) {110p12.setKeyEntry(aliases[i], keys[i], certChains[i]);111}112try (FileOutputStream fos = new FileOutputStream(P12FILE)) {113p12.store(fos, PASSWORD);114}115116// Check private keys still match certs117p12 = KeyStore.getInstance("pkcs12");118try (FileInputStream fis = new FileInputStream(P12FILE)) {119p12.load(fis, PASSWORD);120}121for (int i=0; i<SIZE; i++) {122String a = "p" + i;123X509Certificate x = (X509Certificate)p12.getCertificate(a);124X500Name name = (X500Name)x.getSubjectDN();125if (!name.getCommonName().equals(""+i)) {126throw new Exception(a + "'s cert is " + name);127}128}129}130}131132133