Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/ReadPKCS12.java
38855 views
/*1* Copyright (c) 2006, 2016, 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 640553626* @summary Verify that we can parse ECPrivateKeys from PKCS#12 and use them27* @author Andreas Sterbenz28* @library ..29* @library ../../../../java/security/testlibrary30* @key randomness31* @run main/othervm ReadPKCS1232* @run main/othervm ReadPKCS12 sm policy33*/3435import java.io.BufferedReader;36import java.io.File;37import java.io.FileInputStream;38import java.io.FileOutputStream;39import java.io.FileReader;40import java.io.InputStream;41import java.io.OutputStream;42import java.security.KeyStore;43import java.security.PrivateKey;44import java.security.Provider;45import java.security.PublicKey;46import java.security.Signature;47import java.security.cert.Certificate;48import java.security.cert.CertificateException;49import java.security.cert.CertificateFactory;50import java.security.cert.X509Certificate;51import java.util.Collections;52import java.util.HashMap;53import java.util.List;54import java.util.Map;55import java.util.Random;5657public class ReadPKCS12 extends PKCS11Test {5859private final static boolean COPY = false;6061public static void main(String[] args) throws Exception {62main(new ReadPKCS12(), args);63}6465@Override66public void main(Provider p) throws Exception {67if (p.getService("Signature", "SHA1withECDSA") == null) {68System.out.println("Provider does not support ECDSA, skipping...");69return;70}7172/*73* PKCS11Test.main will remove this provider if needed74*/75Providers.setAt(p, 1);7677CertificateFactory factory = CertificateFactory.getInstance("X.509");78try {79// undocumented way to clear the Sun internal certificate cache80factory.generateCertificate(null);81} catch (CertificateException e) {82// ignore83}8485KeyStore ks2;86if (COPY) {87ks2 = KeyStore.getInstance("JKS");88try (InputStream in = new FileInputStream("keystore.old")) {89ks2.load(in, "passphrase".toCharArray());90}91}9293File dir = new File(BASE, "pkcs12");94File closedDir = new File(CLOSED_BASE, "pkcs12");9596Map<String,char[]> passwords = new HashMap<>();97try (BufferedReader reader = new BufferedReader(98new FileReader(new File(BASE, "p12passwords.txt")))) {99while (true) {100String line = reader.readLine();101if (line == null) {102break;103}104line = line.trim();105if ((line.length() == 0) || line.startsWith("#")) {106continue;107}108String[] s = line.split(" ");109passwords.put(s[0], s[1].toCharArray());110}111}112113for (File file : concat(dir.listFiles(), closedDir.listFiles())) {114String name = file.getName();115if (file.isFile() == false) {116continue;117}118System.out.println();119System.out.println("Reading " + name + "...");120121char[] password = passwords.get(name);122if (password == null) {123password = passwords.get("*");124}125126KeyStore ks;127try (InputStream in = new FileInputStream(file)) {128ks = KeyStore.getInstance("PKCS12");129ks.load(in, password);130}131List<String> aliases = Collections.list(ks.aliases());132System.out.println("Aliases: " + aliases);133134for (String alias : aliases) {135PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);136Certificate[] certs = ks.getCertificateChain(alias);137PublicKey publicKey = certs[0].getPublicKey();138System.out.println("Certificates: " + certs.length);139System.out.println(privateKey);140System.out.println(publicKey);141if (COPY) {142ks2.setKeyEntry(alias, privateKey, "passphrase".toCharArray(), certs);143}144145verifyCerts(certs);146147Random random = new Random();148byte[] data = new byte[1024];149random.nextBytes(data);150151Signature s = Signature.getInstance("SHA1withECDSA");152s.initSign(privateKey);153s.update(data);154byte[] sig = s.sign();155156s.initVerify(publicKey);157s.update(data);158if (s.verify(sig) == false) {159throw new Exception("Signature does not verify");160}161System.out.println("Verified public/private key match");162}163}164165if (COPY) {166try (OutputStream out = new FileOutputStream("keystore.new")) {167ks2.store(out, "passphrase".toCharArray());168}169}170171System.out.println("OK");172}173174private static void verifyCerts(Certificate[] certs) throws Exception {175int n = certs.length;176for (int i = 0; i < n - 1; i++) {177X509Certificate cert = (X509Certificate)certs[i];178X509Certificate issuer = (X509Certificate)certs[i + 1];179if (cert.getIssuerX500Principal().equals(issuer.getSubjectX500Principal()) == false) {180throw new Exception("Certificates do not chain");181}182cert.verify(issuer.getPublicKey());183System.out.println("Verified: " + cert.getSubjectX500Principal());184}185X509Certificate last = (X509Certificate)certs[n - 1];186// if self-signed, verify the final cert187if (last.getIssuerX500Principal().equals(last.getSubjectX500Principal())) {188last.verify(last.getPublicKey());189System.out.println("Verified: " + last.getSubjectX500Principal());190}191}192193}194195196