2
|
1 |
/*
|
|
2 |
* Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6405536 6414980
|
|
27 |
* @summary Make sure that we can parse certificates using various named curves
|
|
28 |
* and verify their signatures
|
|
29 |
* @author Andreas Sterbenz
|
|
30 |
* @library ..
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.*;
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import java.security.cert.*;
|
|
37 |
import java.security.*;
|
|
38 |
import java.security.interfaces.*;
|
|
39 |
|
|
40 |
import javax.security.auth.x500.X500Principal;
|
|
41 |
|
|
42 |
public class ReadCertificates extends PKCS11Test {
|
|
43 |
|
|
44 |
private static CertificateFactory factory;
|
|
45 |
|
|
46 |
private static SecureRandom random;
|
|
47 |
|
|
48 |
private static Collection<X509Certificate> readCertificates(File file) throws Exception {
|
|
49 |
System.out.println("Loading " + file.getName() + "...");
|
|
50 |
InputStream in = new FileInputStream(file);
|
|
51 |
Collection<X509Certificate> certs = (Collection<X509Certificate>)factory.generateCertificates(in);
|
|
52 |
in.close();
|
|
53 |
return certs;
|
|
54 |
}
|
|
55 |
|
|
56 |
public static void main(String[] args) throws Exception {
|
|
57 |
main(new ReadCertificates());
|
|
58 |
}
|
|
59 |
|
|
60 |
public void main(Provider p) throws Exception {
|
|
61 |
if (p.getService("Signature", "SHA1withECDSA") == null) {
|
|
62 |
System.out.println("Provider does not support ECDSA, skipping...");
|
|
63 |
return;
|
|
64 |
}
|
|
65 |
Security.insertProviderAt(p, 1);
|
|
66 |
|
|
67 |
random = new SecureRandom();
|
|
68 |
factory = CertificateFactory.getInstance("X.509");
|
|
69 |
try {
|
|
70 |
// clear certificate cache in from a previous run with a different
|
|
71 |
// provider (undocumented hack for the Sun provider)
|
|
72 |
factory.generateCertificate(null);
|
|
73 |
} catch (CertificateException e) {
|
|
74 |
// ignore
|
|
75 |
}
|
|
76 |
Map<X500Principal,X509Certificate> certs = new LinkedHashMap<X500Principal,X509Certificate>();
|
|
77 |
|
|
78 |
File dir = new File(BASE, "certs");
|
|
79 |
File closedDir = new File(CLOSED_BASE, "certs");
|
|
80 |
File[] files = concat(dir.listFiles(), closedDir.listFiles());
|
|
81 |
Arrays.sort(files);
|
|
82 |
for (File file : files) {
|
|
83 |
if (file.isFile() == false) {
|
|
84 |
continue;
|
|
85 |
}
|
|
86 |
Collection<X509Certificate> certList = readCertificates(file);
|
|
87 |
for (X509Certificate cert : certList) {
|
|
88 |
X509Certificate old = certs.put(cert.getSubjectX500Principal(), cert);
|
|
89 |
if (old != null) {
|
|
90 |
System.out.println("Duplicate subject:");
|
|
91 |
System.out.println("Old Certificate: " + old);
|
|
92 |
System.out.println("New Certificate: " + cert);
|
|
93 |
throw new Exception(file.getPath());
|
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|
|
97 |
System.out.println("OK: " + certs.size() + " certificates.");
|
|
98 |
|
|
99 |
for (X509Certificate cert : certs.values()) {
|
|
100 |
X509Certificate issuer = certs.get(cert.getIssuerX500Principal());
|
|
101 |
System.out.println("Verifying " + cert.getSubjectX500Principal() + "...");
|
|
102 |
PublicKey key = issuer.getPublicKey();
|
|
103 |
cert.verify(key, p.getName());
|
|
104 |
}
|
|
105 |
|
|
106 |
// try some random invalid signatures to make sure we get the correct
|
|
107 |
// error
|
|
108 |
System.out.println("Checking incorrect signatures...");
|
|
109 |
List<X509Certificate> certList = new ArrayList<X509Certificate>(certs.values());
|
|
110 |
for (int i = 0; i < 20; i++) {
|
|
111 |
X509Certificate cert, signer;
|
|
112 |
do {
|
|
113 |
cert = getRandomCert(certList);
|
|
114 |
signer = getRandomCert(certList);
|
|
115 |
} while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal()));
|
|
116 |
try {
|
|
117 |
cert.verify(signer.getPublicKey());
|
|
118 |
throw new Exception("Verified invalid signature");
|
|
119 |
} catch (SignatureException e) {
|
|
120 |
System.out.println("OK: " + e);
|
|
121 |
} catch (InvalidKeyException e) {
|
|
122 |
System.out.println("OK: " + e);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
Security.removeProvider(p.getName());
|
|
127 |
System.out.println("OK");
|
|
128 |
}
|
|
129 |
|
|
130 |
private static X509Certificate getRandomCert(List<X509Certificate> certs) {
|
|
131 |
int n = random.nextInt(certs.size());
|
|
132 |
return certs.get(n);
|
|
133 |
}
|
|
134 |
|
|
135 |
}
|