Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/validator/EndEntityExtensionCheck.java
41149 views
1
/*
2
* Copyright (c) 2015, 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
/*
25
* @test
26
* @bug 8076117
27
* @summary EndEntityChecker should not process custom extensions
28
* after PKIX validation
29
* @modules java.base/sun.security.validator
30
* @run main/othervm -Djdk.security.allowNonCaAnchor EndEntityExtensionCheck
31
*/
32
33
import java.io.ByteArrayInputStream;
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.security.KeyStore;
37
import java.security.cert.CertPathValidatorException;
38
import java.security.cert.Certificate;
39
import java.security.cert.CertificateException;
40
import java.security.cert.CertificateFactory;
41
import java.security.cert.PKIXBuilderParameters;
42
import java.security.cert.PKIXCertPathChecker;
43
import java.security.cert.TrustAnchor;
44
import java.security.cert.X509Certificate;
45
import java.util.Collection;
46
import java.util.Date;
47
import java.util.HashSet;
48
import java.util.Set;
49
import sun.security.validator.TrustStoreUtil;
50
import sun.security.validator.Validator;
51
52
53
public class EndEntityExtensionCheck {
54
55
/*
56
* Owner: CN=TestCA
57
* Issuer: CN=TestCA
58
*/
59
private static final String CA =
60
"-----BEGIN CERTIFICATE-----\n" +
61
"MIICgDCCAj2gAwIBAgIEC18hWjALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +
62
"dENBMB4XDTE1MDQwNzIyMzUyMFoXDTI1MDQwNjIyMzUyMFowETEPMA0GA1UEAxMG\n" +
63
"VGVzdENBMIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +
64
"EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +
65
"mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +
66
"rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +
67
"Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +
68
"FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +
69
"kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYUAAoGBAJOWy2hVy4iNwsi/idWG\n" +
70
"oksr9IZxQIFR2YavoUmD+rIgfYUpiCihzftDLMMaNYqp9PPxuOyoIPGPbwmKpAs5\n" +
71
"nq6gLwH2lSsN+EwyV2SJ0J26PHiMuRNZWWfKR3cpEqbQVb0CmvqSpj8zYfamPzp7\n" +
72
"eXSWwahzgLCGJM3SgCfDFC0uoyEwHzAdBgNVHQ4EFgQU7tLD8FnWM+r6jBr+mCXs\n" +
73
"8G5yBpgwCwYHKoZIzjgEAwUAAzAAMC0CFQCHCtzC3S0ST0EZBucikVui4WXD8QIU\n" +
74
"L3Oxy6989/FhZlZWJlhqc1ungEQ=\n" +
75
"-----END CERTIFICATE-----";
76
77
/*
78
* Owner: CN=TestEE
79
* Issuer: CN=TestCA
80
* Contains a custom critical extension with OID 1.2.3.4:
81
* #1: ObjectId: 1.2.3.4 Criticality=true
82
* 0000: 00 00
83
*/
84
private static final String EE =
85
"-----BEGIN CERTIFICATE-----\n" +
86
"MIICrTCCAmugAwIBAgIELjciKzALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +
87
"dENBMB4XDTE1MDQwNzIzMDA1OFoXDTE1MDcwNjIzMDA1OFowETEPMA0GA1UEAxMG\n" +
88
"VGVzdEVFMIIBtzCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +
89
"EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +
90
"mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +
91
"rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +
92
"Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +
93
"FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +
94
"kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYQAAoGAN97otrAJEuUg/O97vScI\n" +
95
"01xs1jqTz5o0PGpKiDDJNB3tCCUbLqXoBQBvSefQ8vYL3mmlEJLxlwfbajRmJQp0\n" +
96
"tUy5SUCZHk3MdoKxSvrqYnVpYwJHFXKWs6lAawxfuWbkm9SREuepOWnVzy2ecf5z\n" +
97
"hvy9mgEBfi4E9Cy8Byq2TpyjUDBOMAwGAyoDBAEB/wQCAAAwHwYDVR0jBBgwFoAU\n" +
98
"7tLD8FnWM+r6jBr+mCXs8G5yBpgwHQYDVR0OBBYEFNRVqt5F+EAuJ5x1IZLDkoMs\n" +
99
"mDj4MAsGByqGSM44BAMFAAMvADAsAhQyNGhxIp5IshN1zqLs4pUY214IMAIUMmTL\n" +
100
"3ZMpMAjITbuHHlFNUqZ7A9s=\n" +
101
"-----END CERTIFICATE-----";
102
103
public static void main(String[] args) throws Exception {
104
X509Certificate[] chain = createChain();
105
106
/* Test 1: Test SimpleValidator
107
* SimpleValidator doesn't check for unsupported critical
108
* extensions in the end entity certificate, and leaves that up
109
* to EndEntityChecker, which should catch such extensions.
110
*/
111
KeyStore ks = KeyStore.getInstance("JKS");
112
ks.load(null, null);
113
ks.setCertificateEntry("testca", chain[chain.length - 1]);
114
115
Validator v = Validator.getInstance(Validator.TYPE_SIMPLE,
116
Validator.VAR_TLS_CLIENT,
117
TrustStoreUtil.getTrustedCerts(ks));
118
try {
119
v.validate(chain);
120
throw new Exception("Chain should not have validated " +
121
"successfully.");
122
} catch (CertificateException ex) {
123
// EE cert has an unsupported critical extension that is not
124
// checked by SimpleValidator's extension checks, so this
125
// failure is expected
126
}
127
128
/* Test 2: Test PKIXValidator without custom checker
129
* PKIXValidator accepts PKIXParameters that can contain
130
* custom PKIXCertPathCheckers, which would be run against
131
* each cert in the chain, including EE certs.
132
* Check that if PKIXValidator is not provided a custom
133
* PKIXCertPathChecker for an unknown critical extension in
134
* the EE cert, chain validation will fail.
135
*/
136
TrustAnchor ta = new TrustAnchor(chain[chain.length - 1], null);
137
Set<TrustAnchor> tas = new HashSet<>();
138
tas.add(ta);
139
PKIXBuilderParameters params = new PKIXBuilderParameters(tas, null);
140
params.setDate(new Date(115, 5, 1)); // 2015-05-01
141
params.setRevocationEnabled(false);
142
143
v = Validator.getInstance(Validator.TYPE_PKIX,
144
Validator.VAR_TLS_CLIENT,
145
params);
146
try {
147
v.validate(chain);
148
throw new Exception("Chain should not have validated " +
149
"successfully.");
150
} catch (CertificateException ex) {
151
// EE cert has an unsupported critical extension and
152
// PKIXValidator was not provided any custom checker
153
// for it, so this failure ie expected.
154
}
155
156
/* Test 3: Test PKIXValidator with custom checker
157
* Check that PKIXValidator will successfully validate a chain
158
* containing an EE cert with a critical custom extension, given
159
* a corresponding PKIXCertPathChecker for the extension.
160
*/
161
params = new PKIXBuilderParameters(tas, null);
162
params.addCertPathChecker(new CustomChecker());
163
params.setDate(new Date(115, 5, 1)); // 2015-05-01
164
params.setRevocationEnabled(false);
165
166
v = Validator.getInstance(Validator.TYPE_PKIX,
167
Validator.VAR_TLS_CLIENT,
168
params);
169
v.validate(chain); // This should validate successfully
170
171
System.out.println("Tests passed.");
172
}
173
174
public static X509Certificate[] createChain() throws Exception {
175
CertificateFactory cf = CertificateFactory.getInstance("X.509");
176
X509Certificate ee = (X509Certificate)
177
cf.generateCertificate((new ByteArrayInputStream(EE.getBytes())));
178
X509Certificate ca = (X509Certificate)
179
cf.generateCertificate((new ByteArrayInputStream(CA.getBytes())));
180
181
X509Certificate[] chain = {ee, ca};
182
return chain;
183
}
184
185
/*
186
* A custom PKIXCertPathChecker. Looks for a critical extension
187
* in an end entity certificate with the OID 1.2.3.4.
188
*/
189
static class CustomChecker extends PKIXCertPathChecker {
190
191
@Override
192
public void init(boolean forward) throws CertPathValidatorException {
193
// nothing to do
194
}
195
196
@Override
197
public boolean isForwardCheckingSupported() {
198
return false;
199
}
200
201
@Override
202
public Set<String> getSupportedExtensions() {
203
Set<String> exts = new HashSet<>();
204
exts.add("1.2.3.4");
205
return exts;
206
}
207
208
@Override
209
public void check(Certificate cert,
210
Collection<String> unresolvedCritExts)
211
throws CertPathValidatorException {
212
X509Certificate currCert = (X509Certificate)cert;
213
// check that this is an EE cert
214
if (currCert.getBasicConstraints() == -1) {
215
if (unresolvedCritExts != null &&
216
!unresolvedCritExts.isEmpty()) {
217
unresolvedCritExts.remove("1.2.3.4");
218
}
219
}
220
}
221
222
}
223
}
224
225