jdk/src/share/classes/sun/security/provider/certpath/OCSPResponse.java
author xuelei
Mon, 01 Nov 2010 07:57:46 -0700
changeset 7040 659824c2a550
parent 5506 202f599c92aa
child 10336 0bb1999251f8
permissions -rw-r--r--
6792180: Enhance to reject weak algorithms or conform to crypto recommendations Reviewed-by: mullan, weijun, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4673
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4673
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4673
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4673
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4673
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
    31
import java.security.cert.CertificateException;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
    32
import java.security.cert.CertificateParsingException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CRLReason;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    35
import java.security.cert.TrustAnchor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.X509Certificate;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
    37
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class is used to process an OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The OCSP Response is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * in RFC 2560 and the ASN.1 encoding is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  OCSPResponse ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      responseStatus         OCSPResponseStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      responseBytes          [0] EXPLICIT ResponseBytes OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   OCSPResponseStatus ::= ENUMERATED {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *       successful            (0),  --Response has valid confirmations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *       malformedRequest      (1),  --Illegal confirmation request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *       internalError         (2),  --Internal error in issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *       tryLater              (3),  --Try again later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *                                   --(4) is not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *       sigRequired           (5),  --Must sign the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *       unauthorized          (6)   --Request unauthorized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   ResponseBytes ::=       SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *       responseType   OBJECT IDENTIFIER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       response       OCTET STRING }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   BasicOCSPResponse       ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      tbsResponseData      ResponseData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      signatureAlgorithm   AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *      signature            BIT STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      certs                [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   The value for signature SHALL be computed on the hash of the DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   encoding ResponseData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   ResponseData ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      version              [0] EXPLICIT Version DEFAULT v1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      responderID              ResponderID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      producedAt               GeneralizedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      responses                SEQUENCE OF SingleResponse,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      responseExtensions   [1] EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *   ResponderID ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *      byName               [1] Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *      byKey                [2] KeyHash }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   KeyHash ::= OCTET STRING -- SHA-1 hash of responder's public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   (excluding the tag and length fields)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   SingleResponse ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *      certID                       CertID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *      certStatus                   CertStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      thisUpdate                   GeneralizedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *      nextUpdate         [0]       EXPLICIT GeneralizedTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      singleExtensions   [1]       EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   CertStatus ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *       good        [0]     IMPLICIT NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *       revoked     [1]     IMPLICIT RevokedInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *       unknown     [2]     IMPLICIT UnknownInfo }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   RevokedInfo ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *       revocationTime              GeneralizedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *       revocationReason    [0]     EXPLICIT CRLReason OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *   UnknownInfo ::= NULL -- this can be replaced with an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @author      Ram Marti
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: 3314
diff changeset
   116
public final class OCSPResponse {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   118
    public enum ResponseStatus {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   119
        SUCCESSFUL,            // Response has valid confirmations
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   120
        MALFORMED_REQUEST,     // Illegal confirmation request
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   121
        INTERNAL_ERROR,        // Internal error in issuer
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   122
        TRY_LATER,             // Try again later
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   123
        UNUSED,                // is not used
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   124
        SIG_REQUIRED,          // Must sign the request
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   125
        UNAUTHORIZED           // Request unauthorized
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   126
    };
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   127
    private static ResponseStatus[] rsvalues = ResponseStatus.values();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final Debug DEBUG = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final boolean dump = false;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   131
    private static final ObjectIdentifier OCSP_BASIC_RESPONSE_OID =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   132
        ObjectIdentifier.newInternal(new int[] { 1, 3, 6, 1, 5, 5, 7, 48, 1, 1});
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   133
    private static final ObjectIdentifier OCSP_NONCE_EXTENSION_OID =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   134
        ObjectIdentifier.newInternal(new int[] { 1, 3, 6, 1, 5, 5, 7, 48, 1, 2});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   136
    private static final int CERT_STATUS_GOOD = 0;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   137
    private static final int CERT_STATUS_REVOKED = 1;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   138
    private static final int CERT_STATUS_UNKNOWN = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // ResponderID CHOICE tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static final int NAME_TAG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final int KEY_TAG = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // Object identifier for the OCSPSigning key purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final String KP_OCSP_SIGNING_OID = "1.3.6.1.5.5.7.3.9";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   147
    private final ResponseStatus responseStatus;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   148
    private final Map<CertId, SingleResponse> singleResponseMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
3314
1e9d33caef08 6787645: CRL validation code should permit some clock skew when checking validity of CRLs
mullan
parents: 2281
diff changeset
   150
    // Maximum clock skew in milliseconds (15 minutes) allowed when checking
1567
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   151
    // validity of OCSP responses
3314
1e9d33caef08 6787645: CRL validation code should permit some clock skew when checking validity of CRLs
mullan
parents: 2281
diff changeset
   152
    private static final long MAX_CLOCK_SKEW = 900000;
1567
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   153
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // an array of all of the CRLReasons (used in SingleResponse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static CRLReason[] values = CRLReason.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Create an OCSP response from its ASN.1 DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   160
    OCSPResponse(byte[] bytes, Date dateCheckedAgainst,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        X509Certificate responderCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        throws IOException, CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   164
        // OCSPResponse
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   165
        if (dump) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   166
            HexDumpEncoder hexEnc = new HexDumpEncoder();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   167
            System.out.println("OCSPResponse bytes are...");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   168
            System.out.println(hexEnc.encode(bytes));
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   169
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   170
        DerValue der = new DerValue(bytes);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   171
        if (der.tag != DerValue.tag_Sequence) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   172
            throw new IOException("Bad encoding in OCSP response: " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   173
                "expected ASN.1 SEQUENCE tag.");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   174
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   175
        DerInputStream derIn = der.getData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   177
        // responseStatus
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   178
        int status = derIn.getEnumerated();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   179
        if (status >= 0 && status < rsvalues.length) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   180
            responseStatus = rsvalues[status];
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   181
        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   182
            // unspecified responseStatus
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   183
            throw new IOException("Unknown OCSPResponse status: " + status);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   184
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   185
        if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   186
            DEBUG.println("OCSP response status: " + responseStatus);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   187
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   188
        if (responseStatus != ResponseStatus.SUCCESSFUL) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   189
            // no need to continue, responseBytes are not set.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   190
            singleResponseMap = Collections.emptyMap();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   191
            return;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   192
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   194
        // responseBytes
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   195
        der = derIn.getDerValue();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   196
        if (!der.isContextSpecific((byte)0)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   197
            throw new IOException("Bad encoding in responseBytes element " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   198
                "of OCSP response: expected ASN.1 context specific tag 0.");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   199
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   200
        DerValue tmp = der.data.getDerValue();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   201
        if (tmp.tag != DerValue.tag_Sequence) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   202
            throw new IOException("Bad encoding in responseBytes element " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   203
                "of OCSP response: expected ASN.1 SEQUENCE tag.");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   204
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   206
        // responseType
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   207
        derIn = tmp.data;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   208
        ObjectIdentifier responseType = derIn.getOID();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   209
        if (responseType.equals(OCSP_BASIC_RESPONSE_OID)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   210
            if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   211
                DEBUG.println("OCSP response type: basic");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   212
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   213
        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   214
            if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   215
                DEBUG.println("OCSP response type: " + responseType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   217
            throw new IOException("Unsupported OCSP response type: " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   218
                responseType);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   219
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   221
        // BasicOCSPResponse
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   222
        DerInputStream basicOCSPResponse =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   223
            new DerInputStream(derIn.getOctetString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   225
        DerValue[] seqTmp = basicOCSPResponse.getSequence(2);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   226
        if (seqTmp.length < 3) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   227
            throw new IOException("Unexpected BasicOCSPResponse value");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   228
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   230
        DerValue responseData = seqTmp[0];
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   231
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   232
        // Need the DER encoded ResponseData to verify the signature later
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   233
        byte[] responseDataDer = seqTmp[0].toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   235
        // tbsResponseData
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   236
        if (responseData.tag != DerValue.tag_Sequence) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   237
            throw new IOException("Bad encoding in tbsResponseData " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   238
                "element of OCSP response: expected ASN.1 SEQUENCE tag.");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   239
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   240
        DerInputStream seqDerIn = responseData.data;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   241
        DerValue seq = seqDerIn.getDerValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   243
        // version
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   244
        if (seq.isContextSpecific((byte)0)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   245
            // seq[0] is version
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   246
            if (seq.isConstructed() && seq.isContextSpecific()) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   247
                //System.out.println ("version is available");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   248
                seq = seq.data.getDerValue();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   249
                int version = seq.getInteger();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   250
                if (seq.data.available() != 0) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   251
                    throw new IOException("Bad encoding in version " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   252
                        " element of OCSP response: bad format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   254
                seq = seqDerIn.getDerValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   256
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   258
        // responderID
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   259
        short tag = (byte)(seq.tag & 0x1f);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   260
        if (tag == NAME_TAG) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   261
            if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   262
                X500Name responderName = new X500Name(seq.getData());
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   263
                DEBUG.println("OCSP Responder name: " + responderName);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   264
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   265
        } else if (tag == KEY_TAG) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   266
            // Ignore, for now
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   267
        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   268
            throw new IOException("Bad encoding in responderID element of " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   269
                "OCSP response: expected ASN.1 context specific tag 0 or 1");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   270
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   272
        // producedAt
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   273
        seq = seqDerIn.getDerValue();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   274
        if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   275
            Date producedAtDate = seq.getGeneralizedTime();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   276
            DEBUG.println("OCSP response produced at: " + producedAtDate);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   277
        }
2
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: 3314
diff changeset
   279
        // responses
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   280
        DerValue[] singleResponseDer = seqDerIn.getSequence(1);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   281
        singleResponseMap
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   282
            = new HashMap<CertId, SingleResponse>(singleResponseDer.length);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   283
        if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   284
            DEBUG.println("OCSP number of SingleResponses: "
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   285
                + singleResponseDer.length);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   286
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   287
        for (int i = 0; i < singleResponseDer.length; i++) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   288
            SingleResponse singleResponse
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   289
                = new SingleResponse(singleResponseDer[i]);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   290
            singleResponseMap.put(singleResponse.getCertId(), singleResponse);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   291
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   293
        // responseExtensions
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   294
        if (seqDerIn.available() > 0) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   295
            seq = seqDerIn.getDerValue();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   296
            if (seq.isContextSpecific((byte)1)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   297
                DerValue[] responseExtDer = seq.data.getSequence(3);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   298
                for (int i = 0; i < responseExtDer.length; i++) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   299
                    Extension responseExtension
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   300
                        = new Extension(responseExtDer[i]);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   301
                    if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   302
                        DEBUG.println("OCSP extension: " + responseExtension);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   303
                    }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   304
                    if (responseExtension.getExtensionId().equals(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   305
                        OCSP_NONCE_EXTENSION_OID)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   306
                        /*
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   307
                        ocspNonce =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   308
                            responseExtension[i].getExtensionValue();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   309
                         */
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   310
                    } else if (responseExtension.isCritical())  {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   311
                        throw new IOException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   312
                            "Unsupported OCSP critical extension: " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   313
                            responseExtension.getExtensionId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   317
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   319
        // signatureAlgorithmId
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   320
        AlgorithmId sigAlgId = AlgorithmId.parse(seqTmp[1]);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   321
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   322
        // signature
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   323
        byte[] signature = seqTmp[2].getBitString();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   324
        X509CertImpl[] x509Certs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   326
        // if seq[3] is available , then it is a sequence of certificates
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   327
        if (seqTmp.length > 3) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   328
            // certs are available
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   329
            DerValue seqCert = seqTmp[3];
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   330
            if (!seqCert.isContextSpecific((byte)0)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   331
                throw new IOException("Bad encoding in certs element of " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   332
                    "OCSP response: expected ASN.1 context specific tag 0.");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   333
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   334
            DerValue[] certs = seqCert.getData().getSequence(3);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   335
            x509Certs = new X509CertImpl[certs.length];
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   336
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    x509Certs[i] = new X509CertImpl(certs[i].toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   340
            } catch (CertificateException ce) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   341
                throw new IOException("Bad encoding in X509 Certificate", ce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   343
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   345
        // Check whether the cert returned by the responder is trusted
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   346
        if (x509Certs != null && x509Certs[0] != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   347
            X509CertImpl cert = x509Certs[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   349
            // First check if the cert matches the responder cert which
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   350
            // was set locally.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   351
            if (cert.equals(responderCert)) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   352
                // cert is trusted, now verify the signed response
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   354
            // Next check if the cert was issued by the responder cert
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   355
            // which was set locally.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   356
            } else if (cert.getIssuerX500Principal().equals(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   357
                responderCert.getSubjectX500Principal())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   359
                // Check for the OCSPSigning key purpose
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   360
                try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    List<String> keyPurposes = cert.getExtendedKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    if (keyPurposes == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        !keyPurposes.contains(KP_OCSP_SIGNING_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        throw new CertPathValidatorException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            "Responder's certificate not valid for signing " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            "OCSP responses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   368
                } catch (CertificateParsingException cpe) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   369
                    // assume cert is not valid for signing
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   370
                    throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   371
                        "Responder's certificate not valid for signing " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   372
                        "OCSP responses", cpe);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   373
                }
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   374
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   375
                // Check algorithm constraints specified in security property
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   376
                // "jdk.certpath.disabledAlgorithms".
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   377
                AlgorithmChecker algChecker = new AlgorithmChecker(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   378
                                    new TrustAnchor(responderCert, null));
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   379
                algChecker.init(false);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   380
                algChecker.check(cert, Collections.<String>emptySet());
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   381
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   382
                // check the validity
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   383
                try {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   384
                    if (dateCheckedAgainst == null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   385
                        cert.checkValidity();
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   386
                    } else {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   387
                        cert.checkValidity(dateCheckedAgainst);
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   388
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   389
                } catch (GeneralSecurityException e) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   390
                    throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   391
                        "Responder's certificate not within the " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   392
                        "validity period", e);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   393
                }
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   394
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   395
                // check for revocation
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   396
                //
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   397
                // A CA may specify that an OCSP client can trust a
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   398
                // responder for the lifetime of the responder's
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   399
                // certificate. The CA does so by including the
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   400
                // extension id-pkix-ocsp-nocheck.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   401
                //
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   402
                Extension noCheck =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   403
                    cert.getExtension(PKIXExtensions.OCSPNoCheck_Id);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   404
                if (noCheck != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   405
                    if (DEBUG != null) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   406
                        DEBUG.println("Responder's certificate includes " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   407
                            "the extension id-pkix-ocsp-nocheck.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    }
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   409
                } else {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   410
                    // we should do the revocation checking of the
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   411
                    // authorized responder in a future update.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   414
                // verify the signature
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   415
                try {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   416
                    cert.verify(responderCert.getPublicKey());
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   417
                    responderCert = cert;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   418
                    // cert is trusted, now verify the signed response
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   420
                } catch (GeneralSecurityException e) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   421
                    responderCert = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   424
                throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   425
                    "Responder's certificate is not authorized to sign " +
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   426
                    "OCSP responses");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   427
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   428
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   429
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   430
        // Confirm that the signed response was generated using the public
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   431
        // key from the trusted responder cert
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   432
        if (responderCert != null) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   433
            // Check algorithm constraints specified in security property
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   434
            // "jdk.certpath.disabledAlgorithms".
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   435
            AlgorithmChecker.check(responderCert.getPublicKey(), sigAlgId);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   436
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   437
            if (!verifyResponse(responseDataDer, responderCert,
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   438
                sigAlgId, signature)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                throw new CertPathValidatorException(
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   440
                    "Error verifying OCSP Responder's signature");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   442
        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   443
            // Need responder's cert in order to verify the signature
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   444
            throw new CertPathValidatorException(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   445
                "Unable to verify OCSP Responder's signature");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   449
    /**
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   450
     * Returns the OCSP ResponseStatus.
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   451
     */
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   452
    ResponseStatus getResponseStatus() {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   453
        return responseStatus;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   454
    }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   455
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Verify the signature of the OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * The responder's cert is implicitly trusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private boolean verifyResponse(byte[] responseData, X509Certificate cert,
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   461
        AlgorithmId sigAlgId, byte[] signBytes)
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   462
        throws CertPathValidatorException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            Signature respSignature = Signature.getInstance(sigAlgId.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            respSignature.initVerify(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            respSignature.update(responseData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (respSignature.verify(signBytes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                if (DEBUG != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    DEBUG.println("Verified signature of OCSP Responder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if (DEBUG != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    DEBUG.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        "Error verifying signature of OCSP Responder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        } catch (InvalidKeyException ike) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   483
            throw new CertPathValidatorException(ike);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } catch (NoSuchAlgorithmException nsae) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   485
            throw new CertPathValidatorException(nsae);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   486
        } catch (SignatureException se) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   487
            throw new CertPathValidatorException(se);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   491
    /**
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   492
     * Returns the SingleResponse of the specified CertId, or null if
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   493
     * there is no response for that CertId.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   495
    SingleResponse getSingleResponse(CertId certId) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   496
        return singleResponseMap.get(certId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * A class representing a single OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   502
    final static class SingleResponse implements OCSP.RevocationStatus {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   503
        private final CertId certId;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   504
        private final CertStatus certStatus;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   505
        private final Date thisUpdate;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   506
        private final Date nextUpdate;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   507
        private final Date revocationTime;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   508
        private final CRLReason revocationReason;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   509
        private final Map<String, java.security.cert.Extension> singleExtensions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        private SingleResponse(DerValue der) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (der.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                throw new IOException("Bad ASN.1 encoding in SingleResponse");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            DerInputStream tmp = der.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            certId = new CertId(tmp.getDerValue().data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            DerValue derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            short tag = (byte)(derVal.tag & 0x1f);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   520
            if (tag ==  CERT_STATUS_REVOKED) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   521
                certStatus = CertStatus.REVOKED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                revocationTime = derVal.data.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (derVal.data.available() != 0) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   524
                    DerValue dv = derVal.data.getDerValue();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   525
                    tag = (byte)(dv.tag & 0x1f);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   526
                    if (tag == 0) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   527
                        int reason = dv.data.getEnumerated();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   528
                        // if reason out-of-range just leave as UNSPECIFIED
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   529
                        if (reason >= 0 && reason < values.length) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   530
                            revocationReason = values[reason];
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   531
                        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   532
                            revocationReason = CRLReason.UNSPECIFIED;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   533
                        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   534
                    } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   535
                        revocationReason = CRLReason.UNSPECIFIED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   537
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   538
                    revocationReason = CRLReason.UNSPECIFIED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                // RevokedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (DEBUG != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    DEBUG.println("Revocation time: " + revocationTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    DEBUG.println("Revocation reason: " + revocationReason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            } else {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   546
                revocationTime = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   547
                revocationReason = CRLReason.UNSPECIFIED;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   548
                if (tag == CERT_STATUS_GOOD) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   549
                    certStatus = CertStatus.GOOD;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   550
                } else if (tag == CERT_STATUS_UNKNOWN) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   551
                    certStatus = CertStatus.UNKNOWN;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   552
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   553
                    throw new IOException("Invalid certificate status");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   554
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            thisUpdate = tmp.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (tmp.available() == 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                // we are done
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   561
                nextUpdate = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                tag = (byte)(derVal.tag & 0x1f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                if (tag == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    // next update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    nextUpdate = derVal.data.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    if (tmp.available() == 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        // we are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        tag = (byte)(derVal.tag & 0x1f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   575
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   576
                    nextUpdate = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            // singleExtensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            if (tmp.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                if (derVal.isContextSpecific((byte)1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    DerValue[] singleExtDer = derVal.data.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    singleExtensions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        new HashMap<String, java.security.cert.Extension>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                            (singleExtDer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    for (int i = 0; i < singleExtDer.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        Extension ext = new Extension(singleExtDer[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        if (DEBUG != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                            DEBUG.println("OCSP single extension: " + ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        }
4673
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   592
                        // We don't support any extensions yet. Therefore, if it
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   593
                        // is critical we must throw an exception because we
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   594
                        // don't know how to process it.
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   595
                        if (ext.isCritical()) {
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   596
                            throw new IOException(
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   597
                                "Unsupported OCSP critical extension: " +
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   598
                                ext.getExtensionId());
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   599
                        }
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   600
                        singleExtensions.put(ext.getId(), ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   602
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   603
                    singleExtensions = Collections.emptyMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   605
            } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   606
                singleExtensions = Collections.emptyMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
1567
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   609
            long now = System.currentTimeMillis();
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   610
            Date nowPlusSkew = new Date(now + MAX_CLOCK_SKEW);
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   611
            Date nowMinusSkew = new Date(now - MAX_CLOCK_SKEW);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (DEBUG != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                String until = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                if (nextUpdate != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    until = " until " + nextUpdate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                DEBUG.println("Response's validity interval is from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    thisUpdate + until);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            // Check that the test date is within the validity interval
1567
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   621
            if ((thisUpdate != null && nowPlusSkew.before(thisUpdate)) ||
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   622
                (nextUpdate != null && nowMinusSkew.after(nextUpdate))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                if (DEBUG != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    DEBUG.println("Response is unreliable: its validity " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        "interval is out-of-date");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                throw new IOException("Response is unreliable: its validity " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    "interval is out-of-date");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * Return the certificate's revocation status code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   636
        @Override public CertStatus getCertStatus() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return certStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        private CertId getCertId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            return certId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   644
        @Override public Date getRevocationTime() {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   645
            return (Date) revocationTime.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   648
        @Override public CRLReason getRevocationReason() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return revocationReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   652
        @Override
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   653
        public Map<String, java.security.cert.Extension> getSingleExtensions() {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   654
            return Collections.unmodifiableMap(singleExtensions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * Construct a string representation of a single OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   660
        @Override public String toString() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            sb.append("SingleResponse:  \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            sb.append(certId);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   664
            sb.append("\nCertStatus: "+ certStatus + "\n");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   665
            if (certStatus == CertStatus.REVOKED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                sb.append("revocationTime is " + revocationTime + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                sb.append("revocationReason is " + revocationReason + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            sb.append("thisUpdate is " + thisUpdate + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            if (nextUpdate != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                sb.append("nextUpdate is " + nextUpdate + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
}