2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
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
10 /* ====================================================================
11 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
39 * 6. Redistributions of any form whatsoever must retain the following
41 * "This product includes software developed by the OpenSSL Project
42 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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>
72 /* Make sure we work well with older variants of OpenSSL */
73 #ifndef OPENSSL_malloc
74 #define OPENSSL_malloc Malloc
76 #ifndef OPENSSL_realloc
77 #define OPENSSL_realloc Realloc
80 #define OPENSSL_free Free
83 /* also CRL Entry Extensions */
85 ASN1_STRING
*ASN1_STRING_encode(ASN1_STRING
*s
, int (*i2d
)(),
86 char *data
, STACK_OF(ASN1_OBJECT
) *sk
)
89 unsigned char *p
, *b
= NULL
;
93 if ((i
=i2d(data
,NULL
)) <= 0) goto err
;
94 if (!(b
=p
=(unsigned char*)OPENSSL_malloc((unsigned int)i
)))
96 if (i2d(data
, &p
) <= 0) goto err
;
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
)))
104 if (i2d_ASN1_SET_OF_ASN1_OBJECT(sk
,&p
,i2d
,V_ASN1_SEQUENCE
,
105 V_ASN1_UNIVERSAL
,IS_SEQUENCE
)<=0) goto err
;
109 OCSPerr(OCSP_F_ASN1_STRING_ENCODE
,OCSP_R_BAD_DATA
);
112 if (!s
&& !(s
= ASN1_STRING_new())) goto err
;
113 if (!(ASN1_STRING_set(s
, b
, i
))) goto err
;
117 if (b
) OPENSSL_free(b
);
121 X509_EXTENSION
*OCSP_nonce_new(void *p
, unsigned int len
)
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
;
129 if (x
) X509_EXTENSION_free(x
);
133 X509_EXTENSION
*OCSP_crlID_new(char *url
, long *n
, char *tim
)
135 X509_EXTENSION
*x
= NULL
;
136 OCSP_CRLID
*cid
= NULL
;
138 if (!(cid
= OCSP_CRLID_new())) goto err
;
141 if (!(cid
->crlUrl
= ASN1_IA5STRING_new())) goto err
;
142 if (!(ASN1_STRING_set(cid
->crlUrl
, url
, -1))) goto err
;
146 if (!(cid
->crlNum
= ASN1_INTEGER_new())) goto err
;
147 if (!(ASN1_INTEGER_set(cid
->crlNum
, *n
))) goto err
;
151 if (!(cid
->crlTime
= ASN1_GENERALIZEDTIME_new())) goto err
;
152 if (!(ASN1_GENERALIZEDTIME_set_string(cid
->crlTime
, tim
)))
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
)))
159 OCSP_CRLID_free(cid
);
162 if (x
) X509_EXTENSION_free(x
);
163 if (cid
) OCSP_CRLID_free(cid
);
167 /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
168 X509_EXTENSION
*OCSP_accept_responses_new(char **oids
)
171 STACK_OF(ASN1_OBJECT
) *sk
= NULL
;
172 ASN1_OBJECT
*o
= NULL
;
173 X509_EXTENSION
*x
= NULL
;
175 if (!(sk
= sk_ASN1_OBJECT_new(NULL
))) goto err
;
176 while (oids
&& *oids
)
178 if ((nid
=OBJ_txt2nid(*oids
))!=NID_undef
&&(o
=OBJ_nid2obj(nid
)))
179 sk_ASN1_OBJECT_push(sk
, o
);
182 if (!(x
= X509_EXTENSION_new())) goto err
;
183 if (!(x
->object
= OBJ_nid2obj(NID_id_pkix_OCSP_acceptableResponses
)))
185 if (!(ASN1_STRING_encode(x
->value
,i2d_ASN1_OBJECT
,NULL
,sk
)))
187 sk_ASN1_OBJECT_pop_free(sk
, ASN1_OBJECT_free
);
190 if (x
) X509_EXTENSION_free(x
);
191 if (sk
) sk_ASN1_OBJECT_pop_free(sk
, ASN1_OBJECT_free
);
195 /* ArchiveCutoff ::= GeneralizedTime */
196 X509_EXTENSION
*OCSP_archive_cutoff_new(char* tim
)
198 X509_EXTENSION
*x
=NULL
;
199 ASN1_GENERALIZEDTIME
*gt
= NULL
;
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
);
210 if (gt
) ASN1_GENERALIZEDTIME_free(gt
);
211 if (x
) X509_EXTENSION_free(x
);
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.
219 X509_EXTENSION
*OCSP_url_svcloc_new(X509_NAME
* issuer
, char **urls
)
221 X509_EXTENSION
*x
= NULL
;
222 ASN1_IA5STRING
*ia5
= NULL
;
223 OCSP_SERVICELOC
*sloc
= NULL
;
224 ACCESS_DESCRIPTION
*ad
= NULL
;
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
)
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
;
241 if (!(x
= X509_EXTENSION_new())) goto err
;
242 if (!(x
->object
= OBJ_nid2obj(NID_id_pkix_OCSP_serviceLocator
)))
244 if (!(ASN1_STRING_encode(x
->value
, i2d_OCSP_SERVICELOC
,
245 (char*)sloc
, NULL
))) goto err
;
246 OCSP_SERVICELOC_free(sloc
);
249 if (x
) X509_EXTENSION_free(x
);
250 if (sloc
) OCSP_SERVICELOC_free(sloc
);
254 int OCSP_extensions_print(BIO
*bp
,
255 STACK_OF(X509_EXTENSION
) *sk
,
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
);
266 int OCSP_extension_print(BIO
*bp
,
271 STACK_OF(ASN1_OBJECT
) *sk
= NULL
;
273 OCSP_CRLID
*crlid
= NULL
;
274 OCSP_SERVICELOC
*sloc
= NULL
;
275 ASN1_GENERALIZEDTIME
*gt
= NULL
;
278 switch (OBJ_obj2nid(x
->object
))
280 case NID_id_pkix_OCSP_Nonce
:
281 if (BIO_printf(bp
, "%*snonce: ", ind
, "") <= 0)
283 if (M_ASN1_OCTET_STRING_print(bp
, x
->value
) <= 0)
285 if (BIO_write(bp
, "\n", 1) <= 0) goto err
;
287 case NID_id_pkix_OCSP_CrlID
:
288 if (BIO_printf(bp
, "%*scrlId:\n", ind
, "") <= 0)
291 if (!(d2i_OCSP_CRLID(&crlid
, &p
, x
->value
->length
)))
293 if (!OCSP_CRLID_print(bp
, crlid
, (2*ind
))) goto err
;
294 OCSP_CRLID_free(crlid
);
296 case NID_id_pkix_OCSP_acceptableResponses
:
298 "%*sacceptable responses: ",
302 if (!(d2i_ASN1_SET_OF_ASN1_OBJECT(&sk
, &p
, x
->value
->length
,
308 for (i
= 0; i
< sk_ASN1_OBJECT_num(sk
); i
++)
310 j
=OBJ_obj2nid(sk_ASN1_OBJECT_value(sk
,i
));
311 if (BIO_printf(bp
," %s ",
312 (j
== NID_undef
)?"UNKNOWN":
316 if (BIO_write(bp
, "\n", 1) <= 0) goto err
;
317 sk_ASN1_OBJECT_pop_free(sk
, ASN1_OBJECT_free
);
319 case NID_id_pkix_OCSP_archiveCutoff
:
320 if (BIO_printf(bp
, "%*sarchive cutoff: ", ind
, "")<=0)
323 if (!d2i_ASN1_GENERALIZEDTIME(>
, &p
,
326 if (!ASN1_GENERALIZEDTIME_print(bp
, gt
)) goto err
;
327 if (BIO_write(bp
, "\n", 1) <= 0) goto err
;
328 ASN1_GENERALIZEDTIME_free(gt
);
330 case NID_id_pkix_OCSP_serviceLocator
:
331 if (BIO_printf(bp
, "%*sservice locator:\n", ind
, "") <= 0)
334 if (!d2i_OCSP_SERVICELOC(&sloc
, &p
,
337 if (!OCSP_SERVICELOC_print(bp
,sloc
,(2*ind
))) goto err
;
338 OCSP_SERVICELOC_free(sloc
);
342 if (BIO_printf(bp
,"%*sunrecognized oid: ",ind
,"") <= 0)