jdk/src/share/classes/sun/security/provider/certpath/OCSPChecker.java
author asaha
Wed, 14 Oct 2009 14:21:25 -0700
changeset 4211 10ea9f654342
parent 4209 e2e5a973b879
parent 4044 b2b2ee2ca90c
child 4318 fdcb5ce9dc00
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3214
3ee25b27ed91 6853793: OutOfMemoryError in sun.security.provider.certpath.OCSPChecker.check
xuelei
parents: 1238
diff changeset
     2
 * Copyright 2003-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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.*;
1238
6d1f4b722acd 6465942: Add problem identification facility to the CertPathValidator framework
mullan
parents: 715
diff changeset
    34
import java.security.cert.CertPathValidatorException.BasicReason;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    35
import java.net.URI;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    36
import java.net.URISyntaxException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    39
import static sun.security.provider.certpath.OCSP.*;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    40
import sun.security.util.Debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * OCSPChecker is a <code>PKIXCertPathChecker</code> that uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Online Certificate Status Protocol (OCSP) as specified in RFC 2560
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <a href="http://www.ietf.org/rfc/rfc2560.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * http://www.ietf.org/rfc/rfc2560.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author      Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class OCSPChecker extends PKIXCertPathChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    53
    static final String OCSP_ENABLE_PROP = "ocsp.enable";
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    54
    static final String OCSP_URL_PROP = "ocsp.responderURL";
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    55
    static final String OCSP_CERT_SUBJECT_PROP =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "ocsp.responderCertSubjectName";
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    57
    static final String OCSP_CERT_ISSUER_PROP = "ocsp.responderCertIssuerName";
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    58
    static final String OCSP_CERT_NUMBER_PROP =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "ocsp.responderCertSerialNumber";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final String HEX_DIGITS = "0123456789ABCDEFabcdef";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final Debug DEBUG = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final boolean dump = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int remainingCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private X509Certificate[] certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private CertPath cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private PKIXParameters pkixParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    73
    private boolean onlyEECert = false;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Default Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param certPath the X509 certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param pkixParams the input PKIX parameter set
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    80
     * @throws CertPathValidatorException if OCSPChecker can not be created
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    OCSPChecker(CertPath certPath, PKIXParameters pkixParams)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throws CertPathValidatorException {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    84
        this(certPath, pkixParams, false);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    85
    }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    86
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    87
    OCSPChecker(CertPath certPath, PKIXParameters pkixParams, boolean onlyEECert)
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    88
        throws CertPathValidatorException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.cp = certPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.pkixParams = pkixParams;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
    92
        this.onlyEECert = onlyEECert;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        List<? extends Certificate> tmp = cp.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        certs = tmp.toArray(new X509Certificate[tmp.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Initializes the internal state of the checker from parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * specified in the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   102
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public void init(boolean forward) throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!forward) {
625
ba0b63c29671 6383078: OCSP checking does not work on end-entity certificate
vinnie
parents: 2
diff changeset
   105
            remainingCerts = certs.length + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new CertPathValidatorException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                "Forward checking not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   112
    @Override public boolean isForwardCheckingSupported() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   116
    @Override public Set<String> getSupportedExtensions() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return Collections.<String>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Sends an OCSPRequest for the certificate to the OCSP Server and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * processes the response back from the OCSP Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param cert the Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param unresolvedCritExts the unresolved critical extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception CertPathValidatorException Exception is thrown if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *            certificate has been revoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   129
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void check(Certificate cert, Collection<String> unresolvedCritExts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
625
ba0b63c29671 6383078: OCSP checking does not work on end-entity certificate
vinnie
parents: 2
diff changeset
   133
        // Decrement the certificate counter
ba0b63c29671 6383078: OCSP checking does not work on end-entity certificate
vinnie
parents: 2
diff changeset
   134
        remainingCerts--;
ba0b63c29671 6383078: OCSP checking does not work on end-entity certificate
vinnie
parents: 2
diff changeset
   135
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   136
        X509CertImpl currCertImpl = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   138
            currCertImpl = X509CertImpl.toImpl((X509Certificate)cert);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   139
        } catch (CertificateException ce) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   140
            throw new CertPathValidatorException(ce);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   141
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   142
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   143
        if (onlyEECert && currCertImpl.getBasicConstraints() != -1) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   144
            if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   145
                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: 3214
diff changeset
   146
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   147
            return;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   148
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   150
        /*
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   151
         * OCSP security property values, in the following order:
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   152
         *   1. ocsp.responderURL
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   153
         *   2. ocsp.responderCertSubjectName
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   154
         *   3. ocsp.responderCertIssuerName
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   155
         *   4. ocsp.responderCertSerialNumber
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   156
         */
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   157
        // should cache these properties to avoid calling every time?
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   158
        String[] properties = getOCSPProperties();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   159
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   160
        // Check whether OCSP is feasible before seeking cert information
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   161
        URI uri = getOCSPServerURI(currCertImpl, properties[0]);
625
ba0b63c29671 6383078: OCSP checking does not work on end-entity certificate
vinnie
parents: 2
diff changeset
   162
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   163
        // When responder's subject name is set then the issuer/serial
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   164
        // properties are ignored
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   165
        X500Principal responderSubjectName = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   166
        X500Principal responderIssuerName = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   167
        BigInteger responderSerialNumber = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   168
        if (properties[1] != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   169
            responderSubjectName = new X500Principal(properties[1]);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   170
        } else if (properties[2] != null && properties[3] != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   171
            responderIssuerName = new X500Principal(properties[2]);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   172
            // remove colon or space separators
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   173
            String value = stripOutSeparators(properties[3]);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   174
            responderSerialNumber = new BigInteger(value, 16);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   175
        } else if (properties[2] != null || properties[3] != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   176
            throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   177
                "Must specify both ocsp.responderCertIssuerName and " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   178
                "ocsp.responderCertSerialNumber properties");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   179
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   181
        // If the OCSP responder cert properties are set then the
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   182
        // identified cert must be located in the trust anchors or
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   183
        // in the cert stores.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   184
        boolean seekResponderCert = false;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   185
        if (responderSubjectName != null || responderIssuerName != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   186
            seekResponderCert = true;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   187
        }
625
ba0b63c29671 6383078: OCSP checking does not work on end-entity certificate
vinnie
parents: 2
diff changeset
   188
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   189
        // Set the issuer certificate to the next cert in the chain
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   190
        // (unless we're processing the final cert).
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   191
        X509Certificate issuerCert = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   192
        boolean seekIssuerCert = true;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   193
        X509Certificate responderCert = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   194
        if (remainingCerts < certs.length) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   195
            issuerCert = certs[remainingCerts];
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   196
            seekIssuerCert = false; // done
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   198
            // By default, the OCSP responder's cert is the same as the
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   199
            // issuer of the cert being validated.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   200
            if (!seekResponderCert) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   201
                responderCert = issuerCert;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   202
                if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   203
                    DEBUG.println("Responder's certificate is the same " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   204
                        "as the issuer of the certificate being validated");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   205
                }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   206
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   207
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   209
        // Check anchor certs for:
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   210
        //    - the issuer cert (of the cert being validated)
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   211
        //    - the OCSP responder's cert
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   212
        if (seekIssuerCert || seekResponderCert) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   213
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   214
            if (DEBUG != null && seekResponderCert) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   215
                DEBUG.println("Searching trust anchors for responder's " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   216
                    "certificate");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   219
            // Extract the anchor certs
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   220
            Iterator<TrustAnchor> anchors
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   221
                = pkixParams.getTrustAnchors().iterator();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   222
            if (!anchors.hasNext()) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   223
                throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   224
                    "Must specify at least one trust anchor");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   227
            X500Principal certIssuerName =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   228
                currCertImpl.getIssuerX500Principal();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   229
            while (anchors.hasNext() && (seekIssuerCert || seekResponderCert)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   230
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   231
                TrustAnchor anchor = anchors.next();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   232
                X509Certificate anchorCert = anchor.getTrustedCert();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   233
                X500Principal anchorSubjectName =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   234
                    anchorCert.getSubjectX500Principal();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   235
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   236
                if (dump) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   237
                    System.out.println("Issuer DN is " + certIssuerName);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   238
                    System.out.println("Subject DN is " + anchorSubjectName);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   239
                }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   240
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   241
                // Check if anchor cert is the issuer cert
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   242
                if (seekIssuerCert &&
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   243
                    certIssuerName.equals(anchorSubjectName)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   244
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   245
                    issuerCert = anchorCert;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   246
                    seekIssuerCert = false; // done
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   248
                    // By default, the OCSP responder's cert is the same as
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   249
                    // the issuer of the cert being validated.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   250
                    if (!seekResponderCert && responderCert == null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   251
                        responderCert = anchorCert;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   252
                        if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   253
                            DEBUG.println("Responder's certificate is the" +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   254
                                " same as the issuer of the certificate " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   255
                                "being validated");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   256
                        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   257
                    }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   258
                }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   259
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   260
                // Check if anchor cert is the responder cert
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   261
                if (seekResponderCert) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   262
                    // Satisfy the responder subject name property only, or
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   263
                    // satisfy the responder issuer name and serial number
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   264
                    // properties only
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   265
                    if ((responderSubjectName != null &&
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   266
                         responderSubjectName.equals(anchorSubjectName)) ||
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   267
                        (responderIssuerName != null &&
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   268
                         responderSerialNumber != null &&
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   269
                         responderIssuerName.equals(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   270
                         anchorCert.getIssuerX500Principal()) &&
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   271
                         responderSerialNumber.equals(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   272
                         anchorCert.getSerialNumber()))) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   273
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   274
                        responderCert = anchorCert;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   275
                        seekResponderCert = false; // done
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   279
            if (issuerCert == null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   280
                throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   281
                    "No trusted certificate for " + currCertImpl.getIssuerDN());
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   282
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   284
            // Check cert stores if responder cert has not yet been found
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   285
            if (seekResponderCert) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   286
                if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   287
                    DEBUG.println("Searching cert stores for responder's " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        "certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   290
                X509CertSelector filter = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   291
                if (responderSubjectName != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   292
                    filter = new X509CertSelector();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   293
                    filter.setSubject(responderSubjectName);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   294
                } else if (responderIssuerName != null &&
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   295
                    responderSerialNumber != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   296
                    filter = new X509CertSelector();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   297
                    filter.setIssuer(responderIssuerName);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   298
                    filter.setSerialNumber(responderSerialNumber);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   300
                if (filter != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   301
                    List<CertStore> certStores = pkixParams.getCertStores();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   302
                    for (CertStore certStore : certStores) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   303
                        Iterator i = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   304
                        try {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   305
                            i = certStore.getCertificates(filter).iterator();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   306
                        } catch (CertStoreException cse) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   307
                            // ignore and try next certStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            if (DEBUG != null) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   309
                                DEBUG.println("CertStore exception:" + cse);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   311
                            continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   313
                        if (i.hasNext()) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   314
                            responderCert = (X509Certificate) i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                            seekResponderCert = false; // done
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   316
                            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   321
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   323
        // Could not find the certificate identified in the OCSP properties
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   324
        if (seekResponderCert) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   325
            throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   326
                "Cannot find the responder's certificate " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   327
                "(set using the OCSP security properties).");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   328
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   330
        CertId certId = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   331
        OCSPResponse response = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   332
        try {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   333
            certId = new CertId
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   334
                (issuerCert, currCertImpl.getSerialNumberObject());
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   335
            response = OCSP.check(Collections.singletonList(certId), uri,
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   336
                responderCert, pkixParams.getDate());
4044
b2b2ee2ca90c 6885667: CertPath/CertPathValidatorTest/bugs/bug6383078 fails on jdk6u18/b02, jdk7/pit/b73 and passes on b72.
mullan
parents: 3841
diff changeset
   337
        } catch (Exception e) {
b2b2ee2ca90c 6885667: CertPath/CertPathValidatorTest/bugs/bug6383078 fails on jdk6u18/b02, jdk7/pit/b73 and passes on b72.
mullan
parents: 3841
diff changeset
   338
            // Wrap all exceptions in CertPathValidatorException so that
b2b2ee2ca90c 6885667: CertPath/CertPathValidatorTest/bugs/bug6383078 fails on jdk6u18/b02, jdk7/pit/b73 and passes on b72.
mullan
parents: 3841
diff changeset
   339
            // we can fallback to CRLs, if enabled.
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   340
            throw new CertPathValidatorException
4044
b2b2ee2ca90c 6885667: CertPath/CertPathValidatorTest/bugs/bug6383078 fails on jdk6u18/b02, jdk7/pit/b73 and passes on b72.
mullan
parents: 3841
diff changeset
   341
                ("Unable to send OCSP request", e);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   342
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   344
        RevocationStatus rs = (RevocationStatus) response.getSingleResponse(certId);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   345
        RevocationStatus.CertStatus certStatus = rs.getCertStatus();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   346
        if (certStatus == RevocationStatus.CertStatus.REVOKED) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   347
            Throwable t = new CertificateRevokedException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   348
                rs.getRevocationTime(), rs.getRevocationReason(),
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   349
                responderCert.getSubjectX500Principal(),
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   350
                rs.getSingleExtensions());
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   351
            throw new CertPathValidatorException(t.getMessage(), t,
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   352
                null, -1, BasicReason.REVOKED);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   353
        } else if (certStatus == RevocationStatus.CertStatus.UNKNOWN) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   354
            throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   355
                "Certificate's revocation status is unknown", null, cp,
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   356
                remainingCerts, BasicReason.UNDETERMINED_REVOCATION_STATUS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * The OCSP security property values are in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *   1. ocsp.responderURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *   2. ocsp.responderCertSubjectName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *   3. ocsp.responderCertIssuerName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *   4. ocsp.responderCertSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   367
    private static URI getOCSPServerURI(X509CertImpl currCertImpl,
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   368
        String responderURL) throws CertPathValidatorException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   370
        if (responderURL != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   371
            try {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   372
                return new URI(responderURL);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   373
            } catch (URISyntaxException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                throw new CertPathValidatorException(e);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   375
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Examine the certificate's AuthorityInfoAccess extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        AuthorityInfoAccessExtension aia =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            currCertImpl.getAuthorityInfoAccessExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (aia == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new CertPathValidatorException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                "Must specify the location of an OCSP Responder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        List<AccessDescription> descriptions = aia.getAccessDescriptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        for (AccessDescription description : descriptions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (description.getAccessMethod().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                AccessDescription.Ad_OCSP_Id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                GeneralName generalName = description.getAccessLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                if (generalName.getType() == GeneralNameInterface.NAME_URI) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   393
                    URIName uri = (URIName) generalName.getName();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3214
diff changeset
   394
                    return uri.getURI();
2
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
        throw new CertPathValidatorException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            "Cannot find the location of the OCSP Responder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Retrieves the values of the OCSP security properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private static String[] getOCSPProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        final String[] properties = new String[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    properties[0] = Security.getProperty(OCSP_URL_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    properties[1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        Security.getProperty(OCSP_CERT_SUBJECT_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    properties[2] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        Security.getProperty(OCSP_CERT_ISSUER_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    properties[3] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        Security.getProperty(OCSP_CERT_NUMBER_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Removes any non-hexadecimal characters from a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    private static String stripOutSeparators(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        char[] chars = value.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        StringBuilder hexNumber = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        for (int i = 0; i < chars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (HEX_DIGITS.indexOf(chars[i]) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                hexNumber.append(chars[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return hexNumber.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}