jdk/src/share/classes/sun/security/provider/certpath/PKIXCertPathValidator.java
author mullan
Wed, 09 Sep 2009 09:54:13 -0400
changeset 3841 6738c111d48f
parent 1238 6d1f4b722acd
child 4209 e2e5a973b879
permissions -rw-r--r--
6745437: Add option to only check revocation of end-entity certificate in a chain of certificates 6869739: Cannot check revocation of single certificate without validating the entire chain Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
     2
 * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.CertPathParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertPathValidatorSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertPathValidatorResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.PKIXCertPathChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.PKIXCertPathValidatorResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.PKIXParameters;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    39
import java.security.cert.PKIXReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.PolicyNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.TrustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.X509CertSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.security.auth.x500.X500Principal;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
    50
import sun.security.action.GetBooleanSecurityPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This class implements the PKIX validation algorithm for certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * paths consisting exclusively of <code>X509Certificates</code>. It uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * the specified input parameter set (which must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>PKIXParameters</code> object) and signature provider (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class PKIXCertPathValidator extends CertPathValidatorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private Date testDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private List<PKIXCertPathChecker> userCheckers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private String sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private BasicChecker basicChecker;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
    69
    private boolean ocspEnabled = false;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
    70
    private boolean onlyEECert = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public PKIXCertPathValidator() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Validates a certification path consisting exclusively of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <code>X509Certificate</code>s using the PKIX validation algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * which uses the specified input parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The input parameter set must be a <code>PKIXParameters</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param cp the X509 certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param param the input PKIX parameter set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @exception CertPathValidatorException Exception thrown if cert path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * does not validate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @exception InvalidAlgorithmParameterException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * parameters are inappropriate for this certification path validator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public CertPathValidatorResult engineValidate(CertPath cp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        CertPathParameters param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws CertPathValidatorException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            debug.println("PKIXCertPathValidator.engineValidate()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (!(param instanceof PKIXParameters)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new InvalidAlgorithmParameterException("inappropriate "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                + "parameters, must be an instance of PKIXParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (!cp.getType().equals("X.509") && !cp.getType().equals("X509")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new InvalidAlgorithmParameterException("inappropriate "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                + "certification path type specified, must be X.509 or X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        PKIXParameters pkixParam = (PKIXParameters) param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // Make sure that none of the trust anchors include name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // (not supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Set<TrustAnchor> anchors = pkixParam.getTrustAnchors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (TrustAnchor anchor : anchors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (anchor.getNameConstraints() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    ("name constraints in trust anchor not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // the certpath which has been passed in (cp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // has the target cert as the first certificate - we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // need to keep this cp so we can return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // in case of an exception and for policy qualifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // processing - however, for certpath validation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // we need to create a reversed path, where we reverse the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // ordering so that the target cert is the last certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Must copy elements of certList into a new modifiable List before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // calling Collections.reverse().
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   130
        ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ((List<X509Certificate>)cp.getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (certList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                debug.println("PKIXCertPathValidator.engineValidate() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    + "certList is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            debug.println("PKIXCertPathValidator.engineValidate() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                + "reversing certpath...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Collections.reverse(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // now certList has the target cert as the last cert and we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // can proceed with normal validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        populateVariables(pkixParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Retrieve the first certificate in the certpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // (to be used later in pre-screening)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        X509Certificate firstCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (!certList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            firstCert = certList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        CertPathValidatorException lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // We iterate through the set of trust anchors until we find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // one that works at which time we stop iterating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (TrustAnchor anchor : anchors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (trustedCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    debug.println("PKIXCertPathValidator.engineValidate() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        + "anchor.getTrustedCert() != null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                // if this trust anchor is not worth trying,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // we move on to the next one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (!isWorthTrying(trustedCert, firstCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    debug.println("anchor.getTrustedCert()."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        + "getSubjectX500Principal() = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        + trustedCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    debug.println("PKIXCertPathValidator.engineValidate(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        + "anchor.getTrustedCert() == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                PolicyNodeImpl rootNode = new PolicyNodeImpl(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    PolicyChecker.ANY_POLICY, null, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    Collections.singleton(PolicyChecker.ANY_POLICY), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                PolicyNode policyTree =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    doValidate(anchor, cp, certList, pkixParam, rootNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // if this anchor works, return success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return new PKIXCertPathValidatorResult(anchor, policyTree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    basicChecker.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (CertPathValidatorException cpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                // remember this exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                lastException = cpe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // could not find a trust anchor that verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // (a) if we did a validation and it failed, use that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // (b) otherwise, generate new exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   205
            ("Path does not chain with any of the trust anchors",
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   206
             null, null, -1, PKIXReason.NO_TRUST_ANCHOR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Internal method to do some simple checks to see if a given cert is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * worth trying to validate in the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private boolean isWorthTrying(X509Certificate trustedCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                  X509Certificate firstCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            debug.println("PKIXCertPathValidator.isWorthTrying() checking "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                + "if this trusted cert is worth trying ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (firstCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // the subject of the trusted cert should match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // issuer of the first cert in the certpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        X500Principal trustedSubject = trustedCert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (trustedSubject.equals(firstCert.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                debug.println("YES - try this trustedCert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                debug.println("NO - don't try this trustedCert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Internal method to setup the internal state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private void populateVariables(PKIXParameters pkixParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // default value for testDate is current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        testDate = pkixParam.getDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (testDate == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            testDate = new Date(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        userCheckers = pkixParam.getCertPathCheckers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        sigProvider = pkixParam.getSigProvider();
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   253
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   254
        if (pkixParam.isRevocationEnabled()) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   255
            // Examine OCSP security property
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   256
            ocspEnabled = AccessController.doPrivileged(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   257
                new GetBooleanSecurityPropertyAction
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   258
                    (OCSPChecker.OCSP_ENABLE_PROP));
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   259
            onlyEECert = AccessController.doPrivileged(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   260
                new GetBooleanSecurityPropertyAction
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   261
                    ("com.sun.security.onlyCheckRevocationOfEECert"));
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   262
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Internal method to actually validate a constructed path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return the valid policy tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private PolicyNode doValidate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            TrustAnchor anchor, CertPath cpOriginal,
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   272
            ArrayList<X509Certificate> certList, PKIXParameters pkixParam,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            PolicyNodeImpl rootNode) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int certPathLen = certList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        basicChecker = new BasicChecker(anchor, testDate, sigProvider, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        KeyChecker keyChecker = new KeyChecker(certPathLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            pkixParam.getTargetCertConstraints());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        ConstraintsChecker constraintsChecker =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            new ConstraintsChecker(certPathLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        PolicyChecker policyChecker =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            new PolicyChecker(pkixParam.getInitialPolicies(), certPathLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                              pkixParam.isExplicitPolicyRequired(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                              pkixParam.isPolicyMappingInhibited(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                              pkixParam.isAnyPolicyInhibited(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                              pkixParam.getPolicyQualifiersRejected(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                              rootNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   291
        ArrayList<PKIXCertPathChecker> certPathCheckers =
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   292
            new ArrayList<PKIXCertPathChecker>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // add standard checkers that we will be using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        certPathCheckers.add(keyChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        certPathCheckers.add(constraintsChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        certPathCheckers.add(policyChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        certPathCheckers.add(basicChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // only add a revocationChecker if revocation is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (pkixParam.isRevocationEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // Use OCSP if it has been enabled
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   303
            if (ocspEnabled) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                OCSPChecker ocspChecker =
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   305
                    new OCSPChecker(cpOriginal, pkixParam, onlyEECert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                certPathCheckers.add(ocspChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // Always use CRLs
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   310
            CrlRevocationChecker revocationChecker = new
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   311
                CrlRevocationChecker(anchor, pkixParam, certList, onlyEECert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            certPathCheckers.add(revocationChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // add user-specified checkers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        certPathCheckers.addAll(userCheckers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        PKIXMasterCertPathValidator masterValidator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            new PKIXMasterCertPathValidator(certPathCheckers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        masterValidator.validate(cpOriginal, certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return policyChecker.getPolicyTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}