Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/PKCS12/ReadP12Test.java
38828 views
/*1* Copyright (c) 2003,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;24import java.io.ByteArrayInputStream;25import java.io.File;26import java.io.FileInputStream;27import java.nio.file.Files;28import java.nio.file.Paths;29import java.security.Key;30import java.security.KeyStore;31import java.security.cert.Certificate;32import java.security.cert.X509Certificate;33import java.util.Base64;34import java.util.Enumeration;3536/*37* @test38* @bug 804861739* @author Bill Situ40* @summary Read different types p12 key store to Check the read related APIs.41* including following test cases:42* ReadP12_IE_Chain: Read p12 key store (contains private key and associated43* certificate chain) from IE.44* ReadP12_IE_Self: Read p12 key store (contains only private key and45* self-signed certificate) from IE.46* ReadP12_JDK_Chain: Read p12 key store (contains private key and associated47* certificate chain) from JDK48* ReadP12_JDK_Self: Read p12 key store (contains only private key and49* self-signed certificate) from JDK.50* ReadP12_Mozilla_Self: Read p12 key store (contains only private key and51* self-signed certificate) from Mozilla.52* ReadP12_Mozilla_Chain: Read p12 key store (contains private key and53* associated certificate chain) from Mozilla.54* ReadP12_Mozilla_TwoEntries: Read p12 key store (contains 2 entries) from55* Mozilla.56* ReadP12_Netscape_Chain: Read p12 key store (contains private key and57* associated certificate chain) from Netscape.58* ReadP12_Netscape_Self: Read p12 key store (contains only private key and59* self-signed certificate) from Netscape.60* ReadP12_Netscape_TwoEntries: Read p12 key store (contains 2 entries) from61* Netscape.62* ReadP12_OpenSSL: Read p12 key store from OpenSSL.63*/6465public class ReadP12Test {6667private final static String IN_KETYSTORE_TYPE = "pkcs12";68private final static String IN_KEYSTORE_PRV = "SunJSSE";69private final static String IN_STORE_PASS = "pass";7071public static void main(String args[]) throws Exception {7273ReadP12Test jstest = new ReadP12Test();74String testCase = "";75try {76testCase = "ReadP12_IE_Chain";77jstest.readTest("ie_chain.pfx.data");7879testCase = "ReadP12_IE_Self";80jstest.readTest("ie_self.pfx.data");8182testCase = "ReadP12_JDK_Chain";83jstest.readTest("jdk_chain.p12.data");8485testCase = "ReadP12_JDK_Self";86jstest.readTest("jdk_self.p12.data");8788testCase = "ReadP12_Mozilla_Chain";89jstest.readTest("mozilla_chain.p12.data");9091testCase = "ReadP12_Mozilla_Self";92jstest.readTest("mozilla_self.p12.data");9394testCase = "ReadP12_Mozilla_TwoEntries";95jstest.readTest("mozilla_twoentries.p12.data");9697testCase = "ReadP12_Netscape_Chain";98jstest.readTest("netscape_chain.p12.data");99100testCase = "ReadP12_Netscape_Self";101jstest.readTest("netscape_self.p12.data");102103testCase = "ReadP12_Netscape_TwoEntries";104jstest.readTest("netscape_twoentries.p12.data");105106testCase = "ReadP12_openssl";107jstest.readTest("openssl.p12.data");108109} catch (Exception e) {110System.err.println(testCase + ": failed with execption: "111+ e.getMessage());112throw e;113114}115out.println(testCase + ": Pass!!");116}117118private void readTest(String inKeyStore) throws Exception {119120KeyStore inputKeyStore;121122// Initialize KeyStore123String dir = System.getProperty("test.src", ".");124String keystorePath = dir + File.separator + "certs" + File.separator125+ "readP12";126inputKeyStore = KeyStore127.getInstance(IN_KETYSTORE_TYPE, IN_KEYSTORE_PRV);128// KeyStore have encoded by Base64.getMimeEncoder().encode(),need decode129// first.130byte[] input = Files.readAllBytes(Paths.get(keystorePath, inKeyStore));131ByteArrayInputStream arrayIn = new ByteArrayInputStream(Base64132.getMimeDecoder().decode(input));133inputKeyStore.load(arrayIn, IN_STORE_PASS.toCharArray());134out.println("Initialize KeyStore : " + inKeyStore + " success");135136out.println("getProvider : " + inputKeyStore.getProvider());137out.println("getType : " + inputKeyStore.getType());138out.println("getDefaultType : " + KeyStore.getDefaultType());139140int idx = 0;141Enumeration<String> e = inputKeyStore.aliases();142String alias;143while (e.hasMoreElements()) {144alias = e.nextElement();145out.println("Alias " + idx + " : " + alias);146if (inputKeyStore.containsAlias(alias) == false) {147throw new RuntimeException("Alias not found");148}149150out.println("getCreationDate : "151+ inputKeyStore.getCreationDate(alias));152153X509Certificate cert = (X509Certificate) inputKeyStore154.getCertificate(alias);155out.println("getCertificate : " + cert.getSubjectDN());156String retAlias = inputKeyStore.getCertificateAlias(cert);157if (!retAlias.equals(alias)) {158throw new RuntimeException("Alias mismatch");159}160out.println("getCertificateAlias : " + retAlias);161162Certificate[] certs = inputKeyStore.getCertificateChain(alias);163for (int i = 0; i < certs.length; i++) {164out.println("getCertificateChain " + i + " : "165+ ((X509Certificate) certs[i]).getSubjectDN());166}167168boolean isCertEntry = inputKeyStore.isCertificateEntry(alias);169// test KeyStore only contain key pair entries.170if (isCertEntry == true) {171throw new RuntimeException(172"inputKeystore should not be certEntry because test keystore only contain key pair entries.");173}174175boolean isKeyEntry = inputKeyStore.isKeyEntry(alias);176if (isKeyEntry) {177Key key = inputKeyStore.getKey(alias,178IN_STORE_PASS.toCharArray());179out.println("Key : " + key.toString());180} else {181throw new RuntimeException("Entry type unknown\n");182}183idx++;184}185186int size = inputKeyStore.size();187if (idx != size) {188throw new RuntimeException("Size not match");189}190191}192}193194195