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/x509.h>
68 #include <openssl/ocsp.h>
69 #include <openssl/rand.h>
70 #include <openssl/x509v3.h>
72 /* Standard wrapper functions for extensions */
74 /* OCSP request extensions */
76 int OCSP_REQUEST_get_ext_count(OCSP_REQUEST
*x
)
78 return(X509v3_get_ext_count(x
->tbsRequest
->requestExtensions
));
81 int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST
*x
, int nid
, int lastpos
)
83 return(X509v3_get_ext_by_NID(x
->tbsRequest
->requestExtensions
,nid
,lastpos
));
86 int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST
*x
, ASN1_OBJECT
*obj
, int lastpos
)
88 return(X509v3_get_ext_by_OBJ(x
->tbsRequest
->requestExtensions
,obj
,lastpos
));
91 int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST
*x
, int crit
, int lastpos
)
93 return(X509v3_get_ext_by_critical(x
->tbsRequest
->requestExtensions
,crit
,lastpos
));
96 X509_EXTENSION
*OCSP_REQUEST_get_ext(OCSP_REQUEST
*x
, int loc
)
98 return(X509v3_get_ext(x
->tbsRequest
->requestExtensions
,loc
));
101 X509_EXTENSION
*OCSP_REQUEST_delete_ext(OCSP_REQUEST
*x
, int loc
)
103 return(X509v3_delete_ext(x
->tbsRequest
->requestExtensions
,loc
));
106 void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST
*x
, int nid
, int *crit
, int *idx
)
108 return X509V3_get_d2i(x
->tbsRequest
->requestExtensions
, nid
, crit
, idx
);
111 int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST
*x
, int nid
, void *value
, int crit
,
114 return X509V3_add1_i2d(&x
->tbsRequest
->requestExtensions
, nid
, value
, crit
, flags
);
117 int OCSP_REQUEST_add_ext(OCSP_REQUEST
*x
, X509_EXTENSION
*ex
, int loc
)
119 return(X509v3_add_ext(&(x
->tbsRequest
->requestExtensions
),ex
,loc
) != NULL
);
122 /* Single extensions */
124 int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ
*x
)
126 return(X509v3_get_ext_count(x
->singleRequestExtensions
));
129 int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ
*x
, int nid
, int lastpos
)
131 return(X509v3_get_ext_by_NID(x
->singleRequestExtensions
,nid
,lastpos
));
134 int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ
*x
, ASN1_OBJECT
*obj
, int lastpos
)
136 return(X509v3_get_ext_by_OBJ(x
->singleRequestExtensions
,obj
,lastpos
));
139 int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ
*x
, int crit
, int lastpos
)
141 return(X509v3_get_ext_by_critical(x
->singleRequestExtensions
,crit
,lastpos
));
144 X509_EXTENSION
*OCSP_ONEREQ_get_ext(OCSP_ONEREQ
*x
, int loc
)
146 return(X509v3_get_ext(x
->singleRequestExtensions
,loc
));
149 X509_EXTENSION
*OCSP_ONEREQ_delete_ext(OCSP_ONEREQ
*x
, int loc
)
151 return(X509v3_delete_ext(x
->singleRequestExtensions
,loc
));
154 void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ
*x
, int nid
, int *crit
, int *idx
)
156 return X509V3_get_d2i(x
->singleRequestExtensions
, nid
, crit
, idx
);
159 int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ
*x
, int nid
, void *value
, int crit
,
162 return X509V3_add1_i2d(&x
->singleRequestExtensions
, nid
, value
, crit
, flags
);
165 int OCSP_ONEREQ_add_ext(OCSP_ONEREQ
*x
, X509_EXTENSION
*ex
, int loc
)
167 return(X509v3_add_ext(&(x
->singleRequestExtensions
),ex
,loc
) != NULL
);
170 /* OCSP Basic response */
172 int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP
*x
)
174 return(X509v3_get_ext_count(x
->tbsResponseData
->responseExtensions
));
177 int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP
*x
, int nid
, int lastpos
)
179 return(X509v3_get_ext_by_NID(x
->tbsResponseData
->responseExtensions
,nid
,lastpos
));
182 int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP
*x
, ASN1_OBJECT
*obj
, int lastpos
)
184 return(X509v3_get_ext_by_OBJ(x
->tbsResponseData
->responseExtensions
,obj
,lastpos
));
187 int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP
*x
, int crit
, int lastpos
)
189 return(X509v3_get_ext_by_critical(x
->tbsResponseData
->responseExtensions
,crit
,lastpos
));
192 X509_EXTENSION
*OCSP_BASICRESP_get_ext(OCSP_BASICRESP
*x
, int loc
)
194 return(X509v3_get_ext(x
->tbsResponseData
->responseExtensions
,loc
));
197 X509_EXTENSION
*OCSP_BASICRESP_delete_ext(OCSP_BASICRESP
*x
, int loc
)
199 return(X509v3_delete_ext(x
->tbsResponseData
->responseExtensions
,loc
));
202 void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP
*x
, int nid
, int *crit
, int *idx
)
204 return X509V3_get_d2i(x
->tbsResponseData
->responseExtensions
, nid
, crit
, idx
);
207 int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP
*x
, int nid
, void *value
, int crit
,
210 return X509V3_add1_i2d(&x
->tbsResponseData
->responseExtensions
, nid
, value
, crit
, flags
);
213 int OCSP_BASICRESP_add_ext(OCSP_BASICRESP
*x
, X509_EXTENSION
*ex
, int loc
)
215 return(X509v3_add_ext(&(x
->tbsResponseData
->responseExtensions
),ex
,loc
) != NULL
);
218 /* OCSP single response extensions */
220 int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP
*x
)
222 return(X509v3_get_ext_count(x
->singleExtensions
));
225 int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP
*x
, int nid
, int lastpos
)
227 return(X509v3_get_ext_by_NID(x
->singleExtensions
,nid
,lastpos
));
230 int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP
*x
, ASN1_OBJECT
*obj
, int lastpos
)
232 return(X509v3_get_ext_by_OBJ(x
->singleExtensions
,obj
,lastpos
));
235 int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP
*x
, int crit
, int lastpos
)
237 return(X509v3_get_ext_by_critical(x
->singleExtensions
,crit
,lastpos
));
240 X509_EXTENSION
*OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP
*x
, int loc
)
242 return(X509v3_get_ext(x
->singleExtensions
,loc
));
245 X509_EXTENSION
*OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP
*x
, int loc
)
247 return(X509v3_delete_ext(x
->singleExtensions
,loc
));
250 void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP
*x
, int nid
, int *crit
, int *idx
)
252 return X509V3_get_d2i(x
->singleExtensions
, nid
, crit
, idx
);
255 int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP
*x
, int nid
, void *value
, int crit
,
258 return X509V3_add1_i2d(&x
->singleExtensions
, nid
, value
, crit
, flags
);
261 int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP
*x
, X509_EXTENSION
*ex
, int loc
)
263 return(X509v3_add_ext(&(x
->singleExtensions
),ex
,loc
) != NULL
);
266 /* also CRL Entry Extensions */
268 ASN1_STRING
*ASN1_STRING_encode(ASN1_STRING
*s
, int (*i2d
)(),
269 char *data
, STACK_OF(ASN1_OBJECT
) *sk
)
272 unsigned char *p
, *b
= NULL
;
276 if ((i
=i2d(data
,NULL
)) <= 0) goto err
;
277 if (!(b
=p
=(unsigned char*)OPENSSL_malloc((unsigned int)i
)))
279 if (i2d(data
, &p
) <= 0) goto err
;
283 if ((i
=i2d_ASN1_SET_OF_ASN1_OBJECT(sk
,NULL
,i2d
,V_ASN1_SEQUENCE
,
284 V_ASN1_UNIVERSAL
,IS_SEQUENCE
))<=0) goto err
;
285 if (!(b
=p
=(unsigned char*)OPENSSL_malloc((unsigned int)i
)))
287 if (i2d_ASN1_SET_OF_ASN1_OBJECT(sk
,&p
,i2d
,V_ASN1_SEQUENCE
,
288 V_ASN1_UNIVERSAL
,IS_SEQUENCE
)<=0) goto err
;
292 OCSPerr(OCSP_F_ASN1_STRING_ENCODE
,OCSP_R_BAD_DATA
);
295 if (!s
&& !(s
= ASN1_STRING_new())) goto err
;
296 if (!(ASN1_STRING_set(s
, b
, i
))) goto err
;
300 if (b
) OPENSSL_free(b
);
304 /* Nonce handling functions */
306 /* Add a nonce to an OCSP request. A nonce can be specificed or if NULL
307 * a random nonce will be generated.
310 int OCSP_request_add1_nonce(OCSP_REQUEST
*req
, unsigned char *val
, int len
)
312 unsigned char *tmpval
;
313 ASN1_OCTET_STRING os
;
315 if (len
<= 0) len
= OCSP_DEFAULT_NONCE_LENGTH
;
316 if (val
) tmpval
= val
;
319 if (!(tmpval
= OPENSSL_malloc(len
))) goto err
;
320 RAND_pseudo_bytes(tmpval
, len
);
324 if(!OCSP_REQUEST_add1_ext_i2d(req
, NID_id_pkix_OCSP_Nonce
,
325 &os
, 0, X509V3_ADD_REPLACE
))
329 if(!val
) OPENSSL_free(tmpval
);
333 /* Check nonce validity in a request and response: the nonce
334 * must be either absent in both or present and equal in both.
336 int OCSP_check_nonce(OCSP_REQUEST
*req
, OCSP_BASICRESP
*bs
)
339 * Since we are only interested in the presence or absence of
340 * the nonce and comparing its value there is no need to use
341 * the X509V3 routines: this way we can avoid them allocating an
342 * ASN1_OCTET_STRING structure for the value which would be
343 * freed immediately anyway.
346 int ret
= 0, req_idx
, resp_idx
;
347 X509_EXTENSION
*req_ext
, *resp_ext
;
348 req_idx
= OCSP_REQUEST_get_ext_by_NID(req
, NID_id_pkix_OCSP_Nonce
, -1);
349 resp_idx
= OCSP_BASICRESP_get_ext_by_NID(bs
, NID_id_pkix_OCSP_Nonce
, -1);
350 /* If both absent its OK */
351 if((req_idx
< 0) && (resp_idx
< 0)) return 1;
352 if((req_idx
< 0) && (resp_idx
>= 0))
354 OCSPerr(OCSP_F_OCSP_CHECK_NONCE
, OCSP_R_NONCE_MISSING_IN_RESPONSE
);
357 if((req_idx
< 0) && (resp_idx
>= 0))
359 OCSPerr(OCSP_F_OCSP_CHECK_NONCE
, OCSP_R_UNEXPECTED_NONCE_IN_RESPONSE
);
362 req_ext
= OCSP_REQUEST_get_ext(req
, req_idx
);
363 resp_ext
= OCSP_BASICRESP_get_ext(bs
, resp_idx
);
364 if(ASN1_OCTET_STRING_cmp(req_ext
->value
, resp_ext
->value
))
366 OCSPerr(OCSP_F_OCSP_CHECK_NONCE
, OCSP_R_NONCE_VALUE_MISMATCH
);
374 /* Copy the nonce value (if any) from an OCSP request to
378 int OCSP_copy_nonce(OCSP_BASICRESP
*resp
, OCSP_REQUEST
*req
)
380 X509_EXTENSION
*req_ext
;
382 /* Check for nonce in request */
383 req_idx
= OCSP_REQUEST_get_ext_by_NID(req
, NID_id_pkix_OCSP_Nonce
, -1);
384 /* If no nonce that's OK */
385 if (req_idx
< 0) return 2;
386 req_ext
= OCSP_REQUEST_get_ext(req
, req_idx
);
387 return OCSP_BASICRESP_add_ext(resp
, req_ext
, -1);
390 X509_EXTENSION
*OCSP_crlID_new(char *url
, long *n
, char *tim
)
392 X509_EXTENSION
*x
= NULL
;
393 OCSP_CRLID
*cid
= NULL
;
395 if (!(cid
= OCSP_CRLID_new())) goto err
;
398 if (!(cid
->crlUrl
= ASN1_IA5STRING_new())) goto err
;
399 if (!(ASN1_STRING_set(cid
->crlUrl
, url
, -1))) goto err
;
403 if (!(cid
->crlNum
= ASN1_INTEGER_new())) goto err
;
404 if (!(ASN1_INTEGER_set(cid
->crlNum
, *n
))) goto err
;
408 if (!(cid
->crlTime
= ASN1_GENERALIZEDTIME_new())) goto err
;
409 if (!(ASN1_GENERALIZEDTIME_set_string(cid
->crlTime
, tim
)))
412 if (!(x
= X509_EXTENSION_new())) goto err
;
413 if (!(x
->object
= OBJ_nid2obj(NID_id_pkix_OCSP_CrlID
))) goto err
;
414 if (!(ASN1_STRING_encode(x
->value
,i2d_OCSP_CRLID
,(char*)cid
,NULL
)))
416 OCSP_CRLID_free(cid
);
419 if (x
) X509_EXTENSION_free(x
);
420 if (cid
) OCSP_CRLID_free(cid
);
424 /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */
425 X509_EXTENSION
*OCSP_accept_responses_new(char **oids
)
428 STACK_OF(ASN1_OBJECT
) *sk
= NULL
;
429 ASN1_OBJECT
*o
= NULL
;
430 X509_EXTENSION
*x
= NULL
;
432 if (!(sk
= sk_ASN1_OBJECT_new(NULL
))) goto err
;
433 while (oids
&& *oids
)
435 if ((nid
=OBJ_txt2nid(*oids
))!=NID_undef
&&(o
=OBJ_nid2obj(nid
)))
436 sk_ASN1_OBJECT_push(sk
, o
);
439 if (!(x
= X509_EXTENSION_new())) goto err
;
440 if (!(x
->object
= OBJ_nid2obj(NID_id_pkix_OCSP_acceptableResponses
)))
442 if (!(ASN1_STRING_encode(x
->value
,i2d_ASN1_OBJECT
,NULL
,sk
)))
444 sk_ASN1_OBJECT_pop_free(sk
, ASN1_OBJECT_free
);
447 if (x
) X509_EXTENSION_free(x
);
448 if (sk
) sk_ASN1_OBJECT_pop_free(sk
, ASN1_OBJECT_free
);
452 /* ArchiveCutoff ::= GeneralizedTime */
453 X509_EXTENSION
*OCSP_archive_cutoff_new(char* tim
)
455 X509_EXTENSION
*x
=NULL
;
456 ASN1_GENERALIZEDTIME
*gt
= NULL
;
458 if (!(gt
= ASN1_GENERALIZEDTIME_new())) goto err
;
459 if (!(ASN1_GENERALIZEDTIME_set_string(gt
, tim
))) goto err
;
460 if (!(x
= X509_EXTENSION_new())) goto err
;
461 if (!(x
->object
=OBJ_nid2obj(NID_id_pkix_OCSP_archiveCutoff
)))goto err
;
462 if (!(ASN1_STRING_encode(x
->value
,i2d_ASN1_GENERALIZEDTIME
,
463 (char*)gt
,NULL
))) goto err
;
464 ASN1_GENERALIZEDTIME_free(gt
);
467 if (gt
) ASN1_GENERALIZEDTIME_free(gt
);
468 if (x
) X509_EXTENSION_free(x
);
472 /* per ACCESS_DESCRIPTION parameter are oids, of which there are currently
473 * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value. This
474 * method forces NID_ad_ocsp and uniformResourceLocator [6] IA5String.
476 X509_EXTENSION
*OCSP_url_svcloc_new(X509_NAME
* issuer
, char **urls
)
478 X509_EXTENSION
*x
= NULL
;
479 ASN1_IA5STRING
*ia5
= NULL
;
480 OCSP_SERVICELOC
*sloc
= NULL
;
481 ACCESS_DESCRIPTION
*ad
= NULL
;
483 if (!(sloc
= OCSP_SERVICELOC_new())) goto err
;
484 if (!(sloc
->issuer
= X509_NAME_dup(issuer
))) goto err
;
485 if (urls
&& *urls
&& !(sloc
->locator
= sk_ACCESS_DESCRIPTION_new(NULL
))) goto err
;
486 while (urls
&& *urls
)
488 if (!(ad
= ACCESS_DESCRIPTION_new())) goto err
;
489 if (!(ad
->method
=OBJ_nid2obj(NID_ad_OCSP
))) goto err
;
490 if (!(ad
->location
= GENERAL_NAME_new())) goto err
;
491 if (!(ia5
= ASN1_IA5STRING_new())) goto err
;
492 if (!ASN1_STRING_set((ASN1_STRING
*)ia5
, *urls
, -1)) goto err
;
493 ad
->location
->type
= GEN_URI
;
494 ad
->location
->d
.ia5
= ia5
;
495 if (!sk_ACCESS_DESCRIPTION_push(sloc
->locator
, ad
)) goto err
;
498 if (!(x
= X509_EXTENSION_new())) goto err
;
499 if (!(x
->object
= OBJ_nid2obj(NID_id_pkix_OCSP_serviceLocator
)))
501 if (!(ASN1_STRING_encode(x
->value
, i2d_OCSP_SERVICELOC
,
502 (char*)sloc
, NULL
))) goto err
;
503 OCSP_SERVICELOC_free(sloc
);
506 if (x
) X509_EXTENSION_free(x
);
507 if (sloc
) OCSP_SERVICELOC_free(sloc
);