jdk/src/share/classes/sun/security/provider/certpath/OCSPResponse.java
author vinnie
Wed, 29 May 2013 14:57:51 +0100
changeset 17729 90a2bf654c4f
parent 17044 c67bf062ca30
child 19045 bc9a25fff6c5
permissions -rw-r--r--
7174966: With OCSP enabled on Java 7 get error 'Wrong key usage' with Comodo certificate Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17044
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
     2
 * Copyright (c) 2003, 2013, 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.security.*;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
    30
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
    31
import java.security.cert.CertificateParsingException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CRLReason;
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
    34
import java.security.cert.TrustAnchor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.X509Certificate;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    36
import java.util.ArrayList;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    37
import java.util.Arrays;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
    38
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Map;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    43
import javax.security.auth.x500.X500Principal;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.misc.HexDumpEncoder;
17044
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
    46
import sun.security.action.GetIntegerAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This class is used to process an OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The OCSP Response is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * in RFC 2560 and the ASN.1 encoding is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *  OCSPResponse ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      responseStatus         OCSPResponseStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *      responseBytes          [0] EXPLICIT ResponseBytes OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   OCSPResponseStatus ::= ENUMERATED {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *       successful            (0),  --Response has valid confirmations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *       malformedRequest      (1),  --Illegal confirmation request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *       internalError         (2),  --Internal error in issuer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *       tryLater              (3),  --Try again later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *                                   --(4) is not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *       sigRequired           (5),  --Must sign the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *       unauthorized          (6)   --Request unauthorized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   ResponseBytes ::=       SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *       responseType   OBJECT IDENTIFIER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *       response       OCTET STRING }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   BasicOCSPResponse       ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      tbsResponseData      ResponseData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      signatureAlgorithm   AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      signature            BIT STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      certs                [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   The value for signature SHALL be computed on the hash of the DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   encoding ResponseData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *   ResponseData ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      version              [0] EXPLICIT Version DEFAULT v1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *      responderID              ResponderID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *      producedAt               GeneralizedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *      responses                SEQUENCE OF SingleResponse,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *      responseExtensions   [1] EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   ResponderID ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *      byName               [1] Name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *      byKey                [2] KeyHash }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   KeyHash ::= OCTET STRING -- SHA-1 hash of responder's public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   (excluding the tag and length fields)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   SingleResponse ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      certID                       CertID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *      certStatus                   CertStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      thisUpdate                   GeneralizedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *      nextUpdate         [0]       EXPLICIT GeneralizedTime OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      singleExtensions   [1]       EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   CertStatus ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *       good        [0]     IMPLICIT NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *       revoked     [1]     IMPLICIT RevokedInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *       unknown     [2]     IMPLICIT UnknownInfo }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *   RevokedInfo ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *       revocationTime              GeneralizedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *       revocationReason    [0]     EXPLICIT CRLReason OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *   UnknownInfo ::= NULL -- this can be replaced with an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @author      Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   120
public final class OCSPResponse {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   122
    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
   123
        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
   124
        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
   125
        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
   126
        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
   127
        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
   128
        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
   129
        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
   130
    };
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 ResponseStatus[] rsvalues = ResponseStatus.values();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   133
    private static final Debug debug = Debug.getInstance("certpath");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    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
   135
    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
   136
        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
   137
    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
   138
    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
   139
    private static final int CERT_STATUS_UNKNOWN = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // ResponderID CHOICE tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final int NAME_TAG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static final int KEY_TAG = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // Object identifier for the OCSPSigning key purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static final String KP_OCSP_SIGNING_OID = "1.3.6.1.5.5.7.3.9";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
17044
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   148
    // Default maximum clock skew in milliseconds (15 minutes)
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   149
    // allowed when checking validity of OCSP responses
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   150
    private static final int DEFAULT_MAX_CLOCK_SKEW = 900000;
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   151
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   152
    /**
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   153
     * Integer value indicating the maximum allowable clock skew, in seconds,
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   154
     * to be used for the OCSP check.
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   155
     */
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   156
    private static final int MAX_CLOCK_SKEW = initializeClockSkew();
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   157
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   158
    /**
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   159
     * Initialize the maximum allowable clock skew by getting the OCSP
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   160
     * clock skew system property. If the property has not been set, or if its
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   161
     * value is negative, set the skew to the default.
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   162
     */
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   163
    private static int initializeClockSkew() {
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   164
        Integer tmp = java.security.AccessController.doPrivileged(
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   165
                new GetIntegerAction("com.sun.security.ocsp.clockSkew"));
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   166
        if (tmp == null || tmp < 0) {
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   167
            return DEFAULT_MAX_CLOCK_SKEW;
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   168
        }
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   169
        // Convert to milliseconds, as the system property will be
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   170
        // specified in seconds
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   171
        return tmp * 1000;
c67bf062ca30 8013228: Create new system properties to control allowable OCSP clock skew and CRL connection timeout
mullan
parents: 13402
diff changeset
   172
    }
1567
58ef474069d7 6744888: OCSP validation code should permit some clock skew when checking validity of OCSP responses
mullan
parents: 2
diff changeset
   173
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // an array of all of the CRLReasons (used in SingleResponse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static CRLReason[] values = CRLReason.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   177
    private final ResponseStatus responseStatus;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   178
    private final Map<CertId, SingleResponse> singleResponseMap;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   179
    private final List<X509CertImpl> certs;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   180
    private final AlgorithmId sigAlgId;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   181
    private final byte[] signature;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   182
    private final byte[] tbsResponseData;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   183
    private final byte[] responseNonce;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   184
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Create an OCSP response from its ASN.1 DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   188
    OCSPResponse(byte[] bytes) throws IOException {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   189
        if (dump) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   190
            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
   191
            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
   192
            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
   193
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   194
        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
   195
        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
   196
            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
   197
                "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
   198
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   199
        DerInputStream derIn = der.getData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   201
        // responseStatus
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   202
        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
   203
        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
   204
            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
   205
        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   206
            // unspecified responseStatus
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   207
            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
   208
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   209
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   210
            debug.println("OCSP response status: " + responseStatus);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   211
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   212
        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
   213
            // 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
   214
            singleResponseMap = Collections.emptyMap();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   215
            certs = Collections.<X509CertImpl>emptyList();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   216
            sigAlgId = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   217
            signature = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   218
            tbsResponseData = null;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   219
            responseNonce = null;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   220
            return;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   221
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   223
        // responseBytes
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   224
        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
   225
        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
   226
            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
   227
                "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
   228
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   229
        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
   230
        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
   231
            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
   232
                "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
   233
        }
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
        // responseType
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   236
        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
   237
        ObjectIdentifier responseType = derIn.getOID();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7040
diff changeset
   238
        if (responseType.equals((Object)OCSP_BASIC_RESPONSE_OID)) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   239
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   240
                debug.println("OCSP response type: basic");
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   241
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   242
        } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   243
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   244
                debug.println("OCSP response type: " + responseType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   246
            throw new IOException("Unsupported OCSP response type: " +
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   247
                                  responseType);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   248
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   250
        // BasicOCSPResponse
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   251
        DerInputStream basicOCSPResponse =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   252
            new DerInputStream(derIn.getOctetString());
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
        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
   255
        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
   256
            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
   257
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   259
        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
   260
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   261
        // Need the DER encoded ResponseData to verify the signature later
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   262
        tbsResponseData = seqTmp[0].toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   264
        // tbsResponseData
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   265
        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
   266
            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
   267
                "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
   268
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   269
        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
   270
        DerValue seq = seqDerIn.getDerValue();
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
        // version
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   273
        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
   274
            // 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
   275
            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
   276
                //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
   277
                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
   278
                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
   279
                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
   280
                    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
   281
                        " element of OCSP response: bad format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   283
                seq = seqDerIn.getDerValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   285
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   287
        // responderID
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   288
        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
   289
        if (tag == NAME_TAG) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   290
            if (debug != null) {
13402
794d9fb379d2 7179715: OCSP revocation checking fails if the signer certificate is identified using the key ID
mullan
parents: 12860
diff changeset
   291
                X500Principal responderName =
794d9fb379d2 7179715: OCSP revocation checking fails if the signer certificate is identified using the key ID
mullan
parents: 12860
diff changeset
   292
                    new X500Principal(seq.getData().toByteArray());
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   293
                debug.println("OCSP Responder name: " + responderName);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   294
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   295
        } else if (tag == KEY_TAG) {
13402
794d9fb379d2 7179715: OCSP revocation checking fails if the signer certificate is identified using the key ID
mullan
parents: 12860
diff changeset
   296
            if (debug != null) {
794d9fb379d2 7179715: OCSP revocation checking fails if the signer certificate is identified using the key ID
mullan
parents: 12860
diff changeset
   297
                byte[] responderKey = seq.getData().getOctetString();
794d9fb379d2 7179715: OCSP revocation checking fails if the signer certificate is identified using the key ID
mullan
parents: 12860
diff changeset
   298
                debug.println("OCSP Responder key: " +
794d9fb379d2 7179715: OCSP revocation checking fails if the signer certificate is identified using the key ID
mullan
parents: 12860
diff changeset
   299
                              Debug.toString(responderKey));
794d9fb379d2 7179715: OCSP revocation checking fails if the signer certificate is identified using the key ID
mullan
parents: 12860
diff changeset
   300
            }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   301
        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   302
            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
   303
                "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
   304
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   306
        // producedAt
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   307
        seq = seqDerIn.getDerValue();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   308
        if (debug != null) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   309
            Date producedAtDate = seq.getGeneralizedTime();
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   310
            debug.println("OCSP response produced at: " + producedAtDate);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   311
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   313
        // responses
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   314
        DerValue[] singleResponseDer = seqDerIn.getSequence(1);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   315
        singleResponseMap = new HashMap<>(singleResponseDer.length);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   316
        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   317
            debug.println("OCSP number of SingleResponses: "
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   318
                          + singleResponseDer.length);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   319
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   320
        for (int i = 0; i < singleResponseDer.length; i++) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   321
            SingleResponse singleResponse =
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   322
                new SingleResponse(singleResponseDer[i]);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   323
            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
   324
        }
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
        // responseExtensions
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   327
        byte[] nonce = null;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   328
        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
   329
            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
   330
            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
   331
                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
   332
                for (int i = 0; i < responseExtDer.length; i++) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   333
                    Extension ext = new Extension(responseExtDer[i]);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   334
                    if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   335
                        debug.println("OCSP extension: " + ext);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   336
                    }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   337
                    // Only the NONCE extension is recognized
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   338
                    if (ext.getExtensionId().equals((Object)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   339
                        OCSP.NONCE_EXTENSION_OID))
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   340
                    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   341
                        nonce = ext.getExtensionValue();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   342
                    } else if (ext.isCritical())  {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   343
                        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
   344
                            "Unsupported OCSP critical extension: " +
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   345
                            ext.getExtensionId());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
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
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   350
        responseNonce = nonce;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   352
        // signatureAlgorithmId
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   353
        sigAlgId = AlgorithmId.parse(seqTmp[1]);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   354
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   355
        // signature
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   356
        signature = seqTmp[2].getBitString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   358
        // 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
   359
        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
   360
            // 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
   361
            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
   362
            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
   363
                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
   364
                    "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
   365
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   366
            DerValue[] derCerts = seqCert.getData().getSequence(3);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   367
            certs = new ArrayList<X509CertImpl>(derCerts.length);
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   368
            try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   369
                for (int i = 0; i < derCerts.length; i++) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   370
                    certs.add(new X509CertImpl(derCerts[i].toByteArray()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   372
            } 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
   373
                throw new IOException("Bad encoding in X509 Certificate", ce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   375
        } else {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   376
            certs = Collections.<X509CertImpl>emptyList();
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   377
        }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   378
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   379
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   380
    void verify(List<CertId> certIds, X509Certificate responderCert,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   381
                Date date, byte[] nonce)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   382
        throws CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   383
    {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   384
        if (responseStatus != ResponseStatus.SUCCESSFUL) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   385
            throw new CertPathValidatorException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   386
                ("OCSP response error: " + responseStatus);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   387
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   388
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   389
        // Check that the response includes a response for all of the
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   390
        // certs that were supplied in the request
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   391
        for (CertId certId : certIds) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   392
            SingleResponse sr = getSingleResponse(certId);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   393
            if (sr == null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   394
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   395
                    debug.println("No response found for CertId: " + certId);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   396
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   397
                throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   398
                    "OCSP response does not include a response for a " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   399
                    "certificate supplied in the OCSP request");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   400
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   401
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   402
                debug.println("Status of certificate (with serial number " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   403
                    certId.getSerialNumber() + ") is: " + sr.getCertStatus());
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   404
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   405
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   406
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   408
        // Check whether the cert returned by the responder is trusted
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   409
        if (!certs.isEmpty()) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   410
            X509CertImpl cert = certs.get(0);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   411
            // First check if the cert matches the expected responder cert
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   412
            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
   413
                // cert is trusted, now verify the signed response
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   415
            // 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
   416
            // 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
   417
            } else if (cert.getIssuerX500Principal().equals(
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   418
                       responderCert.getSubjectX500Principal())) {
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
                // 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
   421
                try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    List<String> keyPurposes = cert.getExtendedKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (keyPurposes == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        !keyPurposes.contains(KP_OCSP_SIGNING_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        throw new CertPathValidatorException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                            "Responder's certificate not valid for signing " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            "OCSP responses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   429
                } 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
   430
                    // 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
   431
                    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
   432
                        "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
   433
                        "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
   434
                }
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   435
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   436
                // Check algorithm constraints specified in security property
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   437
                // "jdk.certpath.disabledAlgorithms".
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   438
                AlgorithmChecker algChecker = new AlgorithmChecker(
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   439
                                    new TrustAnchor(responderCert, null));
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   440
                algChecker.init(false);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   441
                algChecker.check(cert, Collections.<String>emptySet());
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   442
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   443
                // 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
   444
                try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   445
                    if (date == null) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   446
                        cert.checkValidity();
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   447
                    } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   448
                        cert.checkValidity(date);
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   449
                    }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   450
                } catch (CertificateException e) {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   451
                    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
   452
                        "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
   453
                        "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
   454
                }
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   455
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   456
                // 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
   457
                //
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   458
                // 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
   459
                // 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
   460
                // 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
   461
                // 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
   462
                //
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   463
                Extension noCheck =
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   464
                    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
   465
                if (noCheck != null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   466
                    if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   467
                        debug.println("Responder's certificate includes " +
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   468
                            "the extension id-pkix-ocsp-nocheck.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    }
2281
34fd38495efa 6798714: OCSPResponse class has to check the validity of signing certificate for OCSP response
xuelei
parents: 1639
diff changeset
   470
                } else {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   471
                    // 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
   472
                    // authorized responder in a future update.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   475
                // 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
   476
                try {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   477
                    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
   478
                    responderCert = cert;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   479
                    // cert is trusted, now verify the signed response
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   481
                } 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
   482
                    responderCert = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            } else {
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(
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   486
                    "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
   487
                    "OCSP responses");
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   488
            }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   489
        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   490
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   491
        // 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
   492
        // 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
   493
        if (responderCert != null) {
7040
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   494
            // Check algorithm constraints specified in security property
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   495
            // "jdk.certpath.disabledAlgorithms".
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   496
            AlgorithmChecker.check(responderCert.getPublicKey(), sigAlgId);
659824c2a550 6792180: Enhance to reject weak algorithms or conform to crypto recommendations
xuelei
parents: 5506
diff changeset
   497
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   498
            if (!verifySignature(responderCert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                throw new CertPathValidatorException(
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   500
                    "Error verifying OCSP Response's signature");
2
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
        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   503
            // 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
   504
            throw new CertPathValidatorException(
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   505
                "Unable to verify OCSP Response's signature");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   506
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   507
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   508
        // Check freshness of OCSPResponse
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   509
        if (nonce != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   510
            if (responseNonce != null && !Arrays.equals(nonce, responseNonce)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   511
                throw new CertPathValidatorException("Nonces don't match");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   512
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   513
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   514
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   515
        long now = (date == null) ? System.currentTimeMillis() : date.getTime();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   516
        Date nowPlusSkew = new Date(now + MAX_CLOCK_SKEW);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   517
        Date nowMinusSkew = new Date(now - MAX_CLOCK_SKEW);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   518
        for (SingleResponse sr : singleResponseMap.values()) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   519
            if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   520
                String until = "";
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   521
                if (sr.nextUpdate != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   522
                    until = " until " + sr.nextUpdate;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   523
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   524
                debug.println("Response's validity interval is from " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   525
                              sr.thisUpdate + until);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   526
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   527
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   528
            // Check that the test date is within the validity interval
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   529
            if ((sr.thisUpdate != null && nowPlusSkew.before(sr.thisUpdate)) ||
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   530
                (sr.nextUpdate != null && nowMinusSkew.after(sr.nextUpdate)))
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   531
            {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   532
                throw new CertPathValidatorException(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   533
                                      "Response is unreliable: its validity " +
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   534
                                      "interval is out-of-date");
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   535
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   539
    /**
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   540
     * 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
   541
     */
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   542
    ResponseStatus getResponseStatus() {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   543
        return responseStatus;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   544
    }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   545
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Verify the signature of the OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * The responder's cert is implicitly trusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   550
    private boolean verifySignature(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
   551
        throws CertPathValidatorException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            Signature respSignature = Signature.getInstance(sigAlgId.getName());
17729
90a2bf654c4f 7174966: With OCSP enabled on Java 7 get error 'Wrong key usage' with Comodo certificate
vinnie
parents: 17044
diff changeset
   555
            respSignature.initVerify(cert.getPublicKey());
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   556
            respSignature.update(tbsResponseData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   558
            if (respSignature.verify(signature)) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   559
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   560
                    debug.println("Verified signature of OCSP Response");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   565
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   566
                    debug.println(
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   567
                        "Error verifying signature of OCSP Response");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   571
        } catch (InvalidKeyException | NoSuchAlgorithmException |
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   572
                 SignatureException e)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   573
        {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   574
            throw new CertPathValidatorException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   578
    /**
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   579
     * 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
   580
     * there is no response for that CertId.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   582
    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
   583
        return singleResponseMap.get(certId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * A class representing a single OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   589
    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
   590
        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
   591
        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
   592
        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
   593
        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
   594
        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
   595
        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
   596
        private final Map<String, java.security.cert.Extension> singleExtensions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        private SingleResponse(DerValue der) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (der.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                throw new IOException("Bad ASN.1 encoding in SingleResponse");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            DerInputStream tmp = der.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            certId = new CertId(tmp.getDerValue().data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            DerValue derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            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
   607
            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
   608
                certStatus = CertStatus.REVOKED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                revocationTime = derVal.data.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                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
   611
                    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
   612
                    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
   613
                    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
   614
                        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
   615
                        // 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
   616
                        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
   617
                            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
   618
                        } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   619
                            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
   620
                        }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   621
                    } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   622
                        revocationReason = CRLReason.UNSPECIFIED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   624
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   625
                    revocationReason = CRLReason.UNSPECIFIED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                // RevokedInfo
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   628
                if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   629
                    debug.println("Revocation time: " + revocationTime);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   630
                    debug.println("Revocation reason: " + revocationReason);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            } else {
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   633
                revocationTime = null;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   634
                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
   635
                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
   636
                    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
   637
                } 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
   638
                    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
   639
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   640
                    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
   641
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            thisUpdate = tmp.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (tmp.available() == 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                // 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
   648
                nextUpdate = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                tag = (byte)(derVal.tag & 0x1f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                if (tag == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    // next update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    nextUpdate = derVal.data.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    if (tmp.available() == 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        // we are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        tag = (byte)(derVal.tag & 0x1f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   662
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   663
                    nextUpdate = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            // singleExtensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (tmp.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                derVal = tmp.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                if (derVal.isContextSpecific((byte)1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    DerValue[] singleExtDer = derVal.data.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    singleExtensions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        new HashMap<String, java.security.cert.Extension>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                            (singleExtDer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    for (int i = 0; i < singleExtDer.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        Extension ext = new Extension(singleExtDer[i]);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   676
                        if (debug != null) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   677
                            debug.println("OCSP single extension: " + ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        }
4673
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   679
                        // 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
   680
                        // 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
   681
                        // 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
   682
                        if (ext.isCritical()) {
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   683
                            throw new IOException(
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   684
                                "Unsupported OCSP critical extension: " +
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   685
                                ext.getExtensionId());
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   686
                        }
1536565aebcc 6915939: Exception should be thrown if OCSP SingleResponses contain unresolved critical extensions
mullan
parents: 4209
diff changeset
   687
                        singleExtensions.put(ext.getId(), ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   689
                } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   690
                    singleExtensions = Collections.emptyMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                }
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   692
            } else {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   693
                singleExtensions = Collections.emptyMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * Return the certificate's revocation status code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   700
        @Override public CertStatus getCertStatus() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            return certStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        private CertId getCertId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            return certId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   708
        @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
   709
            return (Date) revocationTime.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   712
        @Override public CRLReason getRevocationReason() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            return revocationReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   716
        @Override
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   717
        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
   718
            return Collections.unmodifiableMap(singleExtensions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         * Construct a string representation of a single OCSP response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 3314
diff changeset
   724
        @Override public String toString() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            sb.append("SingleResponse:  \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            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
   728
            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
   729
            if (certStatus == CertStatus.REVOKED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                sb.append("revocationTime is " + revocationTime + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                sb.append("revocationReason is " + revocationReason + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            sb.append("thisUpdate is " + thisUpdate + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (nextUpdate != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                sb.append("nextUpdate is " + nextUpdate + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
}