author | ascarpino |
Wed, 08 Feb 2017 12:08:28 -0800 | |
changeset 43701 | fe8c324ba97c |
parent 39465 | 8c06651d9138 |
child 44158 | 49deb8a1ed3f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
2 |
* Copyright (c) 2002, 2017, 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 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32032
diff
changeset
|
60 |
private static final boolean checkTLSRevocation = |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
61 |
AccessController.doPrivileged |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
62 |
(new GetBooleanAction("com.sun.net.ssl.checkRevocation")); |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
63 |
|
2 | 64 |
private final Set<X509Certificate> trustedCerts; |
65 |
private final PKIXBuilderParameters parameterTemplate; |
|
66 |
private int certPathLength = -1; |
|
67 |
||
68 |
// needed only for the validator |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
69 |
private final Map<X500Principal, List<PublicKey>> trustedSubjects; |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
70 |
private final CertificateFactory factory; |
2 | 71 |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
72 |
private final boolean plugin; |
2 | 73 |
|
74 |
PKIXValidator(String variant, Collection<X509Certificate> trustedCerts) { |
|
75 |
super(TYPE_PKIX, variant); |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
76 |
this.trustedCerts = (trustedCerts instanceof Set) ? |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
77 |
(Set<X509Certificate>)trustedCerts : |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
78 |
new HashSet<X509Certificate>(trustedCerts); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
79 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
80 |
Set<TrustAnchor> trustAnchors = new HashSet<>(); |
2 | 81 |
for (X509Certificate cert : trustedCerts) { |
82 |
trustAnchors.add(new TrustAnchor(cert, null)); |
|
83 |
} |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
84 |
|
2 | 85 |
try { |
86 |
parameterTemplate = new PKIXBuilderParameters(trustAnchors, null); |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
87 |
factory = CertificateFactory.getInstance("X.509"); |
2 | 88 |
} catch (InvalidAlgorithmParameterException e) { |
89 |
throw new RuntimeException("Unexpected error: " + e.toString(), e); |
|
24704 | 90 |
} catch (CertificateException e) { |
91 |
throw new RuntimeException("Internal error", e); |
|
92 |
} |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
93 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
94 |
setDefaultParameters(variant); |
24704 | 95 |
plugin = variant.equals(VAR_PLUGIN_CODE_SIGNING); |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
96 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
97 |
trustedSubjects = setTrustedSubjects(); |
2 | 98 |
} |
99 |
||
100 |
PKIXValidator(String variant, PKIXBuilderParameters params) { |
|
101 |
super(TYPE_PKIX, variant); |
|
102 |
trustedCerts = new HashSet<X509Certificate>(); |
|
103 |
for (TrustAnchor anchor : params.getTrustAnchors()) { |
|
104 |
X509Certificate cert = anchor.getTrustedCert(); |
|
105 |
if (cert != null) { |
|
106 |
trustedCerts.add(cert); |
|
107 |
} |
|
108 |
} |
|
109 |
parameterTemplate = params; |
|
110 |
||
24704 | 111 |
try { |
112 |
factory = CertificateFactory.getInstance("X.509"); |
|
113 |
} catch (CertificateException e) { |
|
114 |
throw new RuntimeException("Internal error", e); |
|
115 |
} |
|
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
116 |
|
24704 | 117 |
plugin = variant.equals(VAR_PLUGIN_CODE_SIGNING); |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
118 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
119 |
trustedSubjects = setTrustedSubjects(); |
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 |
/** |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
123 |
* 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
|
124 |
* the list of trusted certificates |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
125 |
* |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
126 |
* @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
|
127 |
* tied to those DNs. |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
128 |
*/ |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
129 |
private Map<X500Principal, List<PublicKey>> setTrustedSubjects() { |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
130 |
Map<X500Principal, List<PublicKey>> subjectMap = new HashMap<>(); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
131 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
132 |
for (X509Certificate cert : trustedCerts) { |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
133 |
X500Principal dn = cert.getSubjectX500Principal(); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
134 |
List<PublicKey> keys; |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
135 |
if (subjectMap.containsKey(dn)) { |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
136 |
keys = subjectMap.get(dn); |
24704 | 137 |
} else { |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
138 |
keys = new ArrayList<PublicKey>(); |
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
139 |
subjectMap.put(dn, keys); |
24704 | 140 |
} |
24370
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
141 |
keys.add(cert.getPublicKey()); |
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 |
|
1398d972eef1
6996377: shrink duplicate code in the constructor of PKIXValidator
mullan
parents:
14342
diff
changeset
|
144 |
return subjectMap; |
2 | 145 |
} |
146 |
||
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 |
|
39465
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
198 |
// Check if 'parameter' affects 'pkixParameters' |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
199 |
PKIXBuilderParameters pkixParameters = null; |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
200 |
if (parameter instanceof Timestamp && plugin) { |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
201 |
try { |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
202 |
pkixParameters = new PKIXExtendedParameters( |
39465
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
203 |
(PKIXBuilderParameters) parameterTemplate.clone(), |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
204 |
(Timestamp) parameter, variant); |
39465
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
205 |
} catch (InvalidAlgorithmParameterException e) { |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
206 |
// ignore exception |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
207 |
} |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
208 |
} else { |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
209 |
pkixParameters = (PKIXBuilderParameters) parameterTemplate.clone(); |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
210 |
} |
8c06651d9138
8154015: Apply algorithm constraints to timestamped code
ascarpino
parents:
32649
diff
changeset
|
211 |
|
32032 | 212 |
// add new algorithm constraints checker |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
213 |
if (constraints != null) { |
43701
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
214 |
pkixParameters.addCertPathChecker( |
fe8c324ba97c
8160655: Fix denyAfter and usage types for security properties
ascarpino
parents:
39465
diff
changeset
|
215 |
new AlgorithmChecker(constraints, null, variant)); |
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
216 |
} |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
217 |
|
32032 | 218 |
// attach it to the PKIXBuilderParameters. |
219 |
if (!responseList.isEmpty()) { |
|
220 |
addResponses(pkixParameters, chain, responseList); |
|
221 |
} |
|
222 |
||
24704 | 223 |
// check that chain is in correct order and check if chain contains |
224 |
// trust anchor |
|
225 |
X500Principal prevIssuer = null; |
|
226 |
for (int i = 0; i < chain.length; i++) { |
|
227 |
X509Certificate cert = chain[i]; |
|
228 |
X500Principal dn = cert.getSubjectX500Principal(); |
|
229 |
if (i != 0 && !dn.equals(prevIssuer)) { |
|
230 |
// chain is not ordered correctly, call builder instead |
|
231 |
return doBuild(chain, otherCerts, pkixParameters); |
|
2 | 232 |
} |
233 |
||
24704 | 234 |
// Check if chain[i] is already trusted. It may be inside |
235 |
// trustedCerts, or has the same dn and public key as a cert |
|
236 |
// inside trustedCerts. The latter happens when a CA has |
|
237 |
// updated its cert with a stronger signature algorithm in JRE |
|
238 |
// but the weak one is still in circulation. |
|
239 |
||
240 |
if (trustedCerts.contains(cert) || // trusted cert |
|
241 |
(trustedSubjects.containsKey(dn) && // replacing ... |
|
242 |
trustedSubjects.get(dn).contains( // ... weak cert |
|
243 |
cert.getPublicKey()))) { |
|
244 |
if (i == 0) { |
|
245 |
return new X509Certificate[] {chain[0]}; |
|
246 |
} |
|
247 |
// Remove and call validator on partial chain [0 .. i-1] |
|
248 |
X509Certificate[] newChain = new X509Certificate[i]; |
|
249 |
System.arraycopy(chain, 0, newChain, 0, i); |
|
250 |
return doValidate(newChain, pkixParameters); |
|
2 | 251 |
} |
24704 | 252 |
prevIssuer = cert.getIssuerX500Principal(); |
253 |
} |
|
2 | 254 |
|
24704 | 255 |
// apparently issued by trust anchor? |
256 |
X509Certificate last = chain[chain.length - 1]; |
|
257 |
X500Principal issuer = last.getIssuerX500Principal(); |
|
258 |
X500Principal subject = last.getSubjectX500Principal(); |
|
259 |
if (trustedSubjects.containsKey(issuer) && |
|
260 |
isSignatureValid(trustedSubjects.get(issuer), last)) { |
|
261 |
return doValidate(chain, pkixParameters); |
|
262 |
} |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
263 |
|
24704 | 264 |
// don't fallback to builder if called from plugin/webstart |
265 |
if (plugin) { |
|
266 |
// Validate chain even if no trust anchor is found. This |
|
267 |
// allows plugin/webstart to make sure the chain is |
|
268 |
// otherwise valid |
|
269 |
if (chain.length > 1) { |
|
270 |
X509Certificate[] newChain = |
|
271 |
new X509Certificate[chain.length-1]; |
|
272 |
System.arraycopy(chain, 0, newChain, 0, newChain.length); |
|
273 |
||
274 |
// temporarily set last cert as sole trust anchor |
|
275 |
try { |
|
276 |
pkixParameters.setTrustAnchors |
|
277 |
(Collections.singleton(new TrustAnchor |
|
278 |
(chain[chain.length-1], null))); |
|
279 |
} catch (InvalidAlgorithmParameterException iape) { |
|
280 |
// should never occur, but ... |
|
281 |
throw new CertificateException(iape); |
|
2 | 282 |
} |
24704 | 283 |
doValidate(newChain, pkixParameters); |
2 | 284 |
} |
24704 | 285 |
// if the rest of the chain is valid, throw exception |
286 |
// indicating no trust anchor was found |
|
287 |
throw new ValidatorException |
|
288 |
(ValidatorException.T_NO_TRUST_ANCHOR); |
|
289 |
} |
|
290 |
// otherwise, fall back to builder |
|
2 | 291 |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
292 |
return doBuild(chain, otherCerts, pkixParameters); |
2 | 293 |
} |
294 |
||
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
295 |
private boolean isSignatureValid(List<PublicKey> keys, |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
296 |
X509Certificate sub) { |
2 | 297 |
if (plugin) { |
5782
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
298 |
for (PublicKey key: keys) { |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
299 |
try { |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
300 |
sub.verify(key); |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
301 |
return true; |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
302 |
} catch (Exception ex) { |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
303 |
continue; |
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
304 |
} |
2 | 305 |
} |
5782
50575882b36f
6958869: regression: PKIXValidator fails when multiple trust anchors have same dn
weijun
parents:
5627
diff
changeset
|
306 |
return false; |
2 | 307 |
} |
308 |
return true; // only check if PLUGIN is set |
|
309 |
} |
|
310 |
||
311 |
private static X509Certificate[] toArray(CertPath path, TrustAnchor anchor) |
|
312 |
throws CertificateException { |
|
313 |
List<? extends java.security.cert.Certificate> list = |
|
314 |
path.getCertificates(); |
|
315 |
X509Certificate[] chain = new X509Certificate[list.size() + 1]; |
|
316 |
list.toArray(chain); |
|
317 |
X509Certificate trustedCert = anchor.getTrustedCert(); |
|
318 |
if (trustedCert == null) { |
|
319 |
throw new ValidatorException |
|
320 |
("TrustAnchor must be specified as certificate"); |
|
321 |
} |
|
322 |
chain[chain.length - 1] = trustedCert; |
|
323 |
return chain; |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* Set the check date (for debugging). |
|
328 |
*/ |
|
329 |
private void setDate(PKIXBuilderParameters params) { |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
7040
diff
changeset
|
330 |
@SuppressWarnings("deprecation") |
2 | 331 |
Date date = validationDate; |
332 |
if (date != null) { |
|
333 |
params.setDate(date); |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
private X509Certificate[] doValidate(X509Certificate[] chain, |
|
338 |
PKIXBuilderParameters params) throws CertificateException { |
|
339 |
try { |
|
340 |
setDate(params); |
|
341 |
||
342 |
// do the validation |
|
343 |
CertPathValidator validator = CertPathValidator.getInstance("PKIX"); |
|
344 |
CertPath path = factory.generateCertPath(Arrays.asList(chain)); |
|
345 |
certPathLength = chain.length; |
|
346 |
PKIXCertPathValidatorResult result = |
|
347 |
(PKIXCertPathValidatorResult)validator.validate(path, params); |
|
348 |
||
349 |
return toArray(path, result.getTrustAnchor()); |
|
350 |
} catch (GeneralSecurityException e) { |
|
351 |
throw new ValidatorException |
|
352 |
("PKIX path validation failed: " + e.toString(), e); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
private X509Certificate[] doBuild(X509Certificate[] chain, |
|
7040
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
357 |
Collection<X509Certificate> otherCerts, |
659824c2a550
6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents:
5782
diff
changeset
|
358 |
PKIXBuilderParameters params) throws CertificateException { |
2 | 359 |
|
360 |
try { |
|
361 |
setDate(params); |
|
362 |
||
363 |
// setup target constraints |
|
364 |
X509CertSelector selector = new X509CertSelector(); |
|
365 |
selector.setCertificate(chain[0]); |
|
366 |
params.setTargetCertConstraints(selector); |
|
367 |
||
368 |
// setup CertStores |
|
369 |
Collection<X509Certificate> certs = |
|
370 |
new ArrayList<X509Certificate>(); |
|
371 |
certs.addAll(Arrays.asList(chain)); |
|
372 |
if (otherCerts != null) { |
|
373 |
certs.addAll(otherCerts); |
|
374 |
} |
|
375 |
CertStore store = CertStore.getInstance("Collection", |
|
376 |
new CollectionCertStoreParameters(certs)); |
|
377 |
params.addCertStore(store); |
|
378 |
||
379 |
// do the build |
|
380 |
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX"); |
|
381 |
PKIXCertPathBuilderResult result = |
|
382 |
(PKIXCertPathBuilderResult)builder.build(params); |
|
383 |
||
384 |
return toArray(result.getCertPath(), result.getTrustAnchor()); |
|
385 |
} catch (GeneralSecurityException e) { |
|
386 |
throw new ValidatorException |
|
387 |
("PKIX path building failed: " + e.toString(), e); |
|
388 |
} |
|
389 |
} |
|
32032 | 390 |
|
391 |
/** |
|
392 |
* For OCSP Stapling, add responses that came in during the handshake |
|
393 |
* into a {@code PKIXRevocationChecker} so we can evaluate them. |
|
394 |
* |
|
395 |
* @param pkixParams the pkixParameters object that will be used in |
|
396 |
* path validation. |
|
397 |
* @param chain the chain of certificates to verify |
|
398 |
* @param responseList a {@code List} of zero or more byte arrays, each |
|
399 |
* one being a DER-encoded OCSP response (per RFC 6960). Entries |
|
400 |
* in the List must match the order of the certificates in the |
|
401 |
* chain parameter. |
|
402 |
*/ |
|
403 |
private static void addResponses(PKIXBuilderParameters pkixParams, |
|
404 |
X509Certificate[] chain, List<byte[]> responseList) { |
|
405 |
||
406 |
if (pkixParams.isRevocationEnabled()) { |
|
407 |
try { |
|
408 |
// Make a modifiable copy of the CertPathChecker list |
|
409 |
PKIXRevocationChecker revChecker = null; |
|
410 |
List<PKIXCertPathChecker> checkerList = |
|
411 |
new ArrayList<>(pkixParams.getCertPathCheckers()); |
|
412 |
||
413 |
// Find the first PKIXRevocationChecker in the list |
|
414 |
for (PKIXCertPathChecker checker : checkerList) { |
|
415 |
if (checker instanceof PKIXRevocationChecker) { |
|
416 |
revChecker = (PKIXRevocationChecker)checker; |
|
417 |
break; |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
// If we still haven't found one, make one |
|
422 |
if (revChecker == null) { |
|
423 |
revChecker = (PKIXRevocationChecker)CertPathValidator. |
|
424 |
getInstance("PKIX").getRevocationChecker(); |
|
425 |
checkerList.add(revChecker); |
|
426 |
} |
|
427 |
||
428 |
// Each response in the list should be in parallel with |
|
429 |
// the certificate list. If there is a zero-length response |
|
430 |
// treat it as being absent. If the user has provided their |
|
431 |
// own PKIXRevocationChecker with pre-populated responses, do |
|
432 |
// not overwrite them with the ones from the handshake. |
|
433 |
Map<X509Certificate, byte[]> responseMap = |
|
434 |
revChecker.getOcspResponses(); |
|
435 |
int limit = Integer.min(chain.length, responseList.size()); |
|
436 |
for (int idx = 0; idx < limit; idx++) { |
|
437 |
byte[] respBytes = responseList.get(idx); |
|
438 |
if (respBytes != null && respBytes.length > 0 && |
|
439 |
!responseMap.containsKey(chain[idx])) { |
|
440 |
responseMap.put(chain[idx], respBytes); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
// Add the responses and push it all back into the |
|
445 |
// PKIXBuilderParameters |
|
446 |
revChecker.setOcspResponses(responseMap); |
|
447 |
pkixParams.setCertPathCheckers(checkerList); |
|
448 |
} catch (NoSuchAlgorithmException exc) { |
|
449 |
// This should not occur, but if it does happen then |
|
450 |
// stapled OCSP responses won't be part of revocation checking. |
|
451 |
// Clients can still fall back to other means of revocation |
|
452 |
// checking. |
|
453 |
} |
|
454 |
} |
|
455 |
} |
|
2 | 456 |
} |