jdk/src/share/classes/sun/security/provider/certpath/ForwardBuilder.java
author xuelei
Mon, 14 Feb 2011 13:31:13 -0800
changeset 8395 29afb3ec47e2
parent 8163 d9bcc1208691
child 8790 2a8d836ee007
permissions -rw-r--r--
7018897: CertPath validation cannot handle self-signed cert with bad KeyUsage Summary: Remove KeyUsage checking for trust anchors Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
     2
 * Copyright (c) 2000, 2011, 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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidKeyException;
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    33
import java.security.cert.Certificate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertPathValidatorException;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 792
diff changeset
    36
import java.security.cert.PKIXReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.CertStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CertStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.PKIXBuilderParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.PKIXCertPathChecker;
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 javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.util.Debug;
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    47
import sun.security.util.DerOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.x509.AccessDescription;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.x509.AuthorityInfoAccessExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.x509.PKIXExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.security.x509.PolicyMappingsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.security.x509.X509CertImpl;
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    54
import sun.security.x509.X509CRLImpl;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    55
import sun.security.x509.AuthorityKeyIdentifierExtension;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    56
import sun.security.x509.KeyIdentifier;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    57
import sun.security.x509.SubjectKeyIdentifierExtension;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    58
import sun.security.x509.SerialNumber;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    59
import sun.security.x509.GeneralNames;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    60
import sun.security.x509.GeneralName;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    61
import sun.security.x509.GeneralNameInterface;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
    62
import java.math.BigInteger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * This class represents a forward builder, which is able to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * matching certificates from CertStores and verify a particular certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * against a ForwardState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
class ForwardBuilder extends Builder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final Set<X509Certificate> trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private final Set<X500Principal> trustedSubjectDNs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final Set<TrustAnchor> trustAnchors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private X509CertSelector eeSelector;
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
    80
    private AdaptableX509CertSelector caSelector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private X509CertSelector caTargetSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    TrustAnchor trustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private Comparator<X509Certificate> comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private boolean searchAllCertStores = true;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3225
diff changeset
    85
    private boolean onlyEECert = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Initialize the builder with the input parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param params the parameter set used to build a certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    ForwardBuilder(PKIXBuilderParameters buildParams,
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3225
diff changeset
    93
        X500Principal targetSubjectDN, boolean searchAllCertStores,
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3225
diff changeset
    94
        boolean onlyEECert)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        super(buildParams, targetSubjectDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // populate sets of trusted certificates and subject DNs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        trustAnchors = buildParams.getTrustAnchors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        trustedCerts = new HashSet<X509Certificate>(trustAnchors.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        trustedSubjectDNs = new HashSet<X500Principal>(trustAnchors.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (TrustAnchor anchor : trustAnchors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (trustedCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                trustedCerts.add(trustedCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                trustedSubjectDNs.add(trustedCert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                trustedSubjectDNs.add(anchor.getCA());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        comparator = new PKIXCertComparator(trustedSubjectDNs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.searchAllCertStores = searchAllCertStores;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3225
diff changeset
   113
        this.onlyEECert = onlyEECert;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Retrieves all certs from the specified CertStores that satisfy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * requirements specified in the parameters and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * PKIX state (name constraints, policy constraints, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param currentState the current state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *        Must be an instance of <code>ForwardState</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param certStores list of CertStores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    Collection<X509Certificate> getMatchingCerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        (State currentState, List<CertStore> certStores)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws CertStoreException, CertificateException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            debug.println("ForwardBuilder.getMatchingCerts()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ForwardState currState = (ForwardState) currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * We store certs in a Set because we don't want duplicates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * As each cert is added, it is sorted based on the PKIXCertComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Set<X509Certificate> certs = new TreeSet<X509Certificate>(comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * Only look for EE certs if search has just started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (currState.isInitial()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            getMatchingEECerts(currState, certStores, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        getMatchingCACerts(currState, certStores, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Retrieves all end-entity certificates which satisfy constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * and requirements specified in the parameters and PKIX state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private void getMatchingEECerts(ForwardState currentState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        List<CertStore> certStores, Collection<X509Certificate> eeCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            debug.println("ForwardBuilder.getMatchingEECerts()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * Compose a certificate matching rule to filter out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * certs which don't satisfy constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * First, retrieve clone of current target cert constraints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * and then add more selection criteria based on current validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * state. Since selector never changes, cache local copy & reuse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (eeSelector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            eeSelector = (X509CertSelector) targetCertConstraints.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             * Match on certificate validity date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            eeSelector.setCertificateValid(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             * Policy processing optimizations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (buildParams.isExplicitPolicyRequired()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                eeSelector.setPolicy(getMatchingPolicies());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
             * Require EE certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            eeSelector.setBasicConstraints(-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /* Retrieve matching EE certs from CertStores */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        addMatchingCerts(eeSelector, certStores, eeCerts, searchAllCertStores);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Retrieves all CA certificates which satisfy constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * and requirements specified in the parameters and PKIX state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private void getMatchingCACerts(ForwardState currentState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        List<CertStore> certStores, Collection<X509Certificate> caCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            debug.println("ForwardBuilder.getMatchingCACerts()...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int initialSize = caCerts.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * Compose a CertSelector to filter out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * certs which do not satisfy requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        X509CertSelector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (currentState.isInitial()) {
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents: 2
diff changeset
   216
            if (targetCertConstraints.getBasicConstraints() == -2) {
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents: 2
diff changeset
   217
                // no need to continue: this means we never can match a CA cert
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents: 2
diff changeset
   218
                return;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents: 2
diff changeset
   219
            }
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents: 2
diff changeset
   220
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            /* This means a CA is the target, so match on same stuff as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
             * getMatchingEECerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                debug.println("ForwardBuilder.getMatchingCACerts(): ca is target");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (caTargetSelector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                caTargetSelector = (X509CertSelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    targetCertConstraints.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                /*
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   233
                 * Since we don't check the validity period of trusted
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   234
                 * certificates, please don't set the certificate valid
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   235
                 * criterion unless the trusted certificate matching is
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   236
                 * completed.
2
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
                 * Policy processing optimizations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (buildParams.isExplicitPolicyRequired())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    caTargetSelector.setPolicy(getMatchingPolicies());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             * Require CA certs with a pathLenConstraint that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             * at least as many CA certs that have already been traversed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            caTargetSelector.setBasicConstraints(currentState.traversedCACerts);
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   251
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            sel = caTargetSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (caSelector == null) {
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   256
                caSelector = new AdaptableX509CertSelector();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                /*
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   259
                 * Since we don't check the validity period of trusted
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   260
                 * certificates, please don't set the certificate valid
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   261
                 * criterion unless the trusted certificate matching is
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   262
                 * completed.
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
                 * Policy processing optimizations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if (buildParams.isExplicitPolicyRequired())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    caSelector.setPolicy(getMatchingPolicies());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
             * Match on subject (issuer of previous cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            caSelector.setSubject(currentState.issuerDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
             * Match on subjectNamesTraversed (both DNs and AltNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
             * (checks that current cert's name constraints permit it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
             * to certify all the DNs and AltNames that have been traversed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            CertPathHelper.setPathToNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                (caSelector, currentState.subjectNamesTraversed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
             * Require CA certs with a pathLenConstraint that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
             * at least as many CA certs that have already been traversed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            caSelector.setBasicConstraints(currentState.traversedCACerts);
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   290
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   291
            /*
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   292
             * Facilitate certification path construction with authority
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   293
             * key identifier and subject key identifier.
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   294
             */
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   295
            AuthorityKeyIdentifierExtension akidext =
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   296
                    currentState.cert.getAuthorityKeyIdentifierExtension();
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   297
            caSelector.parseAuthorityKeyIdentifierExtension(akidext);
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   298
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   299
            /*
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   300
             * check the validity period
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   301
             */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   302
            caSelector.setValidityPeriod(currentState.cert.getNotBefore(),
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   303
                                            currentState.cert.getNotAfter());
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   304
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            sel = caSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        for (X509Certificate trustedCert : trustedCerts) {
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   309
            if (sel.match(trustedCert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    debug.println("ForwardBuilder.getMatchingCACerts: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        + "found matching trust anchor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (caCerts.add(trustedCert) && !searchAllCertStores) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   320
        /*
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   321
         * The trusted certificate matching is completed. We need to match
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   322
         * on certificate validity date.
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   323
         */
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   324
        sel.setCertificateValid(date);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * If we have already traversed as many CA certs as the maxPathLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * will allow us to, then we don't bother looking through these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * certificate pairs. If maxPathLength has a value of -1, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * means it is unconstrained, so we always look through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * certificate pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (currentState.isInitial() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
           (buildParams.getMaxPathLength() == -1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
           (buildParams.getMaxPathLength() > currentState.traversedCACerts))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        {
8163
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   337
            if (addMatchingCerts(sel, certStores,
d9bcc1208691 7011497: new CertPathValidatorException.BasicReason enum constant for constrained algorithm
xuelei
parents: 7040
diff changeset
   338
                    caCerts, searchAllCertStores) && !searchAllCertStores) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (!currentState.isInitial() && Builder.USE_AIA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            // check for AuthorityInformationAccess extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            AuthorityInfoAccessExtension aiaExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                currentState.cert.getAuthorityInfoAccessExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (aiaExt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                getCerts(aiaExt, caCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            int numCerts = caCerts.size() - initialSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            debug.println("ForwardBuilder.getMatchingCACerts: found " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                numCerts + " CA certs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Download Certificates from the given AIA and add them to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * specified Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private boolean getCerts(AuthorityInfoAccessExtension aiaExt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Collection<X509Certificate> certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (Builder.USE_AIA == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        List<AccessDescription> adList = aiaExt.getAccessDescriptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (adList == null || adList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        boolean add = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        for (AccessDescription ad : adList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            CertStore cs = URICertStore.getInstance(ad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                if (certs.addAll((Collection<X509Certificate>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    cs.getCertificates(caSelector))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    add = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    if (!searchAllCertStores) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            } catch (CertStoreException cse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    debug.println("exception getting certs from CertStore:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    cse.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return add;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * This inner class compares 2 PKIX certificates according to which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * should be tried first when building a path from the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * The preference order is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Given trusted certificate(s):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *    Subject:ou=D,ou=C,o=B,c=A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Preference order for current cert:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * 1) Issuer matches a trusted subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *    Issuer: ou=D,ou=C,o=B,c=A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * 2) Issuer is a descendant of a trusted subject (in order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *    number of links to the trusted subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *    a) Issuer: ou=E,ou=D,ou=C,o=B,c=A        [links=1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *    b) Issuer: ou=F,ou=E,ou=D,ou=C,ou=B,c=A  [links=2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * 3) Issuer is an ancestor of a trusted subject (in order of number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *    links to the trusted subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *    a) Issuer: ou=C,o=B,c=A [links=1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *    b) Issuer: o=B,c=A      [links=2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * 4) Issuer is in the same namespace as a trusted subject (in order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *    number of links to the trusted subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *    a) Issuer: ou=G,ou=C,o=B,c=A  [links=2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *    b) Issuer: ou=H,o=B,c=A       [links=3]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * 5) Issuer is an ancestor of certificate subject (in order of number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *    of links to the certificate subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *    a) Issuer:  ou=K,o=J,c=A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *       Subject: ou=L,ou=K,o=J,c=A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *    b) Issuer:  o=J,c=A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *       Subject: ou=L,ou=K,0=J,c=A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * 6) Any other certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    static class PKIXCertComparator implements Comparator<X509Certificate> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        final static String METHOD_NME = "PKIXCertComparator.compare()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        private final Set<X500Principal> trustedSubjectDNs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        PKIXCertComparator(Set<X500Principal> trustedSubjectDNs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            this.trustedSubjectDNs = trustedSubjectDNs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * @param oCert1 First X509Certificate to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * @param oCert2 Second X509Certificate to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * @return -1 if oCert1 is preferable to oCert2, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         *            if oCert1 and oCert2 are equally preferable (in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         *            case it doesn't matter which is preferable, but we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         *            return 0 because the comparator would behave strangely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         *            when used in a SortedSet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         *          1 if oCert2 is preferable to oCert1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         *          0 if oCert1.equals(oCert2). We only return 0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         *          certs are equal so that this comparator behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         *          correctly when used in a SortedSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * @throws ClassCastException if either argument is not of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        public int compare(X509Certificate oCert1, X509Certificate oCert2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            // if certs are the same, return 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (oCert1.equals(oCert2)) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            X500Principal cIssuer1 = oCert1.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            X500Principal cIssuer2 = oCert2.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            X500Name cIssuer1Name = X500Name.asX500Name(cIssuer1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            X500Name cIssuer2Name = X500Name.asX500Name(cIssuer2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                debug.println(METHOD_NME + " o1 Issuer:  " + cIssuer1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                debug.println(METHOD_NME + " o2 Issuer:  " + cIssuer2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            /* If one cert's issuer matches a trusted subject, then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
             * preferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                debug.println(METHOD_NME + " MATCH TRUSTED SUBJECT TEST...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            boolean m1 = trustedSubjectDNs.contains(cIssuer1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            boolean m2 = trustedSubjectDNs.contains(cIssuer2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                debug.println(METHOD_NME + " m1: " + m1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                debug.println(METHOD_NME + " m2: " + m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (m1 && m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            } else if (m1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            } else if (m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            /* If one cert's issuer is a naming descendant of a trusted subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
             * then it is preferable, in order of increasing naming distance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                debug.println(METHOD_NME + " NAMING DESCENDANT TEST...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            for (X500Principal tSubject : trustedSubjectDNs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                X500Name tSubjectName = X500Name.asX500Name(tSubject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                int distanceTto1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    Builder.distance(tSubjectName, cIssuer1Name, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                int distanceTto2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    Builder.distance(tSubjectName, cIssuer2Name, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    debug.println(METHOD_NME +" distanceTto1: " + distanceTto1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    debug.println(METHOD_NME +" distanceTto2: " + distanceTto2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (distanceTto1 > 0 || distanceTto2 > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    if (distanceTto1 == distanceTto2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    } else if (distanceTto1 > 0 && distanceTto2 <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    } else if (distanceTto1 <= 0 && distanceTto2 > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    } else if (distanceTto1 < distanceTto2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    } else {    // distanceTto1 > distanceTto2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            /* If one cert's issuer is a naming ancestor of a trusted subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             * then it is preferable, in order of increasing naming distance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                debug.println(METHOD_NME + " NAMING ANCESTOR TEST...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            for (X500Principal tSubject : trustedSubjectDNs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                X500Name tSubjectName = X500Name.asX500Name(tSubject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                int distanceTto1 = Builder.distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    (tSubjectName, cIssuer1Name, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                int distanceTto2 = Builder.distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    (tSubjectName, cIssuer2Name, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    debug.println(METHOD_NME +" distanceTto1: " + distanceTto1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    debug.println(METHOD_NME +" distanceTto2: " + distanceTto2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (distanceTto1 < 0 || distanceTto2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    if (distanceTto1 == distanceTto2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    } else if (distanceTto1 < 0 && distanceTto2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    } else if (distanceTto1 >= 0 && distanceTto2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    } else if (distanceTto1 > distanceTto2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            /* If one cert's issuer is in the same namespace as a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
             * subject, then it is preferable, in order of increasing naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
             * distance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                debug.println(METHOD_NME +" SAME NAMESPACE AS TRUSTED TEST...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            for (X500Principal tSubject : trustedSubjectDNs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                X500Name tSubjectName = X500Name.asX500Name(tSubject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                X500Name tAo1 = tSubjectName.commonAncestor(cIssuer1Name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                X500Name tAo2 = tSubjectName.commonAncestor(cIssuer2Name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    debug.println(METHOD_NME +" tAo1: " + String.valueOf(tAo1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    debug.println(METHOD_NME +" tAo2: " + String.valueOf(tAo2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                if (tAo1 != null || tAo2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    if (tAo1 != null && tAo2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        int hopsTto1 = Builder.hops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                            (tSubjectName, cIssuer1Name, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        int hopsTto2 = Builder.hops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                            (tSubjectName, cIssuer2Name, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                            debug.println(METHOD_NME +" hopsTto1: " + hopsTto1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            debug.println(METHOD_NME +" hopsTto2: " + hopsTto2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        if (hopsTto1 == hopsTto2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        } else if (hopsTto1 > hopsTto2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        } else {  // hopsTto1 < hopsTto2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    } else if (tAo1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            /* If one cert's issuer is an ancestor of that cert's subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
             * then it is preferable, in order of increasing naming distance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                debug.println(METHOD_NME+" CERT ISSUER/SUBJECT COMPARISON TEST...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            X500Principal cSubject1 = oCert1.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            X500Principal cSubject2 = oCert2.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            X500Name cSubject1Name = X500Name.asX500Name(cSubject1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            X500Name cSubject2Name = X500Name.asX500Name(cSubject2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                debug.println(METHOD_NME + " o1 Subject: " + cSubject1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                debug.println(METHOD_NME + " o2 Subject: " + cSubject2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            int distanceStoI1 = Builder.distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                (cSubject1Name, cIssuer1Name, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            int distanceStoI2 = Builder.distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                (cSubject2Name, cIssuer2Name, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                debug.println(METHOD_NME + " distanceStoI1: " + distanceStoI1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                debug.println(METHOD_NME + " distanceStoI2: " + distanceStoI2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (distanceStoI2 > distanceStoI1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            } else if (distanceStoI2 < distanceStoI1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            /* Otherwise, certs are equally preferable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                debug.println(METHOD_NME + " no tests matched; RETURN 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Verifies a matching certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * This method executes the validation steps in the PKIX path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * validation algorithm <draft-ietf-pkix-new-part1-08.txt> which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * not satisfied by the selection criteria used by getCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * to find the certs and only the steps that can be executed in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * forward direction (target to trust anchor). Those steps that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * only be executed in a reverse direction are deferred until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * complete path has been built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Trust anchor certs are not validated, but are used to verify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * signature and revocation status of the previous cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * If the last certificate is being verified (the one whose subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * matches the target subject, then steps in 6.1.4 of the PKIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Certification Path Validation algorithm are NOT executed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * regardless of whether or not the last cert is an end-entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * cert or not. This allows callers to certify CA certs as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * well as EE certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @param cert the certificate to be verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @param currentState the current state against which the cert is verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @param certPathList the certPathList generated thus far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    void verifyCert(X509Certificate cert, State currentState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        List<X509Certificate> certPathList) throws GeneralSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            debug.println("ForwardBuilder.verifyCert(SN: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                + Debug.toHexString(cert.getSerialNumber())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                + "\n  Issuer: " + cert.getIssuerX500Principal() + ")"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                + "\n  Subject: " + cert.getSubjectX500Principal() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        ForwardState currState = (ForwardState) currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * check for looping - abort a loop if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * ((we encounter the same certificate twice) AND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * ((policyMappingInhibited = true) OR (no policy mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         * extensions can be found between the occurences of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * certificate)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (certPathList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            boolean policyMappingFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            for (X509Certificate cpListCert : certPathList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                X509CertImpl cpListCertImpl = X509CertImpl.toImpl(cpListCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                PolicyMappingsExtension policyMappingsExt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    = cpListCertImpl.getPolicyMappingsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                if (policyMappingsExt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    policyMappingFound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    debug.println("policyMappingFound = " + policyMappingFound);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                if (cert.equals(cpListCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    if ((buildParams.isPolicyMappingInhibited()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        (!policyMappingFound)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                            debug.println("loop detected!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        throw new CertPathValidatorException("loop detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        /* check if trusted cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        boolean isTrustedCert = trustedCerts.contains(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        /* we don't perform any validation of the trusted cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (!isTrustedCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
             * Check CRITICAL private extensions for user checkers that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
             * support forward checking (forwardCheckers) and remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
             * ones we know how to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            Set<String> unresCritExts = cert.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            if (unresCritExts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                unresCritExts = Collections.<String>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            for (PKIXCertPathChecker checker : currState.forwardCheckers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                checker.check(cert, unresCritExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
             * Remove extensions from user checkers that don't support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
             * forward checking. After this step, we will have removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
             * all extensions that all user checkers are capable of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
             * processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            for (PKIXCertPathChecker checker : buildParams.getCertPathCheckers()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                if (!checker.isForwardCheckingSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    Set<String> supportedExts = checker.getSupportedExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    if (supportedExts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        unresCritExts.removeAll(supportedExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
             * Look at the remaining extensions and remove any ones we know how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
             * to check. If there are any left, throw an exception!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if (!unresCritExts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    PKIXExtensions.BasicConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    PKIXExtensions.NameConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    PKIXExtensions.CertificatePolicies_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    PKIXExtensions.PolicyMappings_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    PKIXExtensions.PolicyConstraints_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    PKIXExtensions.InhibitAnyPolicy_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    PKIXExtensions.SubjectAlternativeName_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                unresCritExts.remove(PKIXExtensions.KeyUsage_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                unresCritExts.remove(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    PKIXExtensions.ExtendedKeyUsage_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                if (!unresCritExts.isEmpty())
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 792
diff changeset
   759
                    throw new CertPathValidatorException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 792
diff changeset
   760
                        ("Unrecognized critical extension(s)", null, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 792
diff changeset
   761
                         PKIXReason.UNRECOGNIZED_CRIT_EXT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         * if this is the target certificate (init=true), then we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * not able to do any more verification, so just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (currState.isInitial()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        /* we don't perform any validation of the trusted cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (!isTrustedCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            /* Make sure this is a CA cert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            if (cert.getBasicConstraints() == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                throw new CertificateException("cert is NOT a CA cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
             * Check keyUsage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            KeyChecker.verifyCAKeyUsage(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * the following checks are performed even when the cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * is a trusted cert, since we are only extracting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * subjectDN, and publicKey from the cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * in order to verify a previous cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         * Check revocation for the previous cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (buildParams.isRevocationEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // first off, see if this cert can authorize revocation...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if (CrlRevocationChecker.certCanSignCrl(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                // And then check to be sure no key requiring key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                // has been encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                if (!currState.keyParamsNeeded())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    // If all that checks out, we can check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    // revocation status of the cert. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    // we'll just wait until the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    currState.crlChecker.check(currState.cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                               cert.getPublicKey(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                               true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * Check signature only if no key requiring key parameters has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         * encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (!currState.keyParamsNeeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            (currState.cert).verify(cert.getPublicKey(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                                    buildParams.getSigProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * Verifies whether the input certificate completes the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * Checks the cert against each trust anchor that was specified, in order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * and returns true as soon as it finds a valid anchor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Returns true if the cert matches a trust anchor specified as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * certificate or if the cert verifies with a trust anchor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * was specified as a trusted {pubkey, caname} pair. Returns false if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * of the trust anchors are valid for this cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * @param cert the certificate to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @return a boolean value indicating whether the cert completes the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    boolean isPathCompleted(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        for (TrustAnchor anchor : trustAnchors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            if (anchor.getTrustedCert() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                if (cert.equals(anchor.getTrustedCert())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    this.trustAnchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   843
            } else {
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   844
                X500Principal principal = anchor.getCA();
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   845
                java.security.PublicKey publicKey = anchor.getCAPublicKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
3225
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   847
                if (principal != null && publicKey != null &&
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   848
                        principal.equals(cert.getSubjectX500Principal())) {
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   849
                    if (publicKey.equals(cert.getPublicKey())) {
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   850
                        // the cert itself is a trust anchor
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   851
                        this.trustAnchor = anchor;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   852
                        return true;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   853
                    }
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   854
                    // else, it is a self-issued certificate of the anchor
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   855
                }
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   856
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   857
                // Check subject/issuer name chaining
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   858
                if (principal == null ||
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   859
                        !principal.equals(cert.getIssuerX500Principal())) {
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   860
                    continue;
023603ebeb1f 6852744: PIT b61: PKI test suite fails because self signed certificates are beingrejected
xuelei
parents: 1238
diff changeset
   861
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            /* Check revocation if it is enabled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            if (buildParams.isRevocationEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                try {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3225
diff changeset
   867
                    CrlRevocationChecker crlChecker = new CrlRevocationChecker
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3225
diff changeset
   868
                        (anchor, buildParams, null, onlyEECert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    crlChecker.check(cert, anchor.getCAPublicKey(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                } catch (CertPathValidatorException cpve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                        debug.println("ForwardBuilder.isPathCompleted() cpve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        cpve.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
             * Check signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                // NOTE: the DSA public key in the buildParams may lack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                // parameters, yet there is no key to inherit the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                // from.  This is probably such a rare case that it is not worth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                // trying to detect the situation earlier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                cert.verify(anchor.getCAPublicKey(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                            buildParams.getSigProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            } catch (InvalidKeyException ike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                    debug.println("ForwardBuilder.isPathCompleted() invalid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        + "DSA key found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            } catch (Exception e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    debug.println("ForwardBuilder.isPathCompleted() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        "unexpected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            this.trustAnchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    /** Adds the certificate to the certPathList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @param cert the certificate to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @param certPathList the certification path list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    void addCertToPath(X509Certificate cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        LinkedList<X509Certificate> certPathList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        certPathList.addFirst(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /** Removes final certificate from the certPathList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param certPathList the certification path list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    void removeFinalCertFromPath(LinkedList<X509Certificate> certPathList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        certPathList.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
}