author | mullan |
Wed, 30 May 2012 17:19:46 -0400 | |
changeset 12860 | 9ffbd4e43413 |
parent 5506 | 202f599c92aa |
child 13021 | 72a4386f8976 |
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 |
|
54 |
* on a PKIX certificate, namely the signature, timestamp, and subject/issuer |
|
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; |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Initializes the internal state of the checker from parameters |
|
97 |
* specified in the constructor. |
|
98 |
*/ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
99 |
@Override |
2 | 100 |
public void init(boolean forward) throws CertPathValidatorException { |
101 |
if (!forward) { |
|
102 |
prevPubKey = trustedPubKey; |
|
103 |
prevSubject = caName; |
|
104 |
} else { |
|
105 |
throw new |
|
106 |
CertPathValidatorException("forward checking not supported"); |
|
107 |
} |
|
108 |
} |
|
109 |
||
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
110 |
@Override |
2 | 111 |
public boolean isForwardCheckingSupported() { |
112 |
return false; |
|
113 |
} |
|
114 |
||
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
115 |
@Override |
2 | 116 |
public Set<String> getSupportedExtensions() { |
117 |
return null; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Performs the signature, timestamp, and subject/issuer name chaining |
|
122 |
* checks on the certificate using its internal state. This method does |
|
123 |
* not remove any critical extensions from the Collection. |
|
124 |
* |
|
125 |
* @param cert the Certificate |
|
126 |
* @param unresolvedCritExts a Collection of the unresolved critical |
|
127 |
* extensions |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
128 |
* @throws CertPathValidatorException if certificate does not verify |
2 | 129 |
*/ |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
130 |
@Override |
2 | 131 |
public void check(Certificate cert, Collection<String> unresolvedCritExts) |
132 |
throws CertPathValidatorException |
|
133 |
{ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
134 |
X509Certificate currCert = (X509Certificate)cert; |
2 | 135 |
|
136 |
if (!sigOnly) { |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
137 |
verifyTimestamp(currCert); |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
138 |
verifyNameChaining(currCert); |
2 | 139 |
} |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
140 |
verifySignature(currCert); |
2 | 141 |
|
142 |
updateState(currCert); |
|
143 |
} |
|
144 |
||
145 |
/** |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
146 |
* Verifies the signature on the certificate using the previous public key. |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
147 |
* |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
148 |
* @param cert the X509Certificate |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
149 |
* @throws CertPathValidatorException if certificate does not verify |
2 | 150 |
*/ |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
151 |
private void verifySignature(X509Certificate cert) |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
152 |
throws CertPathValidatorException |
2 | 153 |
{ |
154 |
String msg = "signature"; |
|
155 |
if (debug != null) |
|
156 |
debug.println("---checking " + msg + "..."); |
|
157 |
||
158 |
try { |
|
159 |
cert.verify(prevPubKey, sigProvider); |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
160 |
} catch (SignatureException e) { |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
161 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
162 |
(msg + " check failed", e, null, -1, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
163 |
BasicReason.INVALID_SIGNATURE); |
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
164 |
} catch (GeneralSecurityException e) { |
2 | 165 |
throw new CertPathValidatorException(msg + " check failed", e); |
166 |
} |
|
167 |
||
168 |
if (debug != null) |
|
169 |
debug.println(msg + " verified."); |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Internal method to verify the timestamp on a certificate |
|
174 |
*/ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
175 |
private void verifyTimestamp(X509Certificate cert) |
2 | 176 |
throws CertPathValidatorException |
177 |
{ |
|
178 |
String msg = "timestamp"; |
|
179 |
if (debug != null) |
|
180 |
debug.println("---checking " + msg + ":" + date.toString() + "..."); |
|
181 |
||
182 |
try { |
|
183 |
cert.checkValidity(date); |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
184 |
} catch (CertificateExpiredException e) { |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
185 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
186 |
(msg + " check failed", e, null, -1, BasicReason.EXPIRED); |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
187 |
} catch (CertificateNotYetValidException e) { |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
188 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
189 |
(msg + " check failed", e, null, -1, BasicReason.NOT_YET_VALID); |
2 | 190 |
} |
191 |
||
192 |
if (debug != null) |
|
193 |
debug.println(msg + " verified."); |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Internal method to check that cert has a valid DN to be next in a chain |
|
198 |
*/ |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
199 |
private void verifyNameChaining(X509Certificate cert) |
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
200 |
throws CertPathValidatorException |
2 | 201 |
{ |
202 |
if (prevSubject != null) { |
|
203 |
||
204 |
String msg = "subject/issuer name chaining"; |
|
205 |
if (debug != null) |
|
206 |
debug.println("---checking " + msg + "..."); |
|
207 |
||
208 |
X500Principal currIssuer = cert.getIssuerX500Principal(); |
|
12860
9ffbd4e43413
6854712: Revocation checking enhancements (JEP-124)
mullan
parents:
5506
diff
changeset
|
209 |
|
2 | 210 |
// reject null or empty issuer DNs |
211 |
if (X500Name.asX500Name(currIssuer).isEmpty()) { |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
212 |
throw new CertPathValidatorException |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
213 |
(msg + " check failed: " + |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
214 |
"empty/null issuer DN in certificate is invalid", null, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
215 |
null, -1, PKIXReason.NAME_CHAINING); |
2 | 216 |
} |
217 |
||
218 |
if (!(currIssuer.equals(prevSubject))) { |
|
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", null, null, -1, |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
221 |
PKIXReason.NAME_CHAINING); |
2 | 222 |
} |
223 |
||
224 |
if (debug != null) |
|
225 |
debug.println(msg + " verified."); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
/** |
|
230 |
* Internal method to manage state information at each iteration |
|
231 |
*/ |
|
232 |
private void updateState(X509Certificate currCert) |
|
233 |
throws CertPathValidatorException |
|
234 |
{ |
|
235 |
PublicKey cKey = currCert.getPublicKey(); |
|
236 |
if (debug != null) { |
|
237 |
debug.println("BasicChecker.updateState issuer: " + |
|
238 |
currCert.getIssuerX500Principal().toString() + "; subject: " + |
|
239 |
currCert.getSubjectX500Principal() + "; serial#: " + |
|
240 |
currCert.getSerialNumber().toString()); |
|
241 |
} |
|
242 |
if (cKey instanceof DSAPublicKey && |
|
243 |
((DSAPublicKey)cKey).getParams() == null) { |
|
244 |
//cKey needs to inherit DSA parameters from prev key |
|
245 |
cKey = makeInheritedParamsKey(cKey, prevPubKey); |
|
246 |
if (debug != null) debug.println("BasicChecker.updateState Made " + |
|
247 |
"key with inherited params"); |
|
248 |
} |
|
249 |
prevPubKey = cKey; |
|
250 |
prevSubject = currCert.getSubjectX500Principal(); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Internal method to create a new key with inherited key parameters |
|
255 |
* |
|
256 |
* @param keyValueKey key from which to obtain key value |
|
257 |
* @param keyParamsKey key from which to obtain key parameters |
|
258 |
* @return new public key having value and parameters |
|
259 |
* @throws CertPathValidatorException if keys are not appropriate types |
|
260 |
* for this operation |
|
261 |
*/ |
|
262 |
static PublicKey makeInheritedParamsKey(PublicKey keyValueKey, |
|
263 |
PublicKey keyParamsKey) throws CertPathValidatorException |
|
264 |
{ |
|
265 |
PublicKey usableKey; |
|
266 |
if (!(keyValueKey instanceof DSAPublicKey) || |
|
267 |
!(keyParamsKey instanceof DSAPublicKey)) |
|
268 |
throw new CertPathValidatorException("Input key is not " + |
|
269 |
"appropriate type for " + |
|
270 |
"inheriting parameters"); |
|
271 |
DSAParams params = ((DSAPublicKey)keyParamsKey).getParams(); |
|
272 |
if (params == null) |
|
273 |
throw new CertPathValidatorException("Key parameters missing"); |
|
274 |
try { |
|
275 |
BigInteger y = ((DSAPublicKey)keyValueKey).getY(); |
|
276 |
KeyFactory kf = KeyFactory.getInstance("DSA"); |
|
277 |
DSAPublicKeySpec ks = new DSAPublicKeySpec(y, |
|
278 |
params.getP(), |
|
279 |
params.getQ(), |
|
280 |
params.getG()); |
|
281 |
usableKey = kf.generatePublic(ks); |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
282 |
} catch (GeneralSecurityException e) { |
2 | 283 |
throw new CertPathValidatorException("Unable to generate key with" + |
284 |
" inherited parameters: " + |
|
285 |
e.getMessage(), e); |
|
286 |
} |
|
287 |
return usableKey; |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* return the public key associated with the last certificate processed |
|
292 |
* |
|
293 |
* @return PublicKey the last public key processed |
|
294 |
*/ |
|
295 |
PublicKey getPublicKey() { |
|
296 |
return prevPubKey; |
|
297 |
} |
|
298 |
} |