Path: blob/master/src/java.base/share/classes/sun/security/provider/certpath/OCSPRequest.java
41161 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider.certpath;2627import java.io.IOException;28import java.security.cert.Extension;29import java.util.Collections;30import java.util.List;3132import sun.security.util.HexDumpEncoder;33import sun.security.util.*;34import sun.security.x509.PKIXExtensions;3536/**37* This class can be used to generate an OCSP request and send it over38* an output stream. Currently we do not support signing requests.39* The OCSP Request is specified in RFC 2560 and40* the ASN.1 definition is as follows:41* <pre>42*43* OCSPRequest ::= SEQUENCE {44* tbsRequest TBSRequest,45* optionalSignature [0] EXPLICIT Signature OPTIONAL }46*47* TBSRequest ::= SEQUENCE {48* version [0] EXPLICIT Version DEFAULT v1,49* requestorName [1] EXPLICIT GeneralName OPTIONAL,50* requestList SEQUENCE OF Request,51* requestExtensions [2] EXPLICIT Extensions OPTIONAL }52*53* Signature ::= SEQUENCE {54* signatureAlgorithm AlgorithmIdentifier,55* signature BIT STRING,56* certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL57* }58*59* Version ::= INTEGER { v1(0) }60*61* Request ::= SEQUENCE {62* reqCert CertID,63* singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL }64*65* CertID ::= SEQUENCE {66* hashAlgorithm AlgorithmIdentifier,67* issuerNameHash OCTET STRING, -- Hash of Issuer's DN68* issuerKeyHash OCTET STRING, -- Hash of Issuers public key69* serialNumber CertificateSerialNumber70* }71*72* </pre>73*74* @author Ram Marti75*/7677class OCSPRequest {7879private static final Debug debug = Debug.getInstance("certpath");80private static final boolean dump = debug != null && Debug.isOn("ocsp");8182// List of request CertIds83private final List<CertId> certIds;84private final List<Extension> extensions;85private byte[] nonce;8687/*88* Constructs an OCSPRequest. This constructor is used89* to construct an unsigned OCSP Request for a single user cert.90*/91OCSPRequest(CertId certId) {92this(Collections.singletonList(certId));93}9495OCSPRequest(List<CertId> certIds) {96this.certIds = certIds;97this.extensions = Collections.<Extension>emptyList();98}99100OCSPRequest(List<CertId> certIds, List<Extension> extensions) {101this.certIds = certIds;102this.extensions = extensions;103}104105byte[] encodeBytes() throws IOException {106107// encode tbsRequest108DerOutputStream tmp = new DerOutputStream();109DerOutputStream requestsOut = new DerOutputStream();110for (CertId certId : certIds) {111DerOutputStream certIdOut = new DerOutputStream();112certId.encode(certIdOut);113requestsOut.write(DerValue.tag_Sequence, certIdOut);114}115116tmp.write(DerValue.tag_Sequence, requestsOut);117if (!extensions.isEmpty()) {118DerOutputStream extOut = new DerOutputStream();119for (Extension ext : extensions) {120ext.encode(extOut);121if (ext.getId().equals(122PKIXExtensions.OCSPNonce_Id.toString())) {123nonce = ext.getValue();124}125}126DerOutputStream extsOut = new DerOutputStream();127extsOut.write(DerValue.tag_Sequence, extOut);128tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT,129true, (byte)2), extsOut);130}131132DerOutputStream tbsRequest = new DerOutputStream();133tbsRequest.write(DerValue.tag_Sequence, tmp);134135// OCSPRequest without the signature136DerOutputStream ocspRequest = new DerOutputStream();137ocspRequest.write(DerValue.tag_Sequence, tbsRequest);138139byte[] bytes = ocspRequest.toByteArray();140141if (dump) {142HexDumpEncoder hexEnc = new HexDumpEncoder();143debug.println("OCSPRequest bytes...\n\n" +144hexEnc.encode(bytes) + "\n");145}146147return bytes;148}149150List<CertId> getCertIds() {151return certIds;152}153154byte[] getNonce() {155return nonce;156}157}158159160