xref: /freebsd/crypto/openssl/crypto/ocsp/ocsp_ext.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2000-2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 #include <stdio.h>
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>
18 
19 /* Standard wrapper functions for extensions */
20 
21 /* OCSP request extensions */
22 
OCSP_REQUEST_get_ext_count(OCSP_REQUEST * x)23 int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
24 {
25     return X509v3_get_ext_count(x->tbsRequest.requestExtensions);
26 }
27 
OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST * x,int nid,int lastpos)28 int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
29 {
30     return (X509v3_get_ext_by_NID(x->tbsRequest.requestExtensions, nid, lastpos));
31 }
32 
OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST * x,const ASN1_OBJECT * obj,int lastpos)33 int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj,
34     int lastpos)
35 {
36     return (X509v3_get_ext_by_OBJ(x->tbsRequest.requestExtensions, obj, lastpos));
37 }
38 
OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST * x,int crit,int lastpos)39 int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
40 {
41     return (X509v3_get_ext_by_critical(x->tbsRequest.requestExtensions, crit, lastpos));
42 }
43 
OCSP_REQUEST_get_ext(OCSP_REQUEST * x,int loc)44 X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
45 {
46     return X509v3_get_ext(x->tbsRequest.requestExtensions, loc);
47 }
48 
OCSP_REQUEST_delete_ext(OCSP_REQUEST * x,int loc)49 X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
50 {
51     return X509v3_delete_ext(x->tbsRequest.requestExtensions, loc);
52 }
53 
OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST * x,int nid,int * crit,int * idx)54 void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
55 {
56     return X509V3_get_d2i(x->tbsRequest.requestExtensions, nid, crit, idx);
57 }
58 
OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST * x,int nid,void * value,int crit,unsigned long flags)59 int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
60     unsigned long flags)
61 {
62     return X509V3_add1_i2d(&x->tbsRequest.requestExtensions, nid, value,
63         crit, flags);
64 }
65 
OCSP_REQUEST_add_ext(OCSP_REQUEST * x,X509_EXTENSION * ex,int loc)66 int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
67 {
68     return (X509v3_add_ext(&(x->tbsRequest.requestExtensions), ex, loc) != NULL);
69 }
70 
71 /* Single extensions */
72 
OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ * x)73 int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
74 {
75     return X509v3_get_ext_count(x->singleRequestExtensions);
76 }
77 
OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ * x,int nid,int lastpos)78 int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
79 {
80     return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos);
81 }
82 
OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ * x,const ASN1_OBJECT * obj,int lastpos)83 int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj,
84     int lastpos)
85 {
86     return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos);
87 }
88 
OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ * x,int crit,int lastpos)89 int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
90 {
91     return (X509v3_get_ext_by_critical(x->singleRequestExtensions, crit, lastpos));
92 }
93 
OCSP_ONEREQ_get_ext(OCSP_ONEREQ * x,int loc)94 X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
95 {
96     return X509v3_get_ext(x->singleRequestExtensions, loc);
97 }
98 
OCSP_ONEREQ_delete_ext(OCSP_ONEREQ * x,int loc)99 X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
100 {
101     return X509v3_delete_ext(x->singleRequestExtensions, loc);
102 }
103 
OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ * x,int nid,int * crit,int * idx)104 void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
105 {
106     return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
107 }
108 
OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ * x,int nid,void * value,int crit,unsigned long flags)109 int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
110     unsigned long flags)
111 {
112     return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
113         flags);
114 }
115 
OCSP_ONEREQ_add_ext(OCSP_ONEREQ * x,X509_EXTENSION * ex,int loc)116 int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
117 {
118     return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
119 }
120 
121 /* OCSP Basic response */
122 
OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP * x)123 int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
124 {
125     return X509v3_get_ext_count(x->tbsResponseData.responseExtensions);
126 }
127 
OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP * x,int nid,int lastpos)128 int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
129 {
130     return (X509v3_get_ext_by_NID(x->tbsResponseData.responseExtensions, nid, lastpos));
131 }
132 
OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP * x,const ASN1_OBJECT * obj,int lastpos)133 int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj,
134     int lastpos)
135 {
136     return (X509v3_get_ext_by_OBJ(x->tbsResponseData.responseExtensions, obj, lastpos));
137 }
138 
OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP * x,int crit,int lastpos)139 int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
140     int lastpos)
141 {
142     return (X509v3_get_ext_by_critical(x->tbsResponseData.responseExtensions, crit, lastpos));
143 }
144 
OCSP_BASICRESP_get_ext(OCSP_BASICRESP * x,int loc)145 X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
146 {
147     return X509v3_get_ext(x->tbsResponseData.responseExtensions, loc);
148 }
149 
OCSP_BASICRESP_delete_ext(OCSP_BASICRESP * x,int loc)150 X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
151 {
152     return X509v3_delete_ext(x->tbsResponseData.responseExtensions, loc);
153 }
154 
OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP * x,int nid,int * crit,int * idx)155 void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
156     int *idx)
157 {
158     return X509V3_get_d2i(x->tbsResponseData.responseExtensions, nid, crit,
159         idx);
160 }
161 
OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP * x,int nid,void * value,int crit,unsigned long flags)162 int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
163     int crit, unsigned long flags)
164 {
165     return X509V3_add1_i2d(&x->tbsResponseData.responseExtensions, nid,
166         value, crit, flags);
167 }
168 
OCSP_BASICRESP_add_ext(OCSP_BASICRESP * x,X509_EXTENSION * ex,int loc)169 int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
170 {
171     return (X509v3_add_ext(&(x->tbsResponseData.responseExtensions), ex, loc)
172         != NULL);
173 }
174 
175 /* OCSP single response extensions */
176 
OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP * x)177 int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
178 {
179     return X509v3_get_ext_count(x->singleExtensions);
180 }
181 
OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP * x,int nid,int lastpos)182 int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
183 {
184     return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos);
185 }
186 
OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP * x,const ASN1_OBJECT * obj,int lastpos)187 int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj,
188     int lastpos)
189 {
190     return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos);
191 }
192 
OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP * x,int crit,int lastpos)193 int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
194     int lastpos)
195 {
196     return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos);
197 }
198 
OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP * x,int loc)199 X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
200 {
201     return X509v3_get_ext(x->singleExtensions, loc);
202 }
203 
OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP * x,int loc)204 X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
205 {
206     return X509v3_delete_ext(x->singleExtensions, loc);
207 }
208 
OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP * x,int nid,int * crit,int * idx)209 void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
210     int *idx)
211 {
212     return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
213 }
214 
OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP * x,int nid,void * value,int crit,unsigned long flags)215 int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
216     int crit, unsigned long flags)
217 {
218     return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
219 }
220 
OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP * x,X509_EXTENSION * ex,int loc)221 int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
222 {
223     return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
224 }
225 
226 /* also CRL Entry Extensions */
227 
228 /* Nonce handling functions */
229 
230 /*
231  * Add a nonce to an extension stack. A nonce can be specified or if NULL a
232  * random nonce will be generated. Note: OpenSSL 0.9.7d and later create an
233  * OCTET STRING containing the nonce, previous versions used the raw nonce.
234  */
235 
ocsp_add1_nonce(STACK_OF (X509_EXTENSION)** exts,unsigned char * val,int len)236 static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
237     unsigned char *val, int len)
238 {
239     unsigned char *tmpval;
240     ASN1_OCTET_STRING os;
241     int ret = 0;
242     if (len <= 0)
243         len = OCSP_DEFAULT_NONCE_LENGTH;
244     /*
245      * Create the OCTET STRING manually by writing out the header and
246      * appending the content octets. This avoids an extra memory allocation
247      * operation in some cases. Applications should *NOT* do this because it
248      * relies on library internals.
249      */
250     os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
251     if (os.length < 0)
252         return 0;
253 
254     os.data = OPENSSL_malloc(os.length);
255     if (os.data == NULL)
256         goto err;
257     tmpval = os.data;
258     ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
259     if (val)
260         memcpy(tmpval, val, len);
261     else if (RAND_bytes(tmpval, len) <= 0)
262         goto err;
263     if (X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
264             &os, 0, X509V3_ADD_REPLACE)
265         <= 0)
266         goto err;
267     ret = 1;
268 err:
269     OPENSSL_free(os.data);
270     return ret;
271 }
272 
273 /* Add nonce to an OCSP request */
274 
OCSP_request_add1_nonce(OCSP_REQUEST * req,unsigned char * val,int len)275 int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
276 {
277     return ocsp_add1_nonce(&req->tbsRequest.requestExtensions, val, len);
278 }
279 
280 /* Same as above but for a response */
281 
OCSP_basic_add1_nonce(OCSP_BASICRESP * resp,unsigned char * val,int len)282 int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
283 {
284     return ocsp_add1_nonce(&resp->tbsResponseData.responseExtensions, val,
285         len);
286 }
287 
288 /*-
289  * Check nonce validity in a request and response.
290  * Return value reflects result:
291  *  1: nonces present and equal.
292  *  2: nonces both absent.
293  *  3: nonce present in response only.
294  *  0: nonces both present and not equal.
295  * -1: nonce in request only.
296  *
297  *  For most responders clients can check return > 0.
298  *  If responder doesn't handle nonces return != 0 may be
299  *  necessary. return == 0 is always an error.
300  */
301 
OCSP_check_nonce(OCSP_REQUEST * req,OCSP_BASICRESP * bs)302 int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
303 {
304     /*
305      * Since we are only interested in the presence or absence of
306      * the nonce and comparing its value there is no need to use
307      * the X509V3 routines: this way we can avoid them allocating an
308      * ASN1_OCTET_STRING structure for the value which would be
309      * freed immediately anyway.
310      */
311 
312     int req_idx, resp_idx;
313     X509_EXTENSION *req_ext, *resp_ext;
314     req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
315     resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
316     /* Check both absent */
317     if ((req_idx < 0) && (resp_idx < 0))
318         return 2;
319     /* Check in request only */
320     if ((req_idx >= 0) && (resp_idx < 0))
321         return -1;
322     /* Check in response but not request */
323     if ((req_idx < 0) && (resp_idx >= 0))
324         return 3;
325     /*
326      * Otherwise nonce in request and response so retrieve the extensions
327      */
328     req_ext = OCSP_REQUEST_get_ext(req, req_idx);
329     resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
330     if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext),
331             X509_EXTENSION_get_data(resp_ext)))
332         return 0;
333     return 1;
334 }
335 
336 /*
337  * Copy the nonce value (if any) from an OCSP request to a response.
338  */
339 
OCSP_copy_nonce(OCSP_BASICRESP * resp,OCSP_REQUEST * req)340 int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
341 {
342     X509_EXTENSION *req_ext;
343     int req_idx;
344     /* Check for nonce in request */
345     req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
346     /* If no nonce that's OK */
347     if (req_idx < 0)
348         return 2;
349     req_ext = OCSP_REQUEST_get_ext(req, req_idx);
350     return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
351 }
352 
OCSP_crlID_new(const char * url,long * n,char * tim)353 X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim)
354 {
355     X509_EXTENSION *x = NULL;
356     OCSP_CRLID *cid = NULL;
357 
358     if ((cid = OCSP_CRLID_new()) == NULL)
359         goto err;
360     if (url) {
361         if ((cid->crlUrl = ASN1_IA5STRING_new()) == NULL)
362             goto err;
363         if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
364             goto err;
365     }
366     if (n) {
367         if ((cid->crlNum = ASN1_INTEGER_new()) == NULL)
368             goto err;
369         if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
370             goto err;
371     }
372     if (tim) {
373         if ((cid->crlTime = ASN1_GENERALIZEDTIME_new()) == NULL)
374             goto err;
375         if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
376             goto err;
377     }
378     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid);
379 err:
380     OCSP_CRLID_free(cid);
381     return x;
382 }
383 
384 /*   AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
OCSP_accept_responses_new(char ** oids)385 X509_EXTENSION *OCSP_accept_responses_new(char **oids)
386 {
387     int nid;
388     STACK_OF(ASN1_OBJECT) *sk = NULL;
389     ASN1_OBJECT *o = NULL;
390     X509_EXTENSION *x = NULL;
391 
392     if ((sk = sk_ASN1_OBJECT_new_null()) == NULL)
393         goto err;
394     while (oids && *oids) {
395         if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
396             if (!sk_ASN1_OBJECT_push(sk, o))
397                 goto err;
398         oids++;
399     }
400     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk);
401 err:
402     sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
403     return x;
404 }
405 
406 /*  ArchiveCutoff ::= GeneralizedTime */
OCSP_archive_cutoff_new(char * tim)407 X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
408 {
409     X509_EXTENSION *x = NULL;
410     ASN1_GENERALIZEDTIME *gt = NULL;
411 
412     if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
413         goto err;
414     if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
415         goto err;
416     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt);
417 err:
418     ASN1_GENERALIZEDTIME_free(gt);
419     return x;
420 }
421 
422 /*
423  * per ACCESS_DESCRIPTION parameter are oids, of which there are currently
424  * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value.  This method
425  * forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
426  */
OCSP_url_svcloc_new(const X509_NAME * issuer,const char ** urls)427 X509_EXTENSION *OCSP_url_svcloc_new(const X509_NAME *issuer, const char **urls)
428 {
429     X509_EXTENSION *x = NULL;
430     ASN1_IA5STRING *ia5 = NULL;
431     OCSP_SERVICELOC *sloc = NULL;
432     ACCESS_DESCRIPTION *ad = NULL;
433 
434     if ((sloc = OCSP_SERVICELOC_new()) == NULL)
435         goto err;
436     X509_NAME_free(sloc->issuer);
437     if ((sloc->issuer = X509_NAME_dup(issuer)) == NULL)
438         goto err;
439     if (urls && *urls
440         && (sloc->locator = sk_ACCESS_DESCRIPTION_new_null()) == NULL)
441         goto err;
442     while (urls && *urls) {
443         if ((ad = ACCESS_DESCRIPTION_new()) == NULL)
444             goto err;
445         if ((ad->method = OBJ_nid2obj(NID_ad_OCSP)) == NULL)
446             goto err;
447         if ((ia5 = ASN1_IA5STRING_new()) == NULL)
448             goto err;
449         if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
450             goto err;
451         /* ad->location is allocated inside ACCESS_DESCRIPTION_new */
452         ad->location->type = GEN_URI;
453         ad->location->d.ia5 = ia5;
454         ia5 = NULL;
455         if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
456             goto err;
457         ad = NULL;
458         urls++;
459     }
460     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc);
461 err:
462     ASN1_IA5STRING_free(ia5);
463     ACCESS_DESCRIPTION_free(ad);
464     OCSP_SERVICELOC_free(sloc);
465     return x;
466 }
467