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/ssl/X509TrustManagerImpl/Symantec/Distrust.java
38860 views
1
/*
2
* Copyright (c) 2018, 2019, 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
import java.io.*;
25
import java.math.BigInteger;
26
import java.security.*;
27
import java.security.cert.*;
28
import java.time.*;
29
import java.util.*;
30
import javax.net.ssl.*;
31
import sun.security.validator.Validator;
32
import sun.security.validator.ValidatorException;
33
34
/**
35
* @test
36
* @bug 8207258 8216280
37
* @summary Check that TLS Server certificates chaining back to distrusted
38
* Symantec roots are invalid
39
* @library /lib/security
40
* @run main/othervm Distrust after policyOn invalid
41
* @run main/othervm Distrust after policyOff valid
42
* @run main/othervm Distrust before policyOn valid
43
* @run main/othervm Distrust before policyOff valid
44
*/
45
46
public class Distrust {
47
48
private static final String TEST_SRC = System.getProperty("test.src", ".");
49
private static CertificateFactory cf;
50
51
// Each of the roots have a test certificate chain stored in a file
52
// named "<root>-chain.pem".
53
private static String[] rootsToTest = new String[] {
54
"geotrustglobalca", "geotrustprimarycag2", "geotrustprimarycag3",
55
"geotrustuniversalca", "thawteprimaryrootca", "thawteprimaryrootcag2",
56
"thawteprimaryrootcag3", "verisignclass3g3ca", "verisignclass3g4ca",
57
"verisignclass3g5ca", "verisignuniversalrootca" };
58
59
// Each of the subCAs with a delayed distrust date have a test certificate
60
// chain stored in a file named "<subCA>-chain.pem".
61
private static String[] subCAsToTest = new String[] {
62
"appleistca2g1", "appleistca8g1" };
63
64
// A date that is after the restrictions take affect
65
private static final Date APRIL_17_2019 =
66
Date.from(LocalDate.of(2019, 4, 17)
67
.atStartOfDay(ZoneOffset.UTC)
68
.toInstant());
69
70
// A date that is a second before the restrictions take affect
71
private static final Date BEFORE_APRIL_17_2019 =
72
Date.from(LocalDate.of(2019, 4, 17)
73
.atStartOfDay(ZoneOffset.UTC)
74
.minusSeconds(1)
75
.toInstant());
76
77
// A date that is after the subCA restrictions take affect
78
private static final Date JANUARY_1_2020 =
79
Date.from(LocalDate.of(2020, 1, 1)
80
.atStartOfDay(ZoneOffset.UTC)
81
.toInstant());
82
83
// A date that is a second before the subCA restrictions take affect
84
private static final Date BEFORE_JANUARY_1_2020 =
85
Date.from(LocalDate.of(2020, 1, 1)
86
.atStartOfDay(ZoneOffset.UTC)
87
.minusSeconds(1)
88
.toInstant());
89
90
public static void main(String[] args) throws Exception {
91
92
cf = CertificateFactory.getInstance("X.509");
93
94
boolean before = args[0].equals("before");
95
boolean policyOn = args[1].equals("policyOn");
96
boolean isValid = args[2].equals("valid");
97
98
if (!policyOn) {
99
// disable policy (default is on)
100
Security.setProperty("jdk.security.caDistrustPolicies", "");
101
}
102
103
Date notBefore = before ? BEFORE_APRIL_17_2019 : APRIL_17_2019;
104
105
X509TrustManager pkixTM = getTMF("PKIX", null);
106
X509TrustManager sunX509TM = getTMF("SunX509", null);
107
for (String test : rootsToTest) {
108
System.err.println("Testing " + test);
109
X509Certificate[] chain = loadCertificateChain(test);
110
111
testTM(sunX509TM, chain, notBefore, isValid);
112
testTM(pkixTM, chain, notBefore, isValid);
113
}
114
115
// test chain if params are passed to TrustManager
116
System.err.println("Testing verisignuniversalrootca with params");
117
testTM(getTMF("PKIX", getParams()),
118
loadCertificateChain("verisignuniversalrootca"),
119
notBefore, isValid);
120
121
// test code-signing chain (should be valid as restrictions don't apply)
122
System.err.println("Testing verisignclass3g5ca code-signing chain");
123
Validator v = Validator.getInstance(Validator.TYPE_PKIX,
124
Validator.VAR_CODE_SIGNING,
125
getParams());
126
// set validation date so this will still pass when cert expires
127
v.setValidationDate(new Date(1544197375493l));
128
v.validate(loadCertificateChain("verisignclass3g5ca-codesigning"));
129
130
// test chains issued through subCAs
131
notBefore = before ? BEFORE_JANUARY_1_2020 : JANUARY_1_2020;
132
for (String test : subCAsToTest) {
133
System.err.println("Testing " + test);
134
X509Certificate[] chain = loadCertificateChain(test);
135
136
testTM(sunX509TM, chain, notBefore, isValid);
137
testTM(pkixTM, chain, notBefore, isValid);
138
}
139
}
140
141
private static X509TrustManager getTMF(String type,
142
PKIXBuilderParameters params) throws Exception {
143
TrustManagerFactory tmf = TrustManagerFactory.getInstance(type);
144
if (params == null) {
145
tmf.init((KeyStore)null);
146
} else {
147
tmf.init(new CertPathTrustManagerParameters(params));
148
}
149
TrustManager[] tms = tmf.getTrustManagers();
150
for (TrustManager tm : tms) {
151
X509TrustManager xtm = (X509TrustManager)tm;
152
return xtm;
153
}
154
throw new Exception("No TrustManager for " + type);
155
}
156
157
private static PKIXBuilderParameters getParams() throws Exception {
158
PKIXBuilderParameters pbp =
159
new PKIXBuilderParameters(SecurityUtils.getCacertsKeyStore(),
160
new X509CertSelector());
161
pbp.setRevocationEnabled(false);
162
return pbp;
163
}
164
165
private static void testTM(X509TrustManager xtm, X509Certificate[] chain,
166
Date notBefore, boolean valid) throws Exception {
167
// Check if TLS Server certificate (the first element of the chain)
168
// is issued after the specified notBefore date (should be rejected
169
// unless distrust property is false). To do this, we need to
170
// fake the notBefore date since none of the test certs are issued
171
// after then.
172
chain[0] = new DistrustedTLSServerCert(chain[0], notBefore);
173
174
try {
175
xtm.checkServerTrusted(chain, "ECDHE_RSA");
176
if (!valid) {
177
throw new Exception("chain should be invalid");
178
}
179
} catch (CertificateException ce) {
180
if (valid) {
181
throw new Exception("Unexpected exception, chain " +
182
"should be valid", ce);
183
}
184
if (ce instanceof ValidatorException) {
185
ValidatorException ve = (ValidatorException)ce;
186
if (ve.getErrorType() != ValidatorException.T_UNTRUSTED_CERT) {
187
throw new Exception("Unexpected exception: " + ce);
188
}
189
} else {
190
throw new Exception("Unexpected exception: " + ce);
191
}
192
}
193
}
194
195
private static X509Certificate[] loadCertificateChain(String name)
196
throws Exception {
197
try (InputStream in = new FileInputStream(TEST_SRC + File.separator +
198
name + "-chain.pem")) {
199
Collection<X509Certificate> certs =
200
(Collection<X509Certificate>)cf.generateCertificates(in);
201
return certs.toArray(new X509Certificate[0]);
202
}
203
}
204
205
private static class DistrustedTLSServerCert extends X509Certificate {
206
private final X509Certificate cert;
207
private final Date notBefore;
208
DistrustedTLSServerCert(X509Certificate cert, Date notBefore) {
209
this.cert = cert;
210
this.notBefore = notBefore;
211
}
212
public Set<String> getCriticalExtensionOIDs() {
213
return cert.getCriticalExtensionOIDs();
214
}
215
public byte[] getExtensionValue(String oid) {
216
return cert.getExtensionValue(oid);
217
}
218
public Set<String> getNonCriticalExtensionOIDs() {
219
return cert.getNonCriticalExtensionOIDs();
220
}
221
public boolean hasUnsupportedCriticalExtension() {
222
return cert.hasUnsupportedCriticalExtension();
223
}
224
public void checkValidity() throws CertificateExpiredException,
225
CertificateNotYetValidException {
226
// always pass
227
}
228
public void checkValidity(Date date) throws CertificateExpiredException,
229
CertificateNotYetValidException {
230
// always pass
231
}
232
public int getVersion() { return cert.getVersion(); }
233
public BigInteger getSerialNumber() { return cert.getSerialNumber(); }
234
public Principal getIssuerDN() { return cert.getIssuerDN(); }
235
public Principal getSubjectDN() { return cert.getSubjectDN(); }
236
public Date getNotBefore() { return notBefore; }
237
public Date getNotAfter() { return cert.getNotAfter(); }
238
public byte[] getTBSCertificate() throws CertificateEncodingException {
239
return cert.getTBSCertificate();
240
}
241
public byte[] getSignature() { return cert.getSignature(); }
242
public String getSigAlgName() { return cert.getSigAlgName(); }
243
public String getSigAlgOID() { return cert.getSigAlgOID(); }
244
public byte[] getSigAlgParams() { return cert.getSigAlgParams(); }
245
public boolean[] getIssuerUniqueID() {
246
return cert.getIssuerUniqueID();
247
}
248
public boolean[] getSubjectUniqueID() {
249
return cert.getSubjectUniqueID();
250
}
251
public boolean[] getKeyUsage() { return cert.getKeyUsage(); }
252
public int getBasicConstraints() { return cert.getBasicConstraints(); }
253
public byte[] getEncoded() throws CertificateEncodingException {
254
return cert.getEncoded();
255
}
256
public void verify(PublicKey key) throws CertificateException,
257
InvalidKeyException, NoSuchAlgorithmException,
258
NoSuchProviderException, SignatureException {
259
cert.verify(key);
260
}
261
public void verify(PublicKey key, String sigProvider) throws
262
CertificateException, InvalidKeyException, NoSuchAlgorithmException,
263
NoSuchProviderException, SignatureException {
264
cert.verify(key, sigProvider);
265
}
266
public PublicKey getPublicKey() { return cert.getPublicKey(); }
267
public String toString() { return cert.toString(); }
268
}
269
}
270
271