author | bobv |
Thu, 02 Dec 2010 14:00:03 -0500 | |
changeset 7407 | 47339ceb8cb0 |
parent 5506 | 202f599c92aa |
child 10328 | 06c93c42bca0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 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(); |
|
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
103 |
// First try the provider under test (if it does not support the |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
104 |
// necessary algorithm then try any registered provider). |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
105 |
try { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
106 |
cert.verify(key, p.getName()); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
107 |
} catch (NoSuchAlgorithmException e) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
108 |
System.out.println("Warning: " + e.getMessage() + |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
109 |
". Trying another provider..."); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
110 |
cert.verify(key); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
111 |
} |
2 | 112 |
} |
113 |
||
114 |
// try some random invalid signatures to make sure we get the correct |
|
115 |
// error |
|
116 |
System.out.println("Checking incorrect signatures..."); |
|
117 |
List<X509Certificate> certList = new ArrayList<X509Certificate>(certs.values()); |
|
118 |
for (int i = 0; i < 20; i++) { |
|
119 |
X509Certificate cert, signer; |
|
120 |
do { |
|
121 |
cert = getRandomCert(certList); |
|
122 |
signer = getRandomCert(certList); |
|
123 |
} while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal())); |
|
124 |
try { |
|
125 |
cert.verify(signer.getPublicKey()); |
|
126 |
throw new Exception("Verified invalid signature"); |
|
127 |
} catch (SignatureException e) { |
|
128 |
System.out.println("OK: " + e); |
|
129 |
} catch (InvalidKeyException e) { |
|
130 |
System.out.println("OK: " + e); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
Security.removeProvider(p.getName()); |
|
135 |
System.out.println("OK"); |
|
136 |
} |
|
137 |
||
138 |
private static X509Certificate getRandomCert(List<X509Certificate> certs) { |
|
139 |
int n = random.nextInt(certs.size()); |
|
140 |
return certs.get(n); |
|
141 |
} |
|
142 |
||
143 |
} |