| 1 |
// Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include "openssl/ocsp.h" |
| 5 |
#include "openssl/x509.h" |
| 6 |
|
| 7 |
#if defined(__cplusplus) |
| 8 |
extern "C" { |
| 9 |
#endif |
| 10 |
|
| 11 |
// CRLReason does not have a status assigned to the value 7. |
| 12 |
// |
| 13 |
// See Reason Code RFC: https://www.rfc-editor.org/rfc/rfc5280#section-5.3.1. |
| 14 |
#define OCSP_UNASSIGNED_REVOKED_STATUS 7 |
| 15 |
|
| 16 |
// OCSPResponseStatus does not have a status assigned to the value 4. |
| 17 |
// |
| 18 |
// See Reason Code RFC: |
| 19 |
// https://datatracker.ietf.org/doc/html/rfc6960#section-4.2.1 |
| 20 |
#define OCSP_UNASSIGNED_RESPONSE_STATUS 4 |
| 21 |
|
| 22 |
// OCSP Request ASN.1 specification: |
| 23 |
// https://datatracker.ietf.org/doc/html/rfc6960#section-4.1.1 |
| 24 |
// |
| 25 |
// OCSP Response ASN.1 specification: |
| 26 |
// https://datatracker.ietf.org/doc/html/rfc6960#section-4.2.1 |
| 27 |
|
| 28 |
// CertID ::= SEQUENCE { |
| 29 |
// hashAlgorithm AlgorithmIdentifier, |
| 30 |
// issuerNameHash OCTET STRING, --Hash of Issuer's DN |
| 31 |
// issuerKeyHash OCTET STRING, --Hash of Issuers public key (excluding |
| 32 |
// the tag & length fields) |
| 33 |
// serialNumber CertificateSerialNumber } |
| 34 |
// |
| 35 |
struct ocsp_cert_id_st { |
| 36 |
X509_ALGOR *hashAlgorithm; |
| 37 |
ASN1_OCTET_STRING *issuerNameHash; |
| 38 |
ASN1_OCTET_STRING *issuerKeyHash; |
| 39 |
ASN1_INTEGER *serialNumber; |
| 40 |
}; |
| 41 |
|
| 42 |
// Request ::= SEQUENCE { |
| 43 |
// reqCert CertID, |
| 44 |
// singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL } |
| 45 |
// |
| 46 |
struct ocsp_one_request_st { |
| 47 |
OCSP_CERTID *reqCert; |
| 48 |
STACK_OF(X509_EXTENSION) *singleRequestExtensions; |
| 49 |
}; |
| 50 |
|
| 51 |
// TBSRequest ::= SEQUENCE { |
| 52 |
// version [0] EXPLICIT Version DEFAULT v1, |
| 53 |
// requestorName [1] EXPLICIT GeneralName OPTIONAL, |
| 54 |
// requestList SEQUENCE OF Request, |
| 55 |
// requestExtensions [2] EXPLICIT Extensions OPTIONAL } |
| 56 |
// |
| 57 |
struct ocsp_req_info_st { |
| 58 |
ASN1_INTEGER *version; |
| 59 |
GENERAL_NAME *requestorName; |
| 60 |
STACK_OF(OCSP_ONEREQ) *requestList; |
| 61 |
STACK_OF(X509_EXTENSION) *requestExtensions; |
| 62 |
}; |
| 63 |
|
| 64 |
// Signature ::= SEQUENCE { |
| 65 |
// signatureAlgorithm AlgorithmIdentifier, |
| 66 |
// signature BIT STRING, |
| 67 |
// certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } |
| 68 |
// |
| 69 |
struct ocsp_signature_st { |
| 70 |
X509_ALGOR *signatureAlgorithm; |
| 71 |
ASN1_BIT_STRING *signature; |
| 72 |
STACK_OF(X509) *certs; |
| 73 |
}; |
| 74 |
|
| 75 |
// OCSPRequest ::= SEQUENCE { |
| 76 |
// tbsRequest TBSRequest, |
| 77 |
// optionalSignature [0] EXPLICIT Signature OPTIONAL } |
| 78 |
// |
| 79 |
struct ocsp_request_st { |
| 80 |
OCSP_REQINFO *tbsRequest; |
| 81 |
OCSP_SIGNATURE *optionalSignature; |
| 82 |
}; |
| 83 |
|
| 84 |
// Opaque OCSP request status structure |
| 85 |
struct ocsp_req_ctx_st { |
| 86 |
int state; // Current I/O state |
| 87 |
unsigned char *iobuf; // Line buffer. Should only be modified during |
| 88 |
// http exchange in OCSP_REQ_CTX_nbio. |
| 89 |
int iobuflen; // Line buffer length |
| 90 |
BIO *io; // BIO to perform I/O with |
| 91 |
BIO *mem; // Memory BIO response is built into |
| 92 |
unsigned long asn1_len; // ASN1 length of response |
| 93 |
unsigned long max_resp_len; // Maximum length of response |
| 94 |
}; |
| 95 |
|
| 96 |
// OCSPResponseStatus ::= ENUMERATED { |
| 97 |
// successful (0), --Response has valid confirmations |
| 98 |
// malformedRequest (1), --Illegal confirmation request |
| 99 |
// internalError (2), --Internal error in issuer |
| 100 |
// tryLater (3), --Try again later |
| 101 |
// --(4) is not used |
| 102 |
// sigRequired (5), --Must sign the request |
| 103 |
// unauthorized (6) --Request unauthorized |
| 104 |
// } |
| 105 |
// |
| 106 |
|
| 107 |
// ResponseBytes ::= SEQUENCE { |
| 108 |
// responseType OBJECT IDENTIFIER, |
| 109 |
// response OCTET STRING } |
| 110 |
// |
| 111 |
struct ocsp_resp_bytes_st { |
| 112 |
ASN1_OBJECT *responseType; |
| 113 |
ASN1_OCTET_STRING *response; |
| 114 |
}; |
| 115 |
|
| 116 |
// OCSPResponse ::= SEQUENCE { |
| 117 |
// responseStatus OCSPResponseStatus, |
| 118 |
// responseBytes [0] EXPLICIT ResponseBytes OPTIONAL } |
| 119 |
// |
| 120 |
struct ocsp_response_st { |
| 121 |
ASN1_ENUMERATED *responseStatus; |
| 122 |
OCSP_RESPBYTES *responseBytes; |
| 123 |
}; |
| 124 |
|
| 125 |
// ResponderID ::= CHOICE { |
| 126 |
// byName [1] Name, |
| 127 |
// byKey [2] KeyHash } |
| 128 |
// |
| 129 |
// KeyHash ::= OCTET STRING --SHA-1 hash of responder's public key |
| 130 |
// --(excluding the tag and length fields) |
| 131 |
// |
| 132 |
// The RFC requires that the KeyHash value be of a SHA-1 hash. Even though this |
| 133 |
// is not being used cryptographically, there is the possibility of a response |
| 134 |
// being returned with a forced Responder KeyHash when using SHA-1 (assuming a |
| 135 |
// preimage attack, which is beyond the scope of how SHA-1 is currently |
| 136 |
// vulnerable). However, our hand are tied with what the RFC mandates. |
| 137 |
// |
| 138 |
// RFC 6960: https://datatracker.ietf.org/doc/html/rfc6960#appendix-B.2 |
| 139 |
struct ocsp_responder_id_st { |
| 140 |
int type; |
| 141 |
union { |
| 142 |
X509_NAME *byName; |
| 143 |
ASN1_OCTET_STRING *byKey; |
| 144 |
} value; |
| 145 |
}; |
| 146 |
|
| 147 |
// RevokedInfo ::= SEQUENCE { |
| 148 |
// revocationTime GeneralizedTime, |
| 149 |
// revocationReason [0] EXPLICIT CRLReason OPTIONAL } |
| 150 |
// |
| 151 |
struct ocsp_revoked_info_st { |
| 152 |
ASN1_GENERALIZEDTIME *revocationTime; |
| 153 |
ASN1_ENUMERATED *revocationReason; |
| 154 |
}; |
| 155 |
|
| 156 |
// CertStatus ::= CHOICE { |
| 157 |
// good [0] IMPLICIT NULL, |
| 158 |
// revoked [1] IMPLICIT RevokedInfo, |
| 159 |
// unknown [2] IMPLICIT UnknownInfo } |
| 160 |
// |
| 161 |
struct ocsp_cert_status_st { |
| 162 |
int type; |
| 163 |
union { |
| 164 |
ASN1_NULL *good; |
| 165 |
OCSP_REVOKEDINFO *revoked; |
| 166 |
ASN1_NULL *unknown; |
| 167 |
} value; |
| 168 |
}; |
| 169 |
|
| 170 |
// SingleResponse ::= SEQUENCE { |
| 171 |
// certID CertID, |
| 172 |
// certStatus CertStatus, |
| 173 |
// thisUpdate GeneralizedTime, |
| 174 |
// nextUpdate [0] EXPLICIT GeneralizedTime OPTIONAL, |
| 175 |
// singleExtensions [1] EXPLICIT Extensions OPTIONAL } |
| 176 |
// |
| 177 |
struct ocsp_single_response_st { |
| 178 |
OCSP_CERTID *certId; |
| 179 |
OCSP_CERTSTATUS *certStatus; |
| 180 |
ASN1_GENERALIZEDTIME *thisUpdate; |
| 181 |
ASN1_GENERALIZEDTIME *nextUpdate; |
| 182 |
STACK_OF(X509_EXTENSION) *singleExtensions; |
| 183 |
}; |
| 184 |
|
| 185 |
// ResponseData ::= SEQUENCE { |
| 186 |
// version [0] EXPLICIT Version DEFAULT v1, |
| 187 |
// responderID ResponderID, |
| 188 |
// producedAt GeneralizedTime, |
| 189 |
// responses SEQUENCE OF SingleResponse, |
| 190 |
// responseExtensions [1] EXPLICIT Extensions OPTIONAL } |
| 191 |
// |
| 192 |
struct ocsp_response_data_st { |
| 193 |
ASN1_INTEGER *version; |
| 194 |
OCSP_RESPID *responderId; |
| 195 |
ASN1_GENERALIZEDTIME *producedAt; |
| 196 |
STACK_OF(OCSP_SINGLERESP) *responses; |
| 197 |
STACK_OF(X509_EXTENSION) *responseExtensions; |
| 198 |
}; |
| 199 |
|
| 200 |
// BasicOCSPResponse ::= SEQUENCE { |
| 201 |
// tbsResponseData ResponseData, |
| 202 |
// signatureAlgorithm AlgorithmIdentifier, |
| 203 |
// signature BIT STRING, |
| 204 |
// certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } |
| 205 |
// |
| 206 |
// |
| 207 |
// Note 1: The value for "signature" is specified in the OCSP rfc2560 as |
| 208 |
// follows: "The value for the signature SHALL be computed on the hash of |
| 209 |
// the DER encoding ResponseData." This means that you must hash the |
| 210 |
// DER-encoded tbsResponseData, and then run it through a crypto-signing |
| 211 |
// function, which will (at least w/RSA) do a hash-'n'-private-encrypt |
| 212 |
// operation. This seems a bit odd, but that's the spec. Also note that |
| 213 |
// the data structures do not leave anywhere to independently specify the |
| 214 |
// algorithm used for the initial hash. So, we look at the |
| 215 |
// signature-specification algorithm, and try to do something intelligent. |
| 216 |
// -- Kathy Weinhold, CertCo |
| 217 |
// |
| 218 |
// Note 2: It seems that the mentioned passage from RFC 2560 (section |
| 219 |
// 4.2.1) is open for interpretation. I've done tests against another |
| 220 |
// responder, and found that it doesn't do the double hashing that the RFC |
| 221 |
// seems to say one should. Therefore, all relevant functions take a flag |
| 222 |
// saying which variant should be used. -- Richard Levitte, OpenSSL team |
| 223 |
// and CeloCom |
| 224 |
struct ocsp_basic_response_st { |
| 225 |
OCSP_RESPDATA *tbsResponseData; |
| 226 |
X509_ALGOR *signatureAlgorithm; |
| 227 |
ASN1_BIT_STRING *signature; |
| 228 |
STACK_OF(X509) *certs; |
| 229 |
}; |
| 230 |
|
| 231 |
DECLARE_ASN1_FUNCTIONS(OCSP_RESPDATA) |
| 232 |
DECLARE_ASN1_FUNCTIONS(OCSP_REQINFO) |
| 233 |
DECLARE_ASN1_FUNCTIONS(OCSP_SIGNATURE) |
| 234 |
DECLARE_ASN1_FUNCTIONS(OCSP_RESPBYTES) |
| 235 |
DECLARE_ASN1_FUNCTIONS(OCSP_REVOKEDINFO) |
| 236 |
|
| 237 |
// OCSP_get_default_digest sets the default digest according to |signer|. |
| 238 |
// This exists because OpenSSL sets the default to |EVP_sha256| when passing |
| 239 |
// NULL for |type| in |EVP_DigestSignInit| when using certain key types. We wish |
| 240 |
// to avoid this general behavior for all |EVP_DigestSign*| operations, so we |
| 241 |
// only set the default digest from the OCSP layer. |dgst| represents the user's |
| 242 |
// self-defined digest type, if it's non-NULL, |dgst| is directly returned. |
| 243 |
const EVP_MD *OCSP_get_default_digest(const EVP_MD *dgst, EVP_PKEY *signer); |
| 244 |
|
| 245 |
// Try exchanging request and response via HTTP on (non-)blocking BIO in rctx. |
| 246 |
OPENSSL_EXPORT int OCSP_REQ_CTX_nbio(OCSP_REQ_CTX *rctx); |
| 247 |
|
| 248 |
// Tries to exchange the request and response with |OCSP_REQ_CTX_nbio|, but on |
| 249 |
// success, it additionally parses the response, which must be a |
| 250 |
// DER-encoded ASN.1 structure. |
| 251 |
int OCSP_REQ_CTX_nbio_d2i(OCSP_REQ_CTX *rctx, ASN1_VALUE **pval, |
| 252 |
const ASN1_ITEM *it); |
| 253 |
|
| 254 |
// Returns the internal memory BIO of the |OCSP_REQ_CTX|. For AWS-LC, this is |
| 255 |
// only used for testing if contents of |OCSP_REQ_CTX| have been written |
| 256 |
// correctly. |
| 257 |
OPENSSL_EXPORT BIO *OCSP_REQ_CTX_get0_mem_bio(OCSP_REQ_CTX *rctx); |
| 258 |
|
| 259 |
|
| 260 |
// OCSP extension functions |
| 261 |
|
| 262 |
// OCSP_REQUEST_get_ext_by_NID returns the index of an extension from an |
| 263 |
// |OCSP_REQUEST| by its NID. Returns -1 if not found. |
| 264 |
OPENSSL_EXPORT int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *req, int nid, |
| 265 |
int lastpos); |
| 266 |
|
| 267 |
// OCSP_REQUEST_get_ext retrieves an |X509_EXTENSION| from an |OCSP_REQUEST| |
| 268 |
// by its position in the extension list. |
| 269 |
OPENSSL_EXPORT X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *req, int loc); |
| 270 |
|
| 271 |
// OCSP_BASICRESP_add_ext adds a copy of |ex| to the extension list in |
| 272 |
// |*bs|. It returns 1 on success and 0 on error. The new extension is |
| 273 |
// inserted at index |loc|, shifting extensions to the right. If |loc| is -1 or |
| 274 |
// out of bounds, the new extension is appended to the list. |
| 275 |
int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *bs, X509_EXTENSION *ex, int loc); |
| 276 |
|
| 277 |
|
| 278 |
#define IS_OCSP_FLAG_SET(flags, query) (flags & query) |
| 279 |
#define OCSP_MAX_RESP_LENGTH (100 * 1024) |
| 280 |
|
| 281 |
#if defined(__cplusplus) |
| 282 |
} // extern C |
| 283 |
#endif |