2 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/objects.h>
13 #include <openssl/x509.h>
14 #include <openssl/pem.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/ocsp.h>
17 #include "ocsp_local.h"
19 DEFINE_STACK_OF(OCSP_ONEREQ)
21 DEFINE_STACK_OF(OCSP_SINGLERESP)
24 * Utility functions related to sending OCSP responses and extracting
25 * relevant information from the request.
28 int OCSP_request_onereq_count(OCSP_REQUEST *req)
30 return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList);
33 OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
35 return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i);
38 OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
43 int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
44 ASN1_OCTET_STRING **pikeyHash,
45 ASN1_INTEGER **pserial, OCSP_CERTID *cid)
50 *pmd = cid->hashAlgorithm.algorithm;
52 *piNameHash = &cid->issuerNameHash;
54 *pikeyHash = &cid->issuerKeyHash;
56 *pserial = &cid->serialNumber;
60 int OCSP_request_is_signed(OCSP_REQUEST *req)
62 if (req->optionalSignature)
67 /* Create an OCSP response and encode an optional basic response */
68 OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
70 OCSP_RESPONSE *rsp = NULL;
72 if ((rsp = OCSP_RESPONSE_new()) == NULL)
74 if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
78 if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL)
80 rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
82 (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
86 OCSP_RESPONSE_free(rsp);
90 OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
92 int status, int reason,
97 OCSP_SINGLERESP *single = NULL;
101 if (rsp->tbsResponseData.responses == NULL
102 && (rsp->tbsResponseData.responses
103 = sk_OCSP_SINGLERESP_new_null()) == NULL)
106 if ((single = OCSP_SINGLERESP_new()) == NULL)
109 if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
112 !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
115 OCSP_CERTID_free(single->certId);
117 if ((single->certId = OCSP_CERTID_dup(cid)) == NULL)
120 cs = single->certStatus;
121 switch (cs->type = status) {
122 case V_OCSP_CERTSTATUS_REVOKED:
124 OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
127 if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL)
129 if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
131 if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
132 if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL)
134 if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
139 case V_OCSP_CERTSTATUS_GOOD:
140 if ((cs->value.good = ASN1_NULL_new()) == NULL)
144 case V_OCSP_CERTSTATUS_UNKNOWN:
145 if ((cs->value.unknown = ASN1_NULL_new()) == NULL)
153 if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single)))
157 OCSP_SINGLERESP_free(single);
161 /* Add a certificate to an OCSP request */
163 int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
165 if (resp->certs == NULL
166 && (resp->certs = sk_X509_new_null()) == NULL)
169 if (!sk_X509_push(resp->certs, cert))
176 * Sign an OCSP response using the parameters contained in the digest context,
177 * set the responderID to the subject name in the signer's certificate, and
178 * include one or more optional certificates in the response.
181 int OCSP_basic_sign_ctx(OCSP_BASICRESP *brsp,
182 X509 *signer, EVP_MD_CTX *ctx,
183 STACK_OF(X509) *certs, unsigned long flags)
189 if (ctx == NULL || EVP_MD_CTX_pkey_ctx(ctx) == NULL) {
190 OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX, OCSP_R_NO_SIGNER_KEY);
194 pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
195 if (pkey == NULL || !X509_check_private_key(signer, pkey)) {
196 OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX,
197 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
201 if (!(flags & OCSP_NOCERTS)) {
202 if (!OCSP_basic_add1_cert(brsp, signer))
204 for (i = 0; i < sk_X509_num(certs); i++) {
205 X509 *tmpcert = sk_X509_value(certs, i);
206 if (!OCSP_basic_add1_cert(brsp, tmpcert))
211 rid = &brsp->tbsResponseData.responderId;
212 if (flags & OCSP_RESPID_KEY) {
213 if (!OCSP_RESPID_set_by_key(rid, signer))
215 } else if (!OCSP_RESPID_set_by_name(rid, signer)) {
219 if (!(flags & OCSP_NOTIME) &&
220 !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
224 * Right now, I think that not doing double hashing is the right thing.
228 if (!OCSP_BASICRESP_sign_ctx(brsp, ctx, 0))
236 int OCSP_basic_sign(OCSP_BASICRESP *brsp,
237 X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
238 STACK_OF(X509) *certs, unsigned long flags)
240 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
241 EVP_PKEY_CTX *pkctx = NULL;
247 if (!EVP_DigestSignInit(ctx, &pkctx, dgst, NULL, key)) {
248 EVP_MD_CTX_free(ctx);
251 i = OCSP_basic_sign_ctx(brsp, signer, ctx, certs, flags);
252 EVP_MD_CTX_free(ctx);
256 int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert)
258 if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert)))
261 respid->type = V_OCSP_RESPID_NAME;
266 int OCSP_RESPID_set_by_key_ex(OCSP_RESPID *respid, X509 *cert,
267 OPENSSL_CTX *libctx, const char *propq)
269 ASN1_OCTET_STRING *byKey = NULL;
270 unsigned char md[SHA_DIGEST_LENGTH];
271 EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
277 /* RFC2560 requires SHA1 */
278 if (!X509_pubkey_digest(cert, sha1, md, NULL))
281 byKey = ASN1_OCTET_STRING_new();
285 if (!(ASN1_OCTET_STRING_set(byKey, md, SHA_DIGEST_LENGTH))) {
286 ASN1_OCTET_STRING_free(byKey);
290 respid->type = V_OCSP_RESPID_KEY;
291 respid->value.byKey = byKey;
299 int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert)
301 return OCSP_RESPID_set_by_key_ex(respid, cert, NULL, NULL);
304 int OCSP_RESPID_match_ex(OCSP_RESPID *respid, X509 *cert, OPENSSL_CTX *libctx,
310 if (respid->type == V_OCSP_RESPID_KEY) {
311 unsigned char md[SHA_DIGEST_LENGTH];
313 sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
317 if (respid->value.byKey == NULL)
320 /* RFC2560 requires SHA1 */
321 if (!X509_pubkey_digest(cert, sha1, md, NULL))
324 ret = (ASN1_STRING_length(respid->value.byKey) == SHA_DIGEST_LENGTH)
325 && (memcmp(ASN1_STRING_get0_data(respid->value.byKey), md,
326 SHA_DIGEST_LENGTH) == 0);
327 } else if (respid->type == V_OCSP_RESPID_NAME) {
328 if (respid->value.byName == NULL)
331 return X509_NAME_cmp(respid->value.byName,
332 X509_get_subject_name(cert)) == 0;
340 int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert)
342 return OCSP_RESPID_match_ex(respid, cert, NULL, NULL);