"Andrew W. Gray" <agray@iconsinc.com> says /GD is no longer a valid
[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_OF(ASN1_OBJECT) *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_OF_ASN1_OBJECT(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_OF_ASN1_OBJECT(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 (tim)
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
175         if (!(sk = sk_ASN1_OBJECT_new(NULL))) goto err;
176         while (oids && *oids)
177                 {
178                 if ((nid=OBJ_txt2nid(*oids))!=NID_undef&&(o=OBJ_nid2obj(nid))) 
179                         sk_ASN1_OBJECT_push(sk, o);
180                 oids++;
181                 }
182         if (!(x = X509_EXTENSION_new())) goto err;
183         if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_acceptableResponses)))
184                 goto err;
185         if (!(ASN1_STRING_encode(x->value,i2d_ASN1_OBJECT,NULL,sk)))
186                 goto err;
187         sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
188         return x;
189 err:
190         if (x) X509_EXTENSION_free(x);
191         if (sk) sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
192         return NULL;
193         }
194
195 /*  ArchiveCutoff ::= GeneralizedTime */
196 X509_EXTENSION *OCSP_archive_cutoff_new(char* tim)
197         {
198         X509_EXTENSION *x=NULL;
199         ASN1_GENERALIZEDTIME *gt = NULL;
200
201         if (!(gt = ASN1_GENERALIZEDTIME_new())) goto err;
202         if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim))) goto err;
203         if (!(x = X509_EXTENSION_new())) goto err;
204         if (!(x->object=OBJ_nid2obj(NID_id_pkix_OCSP_archiveCutoff)))goto err;
205         if (!(ASN1_STRING_encode(x->value,i2d_ASN1_GENERALIZEDTIME,
206                                  (char*)gt,NULL))) goto err;
207         ASN1_GENERALIZEDTIME_free(gt);
208         return x;
209 err:
210         if (gt) ASN1_GENERALIZEDTIME_free(gt);
211         if (x) X509_EXTENSION_free(x);
212         return NULL;
213         }
214
215 /* per ACCESS_DESCRIPTION parameter are oids, of which there are currently
216  * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value.  This
217  * method forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
218  */
219 X509_EXTENSION *OCSP_url_svcloc_new(X509_NAME* issuer, char **urls)
220         {
221         X509_EXTENSION *x = NULL;
222         ASN1_IA5STRING *ia5 = NULL;
223         OCSP_SERVICELOC *sloc = NULL;
224         ACCESS_DESCRIPTION *ad = NULL;
225         
226         if (!(sloc = OCSP_SERVICELOC_new())) goto err;
227         if (!(sloc->issuer = X509_NAME_dup(issuer))) goto err;
228         if (urls && *urls && !(sloc->locator = sk_ACCESS_DESCRIPTION_new(NULL))) goto err;
229         while (urls && *urls)
230                 {
231                 if (!(ad = ACCESS_DESCRIPTION_new())) goto err;
232                 if (!(ad->method=OBJ_nid2obj(NID_ad_OCSP))) goto err;
233                 if (!(ad->location = GENERAL_NAME_new())) goto err;
234                 if (!(ia5 = ASN1_IA5STRING_new())) goto err;
235                 if (!ASN1_STRING_set((ASN1_STRING*)ia5, *urls, -1)) goto err;
236                 ad->location->type = GEN_URI;
237                 ad->location->d.ia5 = ia5;
238                 if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad)) goto err;
239                 urls++;
240                 }
241         if (!(x = X509_EXTENSION_new())) goto err;
242         if (!(x->object = OBJ_nid2obj(NID_id_pkix_OCSP_serviceLocator))) 
243                 goto err;
244         if (!(ASN1_STRING_encode(x->value, i2d_OCSP_SERVICELOC,
245                                  (char*)sloc, NULL))) goto err;
246         OCSP_SERVICELOC_free(sloc);
247         return x;
248 err:
249         if (x) X509_EXTENSION_free(x);
250         if (sloc) OCSP_SERVICELOC_free(sloc);
251         return NULL;
252         }
253
254 int OCSP_extensions_print(BIO *bp,
255                           STACK_OF(X509_EXTENSION) *sk,
256                           char *title)
257         {
258         int i;
259         if (!sk) return 1;
260         if (BIO_printf(bp, "%s:\n", title) <= 0) return 0; 
261         for (i=0; i<sk_X509_EXTENSION_num(sk); i++)
262                 OCSP_extension_print(bp, sk_X509_EXTENSION_value(sk,i), 4);
263         return sk_X509_EXTENSION_num(sk);
264         }
265
266 int OCSP_extension_print(BIO *bp,
267                          X509_EXTENSION *x,
268                          int ind)
269         {
270         int i, j;
271         STACK_OF(ASN1_OBJECT) *sk = NULL;
272         unsigned char *p;
273         OCSP_CRLID *crlid = NULL;
274         OCSP_SERVICELOC *sloc = NULL;
275         ASN1_GENERALIZEDTIME *gt = NULL;
276
277         if (!x) return 1;
278         switch (OBJ_obj2nid(x->object))
279                 {
280                 case NID_id_pkix_OCSP_Nonce:
281                         if (BIO_printf(bp, "%*snonce: ", ind, "") <= 0) 
282                                 goto err;
283                         if (M_ASN1_OCTET_STRING_print(bp, x->value) <= 0)
284                                 goto err;
285                         if (BIO_write(bp, "\n", 1) <= 0) goto err;
286                         break;
287                 case NID_id_pkix_OCSP_CrlID:
288                         if (BIO_printf(bp, "%*scrlId:\n", ind, "") <= 0) 
289                                 goto err;
290                         p = x->value->data;
291                         if (!(d2i_OCSP_CRLID(&crlid, &p, x->value->length)))
292                                 goto err;
293                         if (!OCSP_CRLID_print(bp, crlid, (2*ind))) goto err;
294                         OCSP_CRLID_free(crlid);
295                         break;
296                 case NID_id_pkix_OCSP_acceptableResponses:
297                         if (BIO_printf(bp, 
298                                       "%*sacceptable responses: ", 
299                                       ind, "") <= 0)
300                                 goto err;
301                         p = x->value->data;
302                         if (!(d2i_ASN1_SET_OF_ASN1_OBJECT(&sk, &p, x->value->length, 
303                                            d2i_ASN1_OBJECT, 
304                                            ASN1_OBJECT_free,
305                                            V_ASN1_SEQUENCE, 
306                                            V_ASN1_UNIVERSAL)))
307                                 goto err;
308                         for (i = 0; i < sk_ASN1_OBJECT_num(sk); i++)
309                                 {
310                                 j=OBJ_obj2nid(sk_ASN1_OBJECT_value(sk,i));
311                                 if (BIO_printf(bp," %s ",
312                                                (j == NID_undef)?"UNKNOWN":
313                                                            OBJ_nid2ln(j)) <= 0)
314                                           goto err;
315                                 }
316                         if (BIO_write(bp, "\n", 1) <= 0) goto err;
317                         sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free);
318                         break;
319                 case NID_id_pkix_OCSP_archiveCutoff:
320                         if (BIO_printf(bp, "%*sarchive cutoff: ", ind, "")<=0)
321                                 goto err;
322                         p = x->value->data;
323                         if (!d2i_ASN1_GENERALIZEDTIME(&gt, &p, 
324                                                       x->value->length))
325                                 goto err;
326                         if (!ASN1_GENERALIZEDTIME_print(bp, gt)) goto err;
327                         if (BIO_write(bp, "\n", 1) <= 0) goto err;
328                         ASN1_GENERALIZEDTIME_free(gt);
329                         break;
330                 case NID_id_pkix_OCSP_serviceLocator:
331                   if (BIO_printf(bp, "%*sservice locator:\n", ind, "") <= 0)
332                                 goto err;
333                         p = x->value->data;
334                         if (!d2i_OCSP_SERVICELOC(&sloc, &p, 
335                                                  x->value->length))
336                                 goto err;
337                         if (!OCSP_SERVICELOC_print(bp,sloc,(2*ind))) goto err;
338                         OCSP_SERVICELOC_free(sloc);
339                         break;
340                 case NID_undef:
341                 default:
342                         if (BIO_printf(bp,"%*sunrecognized oid: ",ind,"") <= 0)
343                                 goto err;
344                         break;
345                 }
346         return 1;
347 err:
348         return 0;
349         }