Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/certpath/PKIX.java
41161 views
1
/*
2
* Copyright (c) 2012, 2020, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package sun.security.provider.certpath;
26
27
import java.security.InvalidAlgorithmParameterException;
28
import java.security.PublicKey;
29
import java.security.Timestamp;
30
import java.security.cert.*;
31
import java.security.interfaces.DSAPublicKey;
32
import java.util.*;
33
import javax.security.auth.x500.X500Principal;
34
35
import sun.security.util.Debug;
36
import sun.security.validator.Validator;
37
38
/**
39
* Common utility methods and classes used by the PKIX CertPathValidator and
40
* CertPathBuilder implementation.
41
*/
42
class PKIX {
43
44
private static final Debug debug = Debug.getInstance("certpath");
45
46
private PKIX() { }
47
48
static boolean isDSAPublicKeyWithoutParams(PublicKey publicKey) {
49
return (publicKey instanceof DSAPublicKey &&
50
((DSAPublicKey)publicKey).getParams() == null);
51
}
52
53
static ValidatorParams checkParams(CertPath cp, CertPathParameters params)
54
throws InvalidAlgorithmParameterException
55
{
56
if (!(params instanceof PKIXParameters)) {
57
throw new InvalidAlgorithmParameterException("inappropriate "
58
+ "params, must be an instance of PKIXParameters");
59
}
60
return new ValidatorParams(cp, (PKIXParameters)params);
61
}
62
63
static BuilderParams checkBuilderParams(CertPathParameters params)
64
throws InvalidAlgorithmParameterException
65
{
66
if (!(params instanceof PKIXBuilderParameters)) {
67
throw new InvalidAlgorithmParameterException("inappropriate "
68
+ "params, must be an instance of PKIXBuilderParameters");
69
}
70
return new BuilderParams((PKIXBuilderParameters)params);
71
}
72
73
/**
74
* PKIXParameters that are shared by the PKIX CertPathValidator
75
* implementation. Provides additional functionality and avoids
76
* unnecessary cloning.
77
*/
78
static class ValidatorParams {
79
private final PKIXParameters params;
80
private CertPath certPath;
81
private List<PKIXCertPathChecker> checkers;
82
private List<CertStore> stores;
83
private boolean gotDate;
84
private Date date;
85
private Set<String> policies;
86
private boolean gotConstraints;
87
private CertSelector constraints;
88
private Set<TrustAnchor> anchors;
89
private List<X509Certificate> certs;
90
private Timestamp timestamp;
91
private String variant = Validator.VAR_GENERIC;
92
93
ValidatorParams(CertPath cp, PKIXParameters params)
94
throws InvalidAlgorithmParameterException
95
{
96
this(params);
97
if (!cp.getType().equals("X.509") && !cp.getType().equals("X509")) {
98
throw new InvalidAlgorithmParameterException("inappropriate "
99
+ "CertPath type specified, must be X.509 or X509");
100
}
101
this.certPath = cp;
102
}
103
104
ValidatorParams(PKIXParameters params)
105
throws InvalidAlgorithmParameterException
106
{
107
if (params instanceof PKIXExtendedParameters) {
108
timestamp = ((PKIXExtendedParameters) params).getTimestamp();
109
variant = ((PKIXExtendedParameters) params).getVariant();
110
}
111
112
this.anchors = params.getTrustAnchors();
113
// Make sure that none of the trust anchors include name constraints
114
// (not supported).
115
for (TrustAnchor anchor : this.anchors) {
116
if (anchor.getNameConstraints() != null) {
117
throw new InvalidAlgorithmParameterException
118
("name constraints in trust anchor not supported");
119
}
120
}
121
this.params = params;
122
}
123
124
CertPath certPath() {
125
return certPath;
126
}
127
// called by CertPathBuilder after path has been built
128
void setCertPath(CertPath cp) {
129
this.certPath = cp;
130
}
131
List<X509Certificate> certificates() {
132
if (certs == null) {
133
if (certPath == null) {
134
certs = Collections.emptyList();
135
} else {
136
// Reverse the ordering for validation so that the target
137
// cert is the last certificate
138
@SuppressWarnings("unchecked")
139
List<X509Certificate> xc = new ArrayList<>
140
((List<X509Certificate>)certPath.getCertificates());
141
Collections.reverse(xc);
142
certs = xc;
143
}
144
}
145
return certs;
146
}
147
List<PKIXCertPathChecker> certPathCheckers() {
148
if (checkers == null)
149
checkers = params.getCertPathCheckers();
150
return checkers;
151
}
152
List<CertStore> certStores() {
153
if (stores == null)
154
stores = params.getCertStores();
155
return stores;
156
}
157
Date date() {
158
if (!gotDate) {
159
// use timestamp if checking signed code that is
160
// timestamped, otherwise use date parameter
161
if (timestamp != null &&
162
(variant.equals(Validator.VAR_CODE_SIGNING) ||
163
variant.equals(Validator.VAR_PLUGIN_CODE_SIGNING))) {
164
date = timestamp.getTimestamp();
165
} else {
166
date = params.getDate();
167
if (date == null)
168
date = new Date();
169
}
170
gotDate = true;
171
}
172
return date;
173
}
174
Set<String> initialPolicies() {
175
if (policies == null)
176
policies = params.getInitialPolicies();
177
return policies;
178
}
179
CertSelector targetCertConstraints() {
180
if (!gotConstraints) {
181
constraints = params.getTargetCertConstraints();
182
gotConstraints = true;
183
}
184
return constraints;
185
}
186
Set<TrustAnchor> trustAnchors() {
187
return anchors;
188
}
189
boolean revocationEnabled() {
190
return params.isRevocationEnabled();
191
}
192
boolean policyMappingInhibited() {
193
return params.isPolicyMappingInhibited();
194
}
195
boolean explicitPolicyRequired() {
196
return params.isExplicitPolicyRequired();
197
}
198
boolean policyQualifiersRejected() {
199
return params.getPolicyQualifiersRejected();
200
}
201
String sigProvider() { return params.getSigProvider(); }
202
boolean anyPolicyInhibited() { return params.isAnyPolicyInhibited(); }
203
204
// in rare cases we need access to the original params, for example
205
// in order to clone CertPathCheckers before building a new chain
206
PKIXParameters getPKIXParameters() {
207
return params;
208
}
209
210
String variant() {
211
return variant;
212
}
213
}
214
215
static class BuilderParams extends ValidatorParams {
216
private PKIXBuilderParameters params;
217
private List<CertStore> stores;
218
private X500Principal targetSubject;
219
220
BuilderParams(PKIXBuilderParameters params)
221
throws InvalidAlgorithmParameterException
222
{
223
super(params);
224
checkParams(params);
225
}
226
private void checkParams(PKIXBuilderParameters params)
227
throws InvalidAlgorithmParameterException
228
{
229
CertSelector sel = targetCertConstraints();
230
if (!(sel instanceof X509CertSelector)) {
231
throw new InvalidAlgorithmParameterException("the "
232
+ "targetCertConstraints parameter must be an "
233
+ "X509CertSelector");
234
}
235
this.params = params;
236
this.targetSubject = getTargetSubject(
237
certStores(), (X509CertSelector)targetCertConstraints());
238
}
239
@Override List<CertStore> certStores() {
240
if (stores == null) {
241
// reorder CertStores so that local CertStores are tried first
242
stores = new ArrayList<>(params.getCertStores());
243
Collections.sort(stores, new CertStoreComparator());
244
}
245
return stores;
246
}
247
int maxPathLength() { return params.getMaxPathLength(); }
248
PKIXBuilderParameters params() { return params; }
249
X500Principal targetSubject() { return targetSubject; }
250
251
/**
252
* Returns the target subject DN from the first X509Certificate that
253
* is fetched that matches the specified X509CertSelector.
254
*/
255
private static X500Principal getTargetSubject(List<CertStore> stores,
256
X509CertSelector sel)
257
throws InvalidAlgorithmParameterException
258
{
259
X500Principal subject = sel.getSubject();
260
if (subject != null) {
261
return subject;
262
}
263
X509Certificate cert = sel.getCertificate();
264
if (cert != null) {
265
subject = cert.getSubjectX500Principal();
266
}
267
if (subject != null) {
268
return subject;
269
}
270
for (CertStore store : stores) {
271
try {
272
Collection<? extends Certificate> certs =
273
(Collection<? extends Certificate>)
274
store.getCertificates(sel);
275
if (!certs.isEmpty()) {
276
X509Certificate xc =
277
(X509Certificate)certs.iterator().next();
278
return xc.getSubjectX500Principal();
279
}
280
} catch (CertStoreException e) {
281
// ignore but log it
282
if (debug != null) {
283
debug.println("BuilderParams.getTargetSubjectDN: " +
284
"non-fatal exception retrieving certs: " + e);
285
e.printStackTrace();
286
}
287
}
288
}
289
throw new InvalidAlgorithmParameterException
290
("Could not determine unique target subject");
291
}
292
}
293
294
/**
295
* A CertStoreException with additional information about the type of
296
* CertStore that generated the exception.
297
*/
298
static class CertStoreTypeException extends CertStoreException {
299
@java.io.Serial
300
private static final long serialVersionUID = 7463352639238322556L;
301
302
private final String type;
303
304
CertStoreTypeException(String type, CertStoreException cse) {
305
super(cse.getMessage(), cse.getCause());
306
this.type = type;
307
}
308
String getType() {
309
return type;
310
}
311
}
312
313
/**
314
* Comparator that orders CertStores so that local CertStores come before
315
* remote CertStores.
316
*/
317
private static class CertStoreComparator implements Comparator<CertStore> {
318
@Override
319
public int compare(CertStore store1, CertStore store2) {
320
if (store1.getType().equals("Collection") ||
321
store1.getCertStoreParameters() instanceof
322
CollectionCertStoreParameters) {
323
return -1;
324
} else {
325
return 1;
326
}
327
}
328
}
329
}
330
331