jdk/src/share/classes/sun/security/provider/certpath/PKIXCertPathValidator.java
author xuelei
Mon, 09 Jan 2012 20:55:52 -0800
changeset 11900 9b1d5bef8038
parent 8395 29afb3ec47e2
child 11902 a94ba35d9c4a
permissions -rw-r--r--
7123519: problems with certification path Summary: Also including the contribution from Dennis Gu Reviewed-by: mullan, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 8395
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4209
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4209
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4209
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4209
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4209
diff changeset
    23
 * questions.
2
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
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
    53
import sun.security.x509.X509CertImpl;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
    54
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
    55
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * This class implements the PKIX validation algorithm for certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * paths consisting exclusively of <code>X509Certificates</code>. It uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the specified input parameter set (which must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>PKIXParameters</code> object) and signature provider (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class PKIXCertPathValidator extends CertPathValidatorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Date testDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private List<PKIXCertPathChecker> userCheckers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private String sigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    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
    72
    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
    73
    private boolean onlyEECert = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public PKIXCertPathValidator() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Validates a certification path consisting exclusively of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <code>X509Certificate</code>s using the PKIX validation algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * which uses the specified input parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The input parameter set must be a <code>PKIXParameters</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param cp the X509 certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param param the input PKIX parameter set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @exception CertPathValidatorException Exception thrown if cert path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * does not validate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception InvalidAlgorithmParameterException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * parameters are inappropriate for this certification path validator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public CertPathValidatorResult engineValidate(CertPath cp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        CertPathParameters param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        throws CertPathValidatorException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            debug.println("PKIXCertPathValidator.engineValidate()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!(param instanceof PKIXParameters)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new InvalidAlgorithmParameterException("inappropriate "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                + "parameters, must be an instance of PKIXParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (!cp.getType().equals("X.509") && !cp.getType().equals("X509")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new InvalidAlgorithmParameterException("inappropriate "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                + "certification path type specified, must be X.509 or X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        PKIXParameters pkixParam = (PKIXParameters) param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Make sure that none of the trust anchors include name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // (not supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        Set<TrustAnchor> anchors = pkixParam.getTrustAnchors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        for (TrustAnchor anchor : anchors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (anchor.getNameConstraints() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    ("name constraints in trust anchor not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // the certpath which has been passed in (cp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // has the target cert as the first certificate - we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // need to keep this cp so we can return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // in case of an exception and for policy qualifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // processing - however, for certpath validation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // we need to create a reversed path, where we reverse the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // ordering so that the target cert is the last certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Must copy elements of certList into a new modifiable List before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // calling Collections.reverse().
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   133
        ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            ((List<X509Certificate>)cp.getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (certList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                debug.println("PKIXCertPathValidator.engineValidate() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    + "certList is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            debug.println("PKIXCertPathValidator.engineValidate() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                + "reversing certpath...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Collections.reverse(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // now certList has the target cert as the last cert and we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // can proceed with normal validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        populateVariables(pkixParam);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Retrieve the first certificate in the certpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // (to be used later in pre-screening)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        X509Certificate firstCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!certList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            firstCert = certList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        CertPathValidatorException lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // We iterate through the set of trust anchors until we find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // one that works at which time we stop iterating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (TrustAnchor anchor : anchors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (trustedCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    debug.println("PKIXCertPathValidator.engineValidate() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        + "anchor.getTrustedCert() != null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   168
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                // if this trust anchor is not worth trying,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // we move on to the next one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (!isWorthTrying(trustedCert, firstCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    debug.println("anchor.getTrustedCert()."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        + "getSubjectX500Principal() = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        + trustedCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    debug.println("PKIXCertPathValidator.engineValidate(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        + "anchor.getTrustedCert() == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                PolicyNodeImpl rootNode = new PolicyNodeImpl(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    PolicyChecker.ANY_POLICY, null, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    Collections.singleton(PolicyChecker.ANY_POLICY), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                PolicyNode policyTree =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    doValidate(anchor, cp, certList, pkixParam, rootNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                // if this anchor works, return success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return new PKIXCertPathValidatorResult(anchor, policyTree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    basicChecker.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } catch (CertPathValidatorException cpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // remember this exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                lastException = cpe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // could not find a trust anchor that verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // (a) if we did a validation and it failed, use that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // (b) otherwise, generate new exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   209
            ("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
   210
             null, null, -1, PKIXReason.NO_TRUST_ANCHOR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Internal method to do some simple checks to see if a given cert is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * worth trying to validate in the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private boolean isWorthTrying(X509Certificate trustedCert,
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   218
          X509Certificate firstCert) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   219
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   220
        boolean worthy = false;
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   221
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            debug.println("PKIXCertPathValidator.isWorthTrying() checking "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                + "if this trusted cert is worth trying ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (firstCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   231
        AdaptableX509CertSelector issuerSelector =
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   232
                        new AdaptableX509CertSelector();
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   233
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   234
        // check trusted certificate's subject
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   235
        issuerSelector.setSubject(firstCert.getIssuerX500Principal());
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   236
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   237
        // check the validity period
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   238
        issuerSelector.setValidityPeriod(firstCert.getNotBefore(),
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   239
                                                firstCert.getNotAfter());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   241
        /*
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   242
         * Facilitate certification path construction with authority
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   243
         * key identifier and subject key identifier.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   244
         */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   245
        try {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   246
            X509CertImpl firstCertImpl = X509CertImpl.toImpl(firstCert);
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   247
            issuerSelector.parseAuthorityKeyIdentifierExtension(
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   248
                        firstCertImpl.getAuthorityKeyIdentifierExtension());
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   249
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   250
            worthy = issuerSelector.match(trustedCert);
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   251
        } catch (Exception e) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   252
            // It is not worth trying.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   253
        }
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   254
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   255
        if (debug != null) {
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   256
            if (worthy) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                debug.println("YES - try this trustedCert");
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   258
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                debug.println("NO - don't try this trustedCert");
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   260
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   262
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   263
        return worthy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Internal method to setup the internal state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private void populateVariables(PKIXParameters pkixParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // default value for testDate is current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        testDate = pkixParam.getDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (testDate == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            testDate = new Date(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        userCheckers = pkixParam.getCertPathCheckers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        sigProvider = pkixParam.getSigProvider();
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   279
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   280
        if (pkixParam.isRevocationEnabled()) {
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   281
            // 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
   282
            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
   283
                new GetBooleanSecurityPropertyAction
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   284
                    (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
   285
            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
   286
                new GetBooleanSecurityPropertyAction
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 1238
diff changeset
   287
                    ("com.sun.security.onlyCheckRevocationOfEECert"));
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   288
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Internal method to actually validate a constructed path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return the valid policy tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private PolicyNode doValidate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            TrustAnchor anchor, CertPath cpOriginal,
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   298
            ArrayList<X509Certificate> certList, PKIXParameters pkixParam,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            PolicyNodeImpl rootNode) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int certPathLen = certList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        basicChecker = new BasicChecker(anchor, testDate, sigProvider, false);
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   304
        AlgorithmChecker algorithmChecker = new AlgorithmChecker(anchor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        KeyChecker keyChecker = new KeyChecker(certPathLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            pkixParam.getTargetCertConstraints());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        ConstraintsChecker constraintsChecker =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            new ConstraintsChecker(certPathLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        PolicyChecker policyChecker =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            new PolicyChecker(pkixParam.getInitialPolicies(), certPathLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                              pkixParam.isExplicitPolicyRequired(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                              pkixParam.isPolicyMappingInhibited(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                              pkixParam.isAnyPolicyInhibited(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                              pkixParam.getPolicyQualifiersRejected(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                              rootNode);
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 8395
diff changeset
   317
        UntrustedChecker untrustedChecker = new UntrustedChecker();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   319
        ArrayList<PKIXCertPathChecker> certPathCheckers =
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   320
            new ArrayList<PKIXCertPathChecker>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // add standard checkers that we will be using
11900
9b1d5bef8038 7123519: problems with certification path
xuelei
parents: 8395
diff changeset
   322
        certPathCheckers.add(untrustedChecker);
4190
227655c2ff8c 6861062: Disable MD2 support
xuelei
parents: 1238
diff changeset
   323
        certPathCheckers.add(algorithmChecker);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        certPathCheckers.add(keyChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        certPathCheckers.add(constraintsChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        certPathCheckers.add(policyChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        certPathCheckers.add(basicChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // only add a revocationChecker if revocation is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (pkixParam.isRevocationEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // 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
   333
            if (ocspEnabled) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                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
   335
                    new OCSPChecker(cpOriginal, pkixParam, onlyEECert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                certPathCheckers.add(ocspChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // 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
   340
            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
   341
                CrlRevocationChecker(anchor, pkixParam, certList, onlyEECert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            certPathCheckers.add(revocationChecker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // add user-specified checkers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        certPathCheckers.addAll(userCheckers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        PKIXMasterCertPathValidator masterValidator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            new PKIXMasterCertPathValidator(certPathCheckers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        masterValidator.validate(cpOriginal, certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return policyChecker.getPolicyTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
}