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