1
1
/*
2
- * Copyright (c) 2009, 2017 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2009, 2020 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
27
27
28
28
import java .security .AlgorithmConstraints ;
29
29
import java .security .CryptoPrimitive ;
30
- import java .security .Timestamp ;
31
- import java .security .cert .CertPathValidator ;
32
30
import java .util .Collection ;
33
31
import java .util .Collections ;
34
32
import java .util .Date ;
53
51
import java .security .interfaces .DSAPublicKey ;
54
52
import java .security .spec .DSAPublicKeySpec ;
55
53
56
- import sun .security .util .AnchorCertificates ;
57
54
import sun .security .util .ConstraintsParameters ;
58
55
import sun .security .util .Debug ;
59
56
import sun .security .util .DisabledAlgorithmConstraints ;
60
57
import sun .security .validator .Validator ;
58
+ import sun .security .x509 .AlgorithmId ;
61
59
import sun .security .x509 .X509CertImpl ;
62
60
import sun .security .x509 .X509CRLImpl ;
63
- import sun .security .x509 .AlgorithmId ;
64
61
65
62
/**
66
63
* A {@code PKIXCertPathChecker} implementation to check whether a
@@ -78,10 +75,10 @@ public final class AlgorithmChecker extends PKIXCertPathChecker {
78
75
79
76
private final AlgorithmConstraints constraints ;
80
77
private final PublicKey trustedPubKey ;
81
- private final Date pkixdate ;
78
+ private final Date date ;
82
79
private PublicKey prevPubKey ;
83
- private final Timestamp jarTimestamp ;
84
80
private final String variant ;
81
+ private TrustAnchor anchor ;
85
82
86
83
private static final Set <CryptoPrimitive > SIGNATURE_PRIMITIVE_SET =
87
84
Collections .unmodifiableSet (EnumSet .of (CryptoPrimitive .SIGNATURE ));
@@ -94,95 +91,70 @@ public final class AlgorithmChecker extends PKIXCertPathChecker {
94
91
CryptoPrimitive .KEY_AGREEMENT ));
95
92
96
93
private static final DisabledAlgorithmConstraints
97
- certPathDefaultConstraints = new DisabledAlgorithmConstraints (
98
- DisabledAlgorithmConstraints .PROPERTY_CERTPATH_DISABLED_ALGS );
99
-
100
- // If there is no "cacerts" keyword, then disable anchor checking
101
- private static final boolean publicCALimits =
102
- certPathDefaultConstraints .checkProperty ("jdkCA" );
103
-
104
- // If anchor checking enabled, this will be true if the trust anchor
105
- // has a match in the cacerts file
106
- private boolean trustedMatch = false ;
94
+ certPathDefaultConstraints =
95
+ DisabledAlgorithmConstraints .certPathConstraints ();
107
96
108
97
/**
109
- * Create a new {@code AlgorithmChecker} with the given algorithm
110
- * given {@code TrustAnchor} and {@code String} variant.
98
+ * Create a new {@code AlgorithmChecker} with the given
99
+ * {@code TrustAnchor} and {@code String} variant.
111
100
*
112
101
* @param anchor the trust anchor selected to validate the target
113
102
* certificate
114
- * @param variant is the Validator variants of the operation. A null value
103
+ * @param variant the Validator variant of the operation. A null value
115
104
* passed will set it to Validator.GENERIC.
116
105
*/
117
106
public AlgorithmChecker (TrustAnchor anchor , String variant ) {
118
- this (anchor , certPathDefaultConstraints , null , null , variant );
107
+ this (anchor , certPathDefaultConstraints , null , variant );
119
108
}
120
109
121
110
/**
122
111
* Create a new {@code AlgorithmChecker} with the given
123
- * {@code AlgorithmConstraints}, {@code Timestamp}, and {@code String}
124
- * variant.
112
+ * {@code AlgorithmConstraints} and {@code String} variant.
125
113
*
126
114
* Note that this constructor can initialize a variation of situations where
127
- * the AlgorithmConstraints, Timestamp, or Variant maybe known.
115
+ * the AlgorithmConstraints or Variant maybe known.
128
116
*
129
117
* @param constraints the algorithm constraints (or null)
130
- * @param jarTimestamp Timestamp passed for JAR timestamp constraint
131
- * checking. Set to null if not applicable.
132
- * @param variant is the Validator variants of the operation. A null value
118
+ * @param variant the Validator variant of the operation. A null value
133
119
* passed will set it to Validator.GENERIC.
134
120
*/
135
- public AlgorithmChecker (AlgorithmConstraints constraints ,
136
- Timestamp jarTimestamp , String variant ) {
137
- this (null , constraints , null , jarTimestamp , variant );
121
+ public AlgorithmChecker (AlgorithmConstraints constraints , String variant ) {
122
+ this (null , constraints , null , variant );
138
123
}
139
124
140
125
/**
141
126
* Create a new {@code AlgorithmChecker} with the
142
- * given {@code TrustAnchor}, {@code AlgorithmConstraints},
143
- * {@code Timestamp}, and {@code String} variant.
127
+ * given {@code TrustAnchor}, {@code AlgorithmConstraints}, {@code Date},
128
+ * and {@code String} variant.
144
129
*
145
130
* @param anchor the trust anchor selected to validate the target
146
131
* certificate
147
132
* @param constraints the algorithm constraints (or null)
148
- * @param pkixdate The date specified by the PKIXParameters date. If the
149
- * PKIXParameters is null, the current date is used. This
150
- * should be null when jar files are being checked.
151
- * @param jarTimestamp Timestamp passed for JAR timestamp constraint
152
- * checking. Set to null if not applicable.
153
- * @param variant is the Validator variants of the operation. A null value
133
+ * @param date the date specified by the PKIXParameters date, or the
134
+ * JAR timestamp if jar files are being validated and the
135
+ * JAR is timestamped. May be null if no timestamp or
136
+ * PKIXParameter date is set.
137
+ * @param variant the Validator variant of the operation. A null value
154
138
* passed will set it to Validator.GENERIC.
155
139
*/
156
140
public AlgorithmChecker (TrustAnchor anchor ,
157
- AlgorithmConstraints constraints , Date pkixdate ,
158
- Timestamp jarTimestamp , String variant ) {
141
+ AlgorithmConstraints constraints , Date date , String variant ) {
159
142
160
143
if (anchor != null ) {
161
144
if (anchor .getTrustedCert () != null ) {
162
145
this .trustedPubKey = anchor .getTrustedCert ().getPublicKey ();
163
- // Check for anchor certificate restrictions
164
- trustedMatch = checkFingerprint (anchor .getTrustedCert ());
165
- if (trustedMatch && debug != null ) {
166
- debug .println ("trustedMatch = true" );
167
- }
168
146
} else {
169
147
this .trustedPubKey = anchor .getCAPublicKey ();
170
148
}
149
+ this .anchor = anchor ;
171
150
} else {
172
151
this .trustedPubKey = null ;
173
- if (debug != null ) {
174
- debug .println ("TrustAnchor is null, trustedMatch is false." );
175
- }
176
152
}
177
153
178
154
this .prevPubKey = this .trustedPubKey ;
179
155
this .constraints = (constraints == null ? certPathDefaultConstraints :
180
156
constraints );
181
- // If we are checking jar files, set pkixdate the same as the timestamp
182
- // for certificate checking
183
- this .pkixdate = (jarTimestamp != null ? jarTimestamp .getTimestamp () :
184
- pkixdate );
185
- this .jarTimestamp = jarTimestamp ;
157
+ this .date = date ;
186
158
this .variant = (variant == null ? Validator .VAR_GENERIC : variant );
187
159
}
188
160
@@ -194,24 +166,11 @@ public AlgorithmChecker(TrustAnchor anchor,
194
166
* certificate
195
167
* @param pkixdate Date the constraints are checked against. The value is
196
168
* either the PKIXParameters date or null for the current date.
197
- * @param variant is the Validator variants of the operation. A null value
169
+ * @param variant the Validator variant of the operation. A null value
198
170
* passed will set it to Validator.GENERIC.
199
171
*/
200
172
public AlgorithmChecker (TrustAnchor anchor , Date pkixdate , String variant ) {
201
- this (anchor , certPathDefaultConstraints , pkixdate , null , variant );
202
- }
203
-
204
- // Check this 'cert' for restrictions in the AnchorCertificates
205
- // trusted certificates list
206
- private static boolean checkFingerprint (X509Certificate cert ) {
207
- if (!publicCALimits ) {
208
- return false ;
209
- }
210
-
211
- if (debug != null ) {
212
- debug .println ("AlgorithmChecker.contains: " + cert .getSigAlgName ());
213
- }
214
- return AnchorCertificates .contains (cert );
173
+ this (anchor , certPathDefaultConstraints , pkixdate , variant );
215
174
}
216
175
217
176
@ Override
@@ -318,18 +277,19 @@ public void check(Certificate cert,
318
277
}
319
278
320
279
ConstraintsParameters cp =
321
- new ConstraintsParameters (( X509Certificate ) cert ,
322
- trustedMatch , pkixdate , jarTimestamp , variant );
280
+ new CertPathConstraintsParameters ( x509Cert , variant ,
281
+ anchor , date );
323
282
324
283
// Check against local constraints if it is DisabledAlgorithmConstraints
325
284
if (constraints instanceof DisabledAlgorithmConstraints ) {
326
- ((DisabledAlgorithmConstraints )constraints ).permits (currSigAlg , cp );
285
+ ((DisabledAlgorithmConstraints )constraints ).permits (currSigAlg ,
286
+ currSigAlgParams , cp );
327
287
// DisabledAlgorithmsConstraints does not check primitives, so key
328
288
// additional key check.
329
289
330
290
} else {
331
291
// Perform the default constraints checking anyway.
332
- certPathDefaultConstraints .permits (currSigAlg , cp );
292
+ certPathDefaultConstraints .permits (currSigAlg , currSigAlgParams , cp );
333
293
// Call locally set constraints to check key with primitives.
334
294
if (!constraints .permits (primitives , currPubKey )) {
335
295
throw new CertPathValidatorException (
@@ -408,14 +368,10 @@ void trySetTrustAnchor(TrustAnchor anchor) {
408
368
// Don't bother to change the trustedPubKey.
409
369
if (anchor .getTrustedCert () != null ) {
410
370
prevPubKey = anchor .getTrustedCert ().getPublicKey ();
411
- // Check for anchor certificate restrictions
412
- trustedMatch = checkFingerprint (anchor .getTrustedCert ());
413
- if (trustedMatch && debug != null ) {
414
- debug .println ("trustedMatch = true" );
415
- }
416
371
} else {
417
372
prevPubKey = anchor .getCAPublicKey ();
418
373
}
374
+ this .anchor = anchor ;
419
375
}
420
376
}
421
377
@@ -424,11 +380,12 @@ void trySetTrustAnchor(TrustAnchor anchor) {
424
380
*
425
381
* @param key the public key to verify the CRL signature
426
382
* @param crl the target CRL
427
- * @param variant is the Validator variants of the operation. A null value
383
+ * @param variant the Validator variant of the operation. A null value
428
384
* passed will set it to Validator.GENERIC.
385
+ * @param anchor the trust anchor selected to validate the CRL issuer
429
386
*/
430
- static void check (PublicKey key , X509CRL crl , String variant )
431
- throws CertPathValidatorException {
387
+ static void check (PublicKey key , X509CRL crl , String variant ,
388
+ TrustAnchor anchor ) throws CertPathValidatorException {
432
389
433
390
X509CRLImpl x509CRLImpl = null ;
434
391
try {
@@ -438,24 +395,24 @@ static void check(PublicKey key, X509CRL crl, String variant)
438
395
}
439
396
440
397
AlgorithmId algorithmId = x509CRLImpl .getSigAlgId ();
441
- check (key , algorithmId , variant );
398
+ check (key , algorithmId , variant , anchor );
442
399
}
443
400
444
401
/**
445
402
* Check the signature algorithm with the specified public key.
446
403
*
447
404
* @param key the public key to verify the CRL signature
448
405
* @param algorithmId signature algorithm Algorithm ID
449
- * @param variant is the Validator variants of the operation. A null value
450
- * passed will set it to Validator.GENERIC.
406
+ * @param variant the Validator variant of the operation. A null
407
+ * value passed will set it to Validator.GENERIC.
408
+ * @param anchor the trust anchor selected to validate the public key
451
409
*/
452
- static void check (PublicKey key , AlgorithmId algorithmId , String variant )
453
- throws CertPathValidatorException {
454
- String sigAlgName = algorithmId .getName ();
455
- AlgorithmParameters sigAlgParams = algorithmId .getParameters ();
410
+ static void check (PublicKey key , AlgorithmId algorithmId , String variant ,
411
+ TrustAnchor anchor ) throws CertPathValidatorException {
456
412
457
- certPathDefaultConstraints .permits (new ConstraintsParameters (
458
- sigAlgName , sigAlgParams , key , variant ));
413
+ certPathDefaultConstraints .permits (algorithmId .getName (),
414
+ algorithmId .getParameters (),
415
+ new CertPathConstraintsParameters (key , variant , anchor ));
459
416
}
460
417
}
461
418
0 commit comments