Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/ReadP12Test.java
41153 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_KEYSTORE_TYPE = "pkcs12";68private final static String IN_STORE_PASS = "pass";6970public static void main(String args[]) throws Exception {7172ReadP12Test jstest = new ReadP12Test();73String testCase = "";74try {75testCase = "ReadP12_IE_Chain";76jstest.readTest("ie_chain.pfx.data");7778testCase = "ReadP12_IE_Self";79jstest.readTest("ie_self.pfx.data");8081testCase = "ReadP12_JDK_Chain";82jstest.readTest("jdk_chain.p12.data");8384testCase = "ReadP12_JDK_Self";85jstest.readTest("jdk_self.p12.data");8687testCase = "ReadP12_Mozilla_Chain";88jstest.readTest("mozilla_chain.p12.data");8990testCase = "ReadP12_Mozilla_Self";91jstest.readTest("mozilla_self.p12.data");9293testCase = "ReadP12_Mozilla_TwoEntries";94jstest.readTest("mozilla_twoentries.p12.data");9596testCase = "ReadP12_Netscape_Chain";97jstest.readTest("netscape_chain.p12.data");9899testCase = "ReadP12_Netscape_Self";100jstest.readTest("netscape_self.p12.data");101102testCase = "ReadP12_Netscape_TwoEntries";103jstest.readTest("netscape_twoentries.p12.data");104105testCase = "ReadP12_openssl";106jstest.readTest("openssl.p12.data");107108} catch (Exception e) {109System.err.println(testCase + ": failed with execption: "110+ e.getMessage());111throw e;112113}114out.println(testCase + ": Pass!!");115}116117private void readTest(String inKeyStore) throws Exception {118119KeyStore inputKeyStore;120121// Initialize KeyStore122String dir = System.getProperty("test.src", ".");123String keystorePath = dir + File.separator + "certs" + File.separator124+ "readP12";125inputKeyStore = KeyStore.getInstance(IN_KEYSTORE_TYPE);126// KeyStore have encoded by Base64.getMimeEncoder().encode(),need decode127// first.128byte[] input = Files.readAllBytes(Paths.get(keystorePath, inKeyStore));129ByteArrayInputStream arrayIn = new ByteArrayInputStream(Base64130.getMimeDecoder().decode(input));131inputKeyStore.load(arrayIn, IN_STORE_PASS.toCharArray());132out.println("Initialize KeyStore : " + inKeyStore + " success");133134out.println("getProvider : " + inputKeyStore.getProvider());135out.println("getType : " + inputKeyStore.getType());136out.println("getDefaultType : " + KeyStore.getDefaultType());137138int idx = 0;139Enumeration<String> e = inputKeyStore.aliases();140String alias;141while (e.hasMoreElements()) {142alias = e.nextElement();143out.println("Alias " + idx + " : " + alias);144if (inputKeyStore.containsAlias(alias) == false) {145throw new RuntimeException("Alias not found");146}147148out.println("getCreationDate : "149+ inputKeyStore.getCreationDate(alias));150151X509Certificate cert = (X509Certificate) inputKeyStore152.getCertificate(alias);153out.println("getCertificate : " + cert.getSubjectDN());154String retAlias = inputKeyStore.getCertificateAlias(cert);155if (!retAlias.equals(alias)) {156throw new RuntimeException("Alias mismatch");157}158out.println("getCertificateAlias : " + retAlias);159160Certificate[] certs = inputKeyStore.getCertificateChain(alias);161for (int i = 0; i < certs.length; i++) {162out.println("getCertificateChain " + i + " : "163+ ((X509Certificate) certs[i]).getSubjectDN());164}165166boolean isCertEntry = inputKeyStore.isCertificateEntry(alias);167// test KeyStore only contain key pair entries.168if (isCertEntry == true) {169throw new RuntimeException(170"inputKeystore should not be certEntry because test keystore only contain key pair entries.");171}172173boolean isKeyEntry = inputKeyStore.isKeyEntry(alias);174if (isKeyEntry) {175Key key = inputKeyStore.getKey(alias,176IN_STORE_PASS.toCharArray());177out.println("Key : " + key.toString());178} else {179throw new RuntimeException("Entry type unknown\n");180}181idx++;182}183184int size = inputKeyStore.size();185if (idx != size) {186throw new RuntimeException("Size not match");187}188189}190}191192193