Redo OCSP response printing. Remove duplicate or
[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