summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/x509/x509_extensions_test.c266
1 files changed, 265 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/x509/x509_extensions_test.c b/src/regress/lib/libcrypto/x509/x509_extensions_test.c
index 9005333fe7..2961b0612b 100644
--- a/src/regress/lib/libcrypto/x509/x509_extensions_test.c
+++ b/src/regress/lib/libcrypto/x509/x509_extensions_test.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: x509_extensions_test.c,v 1.2 2024/05/28 15:42:09 tb Exp $ */ 1/* $OpenBSD: x509_extensions_test.c,v 1.3 2024/06/17 05:04:54 tb Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
@@ -27,6 +27,9 @@
27#define ASN1_BOOLEAN_TRUE 0xff 27#define ASN1_BOOLEAN_TRUE 0xff
28#define ASN1_BOOLEAN_FALSE 0x00 28#define ASN1_BOOLEAN_FALSE 0x00
29 29
30#define X509V3_EXT_CRITICAL 1
31#define X509V3_EXT_NONCRITICAL 0
32
30static BASIC_CONSTRAINTS * 33static BASIC_CONSTRAINTS *
31create_basic_constraints(int ca) 34create_basic_constraints(int ca)
32{ 35{
@@ -40,6 +43,20 @@ create_basic_constraints(int ca)
40 return bc; 43 return bc;
41} 44}
42 45
46static X509_EXTENSION *
47ext_create_basic_constraints(int ca, int critical)
48{
49 X509_EXTENSION *ext;
50 BASIC_CONSTRAINTS *bc;
51
52 bc = create_basic_constraints(ca);
53 if ((ext = X509V3_EXT_i2d(NID_basic_constraints, critical, bc)) == NULL)
54 errx(1, "X509V3_EXT_i2d");
55 BASIC_CONSTRAINTS_free(bc);
56
57 return ext;
58}
59
43static int 60static int
44test_x509v3_add1_i2d_empty_stack(STACK_OF(X509_EXTENSION) **extensions) 61test_x509v3_add1_i2d_empty_stack(STACK_OF(X509_EXTENSION) **extensions)
45{ 62{
@@ -644,12 +661,259 @@ test_x509v3_add1_i2d(void)
644 return failed; 661 return failed;
645} 662}
646 663
664static int
665test_x509v3_get_d2i_null(void)
666{
667 X509_EXTENSION *ext;
668 int crit, idx;
669 int failed = 1;
670
671 if ((ext = X509V3_get_d2i(NULL, NID_undef, NULL, NULL)) != NULL) {
672 fprintf(stderr, "FAIL: %s: expected X509V3_get_d2i with three "
673 "NULL arguments to return NULL\n", __func__);
674 goto err;
675 }
676
677 idx = -5;
678 if (X509V3_get_d2i(NULL, NID_undef, &crit, &idx) != NULL) {
679 /* Leaks whatever garbage libcrypto decoded. What to do... */
680 fprintf(stderr, "FAIL: %s: expected X509V3_get_d2i NULL stack"
681 "to return NULL\n", __func__);
682 goto err;
683 }
684
685 if (crit != -1 || idx != -1) {
686 fprintf(stderr, "FAIL: %s: crit: want: %d, got: %d; "
687 "idx: want: %d, got: %d\n", __func__, -1, crit, -1, idx);
688 goto err;
689 }
690
691 failed = 0;
692
693 err:
694 X509_EXTENSION_free(ext);
695
696 return failed;
697}
698
699static int
700test_x509v3_get_d2i_multiple_basic_constraints(void)
701{
702 STACK_OF(X509_EXTENSION) *exts = NULL;
703 ASN1_BIT_STRING *abs = NULL;
704 BASIC_CONSTRAINTS *bc = NULL;
705 X509_EXTENSION *ext;
706 int crit, idx;
707 int ca, nid;
708 int failed = 1;
709
710 /*
711 * Create extension stack containing three basic constraints extensions:
712 * 1. critical CA basic constraints,
713 * 2. non-critical CA basic constraints,
714 * 3. critical non-CA basic constraints.
715 */
716
717 if ((exts = sk_X509_EXTENSION_new_null()) == NULL)
718 errx(1, "sk_X509_EXTENSION_new_null");
719
720 ca = 1;
721 ext = ext_create_basic_constraints(ca, X509V3_EXT_CRITICAL);
722
723 if (sk_X509_EXTENSION_push(exts, ext) <= 0)
724 errx(1, "sk_X509_EXTENSION_push");
725 ext = NULL;
726
727 ca = 1;
728 ext = ext_create_basic_constraints(ca, X509V3_EXT_NONCRITICAL);
729
730 if (sk_X509_EXTENSION_push(exts, ext) <= 0)
731 errx(1, "sk_X509_EXTENSION_push");
732 ext = NULL;
733
734 ca = 0;
735 ext = ext_create_basic_constraints(ca, X509V3_EXT_CRITICAL);
736
737 if (sk_X509_EXTENSION_push(exts, ext) <= 0)
738 errx(1, "sk_X509_EXTENSION_push");
739 ext = NULL;
740
741 /*
742 * There is no key usage in this stack, so we shouldn't find any.
743 */
744
745 nid = NID_key_usage;
746 if ((abs = X509V3_get_d2i(exts, nid, &crit, NULL)) != NULL) {
747 fprintf(stderr, "FAIL: %s: found key usage extension\n",
748 __func__);
749 goto err;
750 }
751 if (crit != -1) {
752 fprintf(stderr, "FAIL: %s: key usage: crit: want %d, got %d\n",
753 __func__, -1, crit);
754 goto err;
755 }
756
757 /*
758 * If we pass no idx and look for basic constraints,
759 * we should fail with crit == -2.
760 */
761
762 nid = NID_basic_constraints;
763 if ((bc = X509V3_get_d2i(exts, nid, &crit, NULL)) != NULL) {
764 fprintf(stderr, "FAIL: %s (NULL idx): did not expect to find "
765 "basic constraints\n", __func__);
766 goto err;
767 }
768 if (crit != -2) {
769 fprintf(stderr, "FAIL: %s: basic constraints, no idx: \n"
770 "crit: want %d, got %d\n", __func__, -2, crit);
771 goto err;
772 }
773
774 /*
775 * If we pass idx = -1 and look for basic constraints, we should find
776 * the first one: it is critical at idx = 0, with ca bit set to true.
777 */
778
779 nid = NID_basic_constraints;
780 idx = -1;
781 if ((bc = X509V3_get_d2i(exts, nid, &crit, &idx)) == NULL) {
782 fprintf(stderr, "FAIL: %s (idx %d): expected to find"
783 "basic constraints\n", __func__, -1);
784 goto err;
785 }
786 if (crit != 1) {
787 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
788 "crit: want %d, got %d\n", __func__, -1, 1, crit);
789 goto err;
790 }
791 if (idx != 0) {
792 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
793 "idx: want %d, got %d\n", __func__, -1, 0, idx);
794 goto err;
795 }
796 if (bc->ca != ASN1_BOOLEAN_TRUE) {
797 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
798 "cA bit: want %x, got %x\n", __func__, -1,
799 ASN1_BOOLEAN_TRUE, bc->ca);
800 goto err;
801 }
802 BASIC_CONSTRAINTS_free(bc);
803 bc = NULL;
804
805 /*
806 * Now pass idx = 0 and look for basic constraints, we should find
807 * the second one: non-critical at idx = 1, with ca bit set to true.
808 */
809
810 nid = NID_basic_constraints;
811 idx = 0;
812 if ((bc = X509V3_get_d2i(exts, nid, &crit, &idx)) == NULL) {
813 fprintf(stderr, "FAIL: %s (idx %d): expected to find"
814 "basic constraints\n", __func__, 0);
815 goto err;
816 }
817 if (crit != 0) {
818 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
819 "crit: want %d, got %d\n", __func__, 0, 0, crit);
820 goto err;
821 }
822 if (idx != 1) {
823 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
824 "idx: want %d, got %d\n", __func__, 0, 1, idx);
825 goto err;
826 }
827 if (bc->ca != ASN1_BOOLEAN_TRUE) {
828 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
829 "cA bit: want %x, got %x\n", __func__, 0,
830 ASN1_BOOLEAN_TRUE, bc->ca);
831 goto err;
832 }
833 BASIC_CONSTRAINTS_free(bc);
834 bc = NULL;
835
836 /*
837 * Now pass idx = 1 and look for basic constraints, we should find the
838 * third one: critical at idx = 2, with ca bit set to false.
839 */
840
841 nid = NID_basic_constraints;
842 idx = 1;
843 if ((bc = X509V3_get_d2i(exts, nid, &crit, &idx)) == NULL) {
844 fprintf(stderr, "FAIL: %s (idx %d): expected to find"
845 "basic constraints\n", __func__, 1);
846 goto err;
847 }
848 if (crit != 1) {
849 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
850 "crit: want %d, got %d\n", __func__, 1, 0, crit);
851 goto err;
852 }
853 if (idx != 2) {
854 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
855 "idx: want %d, got %d\n", __func__, 1, 2, idx);
856 goto err;
857 }
858 if (bc->ca != ASN1_BOOLEAN_FALSE) {
859 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
860 "cA bit: want %x, got %x\n", __func__, 1,
861 ASN1_BOOLEAN_FALSE, bc->ca);
862 goto err;
863 }
864 BASIC_CONSTRAINTS_free(bc);
865 bc = NULL;
866
867 /*
868 * Finally, pass idx = 2 and we should find no basic constraints.
869 */
870
871 nid = NID_basic_constraints;
872 idx = 2;
873 if ((bc = X509V3_get_d2i(exts, nid, &crit, &idx)) != NULL) {
874 fprintf(stderr, "FAIL: %s (idx %d): expected to find"
875 "no basic constraints\n", __func__, 2);
876 goto err;
877 }
878 if (crit != -1) {
879 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
880 "crit: want %d, got %d\n", __func__, 2, -1, crit);
881 goto err;
882 }
883 if (idx != -1) {
884 fprintf(stderr, "FAIL: %s: basic constraints (idx %d): "
885 "idx: want %d, got %d\n", __func__, 2, -1, idx);
886 goto err;
887 }
888
889 failed = 0;
890
891 err:
892 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
893 ASN1_BIT_STRING_free(abs);
894 BASIC_CONSTRAINTS_free(bc);
895
896 return failed;
897}
898
899static int
900test_x509v3_get_d2i(void)
901{
902 int failed = 0;
903
904 failed |= test_x509v3_get_d2i_null();
905 failed |= test_x509v3_get_d2i_multiple_basic_constraints();
906
907 return failed;
908}
909
647int 910int
648main(void) 911main(void)
649{ 912{
650 int failed = 0; 913 int failed = 0;
651 914
652 failed |= test_x509v3_add1_i2d(); 915 failed |= test_x509v3_add1_i2d();
916 failed |= test_x509v3_get_d2i();
653 917
654 return failed; 918 return failed;
655} 919}