Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs12/PKCS12SameKeyId.java
38840 views
/*1* Copyright (c) 2010, 2013, 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 695802626* @summary Problem with PKCS12 keystore27* @compile -XDignore.symbol.file PKCS12SameKeyId.java28* @run main PKCS12SameKeyId29*/3031import java.io.File;32import java.io.FileInputStream;33import java.io.FileOutputStream;34import java.security.AlgorithmParameters;35import java.security.KeyStore;36import java.security.cert.Certificate;37import java.security.cert.X509Certificate;38import javax.crypto.Cipher;39import javax.crypto.SecretKey;40import javax.crypto.SecretKeyFactory;41import javax.crypto.spec.PBEKeySpec;42import javax.crypto.spec.PBEParameterSpec;43import sun.security.pkcs.EncryptedPrivateKeyInfo;44import sun.security.util.ObjectIdentifier;45import sun.security.x509.AlgorithmId;46import sun.security.x509.X500Name;4748public class PKCS12SameKeyId {4950private static final String JKSFILE = "PKCS12SameKeyId.jks";51private static final String P12FILE = "PKCS12SameKeyId.p12";52private static final char[] PASSWORD = "changeit".toCharArray();53private static final int SIZE = 10;5455public static void main(String[] args) throws Exception {5657// Prepare a JKS keystore with many entries58new File(JKSFILE).delete();59for (int i=0; i<SIZE; i++) {60System.err.print(".");61String cmd = "-keystore " + JKSFILE62+ " -storepass changeit -keypass changeit -keyalg rsa "63+ "-genkeypair -alias p" + i + " -dname CN=" + i;64sun.security.tools.keytool.Main.main(cmd.split(" "));65}6667// Prepare EncryptedPrivateKeyInfo parameters, copied from various68// places in PKCS12KeyStore.java69AlgorithmParameters algParams =70AlgorithmParameters.getInstance("PBEWithSHA1AndDESede");71algParams.init(new PBEParameterSpec("12345678".getBytes(), 1024));72AlgorithmId algid = new AlgorithmId(73new ObjectIdentifier("1.2.840.113549.1.12.1.3"), algParams);7475PBEKeySpec keySpec = new PBEKeySpec(PASSWORD);76SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");77SecretKey skey = skFac.generateSecret(keySpec);7879Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");80cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);8182// Pre-calculated keys and certs and aliases83byte[][] keys = new byte[SIZE][];84Certificate[][] certChains = new Certificate[SIZE][];85String[] aliases = new String[SIZE];8687// Reads from JKS keystore and pre-calculate88KeyStore ks = KeyStore.getInstance("jks");89try (FileInputStream fis = new FileInputStream(JKSFILE)) {90ks.load(fis, PASSWORD);91}92for (int i=0; i<SIZE; i++) {93aliases[i] = "p" + i;94byte[] enckey = cipher.doFinal(95ks.getKey(aliases[i], PASSWORD).getEncoded());96keys[i] = new EncryptedPrivateKeyInfo(algid, enckey).getEncoded();97certChains[i] = ks.getCertificateChain(aliases[i]);98}99100// Write into PKCS12 keystore. Use this overloaded version of101// setKeyEntry() to be as fast as possible, so that they would102// have same localKeyId.103KeyStore p12 = KeyStore.getInstance("pkcs12");104p12.load(null, PASSWORD);105for (int i=0; i<SIZE; i++) {106p12.setKeyEntry(aliases[i], keys[i], certChains[i]);107}108try (FileOutputStream fos = new FileOutputStream(P12FILE)) {109p12.store(fos, PASSWORD);110}111112// Check private keys still match certs113p12 = KeyStore.getInstance("pkcs12");114try (FileInputStream fis = new FileInputStream(P12FILE)) {115p12.load(fis, PASSWORD);116}117for (int i=0; i<SIZE; i++) {118String a = "p" + i;119X509Certificate x = (X509Certificate)p12.getCertificate(a);120X500Name name = (X500Name)x.getSubjectDN();121if (!name.getCommonName().equals(""+i)) {122throw new Exception(a + "'s cert is " + name);123}124}125}126}127128129