src/java.base/share/classes/sun/security/provider/certpath/OCSPRequest.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, 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: 3841
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: 3841
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: 3841
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
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.IOException;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    29
import java.security.cert.Extension;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    30
import java.util.Collections;
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    31
import java.util.List;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    32
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 32032
diff changeset
    33
import sun.security.util.HexDumpEncoder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 25859
diff changeset
    35
import sun.security.x509.PKIXExtensions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class can be used to generate an OCSP request and send it over
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 25859
diff changeset
    39
 * an output stream. Currently we do not support signing requests.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The OCSP Request is specified in RFC 2560 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the ASN.1 definition is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * OCSPRequest     ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      tbsRequest                  TBSRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      optionalSignature   [0]     EXPLICIT Signature OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   TBSRequest      ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      version             [0]     EXPLICIT Version DEFAULT v1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      requestorName       [1]     EXPLICIT GeneralName OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      requestList                 SEQUENCE OF Request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *      requestExtensions   [2]     EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  Signature       ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      signatureAlgorithm      AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      signature               BIT STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      certs               [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *  Version         ::=             INTEGER  {  v1(0) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *  Request         ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      reqCert                     CertID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      singleRequestExtensions     [0] EXPLICIT Extensions OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *  CertID          ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *       hashAlgorithm  AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       issuerNameHash OCTET STRING, -- Hash of Issuer's DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *       issuerKeyHash  OCTET STRING, -- Hash of Issuers public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *       serialNumber   CertificateSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author      Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
class OCSPRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
21819
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 12860
diff changeset
    80
    private static final Debug debug = Debug.getInstance("certpath");
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 12860
diff changeset
    81
    private static final boolean dump = debug != null && Debug.isOn("ocsp");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    83
    // List of request CertIds
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    84
    private final List<CertId> certIds;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    85
    private final List<Extension> extensions;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    86
    private byte[] nonce;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Constructs an OCSPRequest. This constructor is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * to construct an unsigned OCSP Request for a single user cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    92
    OCSPRequest(CertId certId) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    93
        this(Collections.singletonList(certId));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    96
    OCSPRequest(List<CertId> certIds) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    97
        this.certIds = certIds;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    98
        this.extensions = Collections.<Extension>emptyList();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
    99
    }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   100
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   101
    OCSPRequest(List<CertId> certIds, List<Extension> extensions) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   102
        this.certIds = certIds;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   103
        this.extensions = extensions;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   104
    }
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   105
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    byte[] encodeBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // encode tbsRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        DerOutputStream tmp = new DerOutputStream();
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   110
        DerOutputStream requestsOut = new DerOutputStream();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   111
        for (CertId certId : certIds) {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   112
            DerOutputStream certIdOut = new DerOutputStream();
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   113
            certId.encode(certIdOut);
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   114
            requestsOut.write(DerValue.tag_Sequence, certIdOut);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   117
        tmp.write(DerValue.tag_Sequence, requestsOut);
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   118
        if (!extensions.isEmpty()) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   119
            DerOutputStream extOut = new DerOutputStream();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   120
            for (Extension ext : extensions) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   121
                ext.encode(extOut);
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 25859
diff changeset
   122
                if (ext.getId().equals(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 25859
diff changeset
   123
                        PKIXExtensions.OCSPNonce_Id.toString())) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   124
                    nonce = ext.getValue();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   125
                }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   126
            }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   127
            DerOutputStream extsOut = new DerOutputStream();
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   128
            extsOut.write(DerValue.tag_Sequence, extOut);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   129
            tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   130
                                         true, (byte)2), extsOut);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   131
        }
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   132
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        DerOutputStream tbsRequest = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        tbsRequest.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // OCSPRequest without the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        DerOutputStream ocspRequest = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        ocspRequest.write(DerValue.tag_Sequence, tbsRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        byte[] bytes = ocspRequest.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            HexDumpEncoder hexEnc = new HexDumpEncoder();
21819
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 12860
diff changeset
   144
            debug.println("OCSPRequest bytes...\n\n" +
8cd757e836d8 8015571: OCSP validation fails if ocsp.responderCertSubjectName is set
vinnie
parents: 12860
diff changeset
   145
                hexEnc.encode(bytes) + "\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   148
        return bytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   151
    List<CertId> getCertIds() {
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
   152
        return certIds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   154
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   155
    byte[] getNonce() {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   156
        return nonce;
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   157
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}