x509_lu.c revision 68651
1/* crypto/x509/x509_lu.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 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. 8 * 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). 15 * 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. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 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)" 40 * 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 51 * SUCH DAMAGE. 52 * 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.] 57 */ 58 59#include <stdio.h> 60#include "cryptlib.h" 61#include <openssl/lhash.h> 62#include <openssl/x509.h> 63 64static STACK_OF(CRYPTO_EX_DATA_FUNCS) *x509_store_meth=NULL; 65 66X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method) 67 { 68 X509_LOOKUP *ret; 69 70 ret=(X509_LOOKUP *)OPENSSL_malloc(sizeof(X509_LOOKUP)); 71 if (ret == NULL) return NULL; 72 73 ret->init=0; 74 ret->skip=0; 75 ret->method=method; 76 ret->method_data=NULL; 77 ret->store_ctx=NULL; 78 if ((method->new_item != NULL) && !method->new_item(ret)) 79 { 80 OPENSSL_free(ret); 81 return NULL; 82 } 83 return ret; 84 } 85 86void X509_LOOKUP_free(X509_LOOKUP *ctx) 87 { 88 if (ctx == NULL) return; 89 if ( (ctx->method != NULL) && 90 (ctx->method->free != NULL)) 91 ctx->method->free(ctx); 92 OPENSSL_free(ctx); 93 } 94 95int X509_LOOKUP_init(X509_LOOKUP *ctx) 96 { 97 if (ctx->method == NULL) return 0; 98 if (ctx->method->init != NULL) 99 return ctx->method->init(ctx); 100 else 101 return 1; 102 } 103 104int X509_LOOKUP_shutdown(X509_LOOKUP *ctx) 105 { 106 if (ctx->method == NULL) return 0; 107 if (ctx->method->shutdown != NULL) 108 return ctx->method->shutdown(ctx); 109 else 110 return 1; 111 } 112 113int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, 114 char **ret) 115 { 116 if (ctx->method == NULL) return -1; 117 if (ctx->method->ctrl != NULL) 118 return ctx->method->ctrl(ctx,cmd,argc,argl,ret); 119 else 120 return 1; 121 } 122 123int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name, 124 X509_OBJECT *ret) 125 { 126 if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL)) 127 return X509_LU_FAIL; 128 if (ctx->skip) return 0; 129 return ctx->method->get_by_subject(ctx,type,name,ret); 130 } 131 132int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name, 133 ASN1_INTEGER *serial, X509_OBJECT *ret) 134 { 135 if ((ctx->method == NULL) || 136 (ctx->method->get_by_issuer_serial == NULL)) 137 return X509_LU_FAIL; 138 return ctx->method->get_by_issuer_serial(ctx,type,name,serial,ret); 139 } 140 141int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type, 142 unsigned char *bytes, int len, X509_OBJECT *ret) 143 { 144 if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL)) 145 return X509_LU_FAIL; 146 return ctx->method->get_by_fingerprint(ctx,type,bytes,len,ret); 147 } 148 149int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str, int len, 150 X509_OBJECT *ret) 151 { 152 if ((ctx->method == NULL) || (ctx->method->get_by_alias == NULL)) 153 return X509_LU_FAIL; 154 return ctx->method->get_by_alias(ctx,type,str,len,ret); 155 } 156 157 158static int x509_object_cmp(const X509_OBJECT * const *a, const X509_OBJECT * const *b) 159 { 160 int ret; 161 162 ret=((*a)->type - (*b)->type); 163 if (ret) return ret; 164 switch ((*a)->type) 165 { 166 case X509_LU_X509: 167 ret=X509_subject_name_cmp((*a)->data.x509,(*b)->data.x509); 168 break; 169 case X509_LU_CRL: 170 ret=X509_CRL_cmp((*a)->data.crl,(*b)->data.crl); 171 break; 172 default: 173 /* abort(); */ 174 return 0; 175 } 176 return ret; 177 } 178 179X509_STORE *X509_STORE_new(void) 180 { 181 X509_STORE *ret; 182 183 if ((ret=(X509_STORE *)OPENSSL_malloc(sizeof(X509_STORE))) == NULL) 184 return NULL; 185 ret->objs = sk_X509_OBJECT_new(x509_object_cmp); 186 ret->cache=1; 187 ret->get_cert_methods=sk_X509_LOOKUP_new_null(); 188 ret->verify=NULL; 189 ret->verify_cb=NULL; 190 memset(&ret->ex_data,0,sizeof(CRYPTO_EX_DATA)); 191 ret->references=1; 192 ret->depth=0; 193 return ret; 194 } 195 196static void cleanup(X509_OBJECT *a) 197 { 198 if (a->type == X509_LU_X509) 199 { 200 X509_free(a->data.x509); 201 } 202 else if (a->type == X509_LU_CRL) 203 { 204 X509_CRL_free(a->data.crl); 205 } 206 else 207 { 208 /* abort(); */ 209 } 210 211 OPENSSL_free(a); 212 } 213 214void X509_STORE_free(X509_STORE *vfy) 215 { 216 int i; 217 STACK_OF(X509_LOOKUP) *sk; 218 X509_LOOKUP *lu; 219 220 if (vfy == NULL) 221 return; 222 223 sk=vfy->get_cert_methods; 224 for (i=0; i<sk_X509_LOOKUP_num(sk); i++) 225 { 226 lu=sk_X509_LOOKUP_value(sk,i); 227 X509_LOOKUP_shutdown(lu); 228 X509_LOOKUP_free(lu); 229 } 230 sk_X509_LOOKUP_free(sk); 231 sk_X509_OBJECT_pop_free(vfy->objs, cleanup); 232 233 CRYPTO_free_ex_data(x509_store_meth,vfy,&vfy->ex_data); 234 OPENSSL_free(vfy); 235 } 236 237X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m) 238 { 239 int i; 240 STACK_OF(X509_LOOKUP) *sk; 241 X509_LOOKUP *lu; 242 243 sk=v->get_cert_methods; 244 for (i=0; i<sk_X509_LOOKUP_num(sk); i++) 245 { 246 lu=sk_X509_LOOKUP_value(sk,i); 247 if (m == lu->method) 248 { 249 return lu; 250 } 251 } 252 /* a new one */ 253 lu=X509_LOOKUP_new(m); 254 if (lu == NULL) 255 return NULL; 256 else 257 { 258 lu->store_ctx=v; 259 if (sk_X509_LOOKUP_push(v->get_cert_methods,lu)) 260 return lu; 261 else 262 { 263 X509_LOOKUP_free(lu); 264 return NULL; 265 } 266 } 267 } 268 269int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name, 270 X509_OBJECT *ret) 271 { 272 X509_STORE *ctx=vs->ctx; 273 X509_LOOKUP *lu; 274 X509_OBJECT stmp,*tmp; 275 int i,j; 276 277 tmp=X509_OBJECT_retrieve_by_subject(ctx->objs,type,name); 278 279 if (tmp == NULL) 280 { 281 for (i=vs->current_method; i<sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) 282 { 283 lu=sk_X509_LOOKUP_value(ctx->get_cert_methods,i); 284 j=X509_LOOKUP_by_subject(lu,type,name,&stmp); 285 if (j < 0) 286 { 287 vs->current_method=j; 288 return j; 289 } 290 else if (j) 291 { 292 tmp= &stmp; 293 break; 294 } 295 } 296 vs->current_method=0; 297 if (tmp == NULL) 298 return 0; 299 } 300 301/* if (ret->data.ptr != NULL) 302 X509_OBJECT_free_contents(ret); */ 303 304 ret->type=tmp->type; 305 ret->data.ptr=tmp->data.ptr; 306 307 X509_OBJECT_up_ref_count(ret); 308 309 return 1; 310 } 311 312int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) 313 { 314 X509_OBJECT *obj; 315 int ret=1; 316 317 if (x == NULL) return 0; 318 obj=(X509_OBJECT *)OPENSSL_malloc(sizeof(X509_OBJECT)); 319 if (obj == NULL) 320 { 321 X509err(X509_F_X509_STORE_ADD_CERT,ERR_R_MALLOC_FAILURE); 322 return 0; 323 } 324 obj->type=X509_LU_X509; 325 obj->data.x509=x; 326 327 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); 328 329 X509_OBJECT_up_ref_count(obj); 330 331 332 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) 333 { 334 X509_OBJECT_free_contents(obj); 335 OPENSSL_free(obj); 336 X509err(X509_F_X509_STORE_ADD_CERT,X509_R_CERT_ALREADY_IN_HASH_TABLE); 337 ret=0; 338 } 339 else sk_X509_OBJECT_push(ctx->objs, obj); 340 341 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); 342 343 return ret; 344 } 345 346int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) 347 { 348 X509_OBJECT *obj; 349 int ret=1; 350 351 if (x == NULL) return 0; 352 obj=(X509_OBJECT *)OPENSSL_malloc(sizeof(X509_OBJECT)); 353 if (obj == NULL) 354 { 355 X509err(X509_F_X509_STORE_ADD_CRL,ERR_R_MALLOC_FAILURE); 356 return 0; 357 } 358 obj->type=X509_LU_CRL; 359 obj->data.crl=x; 360 361 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); 362 363 X509_OBJECT_up_ref_count(obj); 364 365 if (X509_OBJECT_retrieve_match(ctx->objs, obj)) 366 { 367 X509_OBJECT_free_contents(obj); 368 OPENSSL_free(obj); 369 X509err(X509_F_X509_STORE_ADD_CRL,X509_R_CERT_ALREADY_IN_HASH_TABLE); 370 ret=0; 371 } 372 else sk_X509_OBJECT_push(ctx->objs, obj); 373 374 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); 375 376 return ret; 377 } 378 379void X509_OBJECT_up_ref_count(X509_OBJECT *a) 380 { 381 switch (a->type) 382 { 383 case X509_LU_X509: 384 CRYPTO_add(&a->data.x509->references,1,CRYPTO_LOCK_X509); 385 break; 386 case X509_LU_CRL: 387 CRYPTO_add(&a->data.crl->references,1,CRYPTO_LOCK_X509_CRL); 388 break; 389 } 390 } 391 392void X509_OBJECT_free_contents(X509_OBJECT *a) 393 { 394 switch (a->type) 395 { 396 case X509_LU_X509: 397 X509_free(a->data.x509); 398 break; 399 case X509_LU_CRL: 400 X509_CRL_free(a->data.crl); 401 break; 402 } 403 } 404 405int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, 406 X509_NAME *name) 407 { 408 X509_OBJECT stmp; 409 X509 x509_s; 410 X509_CINF cinf_s; 411 X509_CRL crl_s; 412 X509_CRL_INFO crl_info_s; 413 414 stmp.type=type; 415 switch (type) 416 { 417 case X509_LU_X509: 418 stmp.data.x509= &x509_s; 419 x509_s.cert_info= &cinf_s; 420 cinf_s.subject=name; 421 break; 422 case X509_LU_CRL: 423 stmp.data.crl= &crl_s; 424 crl_s.crl= &crl_info_s; 425 crl_info_s.issuer=name; 426 break; 427 default: 428 /* abort(); */ 429 return -1; 430 } 431 432 return sk_X509_OBJECT_find(h,&stmp); 433 } 434 435X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type, 436 X509_NAME *name) 437{ 438 int idx; 439 idx = X509_OBJECT_idx_by_subject(h, type, name); 440 if (idx==-1) return NULL; 441 return sk_X509_OBJECT_value(h, idx); 442} 443 444X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x) 445{ 446 int idx, i; 447 X509_OBJECT *obj; 448 idx = sk_X509_OBJECT_find(h, x); 449 if (idx == -1) return NULL; 450 if (x->type != X509_LU_X509) return sk_X509_OBJECT_value(h, idx); 451 for (i = idx; i < sk_X509_OBJECT_num(h); i++) 452 { 453 obj = sk_X509_OBJECT_value(h, i); 454 if (x509_object_cmp((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x)) 455 return NULL; 456 if ((x->type != X509_LU_X509) || !X509_cmp(obj->data.x509, x->data.x509)) 457 return obj; 458 } 459 return NULL; 460} 461 462 463/* Try to get issuer certificate from store. Due to limitations 464 * of the API this can only retrieve a single certificate matching 465 * a given subject name. However it will fill the cache with all 466 * matching certificates, so we can examine the cache for all 467 * matches. 468 * 469 * Return values are: 470 * 1 lookup successful. 471 * 0 certificate not found. 472 * -1 some other error. 473 */ 474 475 476int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) 477{ 478 X509_NAME *xn; 479 X509_OBJECT obj, *pobj; 480 int i, ok, idx; 481 xn=X509_get_issuer_name(x); 482 ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); 483 if (ok != X509_LU_X509) 484 { 485 if (ok == X509_LU_RETRY) 486 { 487 X509_OBJECT_free_contents(&obj); 488 X509err(X509_F_X509_VERIFY_CERT,X509_R_SHOULD_RETRY); 489 return -1; 490 } 491 else if (ok != X509_LU_FAIL) 492 { 493 X509_OBJECT_free_contents(&obj); 494 /* not good :-(, break anyway */ 495 return -1; 496 } 497 return 0; 498 } 499 /* If certificate matches all OK */ 500 if (ctx->check_issued(ctx, x, obj.data.x509)) 501 { 502 *issuer = obj.data.x509; 503 return 1; 504 } 505 X509_OBJECT_free_contents(&obj); 506 /* Else find index of first matching cert */ 507 idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn); 508 /* This shouldn't normally happen since we already have one match */ 509 if (idx == -1) return 0; 510 511 /* Look through all matching certificates for a suitable issuer */ 512 for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) 513 { 514 pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); 515 /* See if we've ran out of matches */ 516 if (pobj->type != X509_LU_X509) return 0; 517 if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) return 0; 518 if (ctx->check_issued(ctx, x, pobj->data.x509)) 519 { 520 *issuer = pobj->data.x509; 521 X509_OBJECT_up_ref_count(pobj); 522 return 1; 523 } 524 } 525 return 0; 526} 527 528IMPLEMENT_STACK_OF(X509_LOOKUP) 529IMPLEMENT_STACK_OF(X509_OBJECT) 530