jdk/src/share/classes/sun/security/provider/certpath/OCSPRequest.java
author xuelei
Tue, 18 Aug 2009 20:47:13 -0700
changeset 4190 227655c2ff8c
parent 2 90ce3da70b43
child 3841 6738c111d48f
permissions -rw-r--r--
6861062: Disable MD2 support Reviewed-by: mullan, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.CertPathValidatorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class can be used to generate an OCSP request and send it over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * an outputstream. Currently we do not support signing requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The OCSP Request is specified in RFC 2560 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the ASN.1 definition is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * OCSPRequest     ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      tbsRequest                  TBSRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      optionalSignature   [0]     EXPLICIT Signature OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   TBSRequest      ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      version             [0]     EXPLICIT Version DEFAULT v1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      requestorName       [1]     EXPLICIT GeneralName OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      requestList                 SEQUENCE OF Request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      requestExtensions   [2]     EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  Signature       ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *      signatureAlgorithm      AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      signature               BIT STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      certs               [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  Version         ::=             INTEGER  {  v1(0) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  Request         ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      reqCert                     CertID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      singleRequestExtensions     [0] EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *  CertID          ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *       hashAlgorithm  AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *       issuerNameHash OCTET STRING, -- Hash of Issuer's DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *       issuerKeyHash  OCTET STRING, -- Hash of Issuers public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *       serialNumber   CertificateSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author      Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
class OCSPRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final boolean dump = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Serial number of the certificates to be checked for revocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private SerialNumber serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Issuer's certificate (for computing certId hash values)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private X509CertImpl issuerCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // CertId of the certificate to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private CertId certId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Constructs an OCSPRequest. This constructor is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * to construct an unsigned OCSP Request for a single user cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // used by OCSPChecker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    OCSPRequest(X509CertImpl userCert, X509CertImpl issuerCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throws CertPathValidatorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (issuerCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new CertPathValidatorException("Null IssuerCertificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.issuerCert = issuerCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        serialNumber = userCert.getSerialNumberObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // used by OCSPChecker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    byte[] encodeBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // encode tbsRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        DerOutputStream derSingleReqList  = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        SingleRequest singleRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            singleRequest = new SingleRequest(issuerCert, serialNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new IOException("Error encoding OCSP request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        certId = singleRequest.getCertId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        singleRequest.encode(derSingleReqList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        tmp.write(DerValue.tag_Sequence, derSingleReqList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // No extensions supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        DerOutputStream tbsRequest = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        tbsRequest.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // OCSPRequest without the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        DerOutputStream ocspRequest = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ocspRequest.write(DerValue.tag_Sequence, tbsRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        byte[] bytes = ocspRequest.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            HexDumpEncoder hexEnc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.println ("OCSPRequest bytes are... ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println(hexEnc.encode(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // used by OCSPChecker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    CertId getCertId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return certId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static class SingleRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        private CertId certId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // No extensions are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private SingleRequest(X509CertImpl cert, SerialNumber serialNo) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            certId = new CertId(cert, serialNo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            certId.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        private CertId getCertId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return certId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}