J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 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 java.io.IOException; |
| 29 | import java.security.GeneralSecurityException; |
| 30 | import java.security.cert.*; |
| 31 | import java.util.*; |
| 32 | |
| 33 | import javax.security.auth.x500.X500Principal; |
| 34 | |
| 35 | import sun.security.util.Debug; |
| 36 | import sun.security.x509.GeneralNames; |
| 37 | import sun.security.x509.GeneralNameInterface; |
| 38 | import sun.security.x509.GeneralSubtrees; |
| 39 | import sun.security.x509.NameConstraintsExtension; |
| 40 | import sun.security.x509.SubjectAlternativeNameExtension; |
| 41 | import sun.security.x509.X500Name; |
| 42 | import sun.security.x509.X509CertImpl; |
| 43 | |
| 44 | /** |
| 45 | * Abstract class representing a builder, which is able to retrieve |
| 46 | * matching certificates and is able to verify a particular certificate. |
| 47 | * |
| 48 | * @since 1.4 |
| 49 | * @author Sean Mullan |
| 50 | * @author Yassir Elley |
| 51 | */ |
| 52 | |
| 53 | public abstract class Builder { |
| 54 | |
| 55 | private static final Debug debug = Debug.getInstance("certpath"); |
| 56 | private Set<String> matchingPolicies; |
| 57 | final PKIXBuilderParameters buildParams; |
| 58 | final X500Principal targetSubjectDN; |
| 59 | final Date date; |
| 60 | final X509CertSelector targetCertConstraints; |
| 61 | |
| 62 | /** |
| 63 | * Flag indicating whether support for the caIssuers field of the |
| 64 | * Authority Information Access extension shall be enabled. Currently |
| 65 | * disabled by default for compatibility reasons. |
| 66 | */ |
| 67 | final static boolean USE_AIA = |
| 68 | DistributionPointFetcher.getBooleanProperty |
| 69 | ("com.sun.security.enableAIAcaIssuers", false); |
| 70 | |
| 71 | /** |
| 72 | * Initialize the builder with the input parameters. |
| 73 | * |
| 74 | * @param params the parameter set used to build a certification path |
| 75 | */ |
| 76 | Builder(PKIXBuilderParameters buildParams, X500Principal targetSubjectDN) { |
| 77 | this.buildParams = buildParams; |
| 78 | this.targetSubjectDN = targetSubjectDN; |
| 79 | // Initialize date if not specified |
| 80 | Date paramsDate = buildParams.getDate(); |
| 81 | this.date = paramsDate != null ? paramsDate : new Date(); |
| 82 | this.targetCertConstraints = |
| 83 | (X509CertSelector) buildParams.getTargetCertConstraints(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Retrieves certificates from the list of certStores using the buildParams |
| 88 | * and the currentState as a filter |
| 89 | * |
| 90 | * @param currentState the current State |
| 91 | * @param certStores list of CertStores |
| 92 | */ |
| 93 | abstract Collection<X509Certificate> getMatchingCerts |
| 94 | (State currentState, List<CertStore> certStores) |
| 95 | throws CertStoreException, CertificateException, IOException; |
| 96 | |
| 97 | /** |
| 98 | * Verifies the cert against the currentState, using the certPathList |
| 99 | * generated thus far to help with loop detection |
| 100 | * |
| 101 | * @param cert the certificate to be verified |
| 102 | * @param currentState the current state against which the cert is verified |
| 103 | * @param certPathList the certPathList generated thus far |
| 104 | */ |
| 105 | abstract void verifyCert(X509Certificate cert, State currentState, |
| 106 | List<X509Certificate> certPathList) throws GeneralSecurityException; |
| 107 | |
| 108 | /** |
| 109 | * Verifies whether the input certificate completes the path. |
| 110 | * When building forward, a trust anchor will complete the path. |
| 111 | * When building reverse, the target certificate will complete the path. |
| 112 | * |
| 113 | * @param cert the certificate to test |
| 114 | * @return a boolean value indicating whether the cert completes the path. |
| 115 | */ |
| 116 | abstract boolean isPathCompleted(X509Certificate cert); |
| 117 | |
| 118 | /** |
| 119 | * Adds the certificate to the certPathList |
| 120 | * |
| 121 | * @param cert the certificate to be added |
| 122 | * @param certPathList the certification path list |
| 123 | */ |
| 124 | abstract void addCertToPath(X509Certificate cert, |
| 125 | LinkedList<X509Certificate> certPathList); |
| 126 | |
| 127 | /** |
| 128 | * Removes final certificate from the certPathList |
| 129 | * |
| 130 | * @param certPathList the certification path list |
| 131 | */ |
| 132 | abstract void removeFinalCertFromPath |
| 133 | (LinkedList<X509Certificate> certPathList); |
| 134 | |
| 135 | /** |
| 136 | * get distance of one GeneralName from another |
| 137 | * |
| 138 | * @param base GeneralName at base of subtree |
| 139 | * @param test GeneralName to be tested against base |
| 140 | * @param incomparable the value to return if the names are |
| 141 | * incomparable |
| 142 | * @return distance of test name from base, where 0 |
| 143 | * means exact match, 1 means test is an immediate |
| 144 | * child of base, 2 means test is a grandchild, etc. |
| 145 | * -1 means test is a parent of base, -2 means test |
| 146 | * is a grandparent, etc. |
| 147 | */ |
| 148 | static int distance(GeneralNameInterface base, |
| 149 | GeneralNameInterface test, int incomparable) { |
| 150 | switch (base.constrains(test)) { |
| 151 | case GeneralNameInterface.NAME_DIFF_TYPE: |
| 152 | if (debug != null) { |
| 153 | debug.println("Builder.distance(): Names are different types"); |
| 154 | } |
| 155 | case GeneralNameInterface.NAME_SAME_TYPE: |
| 156 | if (debug != null) { |
| 157 | debug.println("Builder.distance(): Names are same type but " + |
| 158 | "in different subtrees"); |
| 159 | } |
| 160 | return incomparable; |
| 161 | case GeneralNameInterface.NAME_MATCH: |
| 162 | return 0; |
| 163 | case GeneralNameInterface.NAME_WIDENS: |
| 164 | break; |
| 165 | case GeneralNameInterface.NAME_NARROWS: |
| 166 | break; |
| 167 | default: // should never occur |
| 168 | return incomparable; |
| 169 | } |
| 170 | |
| 171 | /* names are in same subtree */ |
| 172 | return test.subtreeDepth() - base.subtreeDepth(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * get hop distance of one GeneralName from another in links where |
| 177 | * the names need not have an ancestor/descendant relationship. |
| 178 | * For example, the hop distance from ou=D,ou=C,o=B,c=US to |
| 179 | * ou=F,ou=E,ou=C,o=B,c=US is 3: D->C, C->E, E->F. The hop distance |
| 180 | * from ou=C,o=B,c=US to ou=D,ou=C,o=B,c=US is -1: C->D |
| 181 | * |
| 182 | * @param base GeneralName |
| 183 | * @param test GeneralName to be tested against base |
| 184 | * @param incomparable the value to return if the names are |
| 185 | * incomparable |
| 186 | * @return distance of test name from base measured in hops in the |
| 187 | * namespace hierarchy, where 0 means exact match. Result |
| 188 | * is positive if path is some number of up hops followed by |
| 189 | * some number of down hops; result is negative if path is |
| 190 | * some number of down hops. |
| 191 | */ |
| 192 | static int hops(GeneralNameInterface base, GeneralNameInterface test, |
| 193 | int incomparable) { |
| 194 | int baseRtest = base.constrains(test); |
| 195 | switch (baseRtest) { |
| 196 | case GeneralNameInterface.NAME_DIFF_TYPE: |
| 197 | if (debug != null) { |
| 198 | debug.println("Builder.hops(): Names are different types"); |
| 199 | } |
| 200 | return incomparable; |
| 201 | case GeneralNameInterface.NAME_SAME_TYPE: |
| 202 | /* base and test are in different subtrees */ |
| 203 | break; |
| 204 | case GeneralNameInterface.NAME_MATCH: |
| 205 | /* base matches test */ |
| 206 | return 0; |
| 207 | case GeneralNameInterface.NAME_WIDENS: |
| 208 | /* base is ancestor of test */ |
| 209 | return (test.subtreeDepth()-base.subtreeDepth()); |
| 210 | case GeneralNameInterface.NAME_NARROWS: |
| 211 | /* base is descendant of test */ |
| 212 | return (test.subtreeDepth()-base.subtreeDepth()); |
| 213 | default: // should never occur |
| 214 | return incomparable; |
| 215 | } |
| 216 | |
| 217 | /* names are in different subtrees */ |
| 218 | if (base.getType() != GeneralNameInterface.NAME_DIRECTORY) { |
| 219 | if (debug != null) { |
| 220 | debug.println("Builder.hops(): hopDistance not implemented " + |
| 221 | "for this name type"); |
| 222 | } |
| 223 | return incomparable; |
| 224 | } |
| 225 | X500Name baseName = (X500Name)base; |
| 226 | X500Name testName = (X500Name)test; |
| 227 | X500Name commonName = baseName.commonAncestor(testName); |
| 228 | if (commonName == null) { |
| 229 | if (debug != null) { |
| 230 | debug.println("Builder.hops(): Names are in different " + |
| 231 | "namespaces"); |
| 232 | } |
| 233 | return incomparable; |
| 234 | } else { |
| 235 | int commonDistance = commonName.subtreeDepth(); |
| 236 | int baseDistance = baseName.subtreeDepth(); |
| 237 | int testDistance = testName.subtreeDepth(); |
| 238 | return (baseDistance + testDistance - (2 * commonDistance)); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Determine how close a given certificate gets you toward |
| 244 | * a given target. |
| 245 | * |
| 246 | * @param constraints Current NameConstraints; if null, |
| 247 | * then caller must verify NameConstraints |
| 248 | * independently, realizing that this certificate |
| 249 | * may not actually lead to the target at all. |
| 250 | * @param cert Candidate certificate for chain |
| 251 | * @param target GeneralNameInterface name of target |
| 252 | * @return distance from this certificate to target: |
| 253 | * <ul> |
| 254 | * <li>-1 means certificate could be CA for target, but |
| 255 | * there are no NameConstraints limiting how close |
| 256 | * <li> 0 means certificate subject or subjectAltName |
| 257 | * matches target |
| 258 | * <li> 1 means certificate is permitted to be CA for |
| 259 | * target. |
| 260 | * <li> 2 means certificate is permitted to be CA for |
| 261 | * parent of target. |
| 262 | * <li>>0 in general, means certificate is permitted |
| 263 | * to be a CA for this distance higher in the naming |
| 264 | * hierarchy than the target, plus 1. |
| 265 | * </ul> |
| 266 | * <p>Note that the subject and/or subjectAltName of the |
| 267 | * candidate cert does not have to be an ancestor of the |
| 268 | * target in order to be a CA that can issue a certificate to |
| 269 | * the target. In these cases, the target distance is calculated |
| 270 | * by inspecting the NameConstraints extension in the candidate |
| 271 | * certificate. For example, suppose the target is an X.500 DN with |
| 272 | * a value of "CN=mullan,OU=ireland,O=sun,C=us" and the |
| 273 | * NameConstraints extension in the candidate certificate |
| 274 | * includes a permitted component of "O=sun,C=us", which implies |
| 275 | * that the candidate certificate is allowed to issue certs in |
| 276 | * the "O=sun,C=us" namespace. The target distance is 3 |
| 277 | * ((distance of permitted NC from target) + 1). |
| 278 | * The (+1) is added to distinguish the result from the case |
| 279 | * which returns (0). |
| 280 | * @throws IOException if certificate does not get closer |
| 281 | */ |
| 282 | static int targetDistance(NameConstraintsExtension constraints, |
| 283 | X509Certificate cert, GeneralNameInterface target) |
| 284 | throws IOException { |
| 285 | |
| 286 | /* ensure that certificate satisfies existing name constraints */ |
| 287 | if (constraints != null && !constraints.verify(cert)) { |
| 288 | throw new IOException("certificate does not satisfy existing name " |
| 289 | + "constraints"); |
| 290 | } |
| 291 | |
| 292 | X509CertImpl certImpl; |
| 293 | try { |
| 294 | certImpl = X509CertImpl.toImpl(cert); |
| 295 | } catch (CertificateException e) { |
| 296 | throw (IOException)new IOException("Invalid certificate").initCause(e); |
| 297 | } |
| 298 | /* see if certificate subject matches target */ |
| 299 | X500Name subject = X500Name.asX500Name(certImpl.getSubjectX500Principal()); |
| 300 | if (subject.equals(target)) { |
| 301 | /* match! */ |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | SubjectAlternativeNameExtension altNameExt = |
| 306 | certImpl.getSubjectAlternativeNameExtension(); |
| 307 | if (altNameExt != null) { |
| 308 | GeneralNames altNames = |
| 309 | (GeneralNames)altNameExt.get(altNameExt.SUBJECT_NAME); |
| 310 | /* see if any alternative name matches target */ |
| 311 | if (altNames != null) { |
| 312 | for (int j = 0, n = altNames.size(); j < n; j++) { |
| 313 | GeneralNameInterface altName = altNames.get(j).getName(); |
| 314 | if (altName.equals(target)) { |
| 315 | return 0; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /* no exact match; see if certificate can get us to target */ |
| 323 | |
| 324 | /* first, get NameConstraints out of certificate */ |
| 325 | NameConstraintsExtension ncExt = certImpl.getNameConstraintsExtension(); |
| 326 | if (ncExt == null) { |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | /* merge certificate's NameConstraints with current NameConstraints */ |
| 331 | if (constraints != null) { |
| 332 | constraints.merge(ncExt); |
| 333 | } else { |
| 334 | // Make sure we do a clone here, because we're probably |
| 335 | // going to modify this object later and we don't want to |
| 336 | // be sharing it with a Certificate object! |
| 337 | constraints = (NameConstraintsExtension) ncExt.clone(); |
| 338 | } |
| 339 | |
| 340 | if (debug != null) { |
| 341 | debug.println("Builder.targetDistance() merged constraints: " |
| 342 | + String.valueOf(constraints)); |
| 343 | } |
| 344 | /* reduce permitted by excluded */ |
| 345 | GeneralSubtrees permitted = (GeneralSubtrees) |
| 346 | constraints.get(constraints.PERMITTED_SUBTREES); |
| 347 | GeneralSubtrees excluded = (GeneralSubtrees) |
| 348 | constraints.get(constraints.EXCLUDED_SUBTREES); |
| 349 | if (permitted != null) { |
| 350 | permitted.reduce(excluded); |
| 351 | } |
| 352 | if (debug != null) { |
| 353 | debug.println("Builder.targetDistance() reduced constraints: " |
| 354 | + permitted); |
| 355 | } |
| 356 | /* see if new merged constraints allow target */ |
| 357 | if (!constraints.verify(target)) { |
| 358 | throw new IOException("New certificate not allowed to sign " |
| 359 | + "certificate for target"); |
| 360 | } |
| 361 | /* find distance to target, if any, in permitted */ |
| 362 | if (permitted == null) { |
| 363 | /* certificate is unconstrained; could sign for anything */ |
| 364 | return -1; |
| 365 | } |
| 366 | for (int i = 0, n = permitted.size(); i < n; i++) { |
| 367 | GeneralNameInterface perName = permitted.get(i).getName().getName(); |
| 368 | int distance = distance(perName, target, -1); |
| 369 | if (distance >= 0) { |
| 370 | return (distance + 1); |
| 371 | } |
| 372 | } |
| 373 | /* no matching type in permitted; cert holder could certify target */ |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * This method can be used as an optimization to filter out |
| 379 | * certificates that do not have policies which are valid. |
| 380 | * It returns the set of policies (String OIDs) that should exist in |
| 381 | * the certificate policies extension of the certificate that is |
| 382 | * needed by the builder. The logic applied is as follows: |
| 383 | * <p> |
| 384 | * 1) If some initial policies have been set *and* policy mappings are |
| 385 | * inhibited, then acceptable certificates are those that include |
| 386 | * the ANY_POLICY OID or with policies that intersect with the |
| 387 | * initial policies. |
| 388 | * 2) If no initial policies have been set *or* policy mappings are |
| 389 | * not inhibited then we don't have much to work with. All we know is |
| 390 | * that a certificate must have *some* policy because if it didn't |
| 391 | * have any policy then the policy tree would become null (and validation |
| 392 | * would fail). |
| 393 | * |
| 394 | * @return the Set of policies any of which must exist in a |
| 395 | * cert's certificate policies extension in order for a cert to be selected. |
| 396 | */ |
| 397 | Set<String> getMatchingPolicies() { |
| 398 | if (matchingPolicies != null) { |
| 399 | Set<String> initialPolicies = buildParams.getInitialPolicies(); |
| 400 | if ((!initialPolicies.isEmpty()) && |
| 401 | (!initialPolicies.contains(PolicyChecker.ANY_POLICY)) && |
| 402 | (buildParams.isPolicyMappingInhibited())) |
| 403 | { |
| 404 | initialPolicies.add(PolicyChecker.ANY_POLICY); |
| 405 | matchingPolicies = initialPolicies; |
| 406 | } else { |
| 407 | // we just return an empty set to make sure that there is |
| 408 | // at least a certificate policies extension in the cert |
| 409 | matchingPolicies = Collections.<String>emptySet(); |
| 410 | } |
| 411 | } |
| 412 | return matchingPolicies; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Search the specified CertStores and add all certificates matching |
| 417 | * selector to resultCerts. Self-signed certs are not useful here |
| 418 | * and therefore ignored. |
| 419 | * |
| 420 | * If the targetCert criterion of the selector is set, only that cert |
| 421 | * is examined and the CertStores are not searched. |
| 422 | * |
| 423 | * If checkAll is true, all CertStores are searched for matching certs. |
| 424 | * If false, the method returns as soon as the first CertStore returns |
| 425 | * a matching cert(s). |
| 426 | * |
| 427 | * Returns true iff resultCerts changed (a cert was added to the collection) |
| 428 | */ |
| 429 | boolean addMatchingCerts(X509CertSelector selector, |
| 430 | Collection<CertStore> certStores, |
| 431 | Collection<X509Certificate> resultCerts, boolean checkAll) { |
| 432 | X509Certificate targetCert = selector.getCertificate(); |
| 433 | if (targetCert != null) { |
| 434 | // no need to search CertStores |
| 435 | if (selector.match(targetCert) && !X509CertImpl.isSelfSigned |
| 436 | (targetCert, buildParams.getSigProvider())) { |
| 437 | if (debug != null) { |
| 438 | debug.println("Builder.addMatchingCerts: adding target cert"); |
| 439 | } |
| 440 | return resultCerts.add(targetCert); |
| 441 | } |
| 442 | return false; |
| 443 | } |
| 444 | boolean add = false; |
| 445 | for (CertStore store : certStores) { |
| 446 | try { |
| 447 | Collection<? extends Certificate> certs = |
| 448 | store.getCertificates(selector); |
| 449 | for (Certificate cert : certs) { |
| 450 | if (!X509CertImpl.isSelfSigned |
| 451 | ((X509Certificate)cert, buildParams.getSigProvider())) { |
| 452 | if (resultCerts.add((X509Certificate)cert)) { |
| 453 | add = true; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | if (!checkAll && add) { |
| 458 | return true; |
| 459 | } |
| 460 | } catch (CertStoreException cse) { |
| 461 | // if getCertificates throws a CertStoreException, we ignore |
| 462 | // it and move on to the next CertStore |
| 463 | if (debug != null) { |
| 464 | debug.println("Builder.addMatchingCerts, non-fatal " + |
| 465 | "exception retrieving certs: " + cse); |
| 466 | cse.printStackTrace(); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | return add; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Returns true if CertStore is local. Currently, returns true if |
| 475 | * type is Collection or if it has been initialized with |
| 476 | * CollectionCertStoreParameters. A new API method should be added |
| 477 | * to CertStore that returns local or remote. |
| 478 | */ |
| 479 | static boolean isLocalCertStore(CertStore certStore) { |
| 480 | return (certStore.getType().equals("Collection") || |
| 481 | certStore.getCertStoreParameters() instanceof |
| 482 | CollectionCertStoreParameters); |
| 483 | } |
| 484 | } |