xref: /netbsd-src/crypto/external/bsd/openssl/dist/crypto/ocsp/ocsp_ext.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1635165faSspz /*
2*b0d17251Schristos  * Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
6c7da899bSchristos  * in the file LICENSE in the source distribution or at
7c7da899bSchristos  * https://www.openssl.org/source/license.html
8a89c9211Schristos  */
9a89c9211Schristos 
10a89c9211Schristos #include <stdio.h>
11c7da899bSchristos #include "internal/cryptlib.h"
12a89c9211Schristos #include <openssl/objects.h>
13a89c9211Schristos #include <openssl/x509.h>
14a89c9211Schristos #include <openssl/ocsp.h>
157d004720Schristos #include "ocsp_local.h"
16a89c9211Schristos #include <openssl/rand.h>
17a89c9211Schristos #include <openssl/x509v3.h>
18a89c9211Schristos 
19a89c9211Schristos /* Standard wrapper functions for extensions */
20a89c9211Schristos 
21a89c9211Schristos /* OCSP request extensions */
22a89c9211Schristos 
OCSP_REQUEST_get_ext_count(OCSP_REQUEST * x)23a89c9211Schristos int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
24a89c9211Schristos {
2513d40330Schristos     return X509v3_get_ext_count(x->tbsRequest.requestExtensions);
26a89c9211Schristos }
27a89c9211Schristos 
OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST * x,int nid,int lastpos)28a89c9211Schristos int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
29a89c9211Schristos {
30635165faSspz     return (X509v3_get_ext_by_NID
31c7da899bSchristos             (x->tbsRequest.requestExtensions, nid, lastpos));
32a89c9211Schristos }
33a89c9211Schristos 
OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST * x,const ASN1_OBJECT * obj,int lastpos)34c7da899bSchristos int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj,
35635165faSspz                                 int lastpos)
36a89c9211Schristos {
37635165faSspz     return (X509v3_get_ext_by_OBJ
38c7da899bSchristos             (x->tbsRequest.requestExtensions, obj, lastpos));
39a89c9211Schristos }
40a89c9211Schristos 
OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST * x,int crit,int lastpos)41a89c9211Schristos int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
42a89c9211Schristos {
43635165faSspz     return (X509v3_get_ext_by_critical
44c7da899bSchristos             (x->tbsRequest.requestExtensions, crit, lastpos));
45a89c9211Schristos }
46a89c9211Schristos 
OCSP_REQUEST_get_ext(OCSP_REQUEST * x,int loc)47a89c9211Schristos X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
48a89c9211Schristos {
4913d40330Schristos     return X509v3_get_ext(x->tbsRequest.requestExtensions, loc);
50a89c9211Schristos }
51a89c9211Schristos 
OCSP_REQUEST_delete_ext(OCSP_REQUEST * x,int loc)52a89c9211Schristos X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
53a89c9211Schristos {
5413d40330Schristos     return X509v3_delete_ext(x->tbsRequest.requestExtensions, loc);
55a89c9211Schristos }
56a89c9211Schristos 
OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST * x,int nid,int * crit,int * idx)57a89c9211Schristos void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
58a89c9211Schristos {
59c7da899bSchristos     return X509V3_get_d2i(x->tbsRequest.requestExtensions, nid, crit, idx);
60a89c9211Schristos }
61a89c9211Schristos 
OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST * x,int nid,void * value,int crit,unsigned long flags)62a89c9211Schristos int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
63a89c9211Schristos                               unsigned long flags)
64a89c9211Schristos {
65c7da899bSchristos     return X509V3_add1_i2d(&x->tbsRequest.requestExtensions, nid, value,
66635165faSspz                            crit, flags);
67a89c9211Schristos }
68a89c9211Schristos 
OCSP_REQUEST_add_ext(OCSP_REQUEST * x,X509_EXTENSION * ex,int loc)69a89c9211Schristos int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
70a89c9211Schristos {
71c7da899bSchristos     return (X509v3_add_ext(&(x->tbsRequest.requestExtensions), ex, loc) !=
72635165faSspz             NULL);
73a89c9211Schristos }
74a89c9211Schristos 
75a89c9211Schristos /* Single extensions */
76a89c9211Schristos 
OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ * x)77a89c9211Schristos int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
78a89c9211Schristos {
7913d40330Schristos     return X509v3_get_ext_count(x->singleRequestExtensions);
80a89c9211Schristos }
81a89c9211Schristos 
OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ * x,int nid,int lastpos)82a89c9211Schristos int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
83a89c9211Schristos {
8413d40330Schristos     return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos);
85a89c9211Schristos }
86a89c9211Schristos 
OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ * x,const ASN1_OBJECT * obj,int lastpos)87c7da899bSchristos int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj,
88c7da899bSchristos                                int lastpos)
89a89c9211Schristos {
9013d40330Schristos     return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos);
91a89c9211Schristos }
92a89c9211Schristos 
OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ * x,int crit,int lastpos)93a89c9211Schristos int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
94a89c9211Schristos {
95635165faSspz     return (X509v3_get_ext_by_critical
96635165faSspz             (x->singleRequestExtensions, crit, lastpos));
97a89c9211Schristos }
98a89c9211Schristos 
OCSP_ONEREQ_get_ext(OCSP_ONEREQ * x,int loc)99a89c9211Schristos X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
100a89c9211Schristos {
10113d40330Schristos     return X509v3_get_ext(x->singleRequestExtensions, loc);
102a89c9211Schristos }
103a89c9211Schristos 
OCSP_ONEREQ_delete_ext(OCSP_ONEREQ * x,int loc)104a89c9211Schristos X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
105a89c9211Schristos {
10613d40330Schristos     return X509v3_delete_ext(x->singleRequestExtensions, loc);
107a89c9211Schristos }
108a89c9211Schristos 
OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ * x,int nid,int * crit,int * idx)109a89c9211Schristos void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
110a89c9211Schristos {
111a89c9211Schristos     return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
112a89c9211Schristos }
113a89c9211Schristos 
OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ * x,int nid,void * value,int crit,unsigned long flags)114a89c9211Schristos int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
115a89c9211Schristos                              unsigned long flags)
116a89c9211Schristos {
117635165faSspz     return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
118635165faSspz                            flags);
119a89c9211Schristos }
120a89c9211Schristos 
OCSP_ONEREQ_add_ext(OCSP_ONEREQ * x,X509_EXTENSION * ex,int loc)121a89c9211Schristos int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
122a89c9211Schristos {
123a89c9211Schristos     return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
124a89c9211Schristos }
125a89c9211Schristos 
126a89c9211Schristos /* OCSP Basic response */
127a89c9211Schristos 
OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP * x)128a89c9211Schristos int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
129a89c9211Schristos {
13013d40330Schristos     return X509v3_get_ext_count(x->tbsResponseData.responseExtensions);
131a89c9211Schristos }
132a89c9211Schristos 
OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP * x,int nid,int lastpos)133a89c9211Schristos int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
134a89c9211Schristos {
135635165faSspz     return (X509v3_get_ext_by_NID
136c7da899bSchristos             (x->tbsResponseData.responseExtensions, nid, lastpos));
137a89c9211Schristos }
138a89c9211Schristos 
OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP * x,const ASN1_OBJECT * obj,int lastpos)139c7da899bSchristos int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj,
140635165faSspz                                   int lastpos)
141a89c9211Schristos {
142635165faSspz     return (X509v3_get_ext_by_OBJ
143c7da899bSchristos             (x->tbsResponseData.responseExtensions, obj, lastpos));
144a89c9211Schristos }
145a89c9211Schristos 
OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP * x,int crit,int lastpos)146635165faSspz int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
147635165faSspz                                        int lastpos)
148a89c9211Schristos {
149635165faSspz     return (X509v3_get_ext_by_critical
150c7da899bSchristos             (x->tbsResponseData.responseExtensions, crit, lastpos));
151a89c9211Schristos }
152a89c9211Schristos 
OCSP_BASICRESP_get_ext(OCSP_BASICRESP * x,int loc)153a89c9211Schristos X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
154a89c9211Schristos {
15513d40330Schristos     return X509v3_get_ext(x->tbsResponseData.responseExtensions, loc);
156a89c9211Schristos }
157a89c9211Schristos 
OCSP_BASICRESP_delete_ext(OCSP_BASICRESP * x,int loc)158a89c9211Schristos X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
159a89c9211Schristos {
16013d40330Schristos     return X509v3_delete_ext(x->tbsResponseData.responseExtensions, loc);
161a89c9211Schristos }
162a89c9211Schristos 
OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP * x,int nid,int * crit,int * idx)163635165faSspz void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
164635165faSspz                                   int *idx)
165a89c9211Schristos {
166c7da899bSchristos     return X509V3_get_d2i(x->tbsResponseData.responseExtensions, nid, crit,
167635165faSspz                           idx);
168a89c9211Schristos }
169a89c9211Schristos 
OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP * x,int nid,void * value,int crit,unsigned long flags)170635165faSspz int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
171635165faSspz                                 int crit, unsigned long flags)
172a89c9211Schristos {
173c7da899bSchristos     return X509V3_add1_i2d(&x->tbsResponseData.responseExtensions, nid,
174635165faSspz                            value, crit, flags);
175a89c9211Schristos }
176a89c9211Schristos 
OCSP_BASICRESP_add_ext(OCSP_BASICRESP * x,X509_EXTENSION * ex,int loc)177a89c9211Schristos int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
178a89c9211Schristos {
179c7da899bSchristos     return (X509v3_add_ext(&(x->tbsResponseData.responseExtensions), ex, loc)
180635165faSspz             != NULL);
181a89c9211Schristos }
182a89c9211Schristos 
183a89c9211Schristos /* OCSP single response extensions */
184a89c9211Schristos 
OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP * x)185a89c9211Schristos int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
186a89c9211Schristos {
18713d40330Schristos     return X509v3_get_ext_count(x->singleExtensions);
188a89c9211Schristos }
189a89c9211Schristos 
OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP * x,int nid,int lastpos)190a89c9211Schristos int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
191a89c9211Schristos {
19213d40330Schristos     return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos);
193a89c9211Schristos }
194a89c9211Schristos 
OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP * x,const ASN1_OBJECT * obj,int lastpos)195c7da899bSchristos int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj,
196635165faSspz                                    int lastpos)
197a89c9211Schristos {
19813d40330Schristos     return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos);
199a89c9211Schristos }
200a89c9211Schristos 
OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP * x,int crit,int lastpos)201635165faSspz int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
202635165faSspz                                         int lastpos)
203a89c9211Schristos {
20413d40330Schristos     return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos);
205a89c9211Schristos }
206a89c9211Schristos 
OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP * x,int loc)207a89c9211Schristos X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
208a89c9211Schristos {
20913d40330Schristos     return X509v3_get_ext(x->singleExtensions, loc);
210a89c9211Schristos }
211a89c9211Schristos 
OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP * x,int loc)212a89c9211Schristos X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
213a89c9211Schristos {
21413d40330Schristos     return X509v3_delete_ext(x->singleExtensions, loc);
215a89c9211Schristos }
216a89c9211Schristos 
OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP * x,int nid,int * crit,int * idx)217635165faSspz void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
218635165faSspz                                    int *idx)
219a89c9211Schristos {
220a89c9211Schristos     return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
221a89c9211Schristos }
222a89c9211Schristos 
OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP * x,int nid,void * value,int crit,unsigned long flags)223635165faSspz int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
224635165faSspz                                  int crit, unsigned long flags)
225a89c9211Schristos {
226a89c9211Schristos     return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
227a89c9211Schristos }
228a89c9211Schristos 
OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP * x,X509_EXTENSION * ex,int loc)229a89c9211Schristos int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
230a89c9211Schristos {
231a89c9211Schristos     return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
232a89c9211Schristos }
233a89c9211Schristos 
234a89c9211Schristos /* also CRL Entry Extensions */
235a89c9211Schristos 
236a89c9211Schristos /* Nonce handling functions */
237a89c9211Schristos 
238635165faSspz /*
239c7da899bSchristos  * Add a nonce to an extension stack. A nonce can be specified or if NULL a
240635165faSspz  * random nonce will be generated. Note: OpenSSL 0.9.7d and later create an
241635165faSspz  * OCTET STRING containing the nonce, previous versions used the raw nonce.
242a89c9211Schristos  */
243a89c9211Schristos 
ocsp_add1_nonce(STACK_OF (X509_EXTENSION)** exts,unsigned char * val,int len)244635165faSspz static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
245635165faSspz                            unsigned char *val, int len)
246a89c9211Schristos {
247a89c9211Schristos     unsigned char *tmpval;
248a89c9211Schristos     ASN1_OCTET_STRING os;
249a89c9211Schristos     int ret = 0;
250635165faSspz     if (len <= 0)
251635165faSspz         len = OCSP_DEFAULT_NONCE_LENGTH;
252635165faSspz     /*
253635165faSspz      * Create the OCTET STRING manually by writing out the header and
254a89c9211Schristos      * appending the content octets. This avoids an extra memory allocation
255635165faSspz      * operation in some cases. Applications should *NOT* do this because it
256635165faSspz      * relies on library internals.
257a89c9211Schristos      */
258a89c9211Schristos     os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
259c7da899bSchristos     if (os.length < 0)
260c7da899bSchristos         return 0;
261c7da899bSchristos 
262a89c9211Schristos     os.data = OPENSSL_malloc(os.length);
263a89c9211Schristos     if (os.data == NULL)
264a89c9211Schristos         goto err;
265a89c9211Schristos     tmpval = os.data;
266a89c9211Schristos     ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
267a89c9211Schristos     if (val)
268a89c9211Schristos         memcpy(tmpval, val, len);
269a6054fbfSspz     else if (RAND_bytes(tmpval, len) <= 0)
2709cae6e10Schristos         goto err;
271*b0d17251Schristos     if (X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
272*b0d17251Schristos                          &os, 0, X509V3_ADD_REPLACE) <= 0)
273a89c9211Schristos         goto err;
274a89c9211Schristos     ret = 1;
275a89c9211Schristos  err:
276a89c9211Schristos     OPENSSL_free(os.data);
277a89c9211Schristos     return ret;
278a89c9211Schristos }
279a89c9211Schristos 
280a89c9211Schristos /* Add nonce to an OCSP request */
281a89c9211Schristos 
OCSP_request_add1_nonce(OCSP_REQUEST * req,unsigned char * val,int len)282a89c9211Schristos int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
283a89c9211Schristos {
284c7da899bSchristos     return ocsp_add1_nonce(&req->tbsRequest.requestExtensions, val, len);
285a89c9211Schristos }
286a89c9211Schristos 
287a89c9211Schristos /* Same as above but for a response */
288a89c9211Schristos 
OCSP_basic_add1_nonce(OCSP_BASICRESP * resp,unsigned char * val,int len)289a89c9211Schristos int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
290a89c9211Schristos {
291c7da899bSchristos     return ocsp_add1_nonce(&resp->tbsResponseData.responseExtensions, val,
292635165faSspz                            len);
293a89c9211Schristos }
294a89c9211Schristos 
295635165faSspz /*-
296635165faSspz  * Check nonce validity in a request and response.
297a89c9211Schristos  * Return value reflects result:
298a89c9211Schristos  *  1: nonces present and equal.
299a89c9211Schristos  *  2: nonces both absent.
300a89c9211Schristos  *  3: nonce present in response only.
301a89c9211Schristos  *  0: nonces both present and not equal.
302a89c9211Schristos  * -1: nonce in request only.
303a89c9211Schristos  *
304a89c9211Schristos  *  For most responders clients can check return > 0.
305a89c9211Schristos  *  If responder doesn't handle nonces return != 0 may be
306a89c9211Schristos  *  necessary. return == 0 is always an error.
307a89c9211Schristos  */
308a89c9211Schristos 
OCSP_check_nonce(OCSP_REQUEST * req,OCSP_BASICRESP * bs)309a89c9211Schristos int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
310a89c9211Schristos {
311a89c9211Schristos     /*
312a89c9211Schristos      * Since we are only interested in the presence or absence of
313a89c9211Schristos      * the nonce and comparing its value there is no need to use
314a89c9211Schristos      * the X509V3 routines: this way we can avoid them allocating an
315a89c9211Schristos      * ASN1_OCTET_STRING structure for the value which would be
316a89c9211Schristos      * freed immediately anyway.
317a89c9211Schristos      */
318a89c9211Schristos 
319a89c9211Schristos     int req_idx, resp_idx;
320a89c9211Schristos     X509_EXTENSION *req_ext, *resp_ext;
321a89c9211Schristos     req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
322a89c9211Schristos     resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
323a89c9211Schristos     /* Check both absent */
324a89c9211Schristos     if ((req_idx < 0) && (resp_idx < 0))
325a89c9211Schristos         return 2;
326a89c9211Schristos     /* Check in request only */
327a89c9211Schristos     if ((req_idx >= 0) && (resp_idx < 0))
328a89c9211Schristos         return -1;
329a89c9211Schristos     /* Check in response but not request */
330a89c9211Schristos     if ((req_idx < 0) && (resp_idx >= 0))
331a89c9211Schristos         return 3;
332635165faSspz     /*
333635165faSspz      * Otherwise nonce in request and response so retrieve the extensions
334635165faSspz      */
335a89c9211Schristos     req_ext = OCSP_REQUEST_get_ext(req, req_idx);
336a89c9211Schristos     resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
337c7da899bSchristos     if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext),
338c7da899bSchristos                               X509_EXTENSION_get_data(resp_ext)))
339a89c9211Schristos         return 0;
340a89c9211Schristos     return 1;
341a89c9211Schristos }
342a89c9211Schristos 
343635165faSspz /*
344635165faSspz  * Copy the nonce value (if any) from an OCSP request to a response.
345a89c9211Schristos  */
346a89c9211Schristos 
OCSP_copy_nonce(OCSP_BASICRESP * resp,OCSP_REQUEST * req)347a89c9211Schristos int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
348a89c9211Schristos {
349a89c9211Schristos     X509_EXTENSION *req_ext;
350a89c9211Schristos     int req_idx;
351a89c9211Schristos     /* Check for nonce in request */
352a89c9211Schristos     req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
353a89c9211Schristos     /* If no nonce that's OK */
354635165faSspz     if (req_idx < 0)
355635165faSspz         return 2;
356a89c9211Schristos     req_ext = OCSP_REQUEST_get_ext(req, req_idx);
357a89c9211Schristos     return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
358a89c9211Schristos }
359a89c9211Schristos 
OCSP_crlID_new(const char * url,long * n,char * tim)360c7da899bSchristos X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim)
361a89c9211Schristos {
362a89c9211Schristos     X509_EXTENSION *x = NULL;
363a89c9211Schristos     OCSP_CRLID *cid = NULL;
364a89c9211Schristos 
365c7da899bSchristos     if ((cid = OCSP_CRLID_new()) == NULL)
366635165faSspz         goto err;
367635165faSspz     if (url) {
368c7da899bSchristos         if ((cid->crlUrl = ASN1_IA5STRING_new()) == NULL)
369635165faSspz             goto err;
370635165faSspz         if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
371635165faSspz             goto err;
372a89c9211Schristos     }
373635165faSspz     if (n) {
374c7da899bSchristos         if ((cid->crlNum = ASN1_INTEGER_new()) == NULL)
375635165faSspz             goto err;
376635165faSspz         if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
377635165faSspz             goto err;
378a89c9211Schristos     }
379635165faSspz     if (tim) {
380c7da899bSchristos         if ((cid->crlTime = ASN1_GENERALIZEDTIME_new()) == NULL)
381635165faSspz             goto err;
382a89c9211Schristos         if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
383a89c9211Schristos             goto err;
384a89c9211Schristos     }
385a89c9211Schristos     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid);
386a89c9211Schristos  err:
387635165faSspz     OCSP_CRLID_free(cid);
388a89c9211Schristos     return x;
389a89c9211Schristos }
390a89c9211Schristos 
391a89c9211Schristos /*   AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
OCSP_accept_responses_new(char ** oids)392a89c9211Schristos X509_EXTENSION *OCSP_accept_responses_new(char **oids)
393a89c9211Schristos {
394a89c9211Schristos     int nid;
395a89c9211Schristos     STACK_OF(ASN1_OBJECT) *sk = NULL;
396a89c9211Schristos     ASN1_OBJECT *o = NULL;
397a89c9211Schristos     X509_EXTENSION *x = NULL;
398a89c9211Schristos 
399c7da899bSchristos     if ((sk = sk_ASN1_OBJECT_new_null()) == NULL)
400635165faSspz         goto err;
401635165faSspz     while (oids && *oids) {
402a89c9211Schristos         if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
403a89c9211Schristos             sk_ASN1_OBJECT_push(sk, o);
404a89c9211Schristos         oids++;
405a89c9211Schristos     }
406a89c9211Schristos     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk);
407a89c9211Schristos  err:
408635165faSspz     sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
409a89c9211Schristos     return x;
410a89c9211Schristos }
411a89c9211Schristos 
412a89c9211Schristos /*  ArchiveCutoff ::= GeneralizedTime */
OCSP_archive_cutoff_new(char * tim)413a89c9211Schristos X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
414a89c9211Schristos {
415a89c9211Schristos     X509_EXTENSION *x = NULL;
416a89c9211Schristos     ASN1_GENERALIZEDTIME *gt = NULL;
417a89c9211Schristos 
418c7da899bSchristos     if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL)
419635165faSspz         goto err;
420635165faSspz     if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
421635165faSspz         goto err;
422a89c9211Schristos     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt);
423a89c9211Schristos  err:
424635165faSspz     ASN1_GENERALIZEDTIME_free(gt);
425a89c9211Schristos     return x;
426a89c9211Schristos }
427a89c9211Schristos 
428635165faSspz /*
429635165faSspz  * per ACCESS_DESCRIPTION parameter are oids, of which there are currently
430635165faSspz  * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value.  This method
431635165faSspz  * forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
432a89c9211Schristos  */
OCSP_url_svcloc_new(const X509_NAME * issuer,const char ** urls)433*b0d17251Schristos X509_EXTENSION *OCSP_url_svcloc_new(const X509_NAME *issuer, const char **urls)
434a89c9211Schristos {
435a89c9211Schristos     X509_EXTENSION *x = NULL;
436a89c9211Schristos     ASN1_IA5STRING *ia5 = NULL;
437a89c9211Schristos     OCSP_SERVICELOC *sloc = NULL;
438a89c9211Schristos     ACCESS_DESCRIPTION *ad = NULL;
439a89c9211Schristos 
440c7da899bSchristos     if ((sloc = OCSP_SERVICELOC_new()) == NULL)
441635165faSspz         goto err;
442a3b08d93Schristos     X509_NAME_free(sloc->issuer);
443c7da899bSchristos     if ((sloc->issuer = X509_NAME_dup(issuer)) == NULL)
444635165faSspz         goto err;
445c7da899bSchristos     if (urls && *urls
446c7da899bSchristos         && (sloc->locator = sk_ACCESS_DESCRIPTION_new_null()) == NULL)
447635165faSspz         goto err;
448635165faSspz     while (urls && *urls) {
449c7da899bSchristos         if ((ad = ACCESS_DESCRIPTION_new()) == NULL)
450635165faSspz             goto err;
451c7da899bSchristos         if ((ad->method = OBJ_nid2obj(NID_ad_OCSP)) == NULL)
452635165faSspz             goto err;
453c7da899bSchristos         if ((ia5 = ASN1_IA5STRING_new()) == NULL)
454635165faSspz             goto err;
455635165faSspz         if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
456635165faSspz             goto err;
457a3b08d93Schristos         /* ad->location is allocated inside ACCESS_DESCRIPTION_new */
458a89c9211Schristos         ad->location->type = GEN_URI;
459a89c9211Schristos         ad->location->d.ia5 = ia5;
460c7da899bSchristos         ia5 = NULL;
461635165faSspz         if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
462635165faSspz             goto err;
463c7da899bSchristos         ad = NULL;
464a89c9211Schristos         urls++;
465a89c9211Schristos     }
466a89c9211Schristos     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc);
467a89c9211Schristos  err:
468c7da899bSchristos     ASN1_IA5STRING_free(ia5);
469c7da899bSchristos     ACCESS_DESCRIPTION_free(ad);
470635165faSspz     OCSP_SERVICELOC_free(sloc);
471a89c9211Schristos     return x;
472a89c9211Schristos }
473