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