Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/ReadCertificates.java
38855 views
1
/*
2
* Copyright (c) 2006, 2016, Oracle and/or its affiliates. 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 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.
22
*/
23
24
/**
25
* @test
26
* @bug 6405536 6414980 8051972
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
* @library ../../../../java/security/testlibrary
32
* @run main/othervm ReadCertificates
33
* @run main/othervm ReadCertificates sm policy
34
*/
35
36
import java.io.File;
37
import java.io.FileInputStream;
38
import java.io.InputStream;
39
import java.security.InvalidKeyException;
40
import java.security.NoSuchAlgorithmException;
41
import java.security.NoSuchProviderException;
42
import java.security.Provider;
43
import java.security.PublicKey;
44
import java.security.SecureRandom;
45
import java.security.SignatureException;
46
import java.security.cert.CertificateException;
47
import java.security.cert.CertificateFactory;
48
import java.security.cert.X509Certificate;
49
import java.security.interfaces.ECPublicKey;
50
import java.security.spec.ECParameterSpec;
51
import java.util.ArrayList;
52
import java.util.Arrays;
53
import java.util.Collection;
54
import java.util.LinkedHashMap;
55
import java.util.List;
56
import java.util.Map;
57
import javax.security.auth.x500.X500Principal;
58
59
public class ReadCertificates extends PKCS11Test {
60
61
private static CertificateFactory factory;
62
63
private static SecureRandom random;
64
65
private static Collection<X509Certificate> readCertificates(File file) throws Exception {
66
System.out.println("Loading " + file.getName() + "...");
67
Collection<X509Certificate> certs;
68
try (InputStream in = new FileInputStream(file)) {
69
certs = (Collection<X509Certificate>)factory.generateCertificates(in);
70
}
71
return certs;
72
}
73
74
public static void main(String[] args) throws Exception {
75
main(new ReadCertificates(), args);
76
}
77
78
@Override
79
public void main(Provider p) throws Exception {
80
if (p.getService("Signature", "SHA1withECDSA") == null) {
81
System.out.println("Provider does not support ECDSA, skipping...");
82
return;
83
}
84
85
/*
86
* PKCS11Test.main will remove this provider if needed
87
*/
88
Providers.setAt(p, 1);
89
90
random = new SecureRandom();
91
factory = CertificateFactory.getInstance("X.509");
92
try {
93
// clear certificate cache in from a previous run with a different
94
// provider (undocumented hack for the Sun provider)
95
factory.generateCertificate(null);
96
} catch (CertificateException e) {
97
// ignore
98
}
99
Map<X500Principal,X509Certificate> certs = new LinkedHashMap<>();
100
101
File dir = new File(BASE, "certs");
102
File closedDir = new File(CLOSED_BASE, "certs");
103
File[] files = concat(dir.listFiles(), closedDir.listFiles());
104
Arrays.sort(files);
105
for (File file : files) {
106
if (file.isFile() == false) {
107
continue;
108
}
109
Collection<X509Certificate> certList = readCertificates(file);
110
for (X509Certificate cert : certList) {
111
X509Certificate old = certs.put(cert.getSubjectX500Principal(), cert);
112
if (old != null) {
113
System.out.println("Duplicate subject:");
114
System.out.println("Old Certificate: " + old);
115
System.out.println("New Certificate: " + cert);
116
throw new Exception(file.getPath());
117
}
118
}
119
}
120
System.out.println("OK: " + certs.size() + " certificates.");
121
122
// Get supported curves
123
List<ECParameterSpec> supportedEC = getKnownCurves(p);
124
125
System.out.println("Test Certs:\n");
126
for (X509Certificate cert : certs.values()) {
127
X509Certificate issuer = certs.get(cert.getIssuerX500Principal());
128
System.out.print("Verifying " + cert.getSubjectX500Principal() +
129
"... ");
130
PublicKey key = issuer.getPublicKey();
131
// Check if curve is supported
132
if (issuer.getPublicKey() instanceof ECPublicKey) {
133
if (!checkSupport(supportedEC,
134
((ECPublicKey)key).getParams())) {
135
System.out.println("Curve not found. Skipped.");
136
continue;
137
}
138
}
139
140
try {
141
cert.verify(key, p.getName());
142
System.out.println("Pass.");
143
} catch (NoSuchAlgorithmException e) {
144
System.out.println("Warning: " + e.getMessage() +
145
". Trying another provider...");
146
cert.verify(key);
147
} catch (CertificateException | InvalidKeyException |
148
NoSuchProviderException | SignatureException e) {
149
System.out.println(e.getMessage());
150
if (key instanceof ECPublicKey) {
151
System.out.println("Failed.\n\tCurve: " +
152
((ECPublicKey)key).getParams() +
153
"\n\tSignature Alg: " + cert.getSigAlgName());
154
} else {
155
System.out.println("Key: "+key.toString());
156
}
157
158
System.err.println("Verifying " + cert.getSubjectX500Principal());
159
e.printStackTrace();
160
}
161
}
162
163
// try some random invalid signatures to make sure we get the correct
164
// error
165
System.out.println("Checking incorrect signatures...");
166
List<X509Certificate> certList = new ArrayList<>(certs.values());
167
for (int i = 0; i < 20; i++) {
168
X509Certificate cert, signer;
169
do {
170
cert = getRandomCert(certList);
171
signer = getRandomCert(certList);
172
} while (cert.getIssuerX500Principal().equals(signer.getSubjectX500Principal()));
173
try {
174
PublicKey signerPublicKey = signer.getPublicKey();
175
cert.verify(signerPublicKey);
176
// Ignore false positives
177
if (cert.getPublicKey().equals(signerPublicKey)) {
178
System.out.println("OK: self-signed certificate detected");
179
} else {
180
throw new Exception("Verified invalid signature");
181
}
182
} catch (SignatureException | InvalidKeyException e) {
183
System.out.println("OK: " + e);
184
}
185
}
186
187
System.out.println("OK");
188
}
189
190
private static X509Certificate getRandomCert(List<X509Certificate> certs) {
191
int n = random.nextInt(certs.size());
192
return certs.get(n);
193
}
194
195
}
196
197