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
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/ocsp.h>
19 #include "ocsp_local.h"
22 * Utility functions related to sending OCSP requests and extracting relevant
23 * information from the response.
27 * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
28 * useful if we want to add extensions.
30 OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
32 OCSP_ONEREQ *one = NULL;
34 if ((one = OCSP_ONEREQ_new()) == NULL)
36 OCSP_CERTID_free(one->reqCert);
38 if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one)) {
39 one->reqCert = NULL; /* do not free on error */
40 OCSP_ONEREQ_free(one);
46 /* Set requestorName from an X509_NAME structure */
47 int OCSP_request_set1_name(OCSP_REQUEST *req, const X509_NAME *nm)
49 GENERAL_NAME *gen = GENERAL_NAME_new();
53 if (!X509_NAME_set(&gen->d.directoryName, nm)) {
54 GENERAL_NAME_free(gen);
57 gen->type = GEN_DIRNAME;
58 GENERAL_NAME_free(req->tbsRequest.requestorName);
59 req->tbsRequest.requestorName = gen;
63 /* Add a certificate to an OCSP request */
64 int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
66 if (req->optionalSignature == NULL
67 && (req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
71 return ossl_x509_add_cert_new(&req->optionalSignature->certs, cert,
72 X509_ADD_FLAG_UP_REF);
76 * Sign an OCSP request set the requestorName to the subject name of an
77 * optional signers certificate and include one or more optional certificates
78 * in the request. Behaves like PKCS7_sign().
80 int OCSP_request_sign(OCSP_REQUEST *req,
84 STACK_OF(X509) *certs, unsigned long flags)
86 if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
89 if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
92 if (!X509_check_private_key(signer, key)) {
93 ERR_raise(ERR_LIB_OCSP,
94 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
97 if (!OCSP_REQUEST_sign(req, key, dgst))
101 if ((flags & OCSP_NOCERTS) == 0) {
102 if (!OCSP_request_add1_cert(req, signer)
103 || !X509_add_certs(req->optionalSignature->certs, certs,
104 X509_ADD_FLAG_UP_REF))
110 OCSP_SIGNATURE_free(req->optionalSignature);
111 req->optionalSignature = NULL;
115 /* Get response status */
116 int OCSP_response_status(OCSP_RESPONSE *resp)
118 return ASN1_ENUMERATED_get(resp->responseStatus);
122 * Extract basic response from OCSP_RESPONSE or NULL if no basic response
125 OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
127 OCSP_RESPBYTES *rb = resp->responseBytes;
130 ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_RESPONSE_DATA);
133 if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
134 ERR_raise(ERR_LIB_OCSP, OCSP_R_NOT_BASIC_RESPONSE);
138 return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
141 const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs)
143 return bs->signature;
146 const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs)
148 return &bs->signatureAlgorithm;
151 const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs)
153 return &bs->tbsResponseData;
156 /* Return number of OCSP_SINGLERESP responses present in a basic response */
158 int OCSP_resp_count(OCSP_BASICRESP *bs)
162 return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses);
165 /* Extract an OCSP_SINGLERESP response with a given index */
166 OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
170 return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx);
173 const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP *bs)
175 return bs->tbsResponseData.producedAt;
178 const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs)
183 int OCSP_resp_get0_id(const OCSP_BASICRESP *bs,
184 const ASN1_OCTET_STRING **pid,
185 const X509_NAME **pname)
187 const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
189 if (rid->type == V_OCSP_RESPID_NAME) {
190 *pname = rid->value.byName;
192 } else if (rid->type == V_OCSP_RESPID_KEY) {
193 *pid = rid->value.byKey;
201 int OCSP_resp_get1_id(const OCSP_BASICRESP *bs,
202 ASN1_OCTET_STRING **pid,
205 const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
207 if (rid->type == V_OCSP_RESPID_NAME) {
208 *pname = X509_NAME_dup(rid->value.byName);
210 } else if (rid->type == V_OCSP_RESPID_KEY) {
211 *pid = ASN1_OCTET_STRING_dup(rid->value.byKey);
216 if (*pname == NULL && *pid == NULL)
221 /* Look single response matching a given certificate ID */
222 int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
225 STACK_OF(OCSP_SINGLERESP) *sresp;
226 OCSP_SINGLERESP *single;
234 sresp = bs->tbsResponseData.responses;
235 for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
236 single = sk_OCSP_SINGLERESP_value(sresp, i);
237 if (!OCSP_id_cmp(id, single->certId))
244 * Extract status information from an OCSP_SINGLERESP structure. Note: the
245 * revtime and reason values are only set if the certificate status is
246 * revoked. Returns numerical value of status.
248 int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
249 ASN1_GENERALIZEDTIME **revtime,
250 ASN1_GENERALIZEDTIME **thisupd,
251 ASN1_GENERALIZEDTIME **nextupd)
254 OCSP_CERTSTATUS *cst;
258 cst = single->certStatus;
260 if (ret == V_OCSP_CERTSTATUS_REVOKED) {
261 OCSP_REVOKEDINFO *rev = cst->value.revoked;
264 *revtime = rev->revocationTime;
266 if (rev->revocationReason)
267 *reason = ASN1_ENUMERATED_get(rev->revocationReason);
273 *thisupd = single->thisUpdate;
275 *nextupd = single->nextUpdate;
280 * This function combines the previous ones: look up a certificate ID and if
281 * found extract status information. Return 0 is successful.
283 int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
285 ASN1_GENERALIZEDTIME **revtime,
286 ASN1_GENERALIZEDTIME **thisupd,
287 ASN1_GENERALIZEDTIME **nextupd)
289 int i = OCSP_resp_find(bs, id, -1);
290 OCSP_SINGLERESP *single;
292 /* Maybe check for multiple responses and give an error? */
295 single = OCSP_resp_get0(bs, i);
296 i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
303 * Check validity of thisUpdate and nextUpdate fields. It is possible that
304 * the request will take a few seconds to process and/or the time won't be
305 * totally accurate. Therefore to avoid rejecting otherwise valid time we
306 * allow the times to be within 'nsec' of the current time. Also to avoid
307 * accepting very old responses without a nextUpdate field an optional maxage
308 * parameter specifies the maximum age the thisUpdate field can be.
310 int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
311 ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
317 /* Check thisUpdate is valid and not more than nsec in the future */
318 if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
319 ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
322 t_tmp = t_now + nsec;
323 if (X509_cmp_time(thisupd, &t_tmp) > 0) {
324 ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_NOT_YET_VALID);
329 * If maxsec specified check thisUpdate is not more than maxsec in
333 t_tmp = t_now - maxsec;
334 if (X509_cmp_time(thisupd, &t_tmp) < 0) {
335 ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_TOO_OLD);
344 /* Check nextUpdate is valid and not more than nsec in the past */
345 if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
346 ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
349 t_tmp = t_now - nsec;
350 if (X509_cmp_time(nextupd, &t_tmp) < 0) {
351 ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_EXPIRED);
356 /* Also don't allow nextUpdate to precede thisUpdate */
357 if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
358 ERR_raise(ERR_LIB_OCSP, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
365 const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
367 return single->certId;