Path: blob/master/test/jdk/sun/security/pkcs11/ec/ReadPKCS12.java
41153 views
/*1* Copyright (c) 2006, 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 640553626* @summary Verify that we can parse ECPrivateKeys from PKCS#12 and use them27* @author Andreas Sterbenz28* @library /test/lib ..29* @library ../../../../java/security/testlibrary30* @key randomness31* @modules jdk.crypto.cryptoki jdk.crypto.ec/sun.security.ec32* @run main/othervm ReadPKCS1233* @run main/othervm -Djava.security.manager=allow ReadPKCS12 sm policy34*/3536import java.io.BufferedReader;37import java.io.File;38import java.io.FileInputStream;39import java.io.FileOutputStream;40import java.io.FileReader;41import java.io.InputStream;42import java.io.OutputStream;43import java.security.KeyStore;44import java.security.PrivateKey;45import java.security.Provider;46import java.security.PublicKey;47import java.security.Signature;48import java.security.cert.Certificate;49import java.security.cert.CertificateException;50import java.security.cert.CertificateFactory;51import java.security.cert.X509Certificate;52import java.util.Collections;53import java.util.HashMap;54import java.util.List;55import java.util.Map;56import java.util.Random;5758public class ReadPKCS12 extends PKCS11Test {5960private final static boolean COPY = false;6162public static void main(String[] args) throws Exception {63main(new ReadPKCS12(), args);64}6566@Override67public void main(Provider p) throws Exception {68if (p.getService("Signature", "SHA1withECDSA") == null) {69System.out.println("Provider does not support ECDSA, skipping...");70return;71}7273/*74* PKCS11Test.main will remove this provider if needed75*/76Providers.setAt(p, 1);7778CertificateFactory factory = CertificateFactory.getInstance("X.509");79try {80// undocumented way to clear the Sun internal certificate cache81factory.generateCertificate(null);82} catch (CertificateException e) {83// ignore84}8586KeyStore ks2;87if (COPY) {88ks2 = KeyStore.getInstance("JKS");89try (InputStream in = new FileInputStream("keystore.old")) {90ks2.load(in, "passphrase".toCharArray());91}92}9394File dir = new File(BASE, "pkcs12");95File closedDir = new File(CLOSED_BASE, "pkcs12");9697Map<String,char[]> passwords = new HashMap<>();98try (BufferedReader reader = new BufferedReader(99new FileReader(new File(BASE, "p12passwords.txt")))) {100while (true) {101String line = reader.readLine();102if (line == null) {103break;104}105line = line.trim();106if ((line.length() == 0) || line.startsWith("#")) {107continue;108}109String[] s = line.split(" ");110passwords.put(s[0], s[1].toCharArray());111}112}113114for (File file : concat(dir.listFiles(), closedDir.listFiles())) {115String name = file.getName();116if (file.isFile() == false) {117continue;118}119System.out.println();120System.out.println("Reading " + name + "...");121122char[] password = passwords.get(name);123if (password == null) {124password = passwords.get("*");125}126127KeyStore ks;128try (InputStream in = new FileInputStream(file)) {129ks = KeyStore.getInstance("PKCS12");130ks.load(in, password);131}132List<String> aliases = Collections.list(ks.aliases());133System.out.println("Aliases: " + aliases);134135for (String alias : aliases) {136PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);137Certificate[] certs = ks.getCertificateChain(alias);138PublicKey publicKey = certs[0].getPublicKey();139System.out.println("Certificates: " + certs.length);140System.out.println(privateKey);141System.out.println(publicKey);142if (COPY) {143ks2.setKeyEntry(alias, privateKey, "passphrase".toCharArray(), certs);144}145146verifyCerts(certs);147148Random random = new Random();149byte[] data = new byte[1024];150random.nextBytes(data);151152Signature s = Signature.getInstance("SHA1withECDSA");153s.initSign(privateKey);154s.update(data);155byte[] sig = s.sign();156157s.initVerify(publicKey);158s.update(data);159if (s.verify(sig) == false) {160throw new Exception("Signature does not verify");161}162System.out.println("Verified public/private key match");163}164}165166if (COPY) {167try (OutputStream out = new FileOutputStream("keystore.new")) {168ks2.store(out, "passphrase".toCharArray());169}170}171172System.out.println("OK");173}174175private static void verifyCerts(Certificate[] certs) throws Exception {176int n = certs.length;177for (int i = 0; i < n - 1; i++) {178X509Certificate cert = (X509Certificate)certs[i];179X509Certificate issuer = (X509Certificate)certs[i + 1];180if (cert.getIssuerX500Principal().equals(issuer.getSubjectX500Principal()) == false) {181throw new Exception("Certificates do not chain");182}183cert.verify(issuer.getPublicKey());184System.out.println("Verified: " + cert.getSubjectX500Principal());185}186X509Certificate last = (X509Certificate)certs[n - 1];187// if self-signed, verify the final cert188if (last.getIssuerX500Principal().equals(last.getSubjectX500Principal())) {189last.verify(last.getPublicKey());190System.out.println("Verified: " + last.getSubjectX500Principal());191}192}193194}195196197