author | jjiang |
Wed, 15 Aug 2018 18:41:18 +0800 | |
changeset 51460 | 97e361fe3433 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2006, 2018, 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 |
||
40975
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
24 |
/* |
2 | 25 |
* @test |
25811
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
26 |
* @bug 6405536 6414980 8051972 |
2 | 27 |
* @summary Make sure that we can parse certificates using various named curves |
28 |
* and verify their signatures |
|
29 |
* @author Andreas Sterbenz |
|
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
30 |
* @library /test/lib .. |
10328 | 31 |
* @library ../../../../java/security/testlibrary |
43248
5e15de85a1a0
8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents:
42693
diff
changeset
|
32 |
* @modules jdk.crypto.cryptoki |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
33 |
* @run main/othervm ReadCertificates |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
34 |
* @run main/othervm ReadCertificates sm policy |
2 | 35 |
*/ |
36 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
37 |
import java.io.File; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
38 |
import java.io.FileInputStream; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
39 |
import java.io.InputStream; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
40 |
import java.security.InvalidKeyException; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
41 |
import java.security.NoSuchAlgorithmException; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
42 |
import java.security.NoSuchProviderException; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
43 |
import java.security.Provider; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
44 |
import java.security.PublicKey; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
45 |
import java.security.SecureRandom; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
46 |
import java.security.SignatureException; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
47 |
import java.security.cert.CertificateException; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
48 |
import java.security.cert.CertificateFactory; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
49 |
import java.security.cert.X509Certificate; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
50 |
import java.security.interfaces.ECPublicKey; |
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
51 |
import java.security.spec.ECParameterSpec; |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
52 |
import java.util.ArrayList; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
53 |
import java.util.Arrays; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
54 |
import java.util.Collection; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
55 |
import java.util.LinkedHashMap; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
56 |
import java.util.List; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
57 |
import java.util.Map; |
2 | 58 |
import javax.security.auth.x500.X500Principal; |
59 |
||
60 |
public class ReadCertificates extends PKCS11Test { |
|
61 |
||
62 |
private static CertificateFactory factory; |
|
63 |
||
64 |
private static SecureRandom random; |
|
65 |
||
66 |
private static Collection<X509Certificate> readCertificates(File file) throws Exception { |
|
67 |
System.out.println("Loading " + file.getName() + "..."); |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
68 |
Collection<X509Certificate> certs; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
69 |
try (InputStream in = new FileInputStream(file)) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
70 |
certs = (Collection<X509Certificate>)factory.generateCertificates(in); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
71 |
} |
2 | 72 |
return certs; |
73 |
} |
|
74 |
||
75 |
public static void main(String[] args) throws Exception { |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
76 |
main(new ReadCertificates(), args); |
2 | 77 |
} |
78 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
79 |
@Override |
2 | 80 |
public void main(Provider p) throws Exception { |
81 |
if (p.getService("Signature", "SHA1withECDSA") == null) { |
|
82 |
System.out.println("Provider does not support ECDSA, skipping..."); |
|
83 |
return; |
|
84 |
} |
|
13661
7c894680910a
6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents:
10328
diff
changeset
|
85 |
|
7c894680910a
6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents:
10328
diff
changeset
|
86 |
/* |
7c894680910a
6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents:
10328
diff
changeset
|
87 |
* PKCS11Test.main will remove this provider if needed |
7c894680910a
6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents:
10328
diff
changeset
|
88 |
*/ |
10328 | 89 |
Providers.setAt(p, 1); |
2 | 90 |
|
91 |
random = new SecureRandom(); |
|
92 |
factory = CertificateFactory.getInstance("X.509"); |
|
93 |
try { |
|
94 |
// clear certificate cache in from a previous run with a different |
|
95 |
// provider (undocumented hack for the Sun provider) |
|
96 |
factory.generateCertificate(null); |
|
97 |
} catch (CertificateException e) { |
|
98 |
// ignore |
|
99 |
} |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
100 |
Map<X500Principal,X509Certificate> certs = new LinkedHashMap<>(); |
2 | 101 |
|
102 |
File dir = new File(BASE, "certs"); |
|
103 |
File closedDir = new File(CLOSED_BASE, "certs"); |
|
104 |
File[] files = concat(dir.listFiles(), closedDir.listFiles()); |
|
105 |
Arrays.sort(files); |
|
106 |
for (File file : files) { |
|
107 |
if (file.isFile() == false) { |
|
108 |
continue; |
|
109 |
} |
|
110 |
Collection<X509Certificate> certList = readCertificates(file); |
|
111 |
for (X509Certificate cert : certList) { |
|
112 |
X509Certificate old = certs.put(cert.getSubjectX500Principal(), cert); |
|
113 |
if (old != null) { |
|
114 |
System.out.println("Duplicate subject:"); |
|
115 |
System.out.println("Old Certificate: " + old); |
|
116 |
System.out.println("New Certificate: " + cert); |
|
117 |
throw new Exception(file.getPath()); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
System.out.println("OK: " + certs.size() + " certificates."); |
|
122 |
||
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
123 |
// Get supported curves |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
124 |
List<ECParameterSpec> supportedEC = getKnownCurves(p); |
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
125 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
126 |
System.out.println("Test Certs:\n"); |
2 | 127 |
for (X509Certificate cert : certs.values()) { |
128 |
X509Certificate issuer = certs.get(cert.getIssuerX500Principal()); |
|
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
129 |
System.out.print("Verifying " + cert.getSubjectX500Principal() + |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
130 |
"... "); |
2 | 131 |
PublicKey key = issuer.getPublicKey(); |
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
132 |
// Check if curve is supported |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
133 |
if (issuer.getPublicKey() instanceof ECPublicKey) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
134 |
if (!checkSupport(supportedEC, |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
135 |
((ECPublicKey)key).getParams())) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
136 |
System.out.println("Curve not found. Skipped."); |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
14342
diff
changeset
|
137 |
continue; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
14342
diff
changeset
|
138 |
} |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
139 |
} |
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
140 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
141 |
try { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
142 |
cert.verify(key, p.getName()); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
143 |
System.out.println("Pass."); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
144 |
} catch (NoSuchAlgorithmException e) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
145 |
System.out.println("Warning: " + e.getMessage() + |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
146 |
". Trying another provider..."); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
147 |
cert.verify(key); |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
148 |
} catch (CertificateException | InvalidKeyException | |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
149 |
NoSuchProviderException | SignatureException e) { |
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
150 |
System.out.println(e.getMessage()); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
151 |
if (key instanceof ECPublicKey) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
152 |
System.out.println("Failed.\n\tCurve: " + |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
153 |
((ECPublicKey)key).getParams() + |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
154 |
"\n\tSignature Alg: " + cert.getSigAlgName()); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
155 |
} else { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
156 |
System.out.println("Key: "+key.toString()); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
157 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
158 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
159 |
System.err.println("Verifying " + cert.getSubjectX500Principal()); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
160 |
e.printStackTrace(); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
161 |
} |
2 | 162 |
} |
163 |
||
164 |
// try some random invalid signatures to make sure we get the correct |
|
165 |
// error |
|
166 |
System.out.println("Checking incorrect signatures..."); |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
167 |
List<X509Certificate> certList = new ArrayList<>(certs.values()); |
2 | 168 |
for (int i = 0; i < 20; i++) { |
169 |
X509Certificate cert, signer; |
|
170 |
do { |
|
171 |
cert = getRandomCert(certList); |
|
172 |
signer = getRandomCert(certList); |
|
173 |
} while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal())); |
|
174 |
try { |
|
25811
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
175 |
PublicKey signerPublicKey = signer.getPublicKey(); |
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
176 |
cert.verify(signerPublicKey); |
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
177 |
// Ignore false positives |
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
178 |
if (cert.getPublicKey().equals(signerPublicKey)) { |
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
179 |
System.out.println("OK: self-signed certificate detected"); |
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
180 |
} else { |
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
181 |
throw new Exception("Verified invalid signature"); |
f4d231e2c1bf
8051972: sun/security/pkcs11/ec/ReadCertificates.java fails intermittently
vinnie
parents:
21977
diff
changeset
|
182 |
} |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
25811
diff
changeset
|
183 |
} catch (SignatureException | InvalidKeyException e) { |
2 | 184 |
System.out.println("OK: " + e); |
185 |
} |
|
186 |
} |
|
187 |
||
188 |
System.out.println("OK"); |
|
189 |
} |
|
190 |
||
191 |
private static X509Certificate getRandomCert(List<X509Certificate> certs) { |
|
192 |
int n = random.nextInt(certs.size()); |
|
193 |
return certs.get(n); |
|
194 |
} |
|
195 |
||
196 |
} |