author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44046 | jdk/src/java.base/share/classes/sun/security/provider/certpath/BasicChecker.java@762e807bfac1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, 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.provider.certpath; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
import java.util.Collection; |
|
30 |
import java.util.Date; |
|
31 |
import java.util.Set; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
32 |
import java.security.GeneralSecurityException; |
2 | 33 |
import java.security.KeyFactory; |
34 |
import java.security.PublicKey; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
35 |
import java.security.SignatureException; |
2 | 36 |
import java.security.cert.Certificate; |
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
37 |
import java.security.cert.CertificateExpiredException; |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
38 |
import java.security.cert.CertificateNotYetValidException; |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
39 |
import java.security.cert.CertPathValidatorException; |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
40 |
import java.security.cert.CertPathValidatorException.BasicReason; |
2 | 41 |
import java.security.cert.X509Certificate; |
42 |
import java.security.cert.PKIXCertPathChecker; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
43 |
import java.security.cert.PKIXReason; |
2 | 44 |
import java.security.cert.TrustAnchor; |
45 |
import java.security.interfaces.DSAParams; |
|
46 |
import java.security.interfaces.DSAPublicKey; |
|
47 |
import java.security.spec.DSAPublicKeySpec; |
|
48 |
import javax.security.auth.x500.X500Principal; |
|
49 |
import sun.security.x509.X500Name; |
|
50 |
import sun.security.util.Debug; |
|
51 |
||
52 |
/** |
|
53 |
* BasicChecker is a PKIXCertPathChecker that checks the basic information |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
25859
diff
changeset
|
54 |
* on a PKIX certificate, namely the signature, validity, and subject/issuer |
2 | 55 |
* name chaining. |
56 |
* |
|
57 |
* @since 1.4 |
|
58 |
* @author Yassir Elley |
|
59 |
*/ |
|
60 |
class BasicChecker extends PKIXCertPathChecker { |
|
61 |
||
62 |
private static final Debug debug = Debug.getInstance("certpath"); |
|
63 |
private final PublicKey trustedPubKey; |
|
64 |
private final X500Principal caName; |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
65 |
private final Date date; |
2 | 66 |
private final String sigProvider; |
67 |
private final boolean sigOnly; |
|
68 |
private X500Principal prevSubject; |
|
69 |
private PublicKey prevPubKey; |
|
70 |
||
71 |
/** |
|
72 |
* Constructor that initializes the input parameters. |
|
73 |
* |
|
74 |
* @param anchor the anchor selected to validate the target certificate |
|
75 |
* @param testDate the time for which the validity of the certificate |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
76 |
* should be determined |
2 | 77 |
* @param sigProvider the name of the signature provider |
78 |
* @param sigOnly true if only signature checking is to be done; |
|
79 |
* if false, all checks are done |
|
80 |
*/ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
81 |
BasicChecker(TrustAnchor anchor, Date date, String sigProvider, |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
82 |
boolean sigOnly) { |
2 | 83 |
if (anchor.getTrustedCert() != null) { |
84 |
this.trustedPubKey = anchor.getTrustedCert().getPublicKey(); |
|
85 |
this.caName = anchor.getTrustedCert().getSubjectX500Principal(); |
|
86 |
} else { |
|
87 |
this.trustedPubKey = anchor.getCAPublicKey(); |
|
88 |
this.caName = anchor.getCA(); |
|
89 |
} |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
90 |
this.date = date; |
2 | 91 |
this.sigProvider = sigProvider; |
92 |
this.sigOnly = sigOnly; |
|
13021
72a4386f8976
7176326: CertPath/CertPathBuilderTest failures after webrev 6854712_6637288_7126011
mullan
parents:
12860
diff
changeset
|
93 |
this.prevPubKey = trustedPubKey; |
2 | 94 |
} |
95 |
||
96 |
/** |
|
97 |
* Initializes the internal state of the checker from parameters |
|
98 |
* specified in the constructor. |
|
99 |
*/ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
100 |
@Override |
2 | 101 |
public void init(boolean forward) throws CertPathValidatorException { |
102 |
if (!forward) { |
|
103 |
prevPubKey = trustedPubKey; |
|
13806
b18118646a65
7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents:
13555
diff
changeset
|
104 |
if (PKIX.isDSAPublicKeyWithoutParams(prevPubKey)) { |
13555
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
105 |
// If TrustAnchor is a DSA public key and it has no params, it |
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
106 |
// cannot be used to verify the signature of the first cert, |
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
107 |
// so throw exception |
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
108 |
throw new CertPathValidatorException("Key parameters missing"); |
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
109 |
} |
2 | 110 |
prevSubject = caName; |
111 |
} else { |
|
112 |
throw new |
|
113 |
CertPathValidatorException("forward checking not supported"); |
|
114 |
} |
|
115 |
} |
|
116 |
||
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
117 |
@Override |
2 | 118 |
public boolean isForwardCheckingSupported() { |
119 |
return false; |
|
120 |
} |
|
121 |
||
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
122 |
@Override |
2 | 123 |
public Set<String> getSupportedExtensions() { |
124 |
return null; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
25859
diff
changeset
|
128 |
* Performs the signature, validity, and subject/issuer name chaining |
2 | 129 |
* checks on the certificate using its internal state. This method does |
130 |
* not remove any critical extensions from the Collection. |
|
131 |
* |
|
132 |
* @param cert the Certificate |
|
133 |
* @param unresolvedCritExts a Collection of the unresolved critical |
|
134 |
* extensions |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
135 |
* @throws CertPathValidatorException if certificate does not verify |
2 | 136 |
*/ |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
137 |
@Override |
2 | 138 |
public void check(Certificate cert, Collection<String> unresolvedCritExts) |
139 |
throws CertPathValidatorException |
|
140 |
{ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
141 |
X509Certificate currCert = (X509Certificate)cert; |
2 | 142 |
|
143 |
if (!sigOnly) { |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
25859
diff
changeset
|
144 |
verifyValidity(currCert); |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
145 |
verifyNameChaining(currCert); |
2 | 146 |
} |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
147 |
verifySignature(currCert); |
2 | 148 |
|
149 |
updateState(currCert); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
153 |
* Verifies the signature on the certificate using the previous public key. |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
154 |
* |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
155 |
* @param cert the X509Certificate |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
156 |
* @throws CertPathValidatorException if certificate does not verify |
2 | 157 |
*/ |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
158 |
private void verifySignature(X509Certificate cert) |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
159 |
throws CertPathValidatorException |
2 | 160 |
{ |
161 |
String msg = "signature"; |
|
162 |
if (debug != null) |
|
163 |
debug.println("---checking " + msg + "..."); |
|
164 |
||
165 |
try { |
|
166 |
cert.verify(prevPubKey, sigProvider); |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
167 |
} catch (SignatureException e) { |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
168 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
169 |
(msg + " check failed", e, null, -1, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
170 |
BasicReason.INVALID_SIGNATURE); |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
171 |
} catch (GeneralSecurityException e) { |
2 | 172 |
throw new CertPathValidatorException(msg + " check failed", e); |
173 |
} |
|
174 |
||
175 |
if (debug != null) |
|
176 |
debug.println(msg + " verified."); |
|
177 |
} |
|
178 |
||
179 |
/** |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
25859
diff
changeset
|
180 |
* Internal method to verify the validity on a certificate |
2 | 181 |
*/ |
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
25859
diff
changeset
|
182 |
private void verifyValidity(X509Certificate cert) |
2 | 183 |
throws CertPathValidatorException |
184 |
{ |
|
44046
762e807bfac1
8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents:
25859
diff
changeset
|
185 |
String msg = "validity"; |
2 | 186 |
if (debug != null) |
187 |
debug.println("---checking " + msg + ":" + date.toString() + "..."); |
|
188 |
||
189 |
try { |
|
190 |
cert.checkValidity(date); |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
191 |
} catch (CertificateExpiredException e) { |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
192 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
193 |
(msg + " check failed", e, null, -1, BasicReason.EXPIRED); |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
194 |
} catch (CertificateNotYetValidException e) { |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
195 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
196 |
(msg + " check failed", e, null, -1, BasicReason.NOT_YET_VALID); |
2 | 197 |
} |
198 |
||
199 |
if (debug != null) |
|
200 |
debug.println(msg + " verified."); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Internal method to check that cert has a valid DN to be next in a chain |
|
205 |
*/ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
206 |
private void verifyNameChaining(X509Certificate cert) |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
207 |
throws CertPathValidatorException |
2 | 208 |
{ |
209 |
if (prevSubject != null) { |
|
210 |
||
211 |
String msg = "subject/issuer name chaining"; |
|
212 |
if (debug != null) |
|
213 |
debug.println("---checking " + msg + "..."); |
|
214 |
||
215 |
X500Principal currIssuer = cert.getIssuerX500Principal(); |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
216 |
|
2 | 217 |
// reject null or empty issuer DNs |
218 |
if (X500Name.asX500Name(currIssuer).isEmpty()) { |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
219 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
220 |
(msg + " check failed: " + |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
221 |
"empty/null issuer DN in certificate is invalid", null, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
222 |
null, -1, PKIXReason.NAME_CHAINING); |
2 | 223 |
} |
224 |
||
225 |
if (!(currIssuer.equals(prevSubject))) { |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
226 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
227 |
(msg + " check failed", null, null, -1, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
228 |
PKIXReason.NAME_CHAINING); |
2 | 229 |
} |
230 |
||
231 |
if (debug != null) |
|
232 |
debug.println(msg + " verified."); |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Internal method to manage state information at each iteration |
|
238 |
*/ |
|
239 |
private void updateState(X509Certificate currCert) |
|
240 |
throws CertPathValidatorException |
|
241 |
{ |
|
242 |
PublicKey cKey = currCert.getPublicKey(); |
|
243 |
if (debug != null) { |
|
244 |
debug.println("BasicChecker.updateState issuer: " + |
|
245 |
currCert.getIssuerX500Principal().toString() + "; subject: " + |
|
246 |
currCert.getSubjectX500Principal() + "; serial#: " + |
|
247 |
currCert.getSerialNumber().toString()); |
|
248 |
} |
|
13806
b18118646a65
7195409: CertPath/CertPathValidatorTest/KeyParamsInheritanceTest fails with NullPointerException
mullan
parents:
13555
diff
changeset
|
249 |
if (PKIX.isDSAPublicKeyWithoutParams(cKey)) { |
13555
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
250 |
// cKey needs to inherit DSA parameters from prev key |
2 | 251 |
cKey = makeInheritedParamsKey(cKey, prevPubKey); |
252 |
if (debug != null) debug.println("BasicChecker.updateState Made " + |
|
253 |
"key with inherited params"); |
|
254 |
} |
|
255 |
prevPubKey = cKey; |
|
256 |
prevSubject = currCert.getSubjectX500Principal(); |
|
257 |
} |
|
258 |
||
259 |
/** |
|
13555
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
260 |
* Internal method to create a new key with inherited key parameters. |
2 | 261 |
* |
262 |
* @param keyValueKey key from which to obtain key value |
|
263 |
* @param keyParamsKey key from which to obtain key parameters |
|
264 |
* @return new public key having value and parameters |
|
265 |
* @throws CertPathValidatorException if keys are not appropriate types |
|
266 |
* for this operation |
|
267 |
*/ |
|
268 |
static PublicKey makeInheritedParamsKey(PublicKey keyValueKey, |
|
269 |
PublicKey keyParamsKey) throws CertPathValidatorException |
|
270 |
{ |
|
271 |
if (!(keyValueKey instanceof DSAPublicKey) || |
|
272 |
!(keyParamsKey instanceof DSAPublicKey)) |
|
273 |
throw new CertPathValidatorException("Input key is not " + |
|
274 |
"appropriate type for " + |
|
275 |
"inheriting parameters"); |
|
276 |
DSAParams params = ((DSAPublicKey)keyParamsKey).getParams(); |
|
277 |
if (params == null) |
|
278 |
throw new CertPathValidatorException("Key parameters missing"); |
|
279 |
try { |
|
280 |
BigInteger y = ((DSAPublicKey)keyValueKey).getY(); |
|
281 |
KeyFactory kf = KeyFactory.getInstance("DSA"); |
|
282 |
DSAPublicKeySpec ks = new DSAPublicKeySpec(y, |
|
283 |
params.getP(), |
|
284 |
params.getQ(), |
|
285 |
params.getG()); |
|
13555
fb41599cf196
7187962: sun.security.pkcs11.P11DSAKeyFactory.implTranslatePublicKey doesn't check if params is null
mullan
parents:
13021
diff
changeset
|
286 |
return kf.generatePublic(ks); |
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
287 |
} catch (GeneralSecurityException e) { |
2 | 288 |
throw new CertPathValidatorException("Unable to generate key with" + |
289 |
" inherited parameters: " + |
|
290 |
e.getMessage(), e); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* return the public key associated with the last certificate processed |
|
296 |
* |
|
297 |
* @return PublicKey the last public key processed |
|
298 |
*/ |
|
299 |
PublicKey getPublicKey() { |
|
300 |
return prevPubKey; |
|
301 |
} |
|
302 |
} |