jdk/src/share/classes/sun/security/provider/certpath/CrlRevocationChecker.java
author mullan
Thu, 21 Apr 2011 17:39:28 -0400
changeset 9511 8f315e0a7b18
parent 9256 230442708954
child 10336 0bb1999251f8
permissions -rw-r--r--
7038175: Expired PKITS certificates causing CertPathBuilder and CertPathValidator regression test failures Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
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: 3841
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: 3841
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
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.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.*;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
    42
import java.security.cert.CertPathValidatorException.BasicReason;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.interfaces.DSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.x509.AccessDescription;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.x509.AuthorityInfoAccessExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.x509.CRLDistributionPointsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.x509.DistributionPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.x509.GeneralName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.security.x509.GeneralNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.security.x509.PKIXExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.security.x509.X509CRLEntryImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * CrlRevocationChecker is a <code>PKIXCertPathChecker</code> that checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * revocation status information on a PKIX certificate using CRLs obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * from one or more <code>CertStores</code>. This is based on section 6.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * of RFC 3280 (http://www.ietf.org/rfc/rfc3280.txt).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author      Seth Proctor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author      Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
class CrlRevocationChecker extends PKIXCertPathChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final TrustAnchor mAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final List<CertStore> mStores;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final String mSigProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final Date mCurrentTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private PublicKey mPrevPubKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean mCRLSignFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private HashSet<X509CRL> mPossibleCRLs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private HashSet<X509CRL> mApprovedCRLs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final PKIXParameters mParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final boolean [] mCrlSignUsage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        { false, false, false, false, false, false, true };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final boolean[] ALL_REASONS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        {true, true, true, true, true, true, true, true, true};
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
    83
    private boolean mOnlyEECert = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
3314
1e9d33caef08 6787645: CRL validation code should permit some clock skew when checking validity of CRLs
mullan
parents: 1238
diff changeset
    85
    // Maximum clock skew in milliseconds (15 minutes) allowed when checking
1e9d33caef08 6787645: CRL validation code should permit some clock skew when checking validity of CRLs
mullan
parents: 1238
diff changeset
    86
    // validity of CRLs
1e9d33caef08 6787645: CRL validation code should permit some clock skew when checking validity of CRLs
mullan
parents: 1238
diff changeset
    87
    private static final long MAX_CLOCK_SKEW = 900000;
1e9d33caef08 6787645: CRL validation code should permit some clock skew when checking validity of CRLs
mullan
parents: 1238
diff changeset
    88
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Creates a <code>CrlRevocationChecker</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param anchor anchor selected to validate the target certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param params <code>PKIXParameters</code> to be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *               finding certificates and CRLs, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    CrlRevocationChecker(TrustAnchor anchor, PKIXParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this(anchor, params, null);
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
     * Creates a <code>CrlRevocationChecker</code>, allowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * extra certificates to be supplied beyond those contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * in the <code>PKIXParameters</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param anchor anchor selected to validate the target certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param params <code>PKIXParameters</code> to be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *               finding certificates and CRLs, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param certs a <code>Collection</code> of certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *              that may be useful, beyond those available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *              through <code>params</code> (<code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *              if none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    CrlRevocationChecker(TrustAnchor anchor, PKIXParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Collection<X509Certificate> certs) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   118
        this(anchor, params, certs, false);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   119
    }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   120
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   121
    CrlRevocationChecker(TrustAnchor anchor, PKIXParameters params,
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   122
        Collection<X509Certificate> certs, boolean onlyEECert)
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   123
        throws CertPathValidatorException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        mAnchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        mParams = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        mStores = new ArrayList<CertStore>(params.getCertStores());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        mSigProvider = params.getSigProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                mStores.add(CertStore.getInstance("Collection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    new CollectionCertStoreParameters(certs)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                // should never occur but not necessarily fatal, so log it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // ignore and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    debug.println("CrlRevocationChecker: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        "error creating Collection CertStore: " + e);
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
        Date testDate = params.getDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        mCurrentTime = (testDate != null ? testDate : new Date());
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   143
        mOnlyEECert = onlyEECert;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Initializes the internal state of the checker from parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * specified in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void init(boolean forward) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (mAnchor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (mAnchor.getCAPublicKey() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    mPrevPubKey = mAnchor.getCAPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    mPrevPubKey = mAnchor.getTrustedCert().getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                mPrevPubKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            mCRLSignFlag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new CertPathValidatorException("forward checking "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                + "not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public boolean isForwardCheckingSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public Set<String> getSupportedExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Performs the revocation status check on the certificate using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * its internal state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param cert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param unresolvedCritExts a Collection of the unresolved critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @exception CertPathValidatorException Exception thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * certificate does not verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public void check(Certificate cert, Collection<String> unresolvedCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        X509Certificate currCert = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        verifyRevocationStatus(currCert, mPrevPubKey, mCRLSignFlag, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // Make new public key if parameters are missing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        PublicKey cKey = currCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (cKey instanceof DSAPublicKey &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ((DSAPublicKey)cKey).getParams() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // cKey needs to inherit DSA parameters from prev key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cKey = BasicChecker.makeInheritedParamsKey(cKey, mPrevPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        mPrevPubKey = cKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        mCRLSignFlag = certCanSignCrl(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Performs the revocation status check on the certificate using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * the provided state variables, as well as the constant internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param currCert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param prevKey the previous PublicKey in the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param signFlag a boolean as returned from the last call, or true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * if this is the first cert in the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return a boolean specifying if the cert is allowed to vouch for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * validity of a CRL for the next iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception CertPathValidatorException Exception thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *            certificate does not verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public boolean check(X509Certificate currCert, PublicKey prevKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        boolean signFlag) throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        verifyRevocationStatus(currCert, prevKey, signFlag, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return certCanSignCrl(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Checks that a cert can be used to verify a CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param currCert an X509Certificate to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return a boolean specifying if the cert is allowed to vouch for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * validity of a CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static boolean certCanSignCrl(X509Certificate currCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // if the cert doesn't include the key usage ext, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // the key usage ext asserts cRLSigning, return true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // otherwise return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        boolean[] kbools = currCert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (kbools != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return kbools[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Internal method to start the verification of a cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private void verifyRevocationStatus(X509Certificate currCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        PublicKey prevKey, boolean signFlag, boolean allowSeparateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        throws CertPathValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        verifyRevocationStatus(currCert, prevKey, signFlag,
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   252
                   allowSeparateKey, null, mParams.getTrustAnchors());
2
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
     * Internal method to start the verification of a cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param stackedCerts a <code>Set</code> of <code>X509Certificate</code>s>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *                     whose revocation status depends on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *                     non-revoked status of this cert. To avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *                     circular dependencies, we assume they're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *                     revoked while checking the revocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *                     status of this cert.
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   263
     * @param trustAnchors a <code>Set</code> of <code>TrustAnchor</code>s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private void verifyRevocationStatus(X509Certificate currCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        PublicKey prevKey, boolean signFlag, boolean allowSeparateKey,
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   267
        Set<X509Certificate> stackedCerts,
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   268
        Set<TrustAnchor> trustAnchors) throws CertPathValidatorException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        String msg = "revocation status";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            debug.println("CrlRevocationChecker.verifyRevocationStatus()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                " ---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   276
        if (mOnlyEECert && currCert.getBasicConstraints() != -1) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   277
            if (debug != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   278
                debug.println("Skipping revocation check, not end entity cert");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   279
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   280
            return;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   281
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   282
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // reject circular dependencies - RFC 3280 is not explicit on how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // to handle this, so we feel it is safest to reject them until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // the issue is resolved in the PKIX WG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if ((stackedCerts != null) && stackedCerts.contains(currCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                debug.println("CrlRevocationChecker.verifyRevocationStatus()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    " circular dependency");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   292
                ("Could not determine revocation status", null, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   293
                 BasicReason.UNDETERMINED_REVOCATION_STATUS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // init the state for this run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        mPossibleCRLs = new HashSet<X509CRL>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        mApprovedCRLs = new HashSet<X509CRL>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        boolean[] reasonsMask = new boolean[9];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            X509CRLSelector sel = new X509CRLSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            sel.setCertificateChecking(currCert);
3314
1e9d33caef08 6787645: CRL validation code should permit some clock skew when checking validity of CRLs
mullan
parents: 1238
diff changeset
   304
            CertPathHelper.setDateAndTime(sel, mCurrentTime, MAX_CLOCK_SKEW);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            for (CertStore mStore : mStores) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                for (java.security.cert.CRL crl : mStore.getCRLs(sel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    mPossibleCRLs.add((X509CRL)crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            DistributionPointFetcher store =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                DistributionPointFetcher.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // all CRLs returned by the DP Fetcher have also been verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            mApprovedCRLs.addAll(store.getCRLs(sel, signFlag, prevKey,
9511
8f315e0a7b18 7038175: Expired PKITS certificates causing CertPathBuilder and CertPathValidator regression test failures
mullan
parents: 9256
diff changeset
   315
                mSigProvider, mStores, reasonsMask, trustAnchors,
8f315e0a7b18 7038175: Expired PKITS certificates causing CertPathBuilder and CertPathValidator regression test failures
mullan
parents: 9256
diff changeset
   316
                mParams.getDate()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                debug.println("CrlRevocationChecker.verifyRevocationStatus() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    + "unexpected exception: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new CertPathValidatorException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            debug.println("CrlRevocationChecker.verifyRevocationStatus() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                "crls.size() = " + mPossibleCRLs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (!mPossibleCRLs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // Now that we have a list of possible CRLs, see which ones can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // be approved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            mApprovedCRLs.addAll(verifyPossibleCRLs(mPossibleCRLs, currCert,
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   333
                signFlag, prevKey, reasonsMask, trustAnchors));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            debug.println("CrlRevocationChecker.verifyRevocationStatus() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                "approved crls.size() = " + mApprovedCRLs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // make sure that we have at least one CRL that _could_ cover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        // the certificate in question and all reasons are covered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (mApprovedCRLs.isEmpty() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            !Arrays.equals(reasonsMask, ALL_REASONS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (allowSeparateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                verifyWithSeparateSigningKey(currCert, prevKey, signFlag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                             stackedCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   350
                ("Could not determine revocation status", null, null, -1,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   351
                 BasicReason.UNDETERMINED_REVOCATION_STATUS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // See if the cert is in the set of approved crls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            BigInteger sn = currCert.getSerialNumber();
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   358
            debug.println("CrlRevocationChecker.verifyRevocationStatus() " +
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   359
                            "starting the final sweep...");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            debug.println("CrlRevocationChecker.verifyRevocationStatus" +
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   361
                            " cert SN: " + sn.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        CRLReason reasonCode = CRLReason.UNSPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        X509CRLEntryImpl entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        for (X509CRL crl : mApprovedCRLs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            X509CRLEntry e = crl.getRevokedCertificate(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    entry = X509CRLEntryImpl.toImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                } catch (CRLException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    throw new CertPathValidatorException(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    debug.println("CrlRevocationChecker.verifyRevocationStatus"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        + " CRL entry: " + entry.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                 * Abort CRL validation and throw exception if there are any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                 * unrecognized critical CRL entry extensions (see section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                 * 5.3 of RFC 3280).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                Set<String> unresCritExts = entry.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (unresCritExts != null && !unresCritExts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    /* remove any that we will process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    unresCritExts.remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        (PKIXExtensions.ReasonCode_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    unresCritExts.remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        (PKIXExtensions.CertificateIssuer_Id.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    if (!unresCritExts.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                            debug.println("Unrecognized "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            + "critical extension(s) in revoked CRL entry: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                            + unresCritExts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   398
                        ("Could not determine revocation status", null, null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   399
                         -1, BasicReason.UNDETERMINED_REVOCATION_STATUS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                reasonCode = entry.getRevocationReason();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (reasonCode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    reasonCode = CRLReason.UNSPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   407
                Throwable t = new CertificateRevokedException
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   408
                    (entry.getRevocationDate(), reasonCode,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   409
                     crl.getIssuerX500Principal(), entry.getExtensions());
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   410
                throw new CertPathValidatorException(t.getMessage(), t,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   411
                    null, -1, BasicReason.REVOKED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * We have a cert whose revocation status couldn't be verified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * a CRL issued by the cert that issued the CRL. See if we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * find a valid CRL issued by a separate key that can verify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * revocation status of this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Note that this does not provide support for indirect CRLs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * only CRLs signed with a different key (but the same issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * name) as the certificate being checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param currCert the <code>X509Certificate</code> to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param prevKey the <code>PublicKey</code> that failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param signFlag <code>true</code> if that key was trusted to sign CRLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param stackedCerts a <code>Set</code> of <code>X509Certificate</code>s>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *                     whose revocation status depends on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *                     non-revoked status of this cert. To avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *                     circular dependencies, we assume they're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *                     revoked while checking the revocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *                     status of this cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @throws CertPathValidatorException if the cert's revocation status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *         cannot be verified successfully with another key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private void verifyWithSeparateSigningKey(X509Certificate currCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        PublicKey prevKey, boolean signFlag, Set<X509Certificate> stackedCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        String msg = "revocation status";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            debug.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                "CrlRevocationChecker.verifyWithSeparateSigningKey()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                " ---checking " + msg + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // reject circular dependencies - RFC 3280 is not explicit on how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // to handle this, so we feel it is safest to reject them until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // the issue is resolved in the PKIX WG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if ((stackedCerts != null) && stackedCerts.contains(currCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                debug.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    "CrlRevocationChecker.verifyWithSeparateSigningKey()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    " circular dependency");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   458
                ("Could not determine revocation status", null, null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   459
                 -1, BasicReason.UNDETERMINED_REVOCATION_STATUS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // If prevKey wasn't trusted, maybe we just didn't have the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // path to it. Don't rule that key out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (!signFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            prevKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        // Try to find another key that might be able to sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // CRLs vouching for this cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        buildToNewKey(currCert, prevKey, stackedCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Tries to find a CertPath that establishes a key that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * used to verify the revocation status of a given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Ignores keys that have previously been tried. Throws a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * CertPathValidatorException if no such key could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param currCert the <code>X509Certificate</code> to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param prevKey the <code>PublicKey</code> of the certificate whose key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *    cannot be used to vouch for the CRL and should be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param stackedCerts a <code>Set</code> of <code>X509Certificate</code>s>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *                     whose revocation status depends on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *                     establishment of this path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @throws CertPathValidatorException on failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private void buildToNewKey(X509Certificate currCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        PublicKey prevKey, Set<X509Certificate> stackedCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            debug.println("CrlRevocationChecker.buildToNewKey()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                          " starting work");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        Set<PublicKey> badKeys = new HashSet<PublicKey>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (prevKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            badKeys.add(prevKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        X509CertSelector certSel = new RejectKeySelector(badKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        certSel.setSubject(currCert.getIssuerX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        certSel.setKeyUsage(mCrlSignUsage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   503
        Set<TrustAnchor> newAnchors =
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   504
            (mAnchor == null ? mParams.getTrustAnchors() :
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   505
                                Collections.singleton(mAnchor));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        PKIXBuilderParameters builderParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (mParams instanceof PKIXBuilderParameters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            builderParams = (PKIXBuilderParameters) mParams.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            builderParams.setTargetCertConstraints(certSel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            // Policy qualifiers must be rejected, since we don't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // any way to convey them back to the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            builderParams.setPolicyQualifiersRejected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                builderParams.setTrustAnchors(newAnchors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                throw new RuntimeException(iape); // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            // It's unfortunate that there's no easy way to make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            // PKIXBuilderParameters object from a PKIXParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            // object. This might miss some things if parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            // are added in the future or the validatorParams object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // is a custom class derived from PKIXValidatorParameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                builderParams = new PKIXBuilderParameters(newAnchors, certSel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                throw new RuntimeException(iape); // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            builderParams.setInitialPolicies(mParams.getInitialPolicies());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            builderParams.setCertStores(mStores);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            builderParams.setExplicitPolicyRequired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                (mParams.isExplicitPolicyRequired());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            builderParams.setPolicyMappingInhibited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                (mParams.isPolicyMappingInhibited());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            builderParams.setAnyPolicyInhibited(mParams.isAnyPolicyInhibited());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            // Policy qualifiers must be rejected, since we don't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // any way to convey them back to the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // That's the default, so no need to write code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            builderParams.setDate(mParams.getDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            builderParams.setCertPathCheckers(mParams.getCertPathCheckers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            builderParams.setSigProvider(mParams.getSigProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        // Skip revocation during this build to detect circular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // references. But check revocation afterwards, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // key (or any other that works).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        builderParams.setRevocationEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // check for AuthorityInformationAccess extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (Builder.USE_AIA == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            X509CertImpl currCertImpl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                currCertImpl = X509CertImpl.toImpl(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // ignore but log it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    debug.println("CrlRevocationChecker.buildToNewKey: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        "error decoding cert: " + ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            AuthorityInfoAccessExtension aiaExt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (currCertImpl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                aiaExt = currCertImpl.getAuthorityInfoAccessExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (aiaExt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                List<AccessDescription> adList = aiaExt.getAccessDescriptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                if (adList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    for (AccessDescription ad : adList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        CertStore cs = URICertStore.getInstance(ad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        if (cs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                debug.println("adding AIAext CertStore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                            builderParams.addCertStore(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        CertPathBuilder builder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            builder = CertPathBuilder.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            throw new CertPathValidatorException(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    debug.println("CrlRevocationChecker.buildToNewKey()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                  " about to try build ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                PKIXCertPathBuilderResult cpbr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    (PKIXCertPathBuilderResult) builder.build(builderParams);
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("CrlRevocationChecker.buildToNewKey()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                  " about to check revocation ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                // Now check revocation of all certs in path, assuming that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                // the stackedCerts are revoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                if (stackedCerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    stackedCerts = new HashSet<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                stackedCerts.add(currCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                TrustAnchor ta = cpbr.getTrustAnchor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                PublicKey prevKey2 = ta.getCAPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                if (prevKey2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    prevKey2 = ta.getTrustedCert().getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                boolean signFlag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                List<? extends Certificate> cpList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    cpbr.getCertPath().getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    for (int i = cpList.size()-1; i >= 0; i-- ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        X509Certificate cert = (X509Certificate) cpList.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                            debug.println("CrlRevocationChecker.buildToNewKey()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                + " index " + i + " checking " + cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                        }
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   623
                        verifyRevocationStatus(cert, prevKey2, signFlag, true,
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   624
                                stackedCerts, newAnchors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        signFlag = certCanSignCrl(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        prevKey2 = cert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                } catch (CertPathValidatorException cpve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    // ignore it and try to get another key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    badKeys.add(cpbr.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    debug.println("CrlRevocationChecker.buildToNewKey()" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                  " got key " + cpbr.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                // Now check revocation on the current cert using that key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                // If it doesn't check out, try to find a different key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                // And if we can't find a key, then return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                PublicKey newKey = cpbr.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    verifyRevocationStatus(currCert, newKey, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    // If that passed, the cert is OK!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                } catch (CertPathValidatorException cpve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    // If it is revoked, rethrow exception
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   648
                    if (cpve.getReason() == BasicReason.REVOKED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        throw cpve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    // Otherwise, ignore the exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    // try to get another key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                badKeys.add(newKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                throw new CertPathValidatorException(iape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            } catch (CertPathBuilderException cpbe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                throw new CertPathValidatorException
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   659
                    ("Could not determine revocation status", null, null,
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 2
diff changeset
   660
                     -1, BasicReason.UNDETERMINED_REVOCATION_STATUS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * This inner class extends the X509CertSelector to add an additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * check to make sure the subject public key isn't on a particular list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * This class is used by buildToNewKey() to make sure the builder doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * end up with a CertPath to a public key that has already been rejected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    private static class RejectKeySelector extends X509CertSelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        private final Set<PublicKey> badKeySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         * Creates a new <code>RejectKeySelector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * @param badPublicKeys a <code>Set</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         *                      <code>PublicKey</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         *                      should be rejected (or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         *                      if no such check should be done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        RejectKeySelector(Set<PublicKey> badPublicKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            this.badKeySet = badPublicKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         * Decides whether a <code>Certificate</code> should be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
         * @param cert the <code>Certificate</code> to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * @return <code>true</code> if the <code>Certificate</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         *         selected, <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        public boolean match(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            if (!super.match(cert))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                return(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (badKeySet.contains(cert.getPublicKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    debug.println("RejectCertSelector.match: bad key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                debug.println("RejectCertSelector.match: returning true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         * Return a printable representation of the <code>CertSelector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         * @return a <code>String</code> describing the contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         *         <code>CertSelector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            sb.append("RejectCertSelector: [\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            sb.append(super.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            sb.append(badKeySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            return sb.toString();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Internal method that verifies a set of possible_crls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * and sees if each is approved, based on the cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @param crls a set of possible CRLs to test for acceptability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @param cert the certificate whose revocation status is being checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @param signFlag <code>true</code> if prevKey was trusted to sign CRLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @param prevKey the public key of the issuer of cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @param reasonsMask the reason code mask
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   733
     * @param trustAnchors a <code>Set</code> of <code>TrustAnchor</code>s>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @return a collection of approved crls (or an empty collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    private Collection<X509CRL> verifyPossibleCRLs(Set<X509CRL> crls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        X509Certificate cert, boolean signFlag, PublicKey prevKey,
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   738
        boolean[] reasonsMask,
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   739
        Set<TrustAnchor> trustAnchors) throws CertPathValidatorException {
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   740
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            X509CertImpl certImpl = X509CertImpl.toImpl(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                debug.println("CRLRevocationChecker.verifyPossibleCRLs: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        "Checking CRLDPs for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        + certImpl.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            CRLDistributionPointsExtension ext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                certImpl.getCRLDistributionPointsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            List<DistributionPoint> points = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                // assume a DP with reasons and CRLIssuer fields omitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                // and a DP name of the cert issuer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                // TODO add issuerAltName too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                X500Name certIssuer = (X500Name)certImpl.getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                DistributionPoint point = new DistributionPoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    (new GeneralNames().add(new GeneralName(certIssuer)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                     null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                points = Collections.singletonList(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                points = (List<DistributionPoint>)ext.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                                        CRLDistributionPointsExtension.POINTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            Set<X509CRL> results = new HashSet<X509CRL>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            DistributionPointFetcher dpf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                DistributionPointFetcher.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            for (Iterator<DistributionPoint> t = points.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                 t.hasNext() && !Arrays.equals(reasonsMask, ALL_REASONS); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                DistributionPoint point = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                for (X509CRL crl : crls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    if (dpf.verifyCRL(certImpl, point, crl, reasonsMask,
9256
230442708954 6882437: CertPath/X509CertPathDiscovery/Test fails on jdk7/pit/b62
xuelei
parents: 5506
diff changeset
   772
                            signFlag, prevKey, mSigProvider,
9511
8f315e0a7b18 7038175: Expired PKITS certificates causing CertPathBuilder and CertPathValidator regression test failures
mullan
parents: 9256
diff changeset
   773
                            trustAnchors, mStores, mParams.getDate())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                        results.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                debug.println("Exception while verifying CRL: "+e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return Collections.emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
}