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