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