author | jnimeh |
Thu, 22 Aug 2019 14:09:36 -0700 | |
changeset 57849 | e1269de19aa5 |
parent 51398 | 3c389a284345 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
2 |
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.validator; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
||
30 |
import java.security.*; |
|
31 |
import java.security.cert.*; |
|
32 |
||
33 |
import javax.security.auth.x500.X500Principal; |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
34 |
import sun.security.action.GetBooleanAction; |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
35 |
import sun.security.provider.certpath.AlgorithmChecker; |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
36 |
import sun.security.provider.certpath.PKIXExtendedParameters; |
2 | 37 |
|
38 |
/** |
|
39 |
* Validator implementation built on the PKIX CertPath API. This |
|
30374 | 40 |
* implementation will be emphasized going forward. |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
41 |
* <p> |
2 | 42 |
* Note that the validate() implementation tries to use a PKIX validator |
43 |
* if that appears possible and a PKIX builder otherwise. This increases |
|
44 |
* performance and currently also leads to better exception messages |
|
45 |
* in case of failures. |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
46 |
* <p> |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
47 |
* {@code PKIXValidator} objects are immutable once they have been created. |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
48 |
* Please DO NOT add methods that can change the state of an instance once |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
49 |
* it has been created. |
2 | 50 |
* |
51 |
* @author Andreas Sterbenz |
|
52 |
*/ |
|
53 |
public final class PKIXValidator extends Validator { |
|
54 |
||
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
55 |
/** |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
56 |
* Flag indicating whether to enable revocation check for the PKIX trust |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
57 |
* manager. Typically, this will only work if the PKIX implementation |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
58 |
* supports CRL distribution points as we do not manually setup CertStores. |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
59 |
*/ |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
60 |
private static final boolean checkTLSRevocation = GetBooleanAction |
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
61 |
.privilegedGetProperty("com.sun.net.ssl.checkRevocation"); |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
62 |
|
2 | 63 |
private final Set<X509Certificate> trustedCerts; |
64 |
private final PKIXBuilderParameters parameterTemplate; |
|
65 |
private int certPathLength = -1; |
|
66 |
||
67 |
// needed only for the validator |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
68 |
private final Map<X500Principal, List<PublicKey>> trustedSubjects; |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
69 |
private final CertificateFactory factory; |
2 | 70 |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
71 |
private final boolean plugin; |
2 | 72 |
|
73 |
PKIXValidator(String variant, Collection<X509Certificate> trustedCerts) { |
|
74 |
super(TYPE_PKIX, variant); |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
75 |
this.trustedCerts = (trustedCerts instanceof Set) ? |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
76 |
(Set<X509Certificate>)trustedCerts : |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
77 |
new HashSet<X509Certificate>(trustedCerts); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
78 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
79 |
Set<TrustAnchor> trustAnchors = new HashSet<>(); |
2 | 80 |
for (X509Certificate cert : trustedCerts) { |
81 |
trustAnchors.add(new TrustAnchor(cert, null)); |
|
82 |
} |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
83 |
|
2 | 84 |
try { |
85 |
parameterTemplate = new PKIXBuilderParameters(trustAnchors, null); |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
86 |
factory = CertificateFactory.getInstance("X.509"); |
2 | 87 |
} catch (InvalidAlgorithmParameterException e) { |
88 |
throw new RuntimeException("Unexpected error: " + e.toString(), e); |
|
24704 | 89 |
} catch (CertificateException e) { |
90 |
throw new RuntimeException("Internal error", e); |
|
91 |
} |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
92 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
93 |
setDefaultParameters(variant); |
24704 | 94 |
plugin = variant.equals(VAR_PLUGIN_CODE_SIGNING); |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
95 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
96 |
trustedSubjects = setTrustedSubjects(); |
2 | 97 |
} |
98 |
||
99 |
PKIXValidator(String variant, PKIXBuilderParameters params) { |
|
100 |
super(TYPE_PKIX, variant); |
|
101 |
trustedCerts = new HashSet<X509Certificate>(); |
|
102 |
for (TrustAnchor anchor : params.getTrustAnchors()) { |
|
103 |
X509Certificate cert = anchor.getTrustedCert(); |
|
104 |
if (cert != null) { |
|
105 |
trustedCerts.add(cert); |
|
106 |
} |
|
107 |
} |
|
108 |
parameterTemplate = params; |
|
109 |
||
24704 | 110 |
try { |
111 |
factory = CertificateFactory.getInstance("X.509"); |
|
112 |
} catch (CertificateException e) { |
|
113 |
throw new RuntimeException("Internal error", e); |
|
114 |
} |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
115 |
|
24704 | 116 |
plugin = variant.equals(VAR_PLUGIN_CODE_SIGNING); |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
117 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
118 |
trustedSubjects = setTrustedSubjects(); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
119 |
} |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
120 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
121 |
/** |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
122 |
* Populate the trustedSubjects Map using the DN and public keys from |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
123 |
* the list of trusted certificates |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
124 |
* |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
125 |
* @return Map containing each subject DN and one or more public keys |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
126 |
* tied to those DNs. |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
127 |
*/ |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
128 |
private Map<X500Principal, List<PublicKey>> setTrustedSubjects() { |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
129 |
Map<X500Principal, List<PublicKey>> subjectMap = new HashMap<>(); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
130 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
131 |
for (X509Certificate cert : trustedCerts) { |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
132 |
X500Principal dn = cert.getSubjectX500Principal(); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
133 |
List<PublicKey> keys; |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
134 |
if (subjectMap.containsKey(dn)) { |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
135 |
keys = subjectMap.get(dn); |
24704 | 136 |
} else { |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
137 |
keys = new ArrayList<PublicKey>(); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
138 |
subjectMap.put(dn, keys); |
24704 | 139 |
} |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
140 |
keys.add(cert.getPublicKey()); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
141 |
} |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
142 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
143 |
return subjectMap; |
2 | 144 |
} |
145 |
||
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
146 |
@Override |
2 | 147 |
public Collection<X509Certificate> getTrustedCertificates() { |
148 |
return trustedCerts; |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Returns the length of the last certification path that is validated by |
|
153 |
* CertPathValidator. This is intended primarily as a callback mechanism |
|
154 |
* for PKIXCertPathCheckers to determine the length of the certification |
|
155 |
* path that is being validated. It is necessary since engineValidate() |
|
156 |
* may modify the length of the path. |
|
157 |
* |
|
158 |
* @return the length of the last certification path passed to |
|
159 |
* CertPathValidator.validate, or -1 if it has not been invoked yet |
|
160 |
*/ |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
161 |
public int getCertPathLength() { // mutable, should be private |
2 | 162 |
return certPathLength; |
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Set J2SE global default PKIX parameters. Currently, hardcoded to disable |
|
167 |
* revocation checking. In the future, this should be configurable. |
|
168 |
*/ |
|
169 |
private void setDefaultParameters(String variant) { |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
170 |
if ((variant == Validator.VAR_TLS_SERVER) || |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
171 |
(variant == Validator.VAR_TLS_CLIENT)) { |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
172 |
parameterTemplate.setRevocationEnabled(checkTLSRevocation); |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
173 |
} else { |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
174 |
parameterTemplate.setRevocationEnabled(false); |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
175 |
} |
2 | 176 |
} |
177 |
||
178 |
/** |
|
179 |
* Return the PKIX parameters used by this instance. An application may |
|
180 |
* modify the parameters but must make sure not to perform any concurrent |
|
181 |
* validations. |
|
182 |
*/ |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
183 |
public PKIXBuilderParameters getParameters() { // mutable, should be private |
2 | 184 |
return parameterTemplate; |
185 |
} |
|
186 |
||
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
187 |
@Override |
2 | 188 |
X509Certificate[] engineValidate(X509Certificate[] chain, |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
189 |
Collection<X509Certificate> otherCerts, |
32032 | 190 |
List<byte[]> responseList, |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
191 |
AlgorithmConstraints constraints, |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
192 |
Object parameter) throws CertificateException { |
2 | 193 |
if ((chain == null) || (chain.length == 0)) { |
194 |
throw new CertificateException |
|
195 |
("null or zero-length certificate chain"); |
|
196 |
} |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
197 |
|
44158
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
198 |
// Use PKIXExtendedParameters for timestamp and variant additions |
39465
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
199 |
PKIXBuilderParameters pkixParameters = null; |
44158
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
200 |
try { |
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
201 |
pkixParameters = new PKIXExtendedParameters( |
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
202 |
(PKIXBuilderParameters) parameterTemplate.clone(), |
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
203 |
(parameter instanceof Timestamp) ? |
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
204 |
(Timestamp) parameter : null, |
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
205 |
variant); |
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
206 |
} catch (InvalidAlgorithmParameterException e) { |
49deb8a1ed3f
8176350: Usage constraints don't take effect when using PKIX
ascarpino
parents:
43701
diff
changeset
|
207 |
// ignore exception |
39465
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
208 |
} |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
209 |
|
32032 | 210 |
// add new algorithm constraints checker |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
211 |
if (constraints != null) { |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
212 |
pkixParameters.addCertPathChecker( |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
213 |
new AlgorithmChecker(constraints, null, variant)); |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
214 |
} |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
215 |
|
32032 | 216 |
// attach it to the PKIXBuilderParameters. |
217 |
if (!responseList.isEmpty()) { |
|
218 |
addResponses(pkixParameters, chain, responseList); |
|
219 |
} |
|
220 |
||
24704 | 221 |
// check that chain is in correct order and check if chain contains |
222 |
// trust anchor |
|
223 |
X500Principal prevIssuer = null; |
|
224 |
for (int i = 0; i < chain.length; i++) { |
|
225 |
X509Certificate cert = chain[i]; |
|
226 |
X500Principal dn = cert.getSubjectX500Principal(); |
|
227 |
if (i != 0 && !dn.equals(prevIssuer)) { |
|
228 |
// chain is not ordered correctly, call builder instead |
|
229 |
return doBuild(chain, otherCerts, pkixParameters); |
|
2 | 230 |
} |
231 |
||
24704 | 232 |
// Check if chain[i] is already trusted. It may be inside |
233 |
// trustedCerts, or has the same dn and public key as a cert |
|
234 |
// inside trustedCerts. The latter happens when a CA has |
|
235 |
// updated its cert with a stronger signature algorithm in JRE |
|
236 |
// but the weak one is still in circulation. |
|
237 |
||
238 |
if (trustedCerts.contains(cert) || // trusted cert |
|
239 |
(trustedSubjects.containsKey(dn) && // replacing ... |
|
240 |
trustedSubjects.get(dn).contains( // ... weak cert |
|
241 |
cert.getPublicKey()))) { |
|
242 |
if (i == 0) { |
|
243 |
return new X509Certificate[] {chain[0]}; |
|
244 |
} |
|
245 |
// Remove and call validator on partial chain [0 .. i-1] |
|
246 |
X509Certificate[] newChain = new X509Certificate[i]; |
|
247 |
System.arraycopy(chain, 0, newChain, 0, i); |
|
248 |
return doValidate(newChain, pkixParameters); |
|
2 | 249 |
} |
24704 | 250 |
prevIssuer = cert.getIssuerX500Principal(); |
251 |
} |
|
2 | 252 |
|
24704 | 253 |
// apparently issued by trust anchor? |
254 |
X509Certificate last = chain[chain.length - 1]; |
|
255 |
X500Principal issuer = last.getIssuerX500Principal(); |
|
256 |
X500Principal subject = last.getSubjectX500Principal(); |
|
257 |
if (trustedSubjects.containsKey(issuer) && |
|
258 |
isSignatureValid(trustedSubjects.get(issuer), last)) { |
|
259 |
return doValidate(chain, pkixParameters); |
|
260 |
} |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
261 |
|
24704 | 262 |
// don't fallback to builder if called from plugin/webstart |
263 |
if (plugin) { |
|
264 |
// Validate chain even if no trust anchor is found. This |
|
265 |
// allows plugin/webstart to make sure the chain is |
|
266 |
// otherwise valid |
|
267 |
if (chain.length > 1) { |
|
268 |
X509Certificate[] newChain = |
|
269 |
new X509Certificate[chain.length-1]; |
|
270 |
System.arraycopy(chain, 0, newChain, 0, newChain.length); |
|
271 |
||
272 |
// temporarily set last cert as sole trust anchor |
|
273 |
try { |
|
274 |
pkixParameters.setTrustAnchors |
|
275 |
(Collections.singleton(new TrustAnchor |
|
276 |
(chain[chain.length-1], null))); |
|
277 |
} catch (InvalidAlgorithmParameterException iape) { |
|
278 |
// should never occur, but ... |
|
279 |
throw new CertificateException(iape); |
|
2 | 280 |
} |
24704 | 281 |
doValidate(newChain, pkixParameters); |
2 | 282 |
} |
24704 | 283 |
// if the rest of the chain is valid, throw exception |
284 |
// indicating no trust anchor was found |
|
285 |
throw new ValidatorException |
|
286 |
(ValidatorException.T_NO_TRUST_ANCHOR); |
|
287 |
} |
|
288 |
// otherwise, fall back to builder |
|
2 | 289 |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
290 |
return doBuild(chain, otherCerts, pkixParameters); |
2 | 291 |
} |
292 |
||
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
293 |
private boolean isSignatureValid(List<PublicKey> keys, |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
294 |
X509Certificate sub) { |
2 | 295 |
if (plugin) { |
5782
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
296 |
for (PublicKey key: keys) { |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
297 |
try { |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
298 |
sub.verify(key); |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
299 |
return true; |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
300 |
} catch (Exception ex) { |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
301 |
continue; |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
302 |
} |
2 | 303 |
} |
5782
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
304 |
return false; |
2 | 305 |
} |
306 |
return true; // only check if PLUGIN is set |
|
307 |
} |
|
308 |
||
309 |
private static X509Certificate[] toArray(CertPath path, TrustAnchor anchor) |
|
310 |
throws CertificateException { |
|
311 |
List<? extends java.security.cert.Certificate> list = |
|
312 |
path.getCertificates(); |
|
313 |
X509Certificate[] chain = new X509Certificate[list.size() + 1]; |
|
314 |
list.toArray(chain); |
|
315 |
X509Certificate trustedCert = anchor.getTrustedCert(); |
|
316 |
if (trustedCert == null) { |
|
317 |
throw new ValidatorException |
|
318 |
("TrustAnchor must be specified as certificate"); |
|
319 |
} |
|
320 |
chain[chain.length - 1] = trustedCert; |
|
321 |
return chain; |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* Set the check date (for debugging). |
|
326 |
*/ |
|
327 |
private void setDate(PKIXBuilderParameters params) { |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
7040
diff
changeset
|
328 |
@SuppressWarnings("deprecation") |
2 | 329 |
Date date = validationDate; |
330 |
if (date != null) { |
|
331 |
params.setDate(date); |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
private X509Certificate[] doValidate(X509Certificate[] chain, |
|
336 |
PKIXBuilderParameters params) throws CertificateException { |
|
337 |
try { |
|
338 |
setDate(params); |
|
339 |
||
340 |
// do the validation |
|
341 |
CertPathValidator validator = CertPathValidator.getInstance("PKIX"); |
|
342 |
CertPath path = factory.generateCertPath(Arrays.asList(chain)); |
|
343 |
certPathLength = chain.length; |
|
344 |
PKIXCertPathValidatorResult result = |
|
345 |
(PKIXCertPathValidatorResult)validator.validate(path, params); |
|
346 |
||
347 |
return toArray(path, result.getTrustAnchor()); |
|
348 |
} catch (GeneralSecurityException e) { |
|
349 |
throw new ValidatorException |
|
350 |
("PKIX path validation failed: " + e.toString(), e); |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
private X509Certificate[] doBuild(X509Certificate[] chain, |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
355 |
Collection<X509Certificate> otherCerts, |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
356 |
PKIXBuilderParameters params) throws CertificateException { |
2 | 357 |
|
358 |
try { |
|
359 |
setDate(params); |
|
360 |
||
361 |
// setup target constraints |
|
362 |
X509CertSelector selector = new X509CertSelector(); |
|
363 |
selector.setCertificate(chain[0]); |
|
364 |
params.setTargetCertConstraints(selector); |
|
365 |
||
366 |
// setup CertStores |
|
367 |
Collection<X509Certificate> certs = |
|
368 |
new ArrayList<X509Certificate>(); |
|
369 |
certs.addAll(Arrays.asList(chain)); |
|
370 |
if (otherCerts != null) { |
|
371 |
certs.addAll(otherCerts); |
|
372 |
} |
|
373 |
CertStore store = CertStore.getInstance("Collection", |
|
374 |
new CollectionCertStoreParameters(certs)); |
|
375 |
params.addCertStore(store); |
|
376 |
||
377 |
// do the build |
|
378 |
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX"); |
|
379 |
PKIXCertPathBuilderResult result = |
|
380 |
(PKIXCertPathBuilderResult)builder.build(params); |
|
381 |
||
382 |
return toArray(result.getCertPath(), result.getTrustAnchor()); |
|
383 |
} catch (GeneralSecurityException e) { |
|
384 |
throw new ValidatorException |
|
385 |
("PKIX path building failed: " + e.toString(), e); |
|
386 |
} |
|
387 |
} |
|
32032 | 388 |
|
389 |
/** |
|
390 |
* For OCSP Stapling, add responses that came in during the handshake |
|
391 |
* into a {@code PKIXRevocationChecker} so we can evaluate them. |
|
392 |
* |
|
393 |
* @param pkixParams the pkixParameters object that will be used in |
|
394 |
* path validation. |
|
395 |
* @param chain the chain of certificates to verify |
|
396 |
* @param responseList a {@code List} of zero or more byte arrays, each |
|
397 |
* one being a DER-encoded OCSP response (per RFC 6960). Entries |
|
398 |
* in the List must match the order of the certificates in the |
|
399 |
* chain parameter. |
|
400 |
*/ |
|
401 |
private static void addResponses(PKIXBuilderParameters pkixParams, |
|
402 |
X509Certificate[] chain, List<byte[]> responseList) { |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
403 |
try { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
404 |
boolean createdRevChk = false; |
32032 | 405 |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
406 |
// Obtain the current CertPathChecker list |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
407 |
PKIXRevocationChecker revChecker = null; |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
408 |
List<PKIXCertPathChecker> checkerList = |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
409 |
pkixParams.getCertPathCheckers(); |
32032 | 410 |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
411 |
// Find the first PKIXRevocationChecker in the list |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
412 |
for (PKIXCertPathChecker checker : checkerList) { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
413 |
if (checker instanceof PKIXRevocationChecker) { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
414 |
revChecker = (PKIXRevocationChecker)checker; |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
415 |
break; |
32032 | 416 |
} |
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
417 |
} |
32032 | 418 |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
419 |
// If we still haven't found one, make one, unless revocation |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
420 |
// is disabled - then there's no point adding OCSP responses. |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
421 |
// If a PKIXRevocationChecker was added externally, then we |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
422 |
// must add the responses since revocation checking is performed |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
423 |
// independent of the revocation flag (per the |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
424 |
// PKIXRevocationChecker spec). |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
425 |
if (revChecker == null) { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
426 |
if (pkixParams.isRevocationEnabled()) { |
32032 | 427 |
revChecker = (PKIXRevocationChecker)CertPathValidator. |
428 |
getInstance("PKIX").getRevocationChecker(); |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
429 |
createdRevChk = true; |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
430 |
} else { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
431 |
return; |
32032 | 432 |
} |
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
433 |
} |
32032 | 434 |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
435 |
// Each response in the list should be in parallel with |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
436 |
// the certificate list. If there is a zero-length response |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
437 |
// treat it as being absent. If the user has provided their |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
438 |
// own PKIXRevocationChecker with pre-populated responses, do |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
439 |
// not overwrite them with the ones from the handshake. |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
440 |
Map<X509Certificate, byte[]> responseMap = |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
441 |
revChecker.getOcspResponses(); |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
442 |
int limit = Integer.min(chain.length, responseList.size()); |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
443 |
for (int idx = 0; idx < limit; idx++) { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
444 |
byte[] respBytes = responseList.get(idx); |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
445 |
if (respBytes != null && respBytes.length > 0 && |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
446 |
!responseMap.containsKey(chain[idx])) { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
447 |
responseMap.put(chain[idx], respBytes); |
32032 | 448 |
} |
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
449 |
} |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
450 |
revChecker.setOcspResponses(responseMap); |
32032 | 451 |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
452 |
// Add the responses and push it all back into the |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
453 |
// PKIXBuilderParameters |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
454 |
if (createdRevChk) { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
455 |
pkixParams.addCertPathChecker(revChecker); |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
456 |
} else { |
32032 | 457 |
pkixParams.setCertPathCheckers(checkerList); |
458 |
} |
|
57849
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
459 |
} catch (NoSuchAlgorithmException exc) { |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
460 |
// This should not occur, but if it does happen then |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
461 |
// stapled OCSP responses won't be part of revocation checking. |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
462 |
// Clients can still fall back to other means of revocation |
e1269de19aa5
8225436: Stapled OCSPResponses should be added to PKIXRevocationChecker irrespective of revocationEnabled flag
jnimeh
parents:
51398
diff
changeset
|
463 |
// checking. |
32032 | 464 |
} |
465 |
} |
|
2 | 466 |
} |