]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ocsp/ocsp_ext.c
In OpenSSL builds, declare STACK for datatypes ...
[thirdparty/openssl.git] / crypto / ocsp / ocsp_ext.c
1 /*
2 * Copyright 2000-2020 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 DEFINE_STACK_OF(ASN1_OBJECT)
20 DEFINE_STACK_OF(ACCESS_DESCRIPTION)
21
22 /* Standard wrapper functions for extensions */
23
24 /* OCSP request extensions */
25
26 int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
27 {
28 return X509v3_get_ext_count(x->tbsRequest.requestExtensions);
29 }
30
31 int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
32 {
33 return (X509v3_get_ext_by_NID
34 (x->tbsRequest.requestExtensions, nid, lastpos));
35 }
36
37 int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj,
38 int lastpos)
39 {
40 return (X509v3_get_ext_by_OBJ
41 (x->tbsRequest.requestExtensions, obj, lastpos));
42 }
43
44 int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
45 {
46 return (X509v3_get_ext_by_critical
47 (x->tbsRequest.requestExtensions, crit, lastpos));
48 }
49
50 X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
51 {
52 return X509v3_get_ext(x->tbsRequest.requestExtensions, loc);
53 }
54
55 X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
56 {
57 return X509v3_delete_ext(x->tbsRequest.requestExtensions, loc);
58 }
59
60 void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
61 {
62 return X509V3_get_d2i(x->tbsRequest.requestExtensions, nid, crit, idx);
63 }
64
65 int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
66 unsigned long flags)
67 {
68 return X509V3_add1_i2d(&x->tbsRequest.requestExtensions, nid, value,
69 crit, flags);
70 }
71
72 int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
73 {
74 return (X509v3_add_ext(&(x->tbsRequest.requestExtensions), ex, loc) !=
75 NULL);
76 }
77
78 /* Single extensions */
79
80 int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
81 {
82 return X509v3_get_ext_count(x->singleRequestExtensions);
83 }
84
85 int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
86 {
87 return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos);
88 }
89
90 int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj,
91 int lastpos)
92 {
93 return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos);
94 }
95
96 int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
97 {
98 return (X509v3_get_ext_by_critical
99 (x->singleRequestExtensions, crit, lastpos));
100 }
101
102 X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
103 {
104 return X509v3_get_ext(x->singleRequestExtensions, loc);
105 }
106
107 X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
108 {
109 return X509v3_delete_ext(x->singleRequestExtensions, loc);
110 }
111
112 void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
113 {
114 return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
115 }
116
117 int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
118 unsigned long flags)
119 {
120 return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
121 flags);
122 }
123
124 int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
125 {
126 return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
127 }
128
129 /* OCSP Basic response */
130
131 int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
132 {
133 return X509v3_get_ext_count(x->tbsResponseData.responseExtensions);
134 }
135
136 int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
137 {
138 return (X509v3_get_ext_by_NID
139 (x->tbsResponseData.responseExtensions, nid, lastpos));
140 }
141
142 int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj,
143 int lastpos)
144 {
145 return (X509v3_get_ext_by_OBJ
146 (x->tbsResponseData.responseExtensions, obj, lastpos));
147 }
148
149 int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
150 int lastpos)
151 {
152 return (X509v3_get_ext_by_critical
153 (x->tbsResponseData.responseExtensions, crit, lastpos));
154 }
155
156 X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
157 {
158 return X509v3_get_ext(x->tbsResponseData.responseExtensions, loc);
159 }
160
161 X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
162 {
163 return X509v3_delete_ext(x->tbsResponseData.responseExtensions, loc);
164 }
165
166 void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
167 int *idx)
168 {
169 return X509V3_get_d2i(x->tbsResponseData.responseExtensions, nid, crit,
170 idx);
171 }
172
173 int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
174 int crit, unsigned long flags)
175 {
176 return X509V3_add1_i2d(&x->tbsResponseData.responseExtensions, nid,
177 value, crit, flags);
178 }
179
180 int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
181 {
182 return (X509v3_add_ext(&(x->tbsResponseData.responseExtensions), ex, loc)
183 != NULL);
184 }
185
186 /* OCSP single response extensions */
187
188 int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
189 {
190 return X509v3_get_ext_count(x->singleExtensions);
191 }
192
193 int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
194 {
195 return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos);
196 }
197
198 int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj,
199 int lastpos)
200 {
201 return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos);
202 }
203
204 int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
205 int lastpos)
206 {
207 return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos);
208 }
209
210 X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
211 {
212 return X509v3_get_ext(x->singleExtensions, loc);
213 }
214
215 X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
216 {
217 return X509v3_delete_ext(x->singleExtensions, loc);
218 }
219
220 void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
221 int *idx)
222 {
223 return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
224 }
225
226 int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
227 int crit, unsigned long flags)
228 {
229 return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
230 }
231
232 int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
233 {
234 return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
235 }
236
237 /* also CRL Entry Extensions */
238
239 /* Nonce handling functions */
240
241 /*
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.
245 */
246
247 static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
248 unsigned char *val, int len)
249 {
250 unsigned char *tmpval;
251 ASN1_OCTET_STRING os;
252 int ret = 0;
253 if (len <= 0)
254 len = OCSP_DEFAULT_NONCE_LENGTH;
255 /*
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.
260 */
261 os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
262 if (os.length < 0)
263 return 0;
264
265 os.data = OPENSSL_malloc(os.length);
266 if (os.data == NULL)
267 goto err;
268 tmpval = os.data;
269 ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
270 if (val)
271 memcpy(tmpval, val, len);
272 else if (RAND_bytes(tmpval, len) <= 0)
273 goto err;
274 if (!X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
275 &os, 0, X509V3_ADD_REPLACE))
276 goto err;
277 ret = 1;
278 err:
279 OPENSSL_free(os.data);
280 return ret;
281 }
282
283 /* Add nonce to an OCSP request */
284
285 int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
286 {
287 return ocsp_add1_nonce(&req->tbsRequest.requestExtensions, val, len);
288 }
289
290 /* Same as above but for a response */
291
292 int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
293 {
294 return ocsp_add1_nonce(&resp->tbsResponseData.responseExtensions, val,
295 len);
296 }
297
298 /*-
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.
306 *
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.
310 */
311
312 int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
313 {
314 /*
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.
320 */
321
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))
328 return 2;
329 /* Check in request only */
330 if ((req_idx >= 0) && (resp_idx < 0))
331 return -1;
332 /* Check in response but not request */
333 if ((req_idx < 0) && (resp_idx >= 0))
334 return 3;
335 /*
336 * Otherwise nonce in request and response so retrieve the extensions
337 */
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)))
342 return 0;
343 return 1;
344 }
345
346 /*
347 * Copy the nonce value (if any) from an OCSP request to a response.
348 */
349
350 int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
351 {
352 X509_EXTENSION *req_ext;
353 int req_idx;
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 */
357 if (req_idx < 0)
358 return 2;
359 req_ext = OCSP_REQUEST_get_ext(req, req_idx);
360 return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
361 }
362
363 X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim)
364 {
365 X509_EXTENSION *x = NULL;
366 OCSP_CRLID *cid = NULL;
367
368 if ((cid = OCSP_CRLID_new()) == NULL)
369 goto err;
370 if (url) {
371 if ((cid->crlUrl = ASN1_IA5STRING_new()) == NULL)
372 goto err;
373 if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
374 goto err;
375 }
376 if (n) {
377 if ((cid->crlNum = ASN1_INTEGER_new()) == NULL)
378 goto err;
379 if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
380 goto err;
381 }
382 if (tim) {
383 if ((cid->crlTime = ASN1_GENERALIZEDTIME_new()) == NULL)
384 goto err;
385 if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
386 goto err;
387 }
388 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid);
389 err:
390 OCSP_CRLID_free(cid);
391 return x;
392 }
393
394 /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
395 X509_EXTENSION *OCSP_accept_responses_new(char **oids)
396 {
397 int nid;
398 STACK_OF(ASN1_OBJECT) *sk = NULL;
399 ASN1_OBJECT *o = NULL;
400 X509_EXTENSION *x = NULL;
401
402 if ((sk = sk_ASN1_OBJECT_new_null()) == NULL)
403 goto err;
404 while (oids && *oids) {
405 if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
406 sk_ASN1_OBJECT_push(sk, o);
407 oids++;
408 }
409 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk);
410 err:
411 sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
412 return x;
413 }
414
415 /* ArchiveCutoff ::= GeneralizedTime */
416 X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
417 {
418 X509_EXTENSION *x = NULL;
419 ASN1_GENERALIZEDTIME *gt = NULL;
420
421 if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
422 goto err;
423 if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
424 goto err;
425 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt);
426 err:
427 ASN1_GENERALIZEDTIME_free(gt);
428 return x;
429 }
430
431 /*
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.
435 */
436 X509_EXTENSION *OCSP_url_svcloc_new(const X509_NAME *issuer, const char **urls)
437 {
438 X509_EXTENSION *x = NULL;
439 ASN1_IA5STRING *ia5 = NULL;
440 OCSP_SERVICELOC *sloc = NULL;
441 ACCESS_DESCRIPTION *ad = NULL;
442
443 if ((sloc = OCSP_SERVICELOC_new()) == NULL)
444 goto err;
445 X509_NAME_free(sloc->issuer);
446 if ((sloc->issuer = X509_NAME_dup(issuer)) == NULL)
447 goto err;
448 if (urls && *urls
449 && (sloc->locator = sk_ACCESS_DESCRIPTION_new_null()) == NULL)
450 goto err;
451 while (urls && *urls) {
452 if ((ad = ACCESS_DESCRIPTION_new()) == NULL)
453 goto err;
454 if ((ad->method = OBJ_nid2obj(NID_ad_OCSP)) == NULL)
455 goto err;
456 if ((ia5 = ASN1_IA5STRING_new()) == NULL)
457 goto err;
458 if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
459 goto err;
460 /* ad->location is allocated inside ACCESS_DESCRIPTION_new */
461 ad->location->type = GEN_URI;
462 ad->location->d.ia5 = ia5;
463 ia5 = NULL;
464 if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
465 goto err;
466 ad = NULL;
467 urls++;
468 }
469 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc);
470 err:
471 ASN1_IA5STRING_free(ia5);
472 ACCESS_DESCRIPTION_free(ad);
473 OCSP_SERVICELOC_free(sloc);
474 return x;
475 }