Instead of just STACK, use STACK_OF(ASN1_OBJECT).
[openssl.git] / crypto / ocsp / ocsp_ext.c
1 /* ocsp_ext.c */
2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3  * project. */
4
5 /* History:
6    This file was transfered to Richard Levitte from CertCo by Kathy
7    Weinhold in mid-spring 2000 to be included in OpenSSL or released
8    as a patch kit. */
9
10 /* ====================================================================
11  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer. 
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in
22  *    the documentation and/or other materials provided with the
23  *    distribution.
24  *
25  * 3. All advertising materials mentioning features or use of this
26  *    software must display the following acknowledgment:
27  *    "This product includes software developed by the OpenSSL Project
28  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
29  *
30  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31  *    endorse or promote products derived from this software without
32  *    prior written permission. For written permission, please contact
33  *    openssl-core@openssl.org.
34  *
35  * 5. Products derived from this software may not be called "OpenSSL"
36  *    nor may "OpenSSL" appear in their names without prior written
37  *    permission of the OpenSSL Project.
38  *
39  * 6. Redistributions of any form whatsoever must retain the following
40  *    acknowledgment:
41  *    "This product includes software developed by the OpenSSL Project
42  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55  * OF THE POSSIBILITY OF SUCH DAMAGE.
56  * ====================================================================
57  *
58  * This product includes cryptographic software written by Eric Young
59  * (eay@cryptsoft.com).  This product includes software written by Tim
60  * Hudson (tjh@cryptsoft.com).
61  *
62  */
63
64 #include <stdio.h>
65 #include <cryptlib.h>
66 #include <openssl/objects.h>
67 #include <openssl/asn1_mac.h>
68 #include <openssl/x509.h>
69 #include <openssl/ocsp.h>
70 #include <openssl/x509v3.h>
71
72 /* Make sure we work well with older variants of OpenSSL */
73 #ifndef OPENSSL_malloc
74 #define OPENSSL_malloc Malloc
75 #endif
76 #ifndef OPENSSL_realloc
77 #define OPENSSL_realloc Realloc
78 #endif
79 #ifndef OPENSSL_free
80 #define OPENSSL_free Free
81 #endif
82
83 /* also CRL Entry Extensions */
84
85 ASN1_STRING *ASN1_STRING_encode(ASN1_STRING *s, int (*i2d)(), 
86                                 char *data, STACK *sk)
87         {
88         int i;
89         unsigned char *p, *b = NULL;
90
91         if (data)
92                 {
93                 if ((i=i2d(data,NULL)) <= 0) goto err;
94                 if (!(b=p=(unsigned char*)OPENSSL_malloc((unsigned int)i)))
95                         goto err;
96                 if (i2d(data, &p) <= 0) goto err;
97                 }
98         else if (sk)
99                 {
100                 if ((i=i2d_ASN1_SET(sk,NULL,i2d,V_ASN1_SEQUENCE,
101                                    V_ASN1_UNIVERSAL,IS_SEQUENCE))<=0) goto err;
102                 if (!(b=p=(unsigned char*)OPENSSL_malloc((unsigned int)i)))
103                         goto err;
104                 if (i2d_ASN1_SET(sk,&p,i2d,V_ASN1_SEQUENCE,
105                                  V_ASN1_UNIVERSAL,IS_SEQUENCE)<=0) goto err;
106                 }
107         else
108                 {
109                 OCSPerr(OCSP_F_ASN1_STRING_ENCODE,OCSP_R_BAD_DATA);
110                 goto err;
111                 }
112         if (!s && !(s = ASN1_STRING_new())) goto err;
113         if (!(ASN1_STRING_set(s, b, i))) goto err;
114         OPENSSL_free(b);
115         return s;
116 err:
117         if (b) OPENSSL_free(b);
118         return NULL;
119         }
120
121 X509_EXTENSION *OCSP_nonce_new(void *p, unsigned int len)
122         {
123         X509_EXTENSION *x=NULL;
124         if (!(x = X509_EXTENSION_new())) goto err;
125         if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_Nonce))) goto err;
126         if (!(ASN1_OCTET_STRING_set(x->value, p, len))) goto err;
127         return x;
128 err:
129         if (x) X509_EXTENSION_free(x);
130         return NULL;
131         }
132
133 X509_EXTENSION *OCSP_crlID_new(char *url, long *n, char *tim)
134         {
135         X509_EXTENSION *x = NULL;
136         OCSP_CRLID *cid = NULL;
137         
138         if (!(cid = OCSP_CRLID_new())) goto err;
139         if (url)
140                 {
141                 if (!(cid->crlUrl = ASN1_IA5STRING_new())) goto err;
142                 if (!(ASN1_STRING_set(cid->crlUrl, url, -1))) goto err;
143                 }
144         if (n)
145                 {
146                 if (!(cid->crlNum = ASN1_INTEGER_new())) goto err;
147                 if (!(ASN1_INTEGER_set(cid->crlNum, *n))) goto err;
148                 }
149         if (time)
150                 {
151                 if (!(cid->crlTime = ASN1_GENERALIZEDTIME_new())) goto err;
152                 if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim))) 
153                         goto err;
154                 }
155         if (!(x = X509_EXTENSION_new())) goto err;
156         if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_CrlID))) goto err;
157         if (!(ASN1_STRING_encode(x->value,i2d_OCSP_CRLID,(char*)cid,NULL)))
158                 goto err;
159         OCSP_CRLID_free(cid);
160         return x;
161 err:
162         if (x) X509_EXTENSION_free(x);
163         if (cid) OCSP_CRLID_free(cid);
164         return NULL;
165         }
166
167 /*   AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
168 X509_EXTENSION *OCSP_accept_responses_new(char **oids)
169         {
170         int nid;
171         STACK_OF(ASN1_OBJECT) *sk = NULL;
172         ASN1_OBJECT *o = NULL;
173         X509_EXTENSION *x = NULL;
174         if (!(sk = sk_new(NULL))) goto err;
175         while (oids && *oids)
176                 {
177                 if ((nid=OBJ_txt2nid(*oids))!=NID_undef&&(o=OBJ_nid2obj(nid))) 
178                         sk_push(sk, (char*) o);
179                 oids++;
180                 }
181         if (!(x = X509_EXTENSION_new())) goto err;
182         if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_acceptableResponses)))
183                 goto err;
184         if (!(ASN1_STRING_encode(x->value,i2d_ASN1_OBJECT,NULL,sk)))
185                 goto err;
186         sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
187         return x;
188 err:
189         if (x) X509_EXTENSION_free(x);
190         if (sk) sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
191         return NULL;
192         }
193
194 /*  ArchiveCutoff ::= GeneralizedTime */
195 X509_EXTENSION *OCSP_archive_cutoff_new(char* tim)
196         {
197         X509_EXTENSION *x=NULL;
198         ASN1_GENERALIZEDTIME *gt = NULL;
199
200         if (!(gt = ASN1_GENERALIZEDTIME_new())) goto err;
201         if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim))) goto err;
202         if (!(x = X509_EXTENSION_new())) goto err;
203         if (!(x->object=OBJ_nid2obj(NID_id_pkix_OCSP_archiveCutoff)))goto err;
204         if (!(ASN1_STRING_encode(x->value,i2d_ASN1_GENERALIZEDTIME,
205                                  (char*)gt,NULL))) goto err;
206         ASN1_GENERALIZEDTIME_free(gt);
207         return x;
208 err:
209         if (gt) ASN1_GENERALIZEDTIME_free(gt);
210         if (x) X509_EXTENSION_free(x);
211         return NULL;
212         }
213
214 /* per ACCESS_DESCRIPTION parameter are oids, of which there are currently
215  * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value.  This
216  * method forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
217  */
218 X509_EXTENSION *OCSP_url_svcloc_new(X509_NAME* issuer, char **urls)
219         {
220         X509_EXTENSION *x = NULL;
221         ASN1_IA5STRING *ia5 = NULL;
222         OCSP_SERVICELOC *sloc = NULL;
223         ACCESS_DESCRIPTION *ad = NULL;
224         
225         if (!(sloc = OCSP_SERVICELOC_new())) goto err;
226         if (!(sloc->issuer = X509_NAME_dup(issuer))) goto err;
227         if (urls && *urls && !(sloc->locator = sk_ACCESS_DESCRIPTION_new(NULL))) goto err;
228         while (urls && *urls)
229                 {
230                 if (!(ad = ACCESS_DESCRIPTION_new())) goto err;
231                 if (!(ad->method=OBJ_nid2obj(NID_ad_OCSP))) goto err;
232                 if (!(ad->location = GENERAL_NAME_new())) goto err;
233                 if (!(ia5 = ASN1_IA5STRING_new())) goto err;
234                 if (!ASN1_STRING_set((ASN1_STRING*)ia5, *urls, -1)) goto err;
235                 ad->location->type = GEN_URI;
236                 ad->location->d.ia5 = ia5;
237                 if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad)) goto err;
238                 urls++;
239                 }
240         if (!(x = X509_EXTENSION_new())) goto err;
241         if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_serviceLocator))) 
242                 goto err;
243         if (!(ASN1_STRING_encode(x->value, i2d_OCSP_SERVICELOC,
244                                  (char*)sloc, NULL))) goto err;
245         OCSP_SERVICELOC_free(sloc);
246         return x;
247 err:
248         if (x) X509_EXTENSION_free(x);
249         if (sloc) OCSP_SERVICELOC_free(sloc);
250         return NULL;
251         }
252
253 int OCSP_extensions_print(BIO *bp,
254                           STACK_OF(X509_EXTENSION) *sk,
255                           char *title)
256         {
257         int i;
258         if (!sk) return 1;
259         if (BIO_printf(bp, "%s:\n", title) <= 0) return 0; 
260         for (i=0; i<sk_X509_EXTENSION_num(sk); i++)
261                 OCSP_extension_print(bp, sk_X509_EXTENSION_value(sk,i), 4);
262         return sk_X509_EXTENSION_num(sk);
263         }
264
265 int OCSP_extension_print(BIO *bp,
266                          X509_EXTENSION *x,
267                          int ind)
268         {
269         int i, j;
270         STACK_OF(ASN1_OBJECT) *sk = NULL;
271         unsigned char *p;
272         OCSP_CRLID *crlid = NULL;
273         OCSP_SERVICELOC *sloc = NULL;
274         ASN1_GENERALIZEDTIME *gt = NULL;
275
276         if (!x) return 1;
277         switch (OBJ_obj2nid(x->object))
278                 {
279                 case NID_id_pkix_OCSP_Nonce:
280                         if (BIO_printf(bp, "%*snonce: ", ind, "") <= 0) 
281                                 goto err;
282                         if (M_ASN1_OCTET_STRING_print(bp, x->value) <= 0)
283                                 goto err;
284                         if (BIO_write(bp, "\n", 1) <= 0) goto err;
285                         break;
286                 case NID_id_pkix_OCSP_CrlID:
287                         if (BIO_printf(bp, "%*scrlId:\n", ind, "") <= 0) 
288                                 goto err;
289                         p = x->value->data;
290                         if (!(d2i_OCSP_CRLID(&crlid, &p, x->value->length)))
291                                 goto err;
292                         if (!OCSP_CRLID_print(bp, crlid, (2*ind))) goto err;
293                         OCSP_CRLID_free(crlid);
294                         break;
295                 case NID_id_pkix_OCSP_acceptableResponses:
296                         if (BIO_printf(bp, 
297                                       "%*sacceptable responses: ", 
298                                       ind, "") <= 0)
299                                 goto err;
300                         p = x->value->data;
301                         if (!(d2i_ASN1_SET(&sk, &p, x->value->length, 
302                                            (char *(*)())d2i_ASN1_OBJECT, 
303                                            (void (*)(void *))ASN1_OBJECT_free,
304                                            V_ASN1_SEQUENCE, 
305                                            V_ASN1_UNIVERSAL)))
306                                 goto err;
307                         for (i = 0; i < sk_num(sk); i++)
308                                 {
309                                 j=OBJ_obj2nid((ASN1_OBJECT*)sk->data[i]);
310                                 if (BIO_printf(bp," %s ",
311                                                (j == NID_undef)?"UNKNOWN":
312                                                            OBJ_nid2ln(j)) <= 0)
313                                           goto err;
314                                 }
315                         if (BIO_write(bp, "\n", 1) <= 0) goto err;
316                         sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
317                         break;
318                 case NID_id_pkix_OCSP_archiveCutoff:
319                         if (BIO_printf(bp, "%*sarchive cutoff: ", ind, "")<=0)
320                                 goto err;
321                         p = x->value->data;
322                         if (!d2i_ASN1_GENERALIZEDTIME(&gt, &p, 
323                                                       x->value->length))
324                                 goto err;
325                         if (!ASN1_GENERALIZEDTIME_print(bp, gt)) goto err;
326                         if (BIO_write(bp, "\n", 1) <= 0) goto err;
327                         ASN1_GENERALIZEDTIME_free(gt);
328                         break;
329                 case NID_id_pkix_OCSP_serviceLocator:
330                   if (BIO_printf(bp, "%*sservice locator:\n", ind, "") <= 0)
331                                 goto err;
332                         p = x->value->data;
333                         if (!d2i_OCSP_SERVICELOC(&sloc, &p, 
334                                                  x->value->length))
335                                 goto err;
336                         if (!OCSP_SERVICELOC_print(bp,sloc,(2*ind))) goto err;
337                         OCSP_SERVICELOC_free(sloc);
338                         break;
339                 case NID_undef:
340                 default:
341                         if (BIO_printf(bp,"%*sunrecognized oid: ",ind,"") <= 0)
342                                 goto err;
343                         break;
344                 }
345         return 1;
346 err:
347         return 0;
348         }