diff options
Diffstat (limited to 'src/lib/libcrypto/x509/x509_vfy.c')
-rw-r--r-- | src/lib/libcrypto/x509/x509_vfy.c | 951 |
1 files changed, 807 insertions, 144 deletions
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c index 336c40ddd7..87ebf62525 100644 --- a/src/lib/libcrypto/x509/x509_vfy.c +++ b/src/lib/libcrypto/x509/x509_vfy.c | |||
@@ -70,14 +70,70 @@ | |||
70 | #include <openssl/x509v3.h> | 70 | #include <openssl/x509v3.h> |
71 | #include <openssl/objects.h> | 71 | #include <openssl/objects.h> |
72 | 72 | ||
73 | /* CRL score values */ | ||
74 | |||
75 | /* No unhandled critical extensions */ | ||
76 | |||
77 | #define CRL_SCORE_NOCRITICAL 0x100 | ||
78 | |||
79 | /* certificate is within CRL scope */ | ||
80 | |||
81 | #define CRL_SCORE_SCOPE 0x080 | ||
82 | |||
83 | /* CRL times valid */ | ||
84 | |||
85 | #define CRL_SCORE_TIME 0x040 | ||
86 | |||
87 | /* Issuer name matches certificate */ | ||
88 | |||
89 | #define CRL_SCORE_ISSUER_NAME 0x020 | ||
90 | |||
91 | /* If this score or above CRL is probably valid */ | ||
92 | |||
93 | #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE) | ||
94 | |||
95 | /* CRL issuer is certificate issuer */ | ||
96 | |||
97 | #define CRL_SCORE_ISSUER_CERT 0x018 | ||
98 | |||
99 | /* CRL issuer is on certificate path */ | ||
100 | |||
101 | #define CRL_SCORE_SAME_PATH 0x008 | ||
102 | |||
103 | /* CRL issuer matches CRL AKID */ | ||
104 | |||
105 | #define CRL_SCORE_AKID 0x004 | ||
106 | |||
107 | /* Have a delta CRL with valid times */ | ||
108 | |||
109 | #define CRL_SCORE_TIME_DELTA 0x002 | ||
110 | |||
73 | static int null_callback(int ok,X509_STORE_CTX *e); | 111 | static int null_callback(int ok,X509_STORE_CTX *e); |
74 | static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer); | 112 | static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer); |
75 | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x); | 113 | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x); |
76 | static int check_chain_extensions(X509_STORE_CTX *ctx); | 114 | static int check_chain_extensions(X509_STORE_CTX *ctx); |
115 | static int check_name_constraints(X509_STORE_CTX *ctx); | ||
77 | static int check_trust(X509_STORE_CTX *ctx); | 116 | static int check_trust(X509_STORE_CTX *ctx); |
78 | static int check_revocation(X509_STORE_CTX *ctx); | 117 | static int check_revocation(X509_STORE_CTX *ctx); |
79 | static int check_cert(X509_STORE_CTX *ctx); | 118 | static int check_cert(X509_STORE_CTX *ctx); |
80 | static int check_policy(X509_STORE_CTX *ctx); | 119 | static int check_policy(X509_STORE_CTX *ctx); |
120 | |||
121 | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, | ||
122 | unsigned int *preasons, | ||
123 | X509_CRL *crl, X509 *x); | ||
124 | static int get_crl_delta(X509_STORE_CTX *ctx, | ||
125 | X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x); | ||
126 | static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pcrl_score, | ||
127 | X509_CRL *base, STACK_OF(X509_CRL) *crls); | ||
128 | static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, | ||
129 | X509 **pissuer, int *pcrl_score); | ||
130 | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, | ||
131 | unsigned int *preasons); | ||
132 | static int check_crl_path(X509_STORE_CTX *ctx, X509 *x); | ||
133 | static int check_crl_chain(X509_STORE_CTX *ctx, | ||
134 | STACK_OF(X509) *cert_path, | ||
135 | STACK_OF(X509) *crl_path); | ||
136 | |||
81 | static int internal_verify(X509_STORE_CTX *ctx); | 137 | static int internal_verify(X509_STORE_CTX *ctx); |
82 | const char X509_version[]="X.509" OPENSSL_VERSION_PTEXT; | 138 | const char X509_version[]="X.509" OPENSSL_VERSION_PTEXT; |
83 | 139 | ||
@@ -289,6 +345,12 @@ int X509_verify_cert(X509_STORE_CTX *ctx) | |||
289 | 345 | ||
290 | if (!ok) goto end; | 346 | if (!ok) goto end; |
291 | 347 | ||
348 | /* Check name constraints */ | ||
349 | |||
350 | ok = check_name_constraints(ctx); | ||
351 | |||
352 | if (!ok) goto end; | ||
353 | |||
292 | /* The chain extensions are OK: check trust */ | 354 | /* The chain extensions are OK: check trust */ |
293 | 355 | ||
294 | if (param->trust > 0) ok = check_trust(ctx); | 356 | if (param->trust > 0) ok = check_trust(ctx); |
@@ -398,8 +460,8 @@ static int check_chain_extensions(X509_STORE_CTX *ctx) | |||
398 | X509 *x; | 460 | X509 *x; |
399 | int (*cb)(int xok,X509_STORE_CTX *xctx); | 461 | int (*cb)(int xok,X509_STORE_CTX *xctx); |
400 | int proxy_path_length = 0; | 462 | int proxy_path_length = 0; |
401 | int allow_proxy_certs = | 463 | int purpose; |
402 | !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS); | 464 | int allow_proxy_certs; |
403 | cb=ctx->verify_cb; | 465 | cb=ctx->verify_cb; |
404 | 466 | ||
405 | /* must_be_ca can have 1 of 3 values: | 467 | /* must_be_ca can have 1 of 3 values: |
@@ -412,10 +474,22 @@ static int check_chain_extensions(X509_STORE_CTX *ctx) | |||
412 | */ | 474 | */ |
413 | must_be_ca = -1; | 475 | must_be_ca = -1; |
414 | 476 | ||
415 | /* A hack to keep people who don't want to modify their software | 477 | /* CRL path validation */ |
416 | happy */ | 478 | if (ctx->parent) |
417 | if (getenv("OPENSSL_ALLOW_PROXY_CERTS")) | 479 | { |
418 | allow_proxy_certs = 1; | 480 | allow_proxy_certs = 0; |
481 | purpose = X509_PURPOSE_CRL_SIGN; | ||
482 | } | ||
483 | else | ||
484 | { | ||
485 | allow_proxy_certs = | ||
486 | !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS); | ||
487 | /* A hack to keep people who don't want to modify their | ||
488 | software happy */ | ||
489 | if (getenv("OPENSSL_ALLOW_PROXY_CERTS")) | ||
490 | allow_proxy_certs = 1; | ||
491 | purpose = ctx->param->purpose; | ||
492 | } | ||
419 | 493 | ||
420 | /* Check all untrusted certificates */ | 494 | /* Check all untrusted certificates */ |
421 | for (i = 0; i < ctx->last_untrusted; i++) | 495 | for (i = 0; i < ctx->last_untrusted; i++) |
@@ -482,8 +556,7 @@ static int check_chain_extensions(X509_STORE_CTX *ctx) | |||
482 | } | 556 | } |
483 | if (ctx->param->purpose > 0) | 557 | if (ctx->param->purpose > 0) |
484 | { | 558 | { |
485 | ret = X509_check_purpose(x, ctx->param->purpose, | 559 | ret = X509_check_purpose(x, purpose, must_be_ca > 0); |
486 | must_be_ca > 0); | ||
487 | if ((ret == 0) | 560 | if ((ret == 0) |
488 | || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) | 561 | || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) |
489 | && (ret != 1))) | 562 | && (ret != 1))) |
@@ -536,6 +609,42 @@ static int check_chain_extensions(X509_STORE_CTX *ctx) | |||
536 | #endif | 609 | #endif |
537 | } | 610 | } |
538 | 611 | ||
612 | static int check_name_constraints(X509_STORE_CTX *ctx) | ||
613 | { | ||
614 | X509 *x; | ||
615 | int i, j, rv; | ||
616 | /* Check name constraints for all certificates */ | ||
617 | for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) | ||
618 | { | ||
619 | x = sk_X509_value(ctx->chain, i); | ||
620 | /* Ignore self issued certs unless last in chain */ | ||
621 | if (i && (x->ex_flags & EXFLAG_SI)) | ||
622 | continue; | ||
623 | /* Check against constraints for all certificates higher in | ||
624 | * chain including trust anchor. Trust anchor not strictly | ||
625 | * speaking needed but if it includes constraints it is to be | ||
626 | * assumed it expects them to be obeyed. | ||
627 | */ | ||
628 | for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) | ||
629 | { | ||
630 | NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc; | ||
631 | if (nc) | ||
632 | { | ||
633 | rv = NAME_CONSTRAINTS_check(x, nc); | ||
634 | if (rv != X509_V_OK) | ||
635 | { | ||
636 | ctx->error = rv; | ||
637 | ctx->error_depth = i; | ||
638 | ctx->current_cert = x; | ||
639 | if (!ctx->verify_cb(0,ctx)) | ||
640 | return 0; | ||
641 | } | ||
642 | } | ||
643 | } | ||
644 | } | ||
645 | return 1; | ||
646 | } | ||
647 | |||
539 | static int check_trust(X509_STORE_CTX *ctx) | 648 | static int check_trust(X509_STORE_CTX *ctx) |
540 | { | 649 | { |
541 | #ifdef OPENSSL_NO_CHAIN_VERIFY | 650 | #ifdef OPENSSL_NO_CHAIN_VERIFY |
@@ -570,7 +679,12 @@ static int check_revocation(X509_STORE_CTX *ctx) | |||
570 | if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) | 679 | if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) |
571 | last = sk_X509_num(ctx->chain) - 1; | 680 | last = sk_X509_num(ctx->chain) - 1; |
572 | else | 681 | else |
682 | { | ||
683 | /* If checking CRL paths this isn't the EE certificate */ | ||
684 | if (ctx->parent) | ||
685 | return 1; | ||
573 | last = 0; | 686 | last = 0; |
687 | } | ||
574 | for(i = 0; i <= last; i++) | 688 | for(i = 0; i <= last; i++) |
575 | { | 689 | { |
576 | ctx->error_depth = i; | 690 | ctx->error_depth = i; |
@@ -582,30 +696,65 @@ static int check_revocation(X509_STORE_CTX *ctx) | |||
582 | 696 | ||
583 | static int check_cert(X509_STORE_CTX *ctx) | 697 | static int check_cert(X509_STORE_CTX *ctx) |
584 | { | 698 | { |
585 | X509_CRL *crl = NULL; | 699 | X509_CRL *crl = NULL, *dcrl = NULL; |
586 | X509 *x; | 700 | X509 *x; |
587 | int ok, cnum; | 701 | int ok, cnum; |
588 | cnum = ctx->error_depth; | 702 | cnum = ctx->error_depth; |
589 | x = sk_X509_value(ctx->chain, cnum); | 703 | x = sk_X509_value(ctx->chain, cnum); |
590 | ctx->current_cert = x; | 704 | ctx->current_cert = x; |
591 | /* Try to retrieve relevant CRL */ | 705 | ctx->current_issuer = NULL; |
592 | ok = ctx->get_crl(ctx, &crl, x); | 706 | ctx->current_reasons = 0; |
593 | /* If error looking up CRL, nothing we can do except | 707 | while (ctx->current_reasons != CRLDP_ALL_REASONS) |
594 | * notify callback | ||
595 | */ | ||
596 | if(!ok) | ||
597 | { | 708 | { |
598 | ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; | 709 | /* Try to retrieve relevant CRL */ |
599 | ok = ctx->verify_cb(0, ctx); | 710 | if (ctx->get_crl) |
600 | goto err; | 711 | ok = ctx->get_crl(ctx, &crl, x); |
712 | else | ||
713 | ok = get_crl_delta(ctx, &crl, &dcrl, x); | ||
714 | /* If error looking up CRL, nothing we can do except | ||
715 | * notify callback | ||
716 | */ | ||
717 | if(!ok) | ||
718 | { | ||
719 | ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; | ||
720 | ok = ctx->verify_cb(0, ctx); | ||
721 | goto err; | ||
722 | } | ||
723 | ctx->current_crl = crl; | ||
724 | ok = ctx->check_crl(ctx, crl); | ||
725 | if (!ok) | ||
726 | goto err; | ||
727 | |||
728 | if (dcrl) | ||
729 | { | ||
730 | ok = ctx->check_crl(ctx, dcrl); | ||
731 | if (!ok) | ||
732 | goto err; | ||
733 | ok = ctx->cert_crl(ctx, dcrl, x); | ||
734 | if (!ok) | ||
735 | goto err; | ||
736 | } | ||
737 | else | ||
738 | ok = 1; | ||
739 | |||
740 | /* Don't look in full CRL if delta reason is removefromCRL */ | ||
741 | if (ok != 2) | ||
742 | { | ||
743 | ok = ctx->cert_crl(ctx, crl, x); | ||
744 | if (!ok) | ||
745 | goto err; | ||
746 | } | ||
747 | |||
748 | X509_CRL_free(crl); | ||
749 | X509_CRL_free(dcrl); | ||
750 | crl = NULL; | ||
751 | dcrl = NULL; | ||
601 | } | 752 | } |
602 | ctx->current_crl = crl; | ||
603 | ok = ctx->check_crl(ctx, crl); | ||
604 | if (!ok) goto err; | ||
605 | ok = ctx->cert_crl(ctx, crl, x); | ||
606 | err: | 753 | err: |
607 | ctx->current_crl = NULL; | ||
608 | X509_CRL_free(crl); | 754 | X509_CRL_free(crl); |
755 | X509_CRL_free(dcrl); | ||
756 | |||
757 | ctx->current_crl = NULL; | ||
609 | return ok; | 758 | return ok; |
610 | 759 | ||
611 | } | 760 | } |
@@ -616,7 +765,8 @@ static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) | |||
616 | { | 765 | { |
617 | time_t *ptime; | 766 | time_t *ptime; |
618 | int i; | 767 | int i; |
619 | ctx->current_crl = crl; | 768 | if (notify) |
769 | ctx->current_crl = crl; | ||
620 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) | 770 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) |
621 | ptime = &ctx->param->check_time; | 771 | ptime = &ctx->param->check_time; |
622 | else | 772 | else |
@@ -625,15 +775,19 @@ static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) | |||
625 | i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime); | 775 | i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime); |
626 | if (i == 0) | 776 | if (i == 0) |
627 | { | 777 | { |
778 | if (!notify) | ||
779 | return 0; | ||
628 | ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD; | 780 | ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD; |
629 | if (!notify || !ctx->verify_cb(0, ctx)) | 781 | if (!ctx->verify_cb(0, ctx)) |
630 | return 0; | 782 | return 0; |
631 | } | 783 | } |
632 | 784 | ||
633 | if (i > 0) | 785 | if (i > 0) |
634 | { | 786 | { |
787 | if (!notify) | ||
788 | return 0; | ||
635 | ctx->error=X509_V_ERR_CRL_NOT_YET_VALID; | 789 | ctx->error=X509_V_ERR_CRL_NOT_YET_VALID; |
636 | if (!notify || !ctx->verify_cb(0, ctx)) | 790 | if (!ctx->verify_cb(0, ctx)) |
637 | return 0; | 791 | return 0; |
638 | } | 792 | } |
639 | 793 | ||
@@ -643,92 +797,545 @@ static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) | |||
643 | 797 | ||
644 | if (i == 0) | 798 | if (i == 0) |
645 | { | 799 | { |
800 | if (!notify) | ||
801 | return 0; | ||
646 | ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD; | 802 | ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD; |
647 | if (!notify || !ctx->verify_cb(0, ctx)) | 803 | if (!ctx->verify_cb(0, ctx)) |
648 | return 0; | 804 | return 0; |
649 | } | 805 | } |
650 | 806 | /* Ignore expiry of base CRL is delta is valid */ | |
651 | if (i < 0) | 807 | if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) |
652 | { | 808 | { |
809 | if (!notify) | ||
810 | return 0; | ||
653 | ctx->error=X509_V_ERR_CRL_HAS_EXPIRED; | 811 | ctx->error=X509_V_ERR_CRL_HAS_EXPIRED; |
654 | if (!notify || !ctx->verify_cb(0, ctx)) | 812 | if (!ctx->verify_cb(0, ctx)) |
655 | return 0; | 813 | return 0; |
656 | } | 814 | } |
657 | } | 815 | } |
658 | 816 | ||
659 | ctx->current_crl = NULL; | 817 | if (notify) |
818 | ctx->current_crl = NULL; | ||
660 | 819 | ||
661 | return 1; | 820 | return 1; |
662 | } | 821 | } |
663 | 822 | ||
664 | /* Lookup CRLs from the supplied list. Look for matching isser name | 823 | static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, |
665 | * and validity. If we can't find a valid CRL return the last one | 824 | X509 **pissuer, int *pscore, unsigned int *preasons, |
666 | * with matching name. This gives more meaningful error codes. Otherwise | 825 | STACK_OF(X509_CRL) *crls) |
667 | * we'd get a CRL not found error if a CRL existed with matching name but | ||
668 | * was invalid. | ||
669 | */ | ||
670 | |||
671 | static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, | ||
672 | X509_NAME *nm, STACK_OF(X509_CRL) *crls) | ||
673 | { | 826 | { |
674 | int i; | 827 | int i, crl_score, best_score = *pscore; |
828 | unsigned int reasons, best_reasons = 0; | ||
829 | X509 *x = ctx->current_cert; | ||
675 | X509_CRL *crl, *best_crl = NULL; | 830 | X509_CRL *crl, *best_crl = NULL; |
831 | X509 *crl_issuer = NULL, *best_crl_issuer = NULL; | ||
832 | |||
676 | for (i = 0; i < sk_X509_CRL_num(crls); i++) | 833 | for (i = 0; i < sk_X509_CRL_num(crls); i++) |
677 | { | 834 | { |
678 | crl = sk_X509_CRL_value(crls, i); | 835 | crl = sk_X509_CRL_value(crls, i); |
679 | if (X509_NAME_cmp(nm, X509_CRL_get_issuer(crl))) | 836 | reasons = *preasons; |
680 | continue; | 837 | crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x); |
681 | if (check_crl_time(ctx, crl, 0)) | 838 | |
839 | if (crl_score > best_score) | ||
682 | { | 840 | { |
683 | *pcrl = crl; | 841 | best_crl = crl; |
684 | CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509); | 842 | best_crl_issuer = crl_issuer; |
685 | return 1; | 843 | best_score = crl_score; |
844 | best_reasons = reasons; | ||
686 | } | 845 | } |
687 | best_crl = crl; | ||
688 | } | 846 | } |
847 | |||
689 | if (best_crl) | 848 | if (best_crl) |
690 | { | 849 | { |
850 | if (*pcrl) | ||
851 | X509_CRL_free(*pcrl); | ||
691 | *pcrl = best_crl; | 852 | *pcrl = best_crl; |
692 | CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509); | 853 | *pissuer = best_crl_issuer; |
854 | *pscore = best_score; | ||
855 | *preasons = best_reasons; | ||
856 | CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL); | ||
857 | if (*pdcrl) | ||
858 | { | ||
859 | X509_CRL_free(*pdcrl); | ||
860 | *pdcrl = NULL; | ||
861 | } | ||
862 | get_delta_sk(ctx, pdcrl, pscore, best_crl, crls); | ||
693 | } | 863 | } |
694 | 864 | ||
865 | if (best_score >= CRL_SCORE_VALID) | ||
866 | return 1; | ||
867 | |||
695 | return 0; | 868 | return 0; |
696 | } | 869 | } |
697 | 870 | ||
698 | /* Retrieve CRL corresponding to certificate: currently just a | 871 | /* Compare two CRL extensions for delta checking purposes. They should be |
699 | * subject lookup: maybe use AKID later... | 872 | * both present or both absent. If both present all fields must be identical. |
700 | */ | 873 | */ |
701 | static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) | 874 | |
875 | static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid) | ||
702 | { | 876 | { |
703 | int ok; | 877 | ASN1_OCTET_STRING *exta, *extb; |
704 | X509_CRL *crl = NULL; | 878 | int i; |
705 | X509_OBJECT xobj; | 879 | i = X509_CRL_get_ext_by_NID(a, nid, 0); |
706 | X509_NAME *nm; | 880 | if (i >= 0) |
707 | nm = X509_get_issuer_name(x); | ||
708 | ok = get_crl_sk(ctx, &crl, nm, ctx->crls); | ||
709 | if (ok) | ||
710 | { | 881 | { |
711 | *pcrl = crl; | 882 | /* Can't have multiple occurrences */ |
883 | if (X509_CRL_get_ext_by_NID(a, nid, i) != -1) | ||
884 | return 0; | ||
885 | exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i)); | ||
886 | } | ||
887 | else | ||
888 | exta = NULL; | ||
889 | |||
890 | i = X509_CRL_get_ext_by_NID(b, nid, 0); | ||
891 | |||
892 | if (i >= 0) | ||
893 | { | ||
894 | |||
895 | if (X509_CRL_get_ext_by_NID(b, nid, i) != -1) | ||
896 | return 0; | ||
897 | extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i)); | ||
898 | } | ||
899 | else | ||
900 | extb = NULL; | ||
901 | |||
902 | if (!exta && !extb) | ||
712 | return 1; | 903 | return 1; |
904 | |||
905 | if (!exta || !extb) | ||
906 | return 0; | ||
907 | |||
908 | |||
909 | if (ASN1_OCTET_STRING_cmp(exta, extb)) | ||
910 | return 0; | ||
911 | |||
912 | return 1; | ||
913 | } | ||
914 | |||
915 | /* See if a base and delta are compatible */ | ||
916 | |||
917 | static int check_delta_base(X509_CRL *delta, X509_CRL *base) | ||
918 | { | ||
919 | /* Delta CRL must be a delta */ | ||
920 | if (!delta->base_crl_number) | ||
921 | return 0; | ||
922 | /* Base must have a CRL number */ | ||
923 | if (!base->crl_number) | ||
924 | return 0; | ||
925 | /* Issuer names must match */ | ||
926 | if (X509_NAME_cmp(X509_CRL_get_issuer(base), | ||
927 | X509_CRL_get_issuer(delta))) | ||
928 | return 0; | ||
929 | /* AKID and IDP must match */ | ||
930 | if (!crl_extension_match(delta, base, NID_authority_key_identifier)) | ||
931 | return 0; | ||
932 | if (!crl_extension_match(delta, base, NID_issuing_distribution_point)) | ||
933 | return 0; | ||
934 | /* Delta CRL base number must not exceed Full CRL number. */ | ||
935 | if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0) | ||
936 | return 0; | ||
937 | /* Delta CRL number must exceed full CRL number */ | ||
938 | if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0) | ||
939 | return 1; | ||
940 | return 0; | ||
941 | } | ||
942 | |||
943 | /* For a given base CRL find a delta... maybe extend to delta scoring | ||
944 | * or retrieve a chain of deltas... | ||
945 | */ | ||
946 | |||
947 | static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore, | ||
948 | X509_CRL *base, STACK_OF(X509_CRL) *crls) | ||
949 | { | ||
950 | X509_CRL *delta; | ||
951 | int i; | ||
952 | if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS)) | ||
953 | return; | ||
954 | if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST)) | ||
955 | return; | ||
956 | for (i = 0; i < sk_X509_CRL_num(crls); i++) | ||
957 | { | ||
958 | delta = sk_X509_CRL_value(crls, i); | ||
959 | if (check_delta_base(delta, base)) | ||
960 | { | ||
961 | if (check_crl_time(ctx, delta, 0)) | ||
962 | *pscore |= CRL_SCORE_TIME_DELTA; | ||
963 | CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL); | ||
964 | *dcrl = delta; | ||
965 | return; | ||
966 | } | ||
967 | } | ||
968 | *dcrl = NULL; | ||
969 | } | ||
970 | |||
971 | /* For a given CRL return how suitable it is for the supplied certificate 'x'. | ||
972 | * The return value is a mask of several criteria. | ||
973 | * If the issuer is not the certificate issuer this is returned in *pissuer. | ||
974 | * The reasons mask is also used to determine if the CRL is suitable: if | ||
975 | * no new reasons the CRL is rejected, otherwise reasons is updated. | ||
976 | */ | ||
977 | |||
978 | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, | ||
979 | unsigned int *preasons, | ||
980 | X509_CRL *crl, X509 *x) | ||
981 | { | ||
982 | |||
983 | int crl_score = 0; | ||
984 | unsigned int tmp_reasons = *preasons, crl_reasons; | ||
985 | |||
986 | /* First see if we can reject CRL straight away */ | ||
987 | |||
988 | /* Invalid IDP cannot be processed */ | ||
989 | if (crl->idp_flags & IDP_INVALID) | ||
990 | return 0; | ||
991 | /* Reason codes or indirect CRLs need extended CRL support */ | ||
992 | if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) | ||
993 | { | ||
994 | if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) | ||
995 | return 0; | ||
996 | } | ||
997 | else if (crl->idp_flags & IDP_REASONS) | ||
998 | { | ||
999 | /* If no new reasons reject */ | ||
1000 | if (!(crl->idp_reasons & ~tmp_reasons)) | ||
1001 | return 0; | ||
1002 | } | ||
1003 | /* Don't process deltas at this stage */ | ||
1004 | else if (crl->base_crl_number) | ||
1005 | return 0; | ||
1006 | /* If issuer name doesn't match certificate need indirect CRL */ | ||
1007 | if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) | ||
1008 | { | ||
1009 | if (!(crl->idp_flags & IDP_INDIRECT)) | ||
1010 | return 0; | ||
1011 | } | ||
1012 | else | ||
1013 | crl_score |= CRL_SCORE_ISSUER_NAME; | ||
1014 | |||
1015 | if (!(crl->flags & EXFLAG_CRITICAL)) | ||
1016 | crl_score |= CRL_SCORE_NOCRITICAL; | ||
1017 | |||
1018 | /* Check expiry */ | ||
1019 | if (check_crl_time(ctx, crl, 0)) | ||
1020 | crl_score |= CRL_SCORE_TIME; | ||
1021 | |||
1022 | /* Check authority key ID and locate certificate issuer */ | ||
1023 | crl_akid_check(ctx, crl, pissuer, &crl_score); | ||
1024 | |||
1025 | /* If we can't locate certificate issuer at this point forget it */ | ||
1026 | |||
1027 | if (!(crl_score & CRL_SCORE_AKID)) | ||
1028 | return 0; | ||
1029 | |||
1030 | /* Check cert for matching CRL distribution points */ | ||
1031 | |||
1032 | if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) | ||
1033 | { | ||
1034 | /* If no new reasons reject */ | ||
1035 | if (!(crl_reasons & ~tmp_reasons)) | ||
1036 | return 0; | ||
1037 | tmp_reasons |= crl_reasons; | ||
1038 | crl_score |= CRL_SCORE_SCOPE; | ||
713 | } | 1039 | } |
714 | 1040 | ||
715 | ok = X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj); | 1041 | *preasons = tmp_reasons; |
1042 | |||
1043 | return crl_score; | ||
1044 | |||
1045 | } | ||
1046 | |||
1047 | static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, | ||
1048 | X509 **pissuer, int *pcrl_score) | ||
1049 | { | ||
1050 | X509 *crl_issuer = NULL; | ||
1051 | X509_NAME *cnm = X509_CRL_get_issuer(crl); | ||
1052 | int cidx = ctx->error_depth; | ||
1053 | int i; | ||
716 | 1054 | ||
717 | if (!ok) | 1055 | if (cidx != sk_X509_num(ctx->chain) - 1) |
1056 | cidx++; | ||
1057 | |||
1058 | crl_issuer = sk_X509_value(ctx->chain, cidx); | ||
1059 | |||
1060 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) | ||
718 | { | 1061 | { |
719 | /* If we got a near match from get_crl_sk use that */ | 1062 | if (*pcrl_score & CRL_SCORE_ISSUER_NAME) |
720 | if (crl) | ||
721 | { | 1063 | { |
722 | *pcrl = crl; | 1064 | *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_ISSUER_CERT; |
723 | return 1; | 1065 | *pissuer = crl_issuer; |
1066 | return; | ||
1067 | } | ||
1068 | } | ||
1069 | |||
1070 | for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) | ||
1071 | { | ||
1072 | crl_issuer = sk_X509_value(ctx->chain, cidx); | ||
1073 | if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) | ||
1074 | continue; | ||
1075 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) | ||
1076 | { | ||
1077 | *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_SAME_PATH; | ||
1078 | *pissuer = crl_issuer; | ||
1079 | return; | ||
1080 | } | ||
1081 | } | ||
1082 | |||
1083 | /* Anything else needs extended CRL support */ | ||
1084 | |||
1085 | if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) | ||
1086 | return; | ||
1087 | |||
1088 | /* Otherwise the CRL issuer is not on the path. Look for it in the | ||
1089 | * set of untrusted certificates. | ||
1090 | */ | ||
1091 | for (i = 0; i < sk_X509_num(ctx->untrusted); i++) | ||
1092 | { | ||
1093 | crl_issuer = sk_X509_value(ctx->untrusted, i); | ||
1094 | if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) | ||
1095 | continue; | ||
1096 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) | ||
1097 | { | ||
1098 | *pissuer = crl_issuer; | ||
1099 | *pcrl_score |= CRL_SCORE_AKID; | ||
1100 | return; | ||
724 | } | 1101 | } |
1102 | } | ||
1103 | } | ||
1104 | |||
1105 | /* Check the path of a CRL issuer certificate. This creates a new | ||
1106 | * X509_STORE_CTX and populates it with most of the parameters from the | ||
1107 | * parent. This could be optimised somewhat since a lot of path checking | ||
1108 | * will be duplicated by the parent, but this will rarely be used in | ||
1109 | * practice. | ||
1110 | */ | ||
1111 | |||
1112 | static int check_crl_path(X509_STORE_CTX *ctx, X509 *x) | ||
1113 | { | ||
1114 | X509_STORE_CTX crl_ctx; | ||
1115 | int ret; | ||
1116 | /* Don't allow recursive CRL path validation */ | ||
1117 | if (ctx->parent) | ||
725 | return 0; | 1118 | return 0; |
1119 | if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted)) | ||
1120 | return -1; | ||
1121 | |||
1122 | crl_ctx.crls = ctx->crls; | ||
1123 | /* Copy verify params across */ | ||
1124 | X509_STORE_CTX_set0_param(&crl_ctx, ctx->param); | ||
1125 | |||
1126 | crl_ctx.parent = ctx; | ||
1127 | crl_ctx.verify_cb = ctx->verify_cb; | ||
1128 | |||
1129 | /* Verify CRL issuer */ | ||
1130 | ret = X509_verify_cert(&crl_ctx); | ||
1131 | |||
1132 | if (ret <= 0) | ||
1133 | goto err; | ||
1134 | |||
1135 | /* Check chain is acceptable */ | ||
1136 | |||
1137 | ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain); | ||
1138 | err: | ||
1139 | X509_STORE_CTX_cleanup(&crl_ctx); | ||
1140 | return ret; | ||
1141 | } | ||
1142 | |||
1143 | /* RFC3280 says nothing about the relationship between CRL path | ||
1144 | * and certificate path, which could lead to situations where a | ||
1145 | * certificate could be revoked or validated by a CA not authorised | ||
1146 | * to do so. RFC5280 is more strict and states that the two paths must | ||
1147 | * end in the same trust anchor, though some discussions remain... | ||
1148 | * until this is resolved we use the RFC5280 version | ||
1149 | */ | ||
1150 | |||
1151 | static int check_crl_chain(X509_STORE_CTX *ctx, | ||
1152 | STACK_OF(X509) *cert_path, | ||
1153 | STACK_OF(X509) *crl_path) | ||
1154 | { | ||
1155 | X509 *cert_ta, *crl_ta; | ||
1156 | cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1); | ||
1157 | crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1); | ||
1158 | if (!X509_cmp(cert_ta, crl_ta)) | ||
1159 | return 1; | ||
1160 | return 0; | ||
1161 | } | ||
1162 | |||
1163 | /* Check for match between two dist point names: three separate cases. | ||
1164 | * 1. Both are relative names and compare X509_NAME types. | ||
1165 | * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES. | ||
1166 | * 3. Both are full names and compare two GENERAL_NAMES. | ||
1167 | * 4. One is NULL: automatic match. | ||
1168 | */ | ||
1169 | |||
1170 | |||
1171 | static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) | ||
1172 | { | ||
1173 | X509_NAME *nm = NULL; | ||
1174 | GENERAL_NAMES *gens = NULL; | ||
1175 | GENERAL_NAME *gena, *genb; | ||
1176 | int i, j; | ||
1177 | if (!a || !b) | ||
1178 | return 1; | ||
1179 | if (a->type == 1) | ||
1180 | { | ||
1181 | if (!a->dpname) | ||
1182 | return 0; | ||
1183 | /* Case 1: two X509_NAME */ | ||
1184 | if (b->type == 1) | ||
1185 | { | ||
1186 | if (!b->dpname) | ||
1187 | return 0; | ||
1188 | if (!X509_NAME_cmp(a->dpname, b->dpname)) | ||
1189 | return 1; | ||
1190 | else | ||
1191 | return 0; | ||
1192 | } | ||
1193 | /* Case 2: set name and GENERAL_NAMES appropriately */ | ||
1194 | nm = a->dpname; | ||
1195 | gens = b->name.fullname; | ||
1196 | } | ||
1197 | else if (b->type == 1) | ||
1198 | { | ||
1199 | if (!b->dpname) | ||
1200 | return 0; | ||
1201 | /* Case 2: set name and GENERAL_NAMES appropriately */ | ||
1202 | gens = a->name.fullname; | ||
1203 | nm = b->dpname; | ||
726 | } | 1204 | } |
727 | 1205 | ||
728 | *pcrl = xobj.data.crl; | 1206 | /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */ |
1207 | if (nm) | ||
1208 | { | ||
1209 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) | ||
1210 | { | ||
1211 | gena = sk_GENERAL_NAME_value(gens, i); | ||
1212 | if (gena->type != GEN_DIRNAME) | ||
1213 | continue; | ||
1214 | if (!X509_NAME_cmp(nm, gena->d.directoryName)) | ||
1215 | return 1; | ||
1216 | } | ||
1217 | return 0; | ||
1218 | } | ||
1219 | |||
1220 | /* Else case 3: two GENERAL_NAMES */ | ||
1221 | |||
1222 | for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) | ||
1223 | { | ||
1224 | gena = sk_GENERAL_NAME_value(a->name.fullname, i); | ||
1225 | for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) | ||
1226 | { | ||
1227 | genb = sk_GENERAL_NAME_value(b->name.fullname, j); | ||
1228 | if (!GENERAL_NAME_cmp(gena, genb)) | ||
1229 | return 1; | ||
1230 | } | ||
1231 | } | ||
1232 | |||
1233 | return 0; | ||
1234 | |||
1235 | } | ||
1236 | |||
1237 | static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score) | ||
1238 | { | ||
1239 | int i; | ||
1240 | X509_NAME *nm = X509_CRL_get_issuer(crl); | ||
1241 | /* If no CRLissuer return is successful iff don't need a match */ | ||
1242 | if (!dp->CRLissuer) | ||
1243 | return !!(crl_score & CRL_SCORE_ISSUER_NAME); | ||
1244 | for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) | ||
1245 | { | ||
1246 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); | ||
1247 | if (gen->type != GEN_DIRNAME) | ||
1248 | continue; | ||
1249 | if (!X509_NAME_cmp(gen->d.directoryName, nm)) | ||
1250 | return 1; | ||
1251 | } | ||
1252 | return 0; | ||
1253 | } | ||
1254 | |||
1255 | /* Check CRLDP and IDP */ | ||
1256 | |||
1257 | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, | ||
1258 | unsigned int *preasons) | ||
1259 | { | ||
1260 | int i; | ||
1261 | if (crl->idp_flags & IDP_ONLYATTR) | ||
1262 | return 0; | ||
1263 | if (x->ex_flags & EXFLAG_CA) | ||
1264 | { | ||
1265 | if (crl->idp_flags & IDP_ONLYUSER) | ||
1266 | return 0; | ||
1267 | } | ||
1268 | else | ||
1269 | { | ||
1270 | if (crl->idp_flags & IDP_ONLYCA) | ||
1271 | return 0; | ||
1272 | } | ||
1273 | *preasons = crl->idp_reasons; | ||
1274 | for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) | ||
1275 | { | ||
1276 | DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i); | ||
1277 | if (crldp_check_crlissuer(dp, crl, crl_score)) | ||
1278 | { | ||
1279 | if (!crl->idp || | ||
1280 | idp_check_dp(dp->distpoint, crl->idp->distpoint)) | ||
1281 | { | ||
1282 | *preasons &= dp->dp_reasons; | ||
1283 | return 1; | ||
1284 | } | ||
1285 | } | ||
1286 | } | ||
1287 | if ((!crl->idp || !crl->idp->distpoint) && (crl_score & CRL_SCORE_ISSUER_NAME)) | ||
1288 | return 1; | ||
1289 | return 0; | ||
1290 | } | ||
1291 | |||
1292 | /* Retrieve CRL corresponding to current certificate. | ||
1293 | * If deltas enabled try to find a delta CRL too | ||
1294 | */ | ||
1295 | |||
1296 | static int get_crl_delta(X509_STORE_CTX *ctx, | ||
1297 | X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x) | ||
1298 | { | ||
1299 | int ok; | ||
1300 | X509 *issuer = NULL; | ||
1301 | int crl_score = 0; | ||
1302 | unsigned int reasons; | ||
1303 | X509_CRL *crl = NULL, *dcrl = NULL; | ||
1304 | STACK_OF(X509_CRL) *skcrl; | ||
1305 | X509_NAME *nm = X509_get_issuer_name(x); | ||
1306 | reasons = ctx->current_reasons; | ||
1307 | ok = get_crl_sk(ctx, &crl, &dcrl, | ||
1308 | &issuer, &crl_score, &reasons, ctx->crls); | ||
1309 | |||
1310 | if (ok) | ||
1311 | goto done; | ||
1312 | |||
1313 | /* Lookup CRLs from store */ | ||
1314 | |||
1315 | skcrl = ctx->lookup_crls(ctx, nm); | ||
1316 | |||
1317 | /* If no CRLs found and a near match from get_crl_sk use that */ | ||
1318 | if (!skcrl && crl) | ||
1319 | goto done; | ||
1320 | |||
1321 | get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl); | ||
1322 | |||
1323 | sk_X509_CRL_pop_free(skcrl, X509_CRL_free); | ||
1324 | |||
1325 | done: | ||
1326 | |||
1327 | /* If we got any kind of CRL use it and return success */ | ||
729 | if (crl) | 1328 | if (crl) |
730 | X509_CRL_free(crl); | 1329 | { |
731 | return 1; | 1330 | ctx->current_issuer = issuer; |
1331 | ctx->current_crl_score = crl_score; | ||
1332 | ctx->current_reasons = reasons; | ||
1333 | *pcrl = crl; | ||
1334 | *pdcrl = dcrl; | ||
1335 | return 1; | ||
1336 | } | ||
1337 | |||
1338 | return 0; | ||
732 | } | 1339 | } |
733 | 1340 | ||
734 | /* Check CRL validity */ | 1341 | /* Check CRL validity */ |
@@ -739,10 +1346,14 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) | |||
739 | int ok = 0, chnum, cnum; | 1346 | int ok = 0, chnum, cnum; |
740 | cnum = ctx->error_depth; | 1347 | cnum = ctx->error_depth; |
741 | chnum = sk_X509_num(ctx->chain) - 1; | 1348 | chnum = sk_X509_num(ctx->chain) - 1; |
742 | /* Find CRL issuer: if not last certificate then issuer | 1349 | /* if we have an alternative CRL issuer cert use that */ |
1350 | if (ctx->current_issuer) | ||
1351 | issuer = ctx->current_issuer; | ||
1352 | |||
1353 | /* Else find CRL issuer: if not last certificate then issuer | ||
743 | * is next certificate in chain. | 1354 | * is next certificate in chain. |
744 | */ | 1355 | */ |
745 | if(cnum < chnum) | 1356 | else if (cnum < chnum) |
746 | issuer = sk_X509_value(ctx->chain, cnum + 1); | 1357 | issuer = sk_X509_value(ctx->chain, cnum + 1); |
747 | else | 1358 | else |
748 | { | 1359 | { |
@@ -758,13 +1369,52 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) | |||
758 | 1369 | ||
759 | if(issuer) | 1370 | if(issuer) |
760 | { | 1371 | { |
761 | /* Check for cRLSign bit if keyUsage present */ | 1372 | /* Skip most tests for deltas because they have already |
762 | if ((issuer->ex_flags & EXFLAG_KUSAGE) && | 1373 | * been done |
763 | !(issuer->ex_kusage & KU_CRL_SIGN)) | 1374 | */ |
1375 | if (!crl->base_crl_number) | ||
764 | { | 1376 | { |
765 | ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN; | 1377 | /* Check for cRLSign bit if keyUsage present */ |
766 | ok = ctx->verify_cb(0, ctx); | 1378 | if ((issuer->ex_flags & EXFLAG_KUSAGE) && |
767 | if(!ok) goto err; | 1379 | !(issuer->ex_kusage & KU_CRL_SIGN)) |
1380 | { | ||
1381 | ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN; | ||
1382 | ok = ctx->verify_cb(0, ctx); | ||
1383 | if(!ok) goto err; | ||
1384 | } | ||
1385 | |||
1386 | if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) | ||
1387 | { | ||
1388 | ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE; | ||
1389 | ok = ctx->verify_cb(0, ctx); | ||
1390 | if(!ok) goto err; | ||
1391 | } | ||
1392 | |||
1393 | if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) | ||
1394 | { | ||
1395 | if (check_crl_path(ctx, ctx->current_issuer) <= 0) | ||
1396 | { | ||
1397 | ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR; | ||
1398 | ok = ctx->verify_cb(0, ctx); | ||
1399 | if(!ok) goto err; | ||
1400 | } | ||
1401 | } | ||
1402 | |||
1403 | if (crl->idp_flags & IDP_INVALID) | ||
1404 | { | ||
1405 | ctx->error = X509_V_ERR_INVALID_EXTENSION; | ||
1406 | ok = ctx->verify_cb(0, ctx); | ||
1407 | if(!ok) goto err; | ||
1408 | } | ||
1409 | |||
1410 | |||
1411 | } | ||
1412 | |||
1413 | if (!(ctx->current_crl_score & CRL_SCORE_TIME)) | ||
1414 | { | ||
1415 | ok = check_crl_time(ctx, crl, 1); | ||
1416 | if (!ok) | ||
1417 | goto err; | ||
768 | } | 1418 | } |
769 | 1419 | ||
770 | /* Attempt to get issuer certificate public key */ | 1420 | /* Attempt to get issuer certificate public key */ |
@@ -788,10 +1438,6 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) | |||
788 | } | 1438 | } |
789 | } | 1439 | } |
790 | 1440 | ||
791 | ok = check_crl_time(ctx, crl, 1); | ||
792 | if (!ok) | ||
793 | goto err; | ||
794 | |||
795 | ok = 1; | 1441 | ok = 1; |
796 | 1442 | ||
797 | err: | 1443 | err: |
@@ -802,62 +1448,43 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) | |||
802 | /* Check certificate against CRL */ | 1448 | /* Check certificate against CRL */ |
803 | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) | 1449 | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) |
804 | { | 1450 | { |
805 | int idx, ok; | 1451 | int ok; |
806 | X509_REVOKED rtmp; | 1452 | X509_REVOKED *rev; |
807 | STACK_OF(X509_EXTENSION) *exts; | 1453 | /* The rules changed for this... previously if a CRL contained |
808 | X509_EXTENSION *ext; | 1454 | * unhandled critical extensions it could still be used to indicate |
809 | /* Look for serial number of certificate in CRL */ | 1455 | * a certificate was revoked. This has since been changed since |
810 | rtmp.serialNumber = X509_get_serialNumber(x); | 1456 | * critical extension can change the meaning of CRL entries. |
811 | /* Sort revoked into serial number order if not already sorted. | 1457 | */ |
812 | * Do this under a lock to avoid race condition. | 1458 | if (crl->flags & EXFLAG_CRITICAL) |
813 | */ | ||
814 | if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) | ||
815 | { | 1459 | { |
816 | CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL); | 1460 | if (ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) |
817 | sk_X509_REVOKED_sort(crl->crl->revoked); | 1461 | return 1; |
818 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL); | 1462 | ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION; |
1463 | ok = ctx->verify_cb(0, ctx); | ||
1464 | if(!ok) | ||
1465 | return 0; | ||
819 | } | 1466 | } |
820 | idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp); | 1467 | /* Look for serial number of certificate in CRL |
821 | /* If found assume revoked: want something cleverer than | 1468 | * If found make sure reason is not removeFromCRL. |
822 | * this to handle entry extensions in V2 CRLs. | ||
823 | */ | 1469 | */ |
824 | if(idx >= 0) | 1470 | if (X509_CRL_get0_by_cert(crl, &rev, x)) |
825 | { | 1471 | { |
1472 | if (rev->reason == CRL_REASON_REMOVE_FROM_CRL) | ||
1473 | return 2; | ||
826 | ctx->error = X509_V_ERR_CERT_REVOKED; | 1474 | ctx->error = X509_V_ERR_CERT_REVOKED; |
827 | ok = ctx->verify_cb(0, ctx); | 1475 | ok = ctx->verify_cb(0, ctx); |
828 | if (!ok) return 0; | 1476 | if (!ok) |
1477 | return 0; | ||
829 | } | 1478 | } |
830 | 1479 | ||
831 | if (ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) | ||
832 | return 1; | ||
833 | |||
834 | /* See if we have any critical CRL extensions: since we | ||
835 | * currently don't handle any CRL extensions the CRL must be | ||
836 | * rejected. | ||
837 | * This code accesses the X509_CRL structure directly: applications | ||
838 | * shouldn't do this. | ||
839 | */ | ||
840 | |||
841 | exts = crl->crl->extensions; | ||
842 | |||
843 | for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) | ||
844 | { | ||
845 | ext = sk_X509_EXTENSION_value(exts, idx); | ||
846 | if (ext->critical > 0) | ||
847 | { | ||
848 | ctx->error = | ||
849 | X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION; | ||
850 | ok = ctx->verify_cb(0, ctx); | ||
851 | if(!ok) return 0; | ||
852 | break; | ||
853 | } | ||
854 | } | ||
855 | return 1; | 1480 | return 1; |
856 | } | 1481 | } |
857 | 1482 | ||
858 | static int check_policy(X509_STORE_CTX *ctx) | 1483 | static int check_policy(X509_STORE_CTX *ctx) |
859 | { | 1484 | { |
860 | int ret; | 1485 | int ret; |
1486 | if (ctx->parent) | ||
1487 | return 1; | ||
861 | ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain, | 1488 | ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain, |
862 | ctx->param->policies, ctx->param->flags); | 1489 | ctx->param->policies, ctx->param->flags); |
863 | if (ret == 0) | 1490 | if (ret == 0) |
@@ -880,7 +1507,8 @@ static int check_policy(X509_STORE_CTX *ctx) | |||
880 | continue; | 1507 | continue; |
881 | ctx->current_cert = x; | 1508 | ctx->current_cert = x; |
882 | ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION; | 1509 | ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION; |
883 | ret = ctx->verify_cb(0, ctx); | 1510 | if(!ctx->verify_cb(0, ctx)) |
1511 | return 0; | ||
884 | } | 1512 | } |
885 | return 1; | 1513 | return 1; |
886 | } | 1514 | } |
@@ -986,7 +1614,12 @@ static int internal_verify(X509_STORE_CTX *ctx) | |||
986 | while (n >= 0) | 1614 | while (n >= 0) |
987 | { | 1615 | { |
988 | ctx->error_depth=n; | 1616 | ctx->error_depth=n; |
989 | if (!xs->valid) | 1617 | |
1618 | /* Skip signature check for self signed certificates unless | ||
1619 | * explicitly asked for. It doesn't add any security and | ||
1620 | * just wastes time. | ||
1621 | */ | ||
1622 | if (!xs->valid && (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) | ||
990 | { | 1623 | { |
991 | if ((pkey=X509_get_pubkey(xi)) == NULL) | 1624 | if ((pkey=X509_get_pubkey(xi)) == NULL) |
992 | { | 1625 | { |
@@ -996,13 +1629,6 @@ static int internal_verify(X509_STORE_CTX *ctx) | |||
996 | if (!ok) goto end; | 1629 | if (!ok) goto end; |
997 | } | 1630 | } |
998 | else if (X509_verify(xs,pkey) <= 0) | 1631 | else if (X509_verify(xs,pkey) <= 0) |
999 | /* XXX For the final trusted self-signed cert, | ||
1000 | * this is a waste of time. That check should | ||
1001 | * optional so that e.g. 'openssl x509' can be | ||
1002 | * used to detect invalid self-signatures, but | ||
1003 | * we don't verify again and again in SSL | ||
1004 | * handshakes and the like once the cert has | ||
1005 | * been declared trusted. */ | ||
1006 | { | 1632 | { |
1007 | ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE; | 1633 | ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE; |
1008 | ctx->current_cert=xs; | 1634 | ctx->current_cert=xs; |
@@ -1041,12 +1667,12 @@ end: | |||
1041 | return ok; | 1667 | return ok; |
1042 | } | 1668 | } |
1043 | 1669 | ||
1044 | int X509_cmp_current_time(ASN1_TIME *ctm) | 1670 | int X509_cmp_current_time(const ASN1_TIME *ctm) |
1045 | { | 1671 | { |
1046 | return X509_cmp_time(ctm, NULL); | 1672 | return X509_cmp_time(ctm, NULL); |
1047 | } | 1673 | } |
1048 | 1674 | ||
1049 | int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time) | 1675 | int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time) |
1050 | { | 1676 | { |
1051 | char *str; | 1677 | char *str; |
1052 | ASN1_TIME atm; | 1678 | ASN1_TIME atm; |
@@ -1101,6 +1727,7 @@ int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time) | |||
1101 | offset= -offset; | 1727 | offset= -offset; |
1102 | } | 1728 | } |
1103 | atm.type=ctm->type; | 1729 | atm.type=ctm->type; |
1730 | atm.flags = 0; | ||
1104 | atm.length=sizeof(buff2); | 1731 | atm.length=sizeof(buff2); |
1105 | atm.data=(unsigned char *)buff2; | 1732 | atm.data=(unsigned char *)buff2; |
1106 | 1733 | ||
@@ -1129,19 +1756,28 @@ ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj) | |||
1129 | return X509_time_adj(s, adj, NULL); | 1756 | return X509_time_adj(s, adj, NULL); |
1130 | } | 1757 | } |
1131 | 1758 | ||
1132 | ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *in_tm) | 1759 | ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm) |
1760 | { | ||
1761 | return X509_time_adj_ex(s, 0, offset_sec, in_tm); | ||
1762 | } | ||
1763 | |||
1764 | ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, | ||
1765 | int offset_day, long offset_sec, time_t *in_tm) | ||
1133 | { | 1766 | { |
1134 | time_t t; | 1767 | time_t t; |
1135 | int type = -1; | ||
1136 | 1768 | ||
1137 | if (in_tm) t = *in_tm; | 1769 | if (in_tm) t = *in_tm; |
1138 | else time(&t); | 1770 | else time(&t); |
1139 | 1771 | ||
1140 | t+=adj; | 1772 | if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) |
1141 | if (s) type = s->type; | 1773 | { |
1142 | if (type == V_ASN1_UTCTIME) return ASN1_UTCTIME_set(s,t); | 1774 | if (s->type == V_ASN1_UTCTIME) |
1143 | if (type == V_ASN1_GENERALIZEDTIME) return ASN1_GENERALIZEDTIME_set(s, t); | 1775 | return ASN1_UTCTIME_adj(s,t, offset_day, offset_sec); |
1144 | return ASN1_TIME_set(s, t); | 1776 | if (s->type == V_ASN1_GENERALIZEDTIME) |
1777 | return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, | ||
1778 | offset_sec); | ||
1779 | } | ||
1780 | return ASN1_TIME_adj(s, t, offset_day, offset_sec); | ||
1145 | } | 1781 | } |
1146 | 1782 | ||
1147 | int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain) | 1783 | int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain) |
@@ -1244,6 +1880,21 @@ STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx) | |||
1244 | return chain; | 1880 | return chain; |
1245 | } | 1881 | } |
1246 | 1882 | ||
1883 | X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx) | ||
1884 | { | ||
1885 | return ctx->current_issuer; | ||
1886 | } | ||
1887 | |||
1888 | X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx) | ||
1889 | { | ||
1890 | return ctx->current_crl; | ||
1891 | } | ||
1892 | |||
1893 | X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx) | ||
1894 | { | ||
1895 | return ctx->parent; | ||
1896 | } | ||
1897 | |||
1247 | void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x) | 1898 | void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x) |
1248 | { | 1899 | { |
1249 | ctx->cert=x; | 1900 | ctx->cert=x; |
@@ -1365,6 +2016,7 @@ int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | |||
1365 | ctx->current_cert=NULL; | 2016 | ctx->current_cert=NULL; |
1366 | ctx->current_issuer=NULL; | 2017 | ctx->current_issuer=NULL; |
1367 | ctx->tree = NULL; | 2018 | ctx->tree = NULL; |
2019 | ctx->parent = NULL; | ||
1368 | 2020 | ||
1369 | ctx->param = X509_VERIFY_PARAM_new(); | 2021 | ctx->param = X509_VERIFY_PARAM_new(); |
1370 | 2022 | ||
@@ -1430,7 +2082,7 @@ int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | |||
1430 | if (store && store->get_crl) | 2082 | if (store && store->get_crl) |
1431 | ctx->get_crl = store->get_crl; | 2083 | ctx->get_crl = store->get_crl; |
1432 | else | 2084 | else |
1433 | ctx->get_crl = get_crl; | 2085 | ctx->get_crl = NULL; |
1434 | 2086 | ||
1435 | if (store && store->check_crl) | 2087 | if (store && store->check_crl) |
1436 | ctx->check_crl = store->check_crl; | 2088 | ctx->check_crl = store->check_crl; |
@@ -1442,6 +2094,16 @@ int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | |||
1442 | else | 2094 | else |
1443 | ctx->cert_crl = cert_crl; | 2095 | ctx->cert_crl = cert_crl; |
1444 | 2096 | ||
2097 | if (store && store->lookup_certs) | ||
2098 | ctx->lookup_certs = store->lookup_certs; | ||
2099 | else | ||
2100 | ctx->lookup_certs = X509_STORE_get1_certs; | ||
2101 | |||
2102 | if (store && store->lookup_crls) | ||
2103 | ctx->lookup_crls = store->lookup_crls; | ||
2104 | else | ||
2105 | ctx->lookup_crls = X509_STORE_get1_crls; | ||
2106 | |||
1445 | ctx->check_policy = check_policy; | 2107 | ctx->check_policy = check_policy; |
1446 | 2108 | ||
1447 | 2109 | ||
@@ -1474,7 +2136,8 @@ void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) | |||
1474 | if (ctx->cleanup) ctx->cleanup(ctx); | 2136 | if (ctx->cleanup) ctx->cleanup(ctx); |
1475 | if (ctx->param != NULL) | 2137 | if (ctx->param != NULL) |
1476 | { | 2138 | { |
1477 | X509_VERIFY_PARAM_free(ctx->param); | 2139 | if (ctx->parent == NULL) |
2140 | X509_VERIFY_PARAM_free(ctx->param); | ||
1478 | ctx->param=NULL; | 2141 | ctx->param=NULL; |
1479 | } | 2142 | } |
1480 | if (ctx->tree != NULL) | 2143 | if (ctx->tree != NULL) |