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, 33 insertions, 182 deletions
diff --git a/src/lib/libcrypto/x509/x509_lu.c b/src/lib/libcrypto/x509/x509_lu.c index 3a6e04a1de..cd2cfb6d85 100644 --- a/src/lib/libcrypto/x509/x509_lu.c +++ b/src/lib/libcrypto/x509/x509_lu.c | |||
@@ -196,17 +196,9 @@ 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; | ||
201 | ret->cleanup = 0; | 199 | ret->cleanup = 0; |
202 | 200 | ||
203 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE, ret, &ret->ex_data)) | 201 | 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 | |||
210 | ret->references=1; | 202 | ret->references=1; |
211 | return ret; | 203 | return ret; |
212 | } | 204 | } |
@@ -294,11 +286,9 @@ int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name, | |||
294 | X509_OBJECT stmp,*tmp; | 286 | X509_OBJECT stmp,*tmp; |
295 | int i,j; | 287 | int i,j; |
296 | 288 | ||
297 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
298 | tmp=X509_OBJECT_retrieve_by_subject(ctx->objs,type,name); | 289 | tmp=X509_OBJECT_retrieve_by_subject(ctx->objs,type,name); |
299 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
300 | 290 | ||
301 | if (tmp == NULL || type == X509_LU_CRL) | 291 | if (tmp == NULL) |
302 | { | 292 | { |
303 | for (i=vs->current_method; i<sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) | 293 | for (i=vs->current_method; i<sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) |
304 | { | 294 | { |
@@ -350,6 +340,7 @@ int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) | |||
350 | 340 | ||
351 | X509_OBJECT_up_ref_count(obj); | 341 | X509_OBJECT_up_ref_count(obj); |
352 | 342 | ||
343 | |||
353 | if (X509_OBJECT_retrieve_match(ctx->objs, obj)) | 344 | if (X509_OBJECT_retrieve_match(ctx->objs, obj)) |
354 | { | 345 | { |
355 | X509_OBJECT_free_contents(obj); | 346 | X509_OBJECT_free_contents(obj); |
@@ -423,15 +414,14 @@ void X509_OBJECT_free_contents(X509_OBJECT *a) | |||
423 | } | 414 | } |
424 | } | 415 | } |
425 | 416 | ||
426 | static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, | 417 | int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
427 | X509_NAME *name, int *pnmatch) | 418 | X509_NAME *name) |
428 | { | 419 | { |
429 | X509_OBJECT stmp; | 420 | X509_OBJECT stmp; |
430 | X509 x509_s; | 421 | X509 x509_s; |
431 | X509_CINF cinf_s; | 422 | X509_CINF cinf_s; |
432 | X509_CRL crl_s; | 423 | X509_CRL crl_s; |
433 | X509_CRL_INFO crl_info_s; | 424 | X509_CRL_INFO crl_info_s; |
434 | int idx; | ||
435 | 425 | ||
436 | stmp.type=type; | 426 | stmp.type=type; |
437 | switch (type) | 427 | switch (type) |
@@ -451,169 +441,41 @@ static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, | |||
451 | return -1; | 441 | return -1; |
452 | } | 442 | } |
453 | 443 | ||
454 | idx = sk_X509_OBJECT_find(h,&stmp); | 444 | return 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); | ||
477 | } | 445 | } |
478 | 446 | ||
479 | X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type, | 447 | X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
480 | X509_NAME *name) | 448 | X509_NAME *name) |
481 | { | 449 | { |
482 | int idx; | 450 | int idx; |
483 | idx = X509_OBJECT_idx_by_subject(h, type, name); | 451 | idx = X509_OBJECT_idx_by_subject(h, type, name); |
484 | if (idx==-1) return NULL; | 452 | if (idx==-1) return NULL; |
485 | return sk_X509_OBJECT_value(h, idx); | 453 | return sk_X509_OBJECT_value(h, idx); |
486 | } | 454 | } |
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 | } | ||
582 | 455 | ||
583 | X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x) | 456 | X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x) |
584 | { | 457 | { |
585 | int idx, i; | 458 | int idx, i; |
586 | X509_OBJECT *obj; | 459 | X509_OBJECT *obj; |
587 | idx = sk_X509_OBJECT_find(h, x); | 460 | idx = sk_X509_OBJECT_find(h, x); |
588 | if (idx == -1) return NULL; | 461 | if (idx == -1) return NULL; |
589 | if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) | 462 | if (x->type != X509_LU_X509) return sk_X509_OBJECT_value(h, idx); |
590 | return sk_X509_OBJECT_value(h, idx); | ||
591 | for (i = idx; i < sk_X509_OBJECT_num(h); i++) | 463 | for (i = idx; i < sk_X509_OBJECT_num(h); i++) |
592 | { | 464 | { |
593 | obj = sk_X509_OBJECT_value(h, i); | 465 | obj = sk_X509_OBJECT_value(h, i); |
594 | if (x509_object_cmp((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x)) | 466 | if (x509_object_cmp((const X509_OBJECT **)&obj, (const X509_OBJECT **)&x)) |
595 | return NULL; | 467 | return NULL; |
596 | if (x->type == X509_LU_X509) | 468 | if ((x->type != X509_LU_X509) || !X509_cmp(obj->data.x509, x->data.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 | ||
607 | return obj; | 469 | return obj; |
608 | } | 470 | } |
609 | return NULL; | 471 | return NULL; |
610 | } | 472 | } |
611 | 473 | ||
612 | 474 | ||
613 | /* Try to get issuer certificate from store. Due to limitations | 475 | /* Try to get issuer certificate from store. Due to limitations |
614 | * of the API this can only retrieve a single certificate matching | 476 | * of the API this can only retrieve a single certificate matching |
615 | * a given subject name. However it will fill the cache with all | 477 | * a given subject name. However it will fill the cache with all |
616 | * matching certificates, so we can examine the cache for all | 478 | * matching certificates, so we can examine the cache for all |
617 | * matches. | 479 | * matches. |
618 | * | 480 | * |
619 | * Return values are: | 481 | * Return values are: |
@@ -621,11 +483,13 @@ X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, X509_OBJECT *x | |||
621 | * 0 certificate not found. | 483 | * 0 certificate not found. |
622 | * -1 some other error. | 484 | * -1 some other error. |
623 | */ | 485 | */ |
486 | |||
487 | |||
624 | int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) | 488 | int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) |
625 | { | 489 | { |
626 | X509_NAME *xn; | 490 | X509_NAME *xn; |
627 | X509_OBJECT obj, *pobj; | 491 | X509_OBJECT obj, *pobj; |
628 | int i, ok, idx, ret; | 492 | int i, ok, idx; |
629 | xn=X509_get_issuer_name(x); | 493 | xn=X509_get_issuer_name(x); |
630 | ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); | 494 | ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); |
631 | if (ok != X509_LU_X509) | 495 | if (ok != X509_LU_X509) |
@@ -651,34 +515,27 @@ int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) | |||
651 | return 1; | 515 | return 1; |
652 | } | 516 | } |
653 | X509_OBJECT_free_contents(&obj); | 517 | X509_OBJECT_free_contents(&obj); |
654 | 518 | /* Else find index of first matching cert */ | |
655 | /* Else find index of first cert accepted by 'check_issued' */ | ||
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); | 519 | 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 */ | 520 | /* This shouldn't normally happen since we already have one match */ |
521 | if (idx == -1) return 0; | ||
522 | |||
523 | /* Look through all matching certificates for a suitable issuer */ | ||
524 | for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) | ||
660 | { | 525 | { |
661 | /* Look through all matching certs for suitable issuer */ | 526 | pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); |
662 | for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) | 527 | /* See if we've ran out of matches */ |
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)) | ||
663 | { | 531 | { |
664 | pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); | 532 | *issuer = pobj->data.x509; |
665 | /* See if we've run past the matches */ | 533 | X509_OBJECT_up_ref_count(pobj); |
666 | if (pobj->type != X509_LU_X509) | 534 | return 1; |
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 | } | ||
677 | } | 535 | } |
678 | } | 536 | } |
679 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | 537 | return 0; |
680 | return ret; | 538 | } |
681 | } | ||
682 | 539 | ||
683 | int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) | 540 | int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) |
684 | { | 541 | { |
@@ -706,11 +563,5 @@ int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *param) | |||
706 | return X509_VERIFY_PARAM_set1(ctx->param, param); | 563 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
707 | } | 564 | } |
708 | 565 | ||
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 | |||
715 | IMPLEMENT_STACK_OF(X509_LOOKUP) | 566 | IMPLEMENT_STACK_OF(X509_LOOKUP) |
716 | IMPLEMENT_STACK_OF(X509_OBJECT) | 567 | IMPLEMENT_STACK_OF(X509_OBJECT) |