2 * Copyright 2000-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/ocsp.h>
15 #include "ocsp_local.h"
16 #include <openssl/rand.h>
17 #include <openssl/x509v3.h>
19 DEFINE_STACK_OF(ASN1_OBJECT)
20 DEFINE_STACK_OF(ACCESS_DESCRIPTION)
22 /* Standard wrapper functions for extensions */
24 /* OCSP request extensions */
26 int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
28 return X509v3_get_ext_count(x->tbsRequest.requestExtensions);
31 int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
33 return (X509v3_get_ext_by_NID
34 (x->tbsRequest.requestExtensions, nid, lastpos));
37 int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj,
40 return (X509v3_get_ext_by_OBJ
41 (x->tbsRequest.requestExtensions, obj, lastpos));
44 int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
46 return (X509v3_get_ext_by_critical
47 (x->tbsRequest.requestExtensions, crit, lastpos));
50 X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
52 return X509v3_get_ext(x->tbsRequest.requestExtensions, loc);
55 X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
57 return X509v3_delete_ext(x->tbsRequest.requestExtensions, loc);
60 void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
62 return X509V3_get_d2i(x->tbsRequest.requestExtensions, nid, crit, idx);
65 int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
68 return X509V3_add1_i2d(&x->tbsRequest.requestExtensions, nid, value,
72 int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
74 return (X509v3_add_ext(&(x->tbsRequest.requestExtensions), ex, loc) !=
78 /* Single extensions */
80 int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
82 return X509v3_get_ext_count(x->singleRequestExtensions);
85 int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
87 return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos);
90 int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj,
93 return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos);
96 int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
98 return (X509v3_get_ext_by_critical
99 (x->singleRequestExtensions, crit, lastpos));
102 X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
104 return X509v3_get_ext(x->singleRequestExtensions, loc);
107 X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
109 return X509v3_delete_ext(x->singleRequestExtensions, loc);
112 void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
114 return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
117 int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
120 return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
124 int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
126 return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
129 /* OCSP Basic response */
131 int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
133 return X509v3_get_ext_count(x->tbsResponseData.responseExtensions);
136 int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
138 return (X509v3_get_ext_by_NID
139 (x->tbsResponseData.responseExtensions, nid, lastpos));
142 int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj,
145 return (X509v3_get_ext_by_OBJ
146 (x->tbsResponseData.responseExtensions, obj, lastpos));
149 int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
152 return (X509v3_get_ext_by_critical
153 (x->tbsResponseData.responseExtensions, crit, lastpos));
156 X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
158 return X509v3_get_ext(x->tbsResponseData.responseExtensions, loc);
161 X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
163 return X509v3_delete_ext(x->tbsResponseData.responseExtensions, loc);
166 void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
169 return X509V3_get_d2i(x->tbsResponseData.responseExtensions, nid, crit,
173 int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
174 int crit, unsigned long flags)
176 return X509V3_add1_i2d(&x->tbsResponseData.responseExtensions, nid,
180 int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
182 return (X509v3_add_ext(&(x->tbsResponseData.responseExtensions), ex, loc)
186 /* OCSP single response extensions */
188 int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
190 return X509v3_get_ext_count(x->singleExtensions);
193 int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
195 return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos);
198 int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj,
201 return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos);
204 int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
207 return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos);
210 X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
212 return X509v3_get_ext(x->singleExtensions, loc);
215 X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
217 return X509v3_delete_ext(x->singleExtensions, loc);
220 void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
223 return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
226 int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
227 int crit, unsigned long flags)
229 return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
232 int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
234 return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
237 /* also CRL Entry Extensions */
239 /* Nonce handling functions */
242 * Add a nonce to an extension stack. A nonce can be specified or if NULL a
243 * random nonce will be generated. Note: OpenSSL 0.9.7d and later create an
244 * OCTET STRING containing the nonce, previous versions used the raw nonce.
247 static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
248 unsigned char *val, int len)
250 unsigned char *tmpval;
251 ASN1_OCTET_STRING os;
254 len = OCSP_DEFAULT_NONCE_LENGTH;
256 * Create the OCTET STRING manually by writing out the header and
257 * appending the content octets. This avoids an extra memory allocation
258 * operation in some cases. Applications should *NOT* do this because it
259 * relies on library internals.
261 os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
265 os.data = OPENSSL_malloc(os.length);
269 ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
271 memcpy(tmpval, val, len);
272 else if (RAND_bytes(tmpval, len) <= 0)
274 if (!X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
275 &os, 0, X509V3_ADD_REPLACE))
279 OPENSSL_free(os.data);
283 /* Add nonce to an OCSP request */
285 int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
287 return ocsp_add1_nonce(&req->tbsRequest.requestExtensions, val, len);
290 /* Same as above but for a response */
292 int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
294 return ocsp_add1_nonce(&resp->tbsResponseData.responseExtensions, val,
299 * Check nonce validity in a request and response.
300 * Return value reflects result:
301 * 1: nonces present and equal.
302 * 2: nonces both absent.
303 * 3: nonce present in response only.
304 * 0: nonces both present and not equal.
305 * -1: nonce in request only.
307 * For most responders clients can check return > 0.
308 * If responder doesn't handle nonces return != 0 may be
309 * necessary. return == 0 is always an error.
312 int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
315 * Since we are only interested in the presence or absence of
316 * the nonce and comparing its value there is no need to use
317 * the X509V3 routines: this way we can avoid them allocating an
318 * ASN1_OCTET_STRING structure for the value which would be
319 * freed immediately anyway.
322 int req_idx, resp_idx;
323 X509_EXTENSION *req_ext, *resp_ext;
324 req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
325 resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
326 /* Check both absent */
327 if ((req_idx < 0) && (resp_idx < 0))
329 /* Check in request only */
330 if ((req_idx >= 0) && (resp_idx < 0))
332 /* Check in response but not request */
333 if ((req_idx < 0) && (resp_idx >= 0))
336 * Otherwise nonce in request and response so retrieve the extensions
338 req_ext = OCSP_REQUEST_get_ext(req, req_idx);
339 resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
340 if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext),
341 X509_EXTENSION_get_data(resp_ext)))
347 * Copy the nonce value (if any) from an OCSP request to a response.
350 int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
352 X509_EXTENSION *req_ext;
354 /* Check for nonce in request */
355 req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
356 /* If no nonce that's OK */
359 req_ext = OCSP_REQUEST_get_ext(req, req_idx);
360 return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
363 X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim)
365 X509_EXTENSION *x = NULL;
366 OCSP_CRLID *cid = NULL;
368 if ((cid = OCSP_CRLID_new()) == NULL)
371 if ((cid->crlUrl = ASN1_IA5STRING_new()) == NULL)
373 if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
377 if ((cid->crlNum = ASN1_INTEGER_new()) == NULL)
379 if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
383 if ((cid->crlTime = ASN1_GENERALIZEDTIME_new()) == NULL)
385 if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
388 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid);
390 OCSP_CRLID_free(cid);
394 /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
395 X509_EXTENSION *OCSP_accept_responses_new(char **oids)
398 STACK_OF(ASN1_OBJECT) *sk = NULL;
399 ASN1_OBJECT *o = NULL;
400 X509_EXTENSION *x = NULL;
402 if ((sk = sk_ASN1_OBJECT_new_null()) == NULL)
404 while (oids && *oids) {
405 if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
406 sk_ASN1_OBJECT_push(sk, o);
409 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk);
411 sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
415 /* ArchiveCutoff ::= GeneralizedTime */
416 X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
418 X509_EXTENSION *x = NULL;
419 ASN1_GENERALIZEDTIME *gt = NULL;
421 if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
423 if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
425 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt);
427 ASN1_GENERALIZEDTIME_free(gt);
432 * per ACCESS_DESCRIPTION parameter are oids, of which there are currently
433 * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value. This method
434 * forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
436 X509_EXTENSION *OCSP_url_svcloc_new(const X509_NAME *issuer, const char **urls)
438 X509_EXTENSION *x = NULL;
439 ASN1_IA5STRING *ia5 = NULL;
440 OCSP_SERVICELOC *sloc = NULL;
441 ACCESS_DESCRIPTION *ad = NULL;
443 if ((sloc = OCSP_SERVICELOC_new()) == NULL)
445 X509_NAME_free(sloc->issuer);
446 if ((sloc->issuer = X509_NAME_dup(issuer)) == NULL)
449 && (sloc->locator = sk_ACCESS_DESCRIPTION_new_null()) == NULL)
451 while (urls && *urls) {
452 if ((ad = ACCESS_DESCRIPTION_new()) == NULL)
454 if ((ad->method = OBJ_nid2obj(NID_ad_OCSP)) == NULL)
456 if ((ia5 = ASN1_IA5STRING_new()) == NULL)
458 if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
460 /* ad->location is allocated inside ACCESS_DESCRIPTION_new */
461 ad->location->type = GEN_URI;
462 ad->location->d.ia5 = ia5;
464 if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
469 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc);
471 ASN1_IA5STRING_free(ia5);
472 ACCESS_DESCRIPTION_free(ad);
473 OCSP_SERVICELOC_free(sloc);