1 /* crypto/x509/x509_lu.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
64 static STACK *x509_store_meth=NULL;
65 static STACK *x509_store_ctx_meth=NULL;
67 X509_LOOKUP *X509_LOOKUP_new(method)
68 X509_LOOKUP_METHOD *method;
72 ret=(X509_LOOKUP *)Malloc(sizeof(X509_LOOKUP));
73 if (ret == NULL) return(NULL);
78 ret->method_data=NULL;
80 if ((method->new_item != NULL) && !method->new_item(ret))
88 void X509_LOOKUP_free(ctx)
91 if (ctx == NULL) return;
92 if ( (ctx->method != NULL) &&
93 (ctx->method->free != NULL))
94 ctx->method->free(ctx);
98 int X509_LOOKUP_init(ctx)
101 if (ctx->method == NULL) return(0);
102 if (ctx->method->init != NULL)
103 return(ctx->method->init(ctx));
108 int X509_LOOKUP_shutdown(ctx)
111 if (ctx->method == NULL) return(0);
112 if (ctx->method->init != NULL)
113 return(ctx->method->shutdown(ctx));
118 int X509_LOOKUP_ctrl(ctx,cmd,argc,argl,ret)
125 if (ctx->method == NULL) return(-1);
126 if (ctx->method->ctrl != NULL)
127 return(ctx->method->ctrl(ctx,cmd,argc,argl,ret));
132 int X509_LOOKUP_by_subject(ctx,type,name,ret)
138 if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))
139 return(X509_LU_FAIL);
140 if (ctx->skip) return(0);
141 return(ctx->method->get_by_subject(ctx,type,name,ret));
144 int X509_LOOKUP_by_issuer_serial(ctx,type,name,serial,ret)
148 ASN1_INTEGER *serial;
151 if ((ctx->method == NULL) ||
152 (ctx->method->get_by_issuer_serial == NULL))
153 return(X509_LU_FAIL);
154 return(ctx->method->get_by_issuer_serial(ctx,type,name,serial,ret));
157 int X509_LOOKUP_by_fingerprint(ctx,type,bytes,len,ret)
160 unsigned char *bytes;
164 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL))
165 return(X509_LU_FAIL);
166 return(ctx->method->get_by_fingerprint(ctx,type,bytes,len,ret));
169 int X509_LOOKUP_by_alias(ctx,type,str,len,ret)
176 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL))
177 return(X509_LU_FAIL);
178 return(ctx->method->get_by_alias(ctx,str,len,ret));
181 static unsigned long x509_object_hash(a)
189 h=X509_NAME_hash(a->data.x509->cert_info->subject);
192 h=X509_NAME_hash(a->data.crl->crl->issuer);
200 static int x509_object_cmp(a,b)
205 ret=(a->type - b->type);
206 if (ret) return(ret);
210 ret=X509_subject_name_cmp(a->data.x509,b->data.x509);
213 ret=X509_CRL_cmp(a->data.crl,b->data.crl);
221 X509_STORE *X509_STORE_new()
225 if ((ret=(X509_STORE *)Malloc(sizeof(X509_STORE))) == NULL)
227 ret->certs=lh_new(x509_object_hash,x509_object_cmp);
229 ret->get_cert_methods=sk_new_null();
232 memset(&ret->ex_data,0,sizeof(CRYPTO_EX_DATA));
237 static void cleanup(a)
240 if (a->type == X509_LU_X509)
242 X509_free(a->data.x509);
244 else if (a->type == X509_LU_CRL)
246 X509_CRL_free(a->data.crl);
254 void X509_STORE_free(vfy)
261 sk=vfy->get_cert_methods;
262 for (i=0; i<sk_num(sk); i++)
264 lu=(X509_LOOKUP *)sk_value(sk,i);
265 X509_LOOKUP_shutdown(lu);
266 X509_LOOKUP_free(lu);
270 CRYPTO_free_ex_data(x509_store_meth,(char *)vfy,&vfy->ex_data);
271 lh_doall(vfy->certs,cleanup);
276 X509_LOOKUP *X509_STORE_add_lookup(v,m)
278 X509_LOOKUP_METHOD *m;
284 sk=v->get_cert_methods;
285 for (i=0; i<sk_num(sk); i++)
287 lu=(X509_LOOKUP *)sk_value(sk,i);
294 lu=X509_LOOKUP_new(m);
300 if (sk_push(v->get_cert_methods,(char *)lu))
304 X509_LOOKUP_free(lu);
310 int X509_STORE_get_by_subject(vs,type,name,ret)
316 X509_STORE *ctx=vs->ctx;
318 X509_OBJECT stmp,*tmp;
321 tmp=X509_OBJECT_retrive_by_subject(ctx->certs,type,name);
325 for (i=vs->current_method; i<sk_num(ctx->get_cert_methods); i++)
327 lu=(X509_LOOKUP *)sk_value(ctx->get_cert_methods,i);
328 j=X509_LOOKUP_by_subject(lu,type,name,&stmp);
331 vs->current_method=j;
340 vs->current_method=0;
345 /* if (ret->data.ptr != NULL)
346 X509_OBJECT_free_contents(ret); */
349 ret->data.ptr=tmp->data.ptr;
351 X509_OBJECT_up_ref_count(ret);
356 void X509_OBJECT_up_ref_count(a)
362 CRYPTO_add(&a->data.x509->references,1,CRYPTO_LOCK_X509);
365 CRYPTO_add(&a->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
370 void X509_OBJECT_free_contents(a)
376 X509_free(a->data.x509);
379 X509_CRL_free(a->data.crl);
384 X509_OBJECT *X509_OBJECT_retrive_by_subject(h,type,name)
389 X509_OBJECT stmp,*tmp;
393 X509_CRL_INFO crl_info_s;
399 stmp.data.x509= &x509_s;
400 x509_s.cert_info= &cinf_s;
404 stmp.data.crl= &crl_s;
405 crl_s.crl= &crl_info_s;
406 crl_info_s.issuer=name;
412 tmp=(X509_OBJECT *)lh_retrieve(h,(char *)&stmp);
416 void X509_STORE_CTX_init(ctx,store,x509,chain)
423 ctx->current_method=0;
425 ctx->untrusted=chain;
426 ctx->last_untrusted=0;
431 ctx->current_cert=NULL;
432 memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA));
435 void X509_STORE_CTX_cleanup(ctx)
438 if (ctx->chain != NULL)
440 sk_pop_free(ctx->chain,X509_free);
443 CRYPTO_free_ex_data(x509_store_ctx_meth,(char *)ctx,&(ctx->ex_data));
444 memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));