0N/A/*
2362N/A * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6405536 6414980
0N/A * @summary Make sure that we can parse certificates using various named curves
0N/A * and verify their signatures
0N/A * @author Andreas Sterbenz
0N/A * @library ..
5361N/A * @library ../../../../java/security/testlibrary
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.cert.*;
0N/Aimport java.security.*;
0N/Aimport java.security.interfaces.*;
0N/A
0N/Aimport javax.security.auth.x500.X500Principal;
0N/A
0N/Apublic class ReadCertificates extends PKCS11Test {
0N/A
0N/A private static CertificateFactory factory;
0N/A
0N/A private static SecureRandom random;
0N/A
0N/A private static Collection<X509Certificate> readCertificates(File file) throws Exception {
0N/A System.out.println("Loading " + file.getName() + "...");
0N/A InputStream in = new FileInputStream(file);
0N/A Collection<X509Certificate> certs = (Collection<X509Certificate>)factory.generateCertificates(in);
0N/A in.close();
0N/A return certs;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A main(new ReadCertificates());
0N/A }
0N/A
0N/A public void main(Provider p) throws Exception {
0N/A if (p.getService("Signature", "SHA1withECDSA") == null) {
0N/A System.out.println("Provider does not support ECDSA, skipping...");
0N/A return;
0N/A }
5361N/A Providers.setAt(p, 1);
0N/A
0N/A random = new SecureRandom();
0N/A factory = CertificateFactory.getInstance("X.509");
0N/A try {
0N/A // clear certificate cache in from a previous run with a different
0N/A // provider (undocumented hack for the Sun provider)
0N/A factory.generateCertificate(null);
0N/A } catch (CertificateException e) {
0N/A // ignore
0N/A }
0N/A Map<X500Principal,X509Certificate> certs = new LinkedHashMap<X500Principal,X509Certificate>();
0N/A
0N/A File dir = new File(BASE, "certs");
0N/A File closedDir = new File(CLOSED_BASE, "certs");
0N/A File[] files = concat(dir.listFiles(), closedDir.listFiles());
0N/A Arrays.sort(files);
0N/A for (File file : files) {
0N/A if (file.isFile() == false) {
0N/A continue;
0N/A }
0N/A Collection<X509Certificate> certList = readCertificates(file);
0N/A for (X509Certificate cert : certList) {
0N/A X509Certificate old = certs.put(cert.getSubjectX500Principal(), cert);
0N/A if (old != null) {
0N/A System.out.println("Duplicate subject:");
0N/A System.out.println("Old Certificate: " + old);
0N/A System.out.println("New Certificate: " + cert);
0N/A throw new Exception(file.getPath());
0N/A }
0N/A }
0N/A }
0N/A System.out.println("OK: " + certs.size() + " certificates.");
0N/A
0N/A for (X509Certificate cert : certs.values()) {
0N/A X509Certificate issuer = certs.get(cert.getIssuerX500Principal());
0N/A System.out.println("Verifying " + cert.getSubjectX500Principal() + "...");
0N/A PublicKey key = issuer.getPublicKey();
1674N/A // First try the provider under test (if it does not support the
1674N/A // necessary algorithm then try any registered provider).
1674N/A try {
1674N/A cert.verify(key, p.getName());
1674N/A } catch (NoSuchAlgorithmException e) {
1674N/A System.out.println("Warning: " + e.getMessage() +
1674N/A ". Trying another provider...");
1674N/A cert.verify(key);
1674N/A }
0N/A }
0N/A
0N/A // try some random invalid signatures to make sure we get the correct
0N/A // error
0N/A System.out.println("Checking incorrect signatures...");
0N/A List<X509Certificate> certList = new ArrayList<X509Certificate>(certs.values());
0N/A for (int i = 0; i < 20; i++) {
0N/A X509Certificate cert, signer;
0N/A do {
0N/A cert = getRandomCert(certList);
0N/A signer = getRandomCert(certList);
0N/A } while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal()));
0N/A try {
0N/A cert.verify(signer.getPublicKey());
0N/A throw new Exception("Verified invalid signature");
0N/A } catch (SignatureException e) {
0N/A System.out.println("OK: " + e);
0N/A } catch (InvalidKeyException e) {
0N/A System.out.println("OK: " + e);
0N/A }
0N/A }
0N/A
0N/A Security.removeProvider(p.getName());
0N/A System.out.println("OK");
0N/A }
0N/A
0N/A private static X509Certificate getRandomCert(List<X509Certificate> certs) {
0N/A int n = random.nextInt(certs.size());
0N/A return certs.get(n);
0N/A }
0N/A
0N/A}