2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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/objects.h>
14 #include <openssl/x509.h>
15 #include <openssl/pem.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/ocsp.h>
21 * Utility functions related to sending OCSP requests and extracting relevant
22 * information from the response.
26 * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
27 * 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 */
44 OCSP_ONEREQ_free(one);
48 /* Set requestorName from an X509_NAME structure */
50 int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)
54 gen = GENERAL_NAME_new();
57 if (!X509_NAME_set(&gen->d.directoryName, nm)) {
58 GENERAL_NAME_free(gen);
61 gen->type = GEN_DIRNAME;
62 GENERAL_NAME_free(req->tbsRequest.requestorName);
63 req->tbsRequest.requestorName = gen;
67 /* Add a certificate to an OCSP request */
69 int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
72 if (req->optionalSignature == NULL)
73 req->optionalSignature = OCSP_SIGNATURE_new();
74 sig = req->optionalSignature;
79 if (sig->certs == NULL
80 && (sig->certs = sk_X509_new_null()) == NULL)
83 if (!sk_X509_push(sig->certs, cert))
90 * Sign an OCSP request set the requestorName to the subject name of an
91 * optional signers certificate and include one or more optional certificates
92 * in the request. Behaves like PKCS7_sign().
95 int OCSP_request_sign(OCSP_REQUEST *req,
99 STACK_OF(X509) *certs, unsigned long flags)
104 if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
107 if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
110 if (!X509_check_private_key(signer, key)) {
111 OCSPerr(OCSP_F_OCSP_REQUEST_SIGN,
112 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
115 if (!OCSP_REQUEST_sign(req, key, dgst))
119 if (!(flags & OCSP_NOCERTS)) {
120 if (!OCSP_request_add1_cert(req, signer))
122 for (i = 0; i < sk_X509_num(certs); i++) {
123 x = sk_X509_value(certs, i);
124 if (!OCSP_request_add1_cert(req, x))
131 OCSP_SIGNATURE_free(req->optionalSignature);
132 req->optionalSignature = NULL;
136 /* Get response status */
138 int OCSP_response_status(OCSP_RESPONSE *resp)
140 return ASN1_ENUMERATED_get(resp->responseStatus);
144 * Extract basic response from OCSP_RESPONSE or NULL if no basic response
148 OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
151 rb = resp->responseBytes;
153 OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA);
156 if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
157 OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE);
161 return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
164 const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs)
166 return bs->signature;
170 * Return number of OCSP_SINGLERESP responses present in a basic response.
173 int OCSP_resp_count(OCSP_BASICRESP *bs)
177 return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses);
180 /* Extract an OCSP_SINGLERESP response with a given index */
182 OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
186 return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx);
189 const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP* bs)
191 return bs->tbsResponseData.producedAt;
194 const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs)
199 int OCSP_resp_get0_id(const OCSP_BASICRESP *bs,
200 const ASN1_OCTET_STRING **pid,
201 const X509_NAME **pname)
204 const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
205 if (rid->type == V_OCSP_RESPID_NAME) {
206 *pname = rid->value.byName;
208 } else if (rid->type == V_OCSP_RESPID_KEY) {
209 *pid = rid->value.byKey;
217 /* Look single response matching a given certificate ID */
219 int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
222 STACK_OF(OCSP_SINGLERESP) *sresp;
223 OCSP_SINGLERESP *single;
230 sresp = bs->tbsResponseData.responses;
231 for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
232 single = sk_OCSP_SINGLERESP_value(sresp, i);
233 if (!OCSP_id_cmp(id, single->certId))
240 * Extract status information from an OCSP_SINGLERESP structure. Note: the
241 * revtime and reason values are only set if the certificate status is
242 * revoked. Returns numerical value of status.
245 int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
246 ASN1_GENERALIZEDTIME **revtime,
247 ASN1_GENERALIZEDTIME **thisupd,
248 ASN1_GENERALIZEDTIME **nextupd)
251 OCSP_CERTSTATUS *cst;
254 cst = single->certStatus;
256 if (ret == V_OCSP_CERTSTATUS_REVOKED) {
257 OCSP_REVOKEDINFO *rev = cst->value.revoked;
259 *revtime = rev->revocationTime;
261 if (rev->revocationReason)
262 *reason = ASN1_ENUMERATED_get(rev->revocationReason);
268 *thisupd = single->thisUpdate;
270 *nextupd = single->nextUpdate;
275 * This function combines the previous ones: look up a certificate ID and if
276 * found extract status information. Return 0 is successful.
279 int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
281 ASN1_GENERALIZEDTIME **revtime,
282 ASN1_GENERALIZEDTIME **thisupd,
283 ASN1_GENERALIZEDTIME **nextupd)
286 OCSP_SINGLERESP *single;
287 i = OCSP_resp_find(bs, id, -1);
288 /* Maybe check for multiple responses and give an error? */
291 single = OCSP_resp_get0(bs, i);
292 i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
299 * Check validity of thisUpdate and nextUpdate fields. It is possible that
300 * the request will take a few seconds to process and/or the time won't be
301 * totally accurate. Therefore to avoid rejecting otherwise valid time we
302 * allow the times to be within 'nsec' of the current time. Also to avoid
303 * accepting very old responses without a nextUpdate field an optional maxage
304 * parameter specifies the maximum age the thisUpdate field can be.
307 int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
308 ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
313 /* Check thisUpdate is valid and not more than nsec in the future */
314 if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
315 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
318 t_tmp = t_now + nsec;
319 if (X509_cmp_time(thisupd, &t_tmp) > 0) {
320 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
325 * If maxsec specified check thisUpdate is not more than maxsec in
329 t_tmp = t_now - maxsec;
330 if (X509_cmp_time(thisupd, &t_tmp) < 0) {
331 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
340 /* Check nextUpdate is valid and not more than nsec in the past */
341 if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
342 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
345 t_tmp = t_now - nsec;
346 if (X509_cmp_time(nextupd, &t_tmp) < 0) {
347 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
352 /* Also don't allow nextUpdate to precede thisUpdate */
353 if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
354 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
355 OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
362 const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
364 return single->certId;