diff options
Diffstat (limited to 'src/lib/libcrypto/x509/x509_lu.c')
-rw-r--r-- | src/lib/libcrypto/x509/x509_lu.c | 215 |
1 files changed, 182 insertions, 33 deletions
diff --git a/src/lib/libcrypto/x509/x509_lu.c b/src/lib/libcrypto/x509/x509_lu.c index cd2cfb6d85..3a6e04a1de 100644 --- a/src/lib/libcrypto/x509/x509_lu.c +++ b/src/lib/libcrypto/x509/x509_lu.c | |||
@@ -196,9 +196,17 @@ X509_STORE *X509_STORE_new(void) | |||
196 | ret->get_crl = 0; | 196 | ret->get_crl = 0; |
197 | ret->check_crl = 0; | 197 | ret->check_crl = 0; |
198 | ret->cert_crl = 0; | 198 | ret->cert_crl = 0; |
199 | ret->lookup_certs = 0; | ||
200 | ret->lookup_crls = 0; | ||
199 | ret->cleanup = 0; | 201 | ret->cleanup = 0; |
200 | 202 | ||
201 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data); | 203 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data)) |
204 | { | ||
205 | sk_X509_OBJECT_free(ret->objs); | ||
206 | OPENSSL_free(ret); | ||
207 | return NULL; | ||
208 | } | ||
209 | |||
202 | ret->references=1; | 210 | ret->references=1; |
203 | return ret; | 211 | return ret; |
204 | } | 212 | } |
@@ -286,9 +294,11 @@ int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name, | |||
286 | X509_OBJECT stmp,*tmp; | 294 | X509_OBJECT stmp,*tmp; |
287 | int i,j; | 295 | int i,j; |
288 | 296 | ||
297 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
289 | tmp=X509_OBJECT_retrieve_by_subject(ctx->objs,type,name); | 298 | tmp=X509_OBJECT_retrieve_by_subject(ctx->objs,type,name); |
299 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
290 | 300 | ||
291 | if (tmp == NULL) | 301 | if (tmp == NULL || type == X509_LU_CRL) |
292 | { | 302 | { |
293 | for (i=vs->current_method; i<sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) | 303 | for (i=vs->current_method; i<sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) |
294 | { | 304 | { |
@@ -340,7 +350,6 @@ int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) | |||
340 | 350 | ||
341 | X509_OBJECT_up_ref_count(obj); | 351 | X509_OBJECT_up_ref_count(obj); |
342 | 352 | ||
343 | |||
344 | if (X509_OBJECT_retrieve_match(ctx->objs, obj)) | 353 | if (X509_OBJECT_retrieve_match(ctx->objs, obj)) |
345 | { | 354 | { |
346 | X509_OBJECT_free_contents(obj); | 355 | X509_OBJECT_free_contents(obj); |
@@ -414,14 +423,15 @@ void X509_OBJECT_free_contents(X509_OBJECT *a) | |||
414 | } | 423 | } |
415 | } | 424 | } |
416 | 425 | ||
417 | int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, | 426 | static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, |
418 | X509_NAME *name) | 427 | X509_NAME *name, int *pnmatch) |
419 | { | 428 | { |
420 | X509_OBJECT stmp; | 429 | X509_OBJECT stmp; |
421 | X509 x509_s; | 430 | X509 x509_s; |
422 | X509_CINF cinf_s; | 431 | X509_CINF cinf_s; |
423 | X509_CRL crl_s; | 432 | X509_CRL crl_s; |
424 | X509_CRL_INFO crl_info_s; | 433 | X509_CRL_INFO crl_info_s; |
434 | int idx; | ||
425 | 435 | ||
426 | stmp.type=type; | 436 | stmp.type=type; |
427 | switch (type) | 437 | switch (type) |
@@ -441,41 +451,169 @@ int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, | |||
441 | return -1; | 451 | return -1; |
442 | } | 452 | } |
443 | 453 | ||
444 | return sk_X509_OBJECT_find(h,&stmp); | 454 | idx = sk_X509_OBJECT_find(h,&stmp); |
455 | if (idx >= 0 && pnmatch) | ||
456 | { | ||
457 | int tidx; | ||
458 | const X509_OBJECT *tobj, *pstmp; | ||
459 | *pnmatch = 1; | ||
460 | pstmp = &stmp; | ||
461 | for (tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) | ||
462 | { | ||
463 | tobj = sk_X509_OBJECT_value(h, tidx); | ||
464 | if (x509_object_cmp(&tobj, &pstmp)) | ||
465 | break; | ||
466 | (*pnmatch)++; | ||
467 | } | ||
468 | } | ||
469 | return idx; | ||
470 | } | ||
471 | |||
472 | |||
473 | int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, | ||
474 | X509_NAME *name) | ||
475 | { | ||
476 | return x509_object_idx_cnt(h, type, name, NULL); | ||
445 | } | 477 | } |
446 | 478 | ||
447 | X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type, | 479 | X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
448 | X509_NAME *name) | 480 | X509_NAME *name) |
449 | { | 481 | { |
450 | int idx; | 482 | int idx; |
451 | idx = X509_OBJECT_idx_by_subject(h, type, name); | 483 | idx = X509_OBJECT_idx_by_subject(h, type, name); |
452 | if (idx==-1) return NULL; | 484 | if (idx==-1) return NULL; |
453 | return sk_X509_OBJECT_value(h, idx); | 485 | return sk_X509_OBJECT_value(h, idx); |
454 | } | 486 | } |
487 | |||
488 | STACK_OF(X509)* X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm) | ||
489 | { | ||
490 | int i, idx, cnt; | ||
491 | STACK_OF(X509) *sk; | ||
492 | X509 *x; | ||
493 | X509_OBJECT *obj; | ||
494 | sk = sk_X509_new_null(); | ||
495 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
496 | idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt); | ||
497 | if (idx < 0) | ||
498 | { | ||
499 | /* Nothing found in cache: do lookup to possibly add new | ||
500 | * objects to cache | ||
501 | */ | ||
502 | X509_OBJECT xobj; | ||
503 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
504 | if (!X509_STORE_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) | ||
505 | { | ||
506 | sk_X509_free(sk); | ||
507 | return NULL; | ||
508 | } | ||
509 | X509_OBJECT_free_contents(&xobj); | ||
510 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
511 | idx = x509_object_idx_cnt(ctx->ctx->objs,X509_LU_X509,nm, &cnt); | ||
512 | if (idx < 0) | ||
513 | { | ||
514 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
515 | sk_X509_free(sk); | ||
516 | return NULL; | ||
517 | } | ||
518 | } | ||
519 | for (i = 0; i < cnt; i++, idx++) | ||
520 | { | ||
521 | obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx); | ||
522 | x = obj->data.x509; | ||
523 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | ||
524 | if (!sk_X509_push(sk, x)) | ||
525 | { | ||
526 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
527 | X509_free(x); | ||
528 | sk_X509_pop_free(sk, X509_free); | ||
529 | return NULL; | ||
530 | } | ||
531 | } | ||
532 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
533 | return sk; | ||
534 | |||
535 | } | ||
536 | |||
537 | STACK_OF(X509_CRL)* X509_STORE_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm) | ||
538 | { | ||
539 | int i, idx, cnt; | ||
540 | STACK_OF(X509_CRL) *sk; | ||
541 | X509_CRL *x; | ||
542 | X509_OBJECT *obj, xobj; | ||
543 | sk = sk_X509_CRL_new_null(); | ||
544 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
545 | /* Check cache first */ | ||
546 | idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt); | ||
547 | |||
548 | /* Always do lookup to possibly add new CRLs to cache | ||
549 | */ | ||
550 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
551 | if (!X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj)) | ||
552 | { | ||
553 | sk_X509_CRL_free(sk); | ||
554 | return NULL; | ||
555 | } | ||
556 | X509_OBJECT_free_contents(&xobj); | ||
557 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
558 | idx = x509_object_idx_cnt(ctx->ctx->objs,X509_LU_CRL, nm, &cnt); | ||
559 | if (idx < 0) | ||
560 | { | ||
561 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
562 | sk_X509_CRL_free(sk); | ||
563 | return NULL; | ||
564 | } | ||
565 | |||
566 | for (i = 0; i < cnt; i++, idx++) | ||
567 | { | ||
568 | obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx); | ||
569 | x = obj->data.crl; | ||
570 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509_CRL); | ||
571 | if (!sk_X509_CRL_push(sk, x)) | ||
572 | { | ||
573 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
574 | X509_CRL_free(x); | ||
575 | sk_X509_CRL_pop_free(sk, X509_CRL_free); | ||
576 | return NULL; | ||
577 | } | ||
578 | } | ||
579 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
580 | return sk; | ||
581 | } | ||
455 | 582 | ||
456 | X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x) | 583 | X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x) |
457 | { | 584 | { |
458 | int idx, i; | 585 | int idx, i; |
459 | X509_OBJECT *obj; | 586 | X509_OBJECT *obj; |
460 | idx = sk_X509_OBJECT_find(h, x); | 587 | idx = sk_X509_OBJECT_find(h, x); |
461 | if (idx == -1) return NULL; | 588 | if (idx == -1) return NULL; |
462 | if (x->type != X509_LU_X509) return sk_X509_OBJECT_value(h, idx); | 589 | if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) |
590 | return sk_X509_OBJECT_value(h, idx); | ||
463 | for (i = idx; i < sk_X509_OBJECT_num(h); i++) | 591 | for (i = idx; i < sk_X509_OBJECT_num(h); i++) |
464 | { | 592 | { |
465 | obj = sk_X509_OBJECT_value(h, i); | 593 | obj = sk_X509_OBJECT_value(h, i); |
466 | if (x509_object_cmp((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x)) | 594 | if (x509_object_cmp((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x)) |
467 | return NULL; | 595 | return NULL; |
468 | if ((x->type != X509_LU_X509) || !X509_cmp(obj->data.x509, x->data.x509)) | 596 | if (x->type == X509_LU_X509) |
597 | { | ||
598 | if (!X509_cmp(obj->data.x509, x->data.x509)) | ||
599 | return obj; | ||
600 | } | ||
601 | else if (x->type == X509_LU_CRL) | ||
602 | { | ||
603 | if (!X509_CRL_match(obj->data.crl, x->data.crl)) | ||
604 | return obj; | ||
605 | } | ||
606 | else | ||
469 | return obj; | 607 | return obj; |
470 | } | 608 | } |
471 | return NULL; | 609 | return NULL; |
472 | } | 610 | } |
473 | 611 | ||
474 | 612 | ||
475 | /* Try to get issuer certificate from store. Due to limitations | 613 | /* Try to get issuer certificate from store. Due to limitations |
476 | * of the API this can only retrieve a single certificate matching | 614 | * of the API this can only retrieve a single certificate matching |
477 | * a given subject name. However it will fill the cache with all | 615 | * a given subject name. However it will fill the cache with all |
478 | * matching certificates, so we can examine the cache for all | 616 | * matching certificates, so we can examine the cache for all |
479 | * matches. | 617 | * matches. |
480 | * | 618 | * |
481 | * Return values are: | 619 | * Return values are: |
@@ -483,13 +621,11 @@ X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x | |||
483 | * 0 certificate not found. | 621 | * 0 certificate not found. |
484 | * -1 some other error. | 622 | * -1 some other error. |
485 | */ | 623 | */ |
486 | |||
487 | |||
488 | int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) | 624 | int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) |
489 | { | 625 | { |
490 | X509_NAME *xn; | 626 | X509_NAME *xn; |
491 | X509_OBJECT obj, *pobj; | 627 | X509_OBJECT obj, *pobj; |
492 | int i, ok, idx; | 628 | int i, ok, idx, ret; |
493 | xn=X509_get_issuer_name(x); | 629 | xn=X509_get_issuer_name(x); |
494 | ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); | 630 | ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); |
495 | if (ok != X509_LU_X509) | 631 | if (ok != X509_LU_X509) |
@@ -515,27 +651,34 @@ int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) | |||
515 | return 1; | 651 | return 1; |
516 | } | 652 | } |
517 | X509_OBJECT_free_contents(&obj); | 653 | X509_OBJECT_free_contents(&obj); |
518 | /* Else find index of first matching cert */ | ||
519 | idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn); | ||
520 | /* This shouldn't normally happen since we already have one match */ | ||
521 | if (idx == -1) return 0; | ||
522 | 654 | ||
523 | /* Look through all matching certificates for a suitable issuer */ | 655 | /* Else find index of first cert accepted by 'check_issued' */ |
524 | for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) | 656 | ret = 0; |
657 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
658 | idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn); | ||
659 | if (idx != -1) /* should be true as we've had at least one match */ | ||
525 | { | 660 | { |
526 | pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); | 661 | /* Look through all matching certs for suitable issuer */ |
527 | /* See if we've ran out of matches */ | 662 | for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) |
528 | if (pobj->type != X509_LU_X509) return 0; | ||
529 | if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) return 0; | ||
530 | if (ctx->check_issued(ctx, x, pobj->data.x509)) | ||
531 | { | 663 | { |
532 | *issuer = pobj->data.x509; | 664 | pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); |
533 | X509_OBJECT_up_ref_count(pobj); | 665 | /* See if we've run past the matches */ |
534 | return 1; | 666 | if (pobj->type != X509_LU_X509) |
667 | break; | ||
668 | if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) | ||
669 | break; | ||
670 | if (ctx->check_issued(ctx, x, pobj->data.x509)) | ||
671 | { | ||
672 | *issuer = pobj->data.x509; | ||
673 | X509_OBJECT_up_ref_count(pobj); | ||
674 | ret = 1; | ||
675 | break; | ||
676 | } | ||
535 | } | 677 | } |
536 | } | 678 | } |
537 | return 0; | 679 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); |
538 | } | 680 | return ret; |
681 | } | ||
539 | 682 | ||
540 | int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) | 683 | int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) |
541 | { | 684 | { |
@@ -563,5 +706,11 @@ int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param) | |||
563 | return X509_VERIFY_PARAM_set1(ctx->param, param); | 706 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
564 | } | 707 | } |
565 | 708 | ||
709 | void X509_STORE_set_verify_cb(X509_STORE *ctx, | ||
710 | int (*verify_cb)(int, X509_STORE_CTX *)) | ||
711 | { | ||
712 | ctx->verify_cb = verify_cb; | ||
713 | } | ||
714 | |||
566 | IMPLEMENT_STACK_OF(X509_LOOKUP) | 715 | IMPLEMENT_STACK_OF(X509_LOOKUP) |
567 | IMPLEMENT_STACK_OF(X509_OBJECT) | 716 | IMPLEMENT_STACK_OF(X509_OBJECT) |