author | mullan |
Thu, 11 Sep 2008 14:05:16 -0400 | |
changeset 1238 | 6d1f4b722acd |
parent 2 | 90ce3da70b43 |
child 2283 | 495fbbcc7569 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
2 |
* Copyright 2000-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.provider.certpath; |
|
27 |
||
28 |
import sun.security.util.Debug; |
|
29 |
||
30 |
import java.util.Collections; |
|
31 |
import java.util.List; |
|
32 |
import java.util.Set; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
33 |
import java.security.cert.CertificateRevokedException; |
2 | 34 |
import java.security.cert.CertPath; |
35 |
import java.security.cert.CertPathValidatorException; |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
36 |
import java.security.cert.CertPathValidatorException.BasicReason; |
2 | 37 |
import java.security.cert.PKIXCertPathChecker; |
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
38 |
import java.security.cert.PKIXReason; |
2 | 39 |
import java.security.cert.X509Certificate; |
40 |
||
41 |
/** |
|
42 |
* This class is initialized with a list of <code>PKIXCertPathChecker</code>s |
|
43 |
* and is used to verify the certificates in a <code>CertPath</code> by |
|
44 |
* feeding each certificate to each <code>PKIXCertPathChecker</code>. |
|
45 |
* |
|
46 |
* @since 1.4 |
|
47 |
* @author Yassir Elley |
|
48 |
*/ |
|
49 |
class PKIXMasterCertPathValidator { |
|
50 |
||
51 |
private static final Debug debug = Debug.getInstance("certpath"); |
|
52 |
private List<PKIXCertPathChecker> certPathCheckers; |
|
53 |
||
54 |
/** |
|
55 |
* Initializes the list of PKIXCertPathCheckers whose checks |
|
56 |
* will be performed on each certificate in the certpath. |
|
57 |
* |
|
58 |
* @param certPathCheckers a List of checkers to use |
|
59 |
*/ |
|
60 |
PKIXMasterCertPathValidator(List<PKIXCertPathChecker> certPathCheckers) { |
|
61 |
this.certPathCheckers = certPathCheckers; |
|
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* Validates a certification path consisting exclusively of |
|
66 |
* <code>X509Certificate</code>s using the |
|
67 |
* <code>PKIXCertPathChecker</code>s specified |
|
68 |
* in the constructor. It is assumed that the |
|
69 |
* <code>PKIXCertPathChecker</code>s |
|
70 |
* have been initialized with any input parameters they may need. |
|
71 |
* |
|
72 |
* @param cpOriginal the original X509 CertPath passed in by the user |
|
73 |
* @param reversedCertList the reversed X509 CertPath (as a List) |
|
74 |
* @exception CertPathValidatorException Exception thrown if cert |
|
75 |
* path does not validate. |
|
76 |
*/ |
|
77 |
void validate(CertPath cpOriginal, List<X509Certificate> reversedCertList) |
|
78 |
throws CertPathValidatorException |
|
79 |
{ |
|
80 |
// we actually process reversedCertList, but we keep cpOriginal because |
|
81 |
// we need to return the original certPath when we throw an exception. |
|
82 |
// we will also need to modify the index appropriately when we |
|
83 |
// throw an exception. |
|
84 |
||
85 |
int cpSize = reversedCertList.size(); |
|
86 |
||
87 |
if (debug != null) { |
|
88 |
debug.println("--------------------------------------------------" |
|
89 |
+ "------------"); |
|
90 |
debug.println("Executing PKIX certification path validation " |
|
91 |
+ "algorithm."); |
|
92 |
} |
|
93 |
||
94 |
for (int i = 0; i < cpSize; i++) { |
|
95 |
||
96 |
/* The basic loop algorithm is that we get the |
|
97 |
* current certificate, we verify the current certificate using |
|
98 |
* information from the previous certificate and from the state, |
|
99 |
* and we modify the state for the next loop by setting the |
|
100 |
* current certificate of this loop to be the previous certificate |
|
101 |
* of the next loop. The state is initialized during first loop. |
|
102 |
*/ |
|
103 |
if (debug != null) |
|
104 |
debug.println("Checking cert" + (i+1) + " ..."); |
|
105 |
||
106 |
X509Certificate currCert = reversedCertList.get(i); |
|
107 |
Set<String> unresolvedCritExts = |
|
108 |
currCert.getCriticalExtensionOIDs(); |
|
109 |
if (unresolvedCritExts == null) { |
|
110 |
unresolvedCritExts = Collections.<String>emptySet(); |
|
111 |
} |
|
112 |
||
113 |
if (debug != null && !unresolvedCritExts.isEmpty()) { |
|
114 |
debug.println("Set of critical extensions:"); |
|
115 |
for (String oid : unresolvedCritExts) { |
|
116 |
debug.println(oid); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
CertPathValidatorException ocspCause = null; |
|
121 |
for (int j = 0; j < certPathCheckers.size(); j++) { |
|
122 |
||
123 |
PKIXCertPathChecker currChecker = certPathCheckers.get(j); |
|
124 |
if (debug != null) { |
|
125 |
debug.println("-Using checker" + (j + 1) + " ... [" + |
|
126 |
currChecker.getClass().getName() + "]"); |
|
127 |
} |
|
128 |
||
129 |
if (i == 0) |
|
130 |
currChecker.init(false); |
|
131 |
||
132 |
try { |
|
133 |
currChecker.check(currCert, unresolvedCritExts); |
|
134 |
||
135 |
// OCSP has validated the cert so skip the CRL check |
|
136 |
if (isRevocationCheck(currChecker, j, certPathCheckers)) { |
|
137 |
if (debug != null) { |
|
138 |
debug.println("-checker" + (j + 1) + |
|
139 |
" validation succeeded"); |
|
140 |
} |
|
141 |
j++; |
|
142 |
continue; // skip |
|
143 |
} |
|
144 |
||
145 |
} catch (CertPathValidatorException cpve) { |
|
146 |
// Throw the saved OCSP exception |
|
147 |
// (when the CRL check has also failed) |
|
148 |
if (ocspCause != null && |
|
149 |
currChecker instanceof CrlRevocationChecker) { |
|
150 |
throw ocspCause; |
|
151 |
} |
|
152 |
/* |
|
153 |
* Handle failover from OCSP to CRLs |
|
154 |
*/ |
|
155 |
CertPathValidatorException currentCause = |
|
156 |
new CertPathValidatorException(cpve.getMessage(), |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
157 |
cpve.getCause(), cpOriginal, cpSize - (i + 1), |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
158 |
cpve.getReason()); |
2 | 159 |
|
160 |
// Check if OCSP has confirmed that the cert was revoked |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
161 |
if (cpve.getReason() == BasicReason.REVOKED) { |
2 | 162 |
throw currentCause; |
163 |
} |
|
164 |
// Check if it is appropriate to failover |
|
165 |
if (! isRevocationCheck(currChecker, j, certPathCheckers)) { |
|
166 |
// no failover |
|
167 |
throw currentCause; |
|
168 |
} |
|
169 |
// Save the current exception |
|
170 |
// (in case the CRL check also fails) |
|
171 |
ocspCause = currentCause; |
|
172 |
||
173 |
// Otherwise, failover to CRLs |
|
174 |
if (debug != null) { |
|
175 |
debug.println(cpve.getMessage()); |
|
176 |
debug.println( |
|
177 |
"preparing to failover (from OCSP to CRLs)"); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
if (debug != null) |
|
182 |
debug.println("-checker" + (j+1) + " validation succeeded"); |
|
183 |
} |
|
184 |
||
185 |
if (debug != null) |
|
186 |
debug.println("checking for unresolvedCritExts"); |
|
187 |
if (!unresolvedCritExts.isEmpty()) { |
|
188 |
throw new CertPathValidatorException("unrecognized " + |
|
1238
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
189 |
"critical extension(s)", null, cpOriginal, cpSize-(i+1), |
6d1f4b722acd
6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents:
2
diff
changeset
|
190 |
PKIXReason.UNRECOGNIZED_CRIT_EXT); |
2 | 191 |
} |
192 |
||
193 |
if (debug != null) |
|
194 |
debug.println("\ncert" + (i+1) + " validation succeeded.\n"); |
|
195 |
} |
|
196 |
||
197 |
if (debug != null) { |
|
198 |
debug.println("Cert path validation succeeded. (PKIX validation " |
|
199 |
+ "algorithm)"); |
|
200 |
debug.println("-------------------------------------------------" |
|
201 |
+ "-------------"); |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
/* |
|
206 |
* Examines the list of PKIX cert path checkers to determine whether |
|
207 |
* both the current checker and the next checker are revocation checkers. |
|
208 |
* OCSPChecker and CrlRevocationChecker are both revocation checkers. |
|
209 |
*/ |
|
210 |
private static boolean isRevocationCheck(PKIXCertPathChecker checker, |
|
211 |
int index, List<PKIXCertPathChecker> checkers) { |
|
212 |
||
213 |
if (checker instanceof OCSPChecker && index + 1 < checkers.size()) { |
|
214 |
PKIXCertPathChecker nextChecker = checkers.get(index + 1); |
|
215 |
if (nextChecker instanceof CrlRevocationChecker) { |
|
216 |
return true; |
|
217 |
} |
|
218 |
} |
|
219 |
return false; |
|
220 |
} |
|
221 |
} |