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