b56425913486e5beaf0edd32a8b5fac5396293fe
[openssl.git] / crypto / ocsp / ocsp_ext.c
1 /* ocsp_ext.c */
2 /*
3  * Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
4  * project.
5  */
6
7 /*
8  * History: This file was transfered to Richard Levitte from CertCo by Kathy
9  * Weinhold in mid-spring 2000 to be included in OpenSSL or released as a
10  * patch kit.
11  */
12
13 /* ====================================================================
14  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  *
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in
25  *    the documentation and/or other materials provided with the
26  *    distribution.
27  *
28  * 3. All advertising materials mentioning features or use of this
29  *    software must display the following acknowledgment:
30  *    "This product includes software developed by the OpenSSL Project
31  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
32  *
33  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
34  *    endorse or promote products derived from this software without
35  *    prior written permission. For written permission, please contact
36  *    openssl-core@openssl.org.
37  *
38  * 5. Products derived from this software may not be called "OpenSSL"
39  *    nor may "OpenSSL" appear in their names without prior written
40  *    permission of the OpenSSL Project.
41  *
42  * 6. Redistributions of any form whatsoever must retain the following
43  *    acknowledgment:
44  *    "This product includes software developed by the OpenSSL Project
45  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
48  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
51  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58  * OF THE POSSIBILITY OF SUCH DAMAGE.
59  * ====================================================================
60  *
61  * This product includes cryptographic software written by Eric Young
62  * (eay@cryptsoft.com).  This product includes software written by Tim
63  * Hudson (tjh@cryptsoft.com).
64  *
65  */
66
67 #include <stdio.h>
68 #include <cryptlib.h>
69 #include <openssl/objects.h>
70 #include <openssl/x509.h>
71 #include <openssl/ocsp.h>
72 #include "ocsp_lcl.h"
73 #include <openssl/rand.h>
74 #include <openssl/x509v3.h>
75
76 /* Standard wrapper functions for extensions */
77
78 /* OCSP request extensions */
79
80 int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x)
81 {
82     return (X509v3_get_ext_count(x->tbsRequest->requestExtensions));
83 }
84
85 int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos)
86 {
87     return (X509v3_get_ext_by_NID
88             (x->tbsRequest->requestExtensions, nid, lastpos));
89 }
90
91 int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, ASN1_OBJECT *obj,
92                                 int lastpos)
93 {
94     return (X509v3_get_ext_by_OBJ
95             (x->tbsRequest->requestExtensions, obj, lastpos));
96 }
97
98 int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos)
99 {
100     return (X509v3_get_ext_by_critical
101             (x->tbsRequest->requestExtensions, crit, lastpos));
102 }
103
104 X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc)
105 {
106     return (X509v3_get_ext(x->tbsRequest->requestExtensions, loc));
107 }
108
109 X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc)
110 {
111     return (X509v3_delete_ext(x->tbsRequest->requestExtensions, loc));
112 }
113
114 void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx)
115 {
116     return X509V3_get_d2i(x->tbsRequest->requestExtensions, nid, crit, idx);
117 }
118
119 int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit,
120                               unsigned long flags)
121 {
122     return X509V3_add1_i2d(&x->tbsRequest->requestExtensions, nid, value,
123                            crit, flags);
124 }
125
126 int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc)
127 {
128     return (X509v3_add_ext(&(x->tbsRequest->requestExtensions), ex, loc) !=
129             NULL);
130 }
131
132 /* Single extensions */
133
134 int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x)
135 {
136     return (X509v3_get_ext_count(x->singleRequestExtensions));
137 }
138
139 int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos)
140 {
141     return (X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos));
142 }
143
144 int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, ASN1_OBJECT *obj, int lastpos)
145 {
146     return (X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos));
147 }
148
149 int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos)
150 {
151     return (X509v3_get_ext_by_critical
152             (x->singleRequestExtensions, crit, lastpos));
153 }
154
155 X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc)
156 {
157     return (X509v3_get_ext(x->singleRequestExtensions, loc));
158 }
159
160 X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc)
161 {
162     return (X509v3_delete_ext(x->singleRequestExtensions, loc));
163 }
164
165 void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx)
166 {
167     return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx);
168 }
169
170 int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit,
171                              unsigned long flags)
172 {
173     return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit,
174                            flags);
175 }
176
177 int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc)
178 {
179     return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL);
180 }
181
182 /* OCSP Basic response */
183
184 int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x)
185 {
186     return (X509v3_get_ext_count(x->tbsResponseData->responseExtensions));
187 }
188
189 int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos)
190 {
191     return (X509v3_get_ext_by_NID
192             (x->tbsResponseData->responseExtensions, nid, lastpos));
193 }
194
195 int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, ASN1_OBJECT *obj,
196                                   int lastpos)
197 {
198     return (X509v3_get_ext_by_OBJ
199             (x->tbsResponseData->responseExtensions, obj, lastpos));
200 }
201
202 int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit,
203                                        int lastpos)
204 {
205     return (X509v3_get_ext_by_critical
206             (x->tbsResponseData->responseExtensions, crit, lastpos));
207 }
208
209 X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc)
210 {
211     return (X509v3_get_ext(x->tbsResponseData->responseExtensions, loc));
212 }
213
214 X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc)
215 {
216     return (X509v3_delete_ext(x->tbsResponseData->responseExtensions, loc));
217 }
218
219 void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit,
220                                   int *idx)
221 {
222     return X509V3_get_d2i(x->tbsResponseData->responseExtensions, nid, crit,
223                           idx);
224 }
225
226 int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value,
227                                 int crit, unsigned long flags)
228 {
229     return X509V3_add1_i2d(&x->tbsResponseData->responseExtensions, nid,
230                            value, crit, flags);
231 }
232
233 int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc)
234 {
235     return (X509v3_add_ext(&(x->tbsResponseData->responseExtensions), ex, loc)
236             != NULL);
237 }
238
239 /* OCSP single response extensions */
240
241 int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x)
242 {
243     return (X509v3_get_ext_count(x->singleExtensions));
244 }
245
246 int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos)
247 {
248     return (X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos));
249 }
250
251 int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, ASN1_OBJECT *obj,
252                                    int lastpos)
253 {
254     return (X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos));
255 }
256
257 int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit,
258                                         int lastpos)
259 {
260     return (X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos));
261 }
262
263 X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc)
264 {
265     return (X509v3_get_ext(x->singleExtensions, loc));
266 }
267
268 X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc)
269 {
270     return (X509v3_delete_ext(x->singleExtensions, loc));
271 }
272
273 void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit,
274                                    int *idx)
275 {
276     return X509V3_get_d2i(x->singleExtensions, nid, crit, idx);
277 }
278
279 int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value,
280                                  int crit, unsigned long flags)
281 {
282     return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags);
283 }
284
285 int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc)
286 {
287     return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL);
288 }
289
290 /* also CRL Entry Extensions */
291
292 /* Nonce handling functions */
293
294 /*
295  * Add a nonce to an extension stack. A nonce can be specificed or if NULL a
296  * random nonce will be generated. Note: OpenSSL 0.9.7d and later create an
297  * OCTET STRING containing the nonce, previous versions used the raw nonce.
298  */
299
300 static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts,
301                            unsigned char *val, int len)
302 {
303     unsigned char *tmpval;
304     ASN1_OCTET_STRING os;
305     int ret = 0;
306     if (len <= 0)
307         len = OCSP_DEFAULT_NONCE_LENGTH;
308     /*
309      * Create the OCTET STRING manually by writing out the header and
310      * appending the content octets. This avoids an extra memory allocation
311      * operation in some cases. Applications should *NOT* do this because it
312      * relies on library internals.
313      */
314     os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING);
315     os.data = OPENSSL_malloc(os.length);
316     if (os.data == NULL)
317         goto err;
318     tmpval = os.data;
319     ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL);
320     if (val)
321         memcpy(tmpval, val, len);
322     else if (RAND_bytes(tmpval, len) <= 0)
323         goto err;
324     if (!X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce,
325                          &os, 0, X509V3_ADD_REPLACE))
326         goto err;
327     ret = 1;
328  err:
329     if (os.data)
330         OPENSSL_free(os.data);
331     return ret;
332 }
333
334 /* Add nonce to an OCSP request */
335
336 int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len)
337 {
338     return ocsp_add1_nonce(&req->tbsRequest->requestExtensions, val, len);
339 }
340
341 /* Same as above but for a response */
342
343 int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len)
344 {
345     return ocsp_add1_nonce(&resp->tbsResponseData->responseExtensions, val,
346                            len);
347 }
348
349 /*-
350  * Check nonce validity in a request and response.
351  * Return value reflects result:
352  *  1: nonces present and equal.
353  *  2: nonces both absent.
354  *  3: nonce present in response only.
355  *  0: nonces both present and not equal.
356  * -1: nonce in request only.
357  *
358  *  For most responders clients can check return > 0.
359  *  If responder doesn't handle nonces return != 0 may be
360  *  necessary. return == 0 is always an error.
361  */
362
363 int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs)
364 {
365     /*
366      * Since we are only interested in the presence or absence of
367      * the nonce and comparing its value there is no need to use
368      * the X509V3 routines: this way we can avoid them allocating an
369      * ASN1_OCTET_STRING structure for the value which would be
370      * freed immediately anyway.
371      */
372
373     int req_idx, resp_idx;
374     X509_EXTENSION *req_ext, *resp_ext;
375     req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
376     resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1);
377     /* Check both absent */
378     if ((req_idx < 0) && (resp_idx < 0))
379         return 2;
380     /* Check in request only */
381     if ((req_idx >= 0) && (resp_idx < 0))
382         return -1;
383     /* Check in response but not request */
384     if ((req_idx < 0) && (resp_idx >= 0))
385         return 3;
386     /*
387      * Otherwise nonce in request and response so retrieve the extensions
388      */
389     req_ext = OCSP_REQUEST_get_ext(req, req_idx);
390     resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx);
391     if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext),
392                               X509_EXTENSION_get_data(resp_ext)))
393         return 0;
394     return 1;
395 }
396
397 /*
398  * Copy the nonce value (if any) from an OCSP request to a response.
399  */
400
401 int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req)
402 {
403     X509_EXTENSION *req_ext;
404     int req_idx;
405     /* Check for nonce in request */
406     req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1);
407     /* If no nonce that's OK */
408     if (req_idx < 0)
409         return 2;
410     req_ext = OCSP_REQUEST_get_ext(req, req_idx);
411     return OCSP_BASICRESP_add_ext(resp, req_ext, -1);
412 }
413
414 X509_EXTENSION *OCSP_crlID_new(char *url, long *n, char *tim)
415 {
416     X509_EXTENSION *x = NULL;
417     OCSP_CRLID *cid = NULL;
418
419     if (!(cid = OCSP_CRLID_new()))
420         goto err;
421     if (url) {
422         if (!(cid->crlUrl = ASN1_IA5STRING_new()))
423             goto err;
424         if (!(ASN1_STRING_set(cid->crlUrl, url, -1)))
425             goto err;
426     }
427     if (n) {
428         if (!(cid->crlNum = ASN1_INTEGER_new()))
429             goto err;
430         if (!(ASN1_INTEGER_set(cid->crlNum, *n)))
431             goto err;
432     }
433     if (tim) {
434         if (!(cid->crlTime = ASN1_GENERALIZEDTIME_new()))
435             goto err;
436         if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim)))
437             goto err;
438     }
439     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid);
440  err:
441     if (cid)
442         OCSP_CRLID_free(cid);
443     return x;
444 }
445
446 /*   AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
447 X509_EXTENSION *OCSP_accept_responses_new(char **oids)
448 {
449     int nid;
450     STACK_OF(ASN1_OBJECT) *sk = NULL;
451     ASN1_OBJECT *o = NULL;
452     X509_EXTENSION *x = NULL;
453
454     if (!(sk = sk_ASN1_OBJECT_new_null()))
455         goto err;
456     while (oids && *oids) {
457         if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid)))
458             sk_ASN1_OBJECT_push(sk, o);
459         oids++;
460     }
461     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk);
462  err:
463     if (sk)
464         sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
465     return x;
466 }
467
468 /*  ArchiveCutoff ::= GeneralizedTime */
469 X509_EXTENSION *OCSP_archive_cutoff_new(char *tim)
470 {
471     X509_EXTENSION *x = NULL;
472     ASN1_GENERALIZEDTIME *gt = NULL;
473
474     if (!(gt = ASN1_GENERALIZEDTIME_new()))
475         goto err;
476     if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim)))
477         goto err;
478     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt);
479  err:
480     if (gt)
481         ASN1_GENERALIZEDTIME_free(gt);
482     return x;
483 }
484
485 /*
486  * per ACCESS_DESCRIPTION parameter are oids, of which there are currently
487  * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value.  This method
488  * forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
489  */
490 X509_EXTENSION *OCSP_url_svcloc_new(X509_NAME *issuer, char **urls)
491 {
492     X509_EXTENSION *x = NULL;
493     ASN1_IA5STRING *ia5 = NULL;
494     OCSP_SERVICELOC *sloc = NULL;
495     ACCESS_DESCRIPTION *ad = NULL;
496
497     if (!(sloc = OCSP_SERVICELOC_new()))
498         goto err;
499     if (!(sloc->issuer = X509_NAME_dup(issuer)))
500         goto err;
501     if (urls && *urls && !(sloc->locator = sk_ACCESS_DESCRIPTION_new_null()))
502         goto err;
503     while (urls && *urls) {
504         if (!(ad = ACCESS_DESCRIPTION_new()))
505             goto err;
506         if (!(ad->method = OBJ_nid2obj(NID_ad_OCSP)))
507             goto err;
508         if (!(ad->location = GENERAL_NAME_new()))
509             goto err;
510         if (!(ia5 = ASN1_IA5STRING_new()))
511             goto err;
512         if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1))
513             goto err;
514         ad->location->type = GEN_URI;
515         ad->location->d.ia5 = ia5;
516         if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad))
517             goto err;
518         urls++;
519     }
520     x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc);
521  err:
522     if (sloc)
523         OCSP_SERVICELOC_free(sloc);
524     return x;
525 }