summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2022-05-21 11:21:31 +0000
committerjsing <>2022-05-21 11:21:31 +0000
commita8c92a1dc658d94b5480f67cbc00a974f12f4e94 (patch)
treef90d94a6c9396741701294a8ddbb862b77984a94 /src/lib
parentae0791b7327af957c26affd56876662cab6e8d78 (diff)
downloadopenbsd-a8c92a1dc658d94b5480f67cbc00a974f12f4e94.tar.gz
openbsd-a8c92a1dc658d94b5480f67cbc00a974f12f4e94.tar.bz2
openbsd-a8c92a1dc658d94b5480f67cbc00a974f12f4e94.zip
Use 'at' for ASN1_TEMPLATE variable names rather than 'tt'.
Also use array indexes for it->templates, rather than trying to be extra clever in for loops (suggested by tb@ during a review). No functional change. ok tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c110
1 files changed, 58 insertions, 52 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index e3b631d61d..a769ace975 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_dec.c,v 1.73 2022/05/21 11:12:03 jsing Exp $ */ 1/* $OpenBSD: tasn_dec.c,v 1.74 2022/05/21 11:21:31 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -637,7 +637,7 @@ static int
637asn1_item_d2i_choice(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, 637asn1_item_d2i_choice(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
638 int tag_number, int tag_class, char optional, int depth) 638 int tag_number, int tag_class, char optional, int depth)
639{ 639{
640 const ASN1_TEMPLATE *tt, *errtt = NULL; 640 const ASN1_TEMPLATE *at, *errat = NULL;
641 const ASN1_AUX *aux; 641 const ASN1_AUX *aux;
642 ASN1_aux_cb *asn1_cb = NULL; 642 ASN1_aux_cb *asn1_cb = NULL;
643 ASN1_VALUE *achoice = NULL; 643 ASN1_VALUE *achoice = NULL;
@@ -675,16 +675,18 @@ asn1_item_d2i_choice(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
675 } 675 }
676 676
677 /* Try each possible CHOICE in turn. */ 677 /* Try each possible CHOICE in turn. */
678 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 678 for (i = 0; i < it->tcount; i++) {
679 pchptr = asn1_get_field_ptr(&achoice, tt); 679 at = &it->templates[i];
680
681 pchptr = asn1_get_field_ptr(&achoice, at);
680 682
681 /* Mark field as OPTIONAL so its absence can be identified. */ 683 /* Mark field as OPTIONAL so its absence can be identified. */
682 ret = asn1_template_d2i(pchptr, cbs, tt, 1, depth); 684 ret = asn1_template_d2i(pchptr, cbs, at, 1, depth);
683 if (ret == -1) 685 if (ret == -1)
684 continue; 686 continue;
685 if (ret != 1) { 687 if (ret != 1) {
686 ASN1error(ERR_R_NESTED_ASN1_ERROR); 688 ASN1error(ERR_R_NESTED_ASN1_ERROR);
687 errtt = tt; 689 errat = at;
688 goto err; 690 goto err;
689 } 691 }
690 692
@@ -716,8 +718,8 @@ asn1_item_d2i_choice(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
716 err: 718 err:
717 ASN1_item_ex_free(&achoice, it); 719 ASN1_item_ex_free(&achoice, it);
718 720
719 if (errtt) 721 if (errat != NULL)
720 ERR_asprintf_error_data("Field=%s, Type=%s", errtt->field_name, 722 ERR_asprintf_error_data("Field=%s, Type=%s", errat->field_name,
721 it->sname); 723 it->sname);
722 else 724 else
723 ERR_asprintf_error_data("Type=%s", it->sname); 725 ERR_asprintf_error_data("Type=%s", it->sname);
@@ -731,8 +733,8 @@ asn1_item_d2i_sequence(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
731{ 733{
732 CBS cbs_seq, cbs_seq_content, cbs_object; 734 CBS cbs_seq, cbs_seq_content, cbs_object;
733 char constructed, indefinite, optional_field; 735 char constructed, indefinite, optional_field;
734 const ASN1_TEMPLATE *errtt = NULL; 736 const ASN1_TEMPLATE *errat = NULL;
735 const ASN1_TEMPLATE *seqtt, *tt; 737 const ASN1_TEMPLATE *seqat, *at;
736 ASN1_aux_cb *asn1_cb = NULL; 738 ASN1_aux_cb *asn1_cb = NULL;
737 const ASN1_AUX *aux; 739 const ASN1_AUX *aux;
738 ASN1_VALUE *aseq = NULL; 740 ASN1_VALUE *aseq = NULL;
@@ -795,7 +797,9 @@ asn1_item_d2i_sequence(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
795 goto err; 797 goto err;
796 } 798 }
797 799
798 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 800 for (i = 0; i < it->tcount; i++) {
801 at = &it->templates[i];
802
799 if (asn1_check_eoc(&cbs_seq_content)) { 803 if (asn1_check_eoc(&cbs_seq_content)) {
800 if (!indefinite) { 804 if (!indefinite) {
801 ASN1error(ASN1_R_UNEXPECTED_EOC); 805 ASN1error(ASN1_R_UNEXPECTED_EOC);
@@ -807,10 +811,10 @@ asn1_item_d2i_sequence(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
807 if (CBS_len(&cbs_seq_content) == 0) 811 if (CBS_len(&cbs_seq_content) == 0)
808 break; 812 break;
809 813
810 if ((seqtt = asn1_do_adb(&aseq, tt, 1)) == NULL) 814 if ((seqat = asn1_do_adb(&aseq, at, 1)) == NULL)
811 goto err; 815 goto err;
812 816
813 pseqval = asn1_get_field_ptr(&aseq, seqtt); 817 pseqval = asn1_get_field_ptr(&aseq, seqat);
814 818
815 /* 819 /*
816 * This was originally implemented to "increase efficiency", 820 * This was originally implemented to "increase efficiency",
@@ -818,19 +822,19 @@ asn1_item_d2i_sequence(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
818 * the use of ASN.1 ANY with OPTIONAL in SEQUENCEs (which 822 * the use of ASN.1 ANY with OPTIONAL in SEQUENCEs (which
819 * asn1_d2i_primitive() currently rejects). 823 * asn1_d2i_primitive() currently rejects).
820 */ 824 */
821 optional_field = (seqtt->flags & ASN1_TFLG_OPTIONAL) != 0; 825 optional_field = (seqat->flags & ASN1_TFLG_OPTIONAL) != 0;
822 if (i == it->tcount - 1) 826 if (i == it->tcount - 1)
823 optional_field = 0; 827 optional_field = 0;
824 828
825 ret = asn1_template_d2i(pseqval, &cbs_seq_content, 829 ret = asn1_template_d2i(pseqval, &cbs_seq_content,
826 seqtt, optional_field, depth); 830 seqat, optional_field, depth);
827 if (ret == -1) { 831 if (ret == -1) {
828 /* Absent OPTIONAL component. */ 832 /* Absent OPTIONAL component. */
829 ASN1_template_free(pseqval, seqtt); 833 ASN1_template_free(pseqval, seqat);
830 continue; 834 continue;
831 } 835 }
832 if (ret != 1) { 836 if (ret != 1) {
833 errtt = seqtt; 837 errat = seqat;
834 goto err; 838 goto err;
835 } 839 }
836 } 840 }
@@ -852,19 +856,21 @@ asn1_item_d2i_sequence(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
852 * There is no more data in the ASN.1 SEQUENCE, however we may not have 856 * There is no more data in the ASN.1 SEQUENCE, however we may not have
853 * populated all fields - check that any remaining are OPTIONAL. 857 * populated all fields - check that any remaining are OPTIONAL.
854 */ 858 */
855 for (; i < it->tcount; tt++, i++) { 859 for (; i < it->tcount; i++) {
856 if ((seqtt = asn1_do_adb(&aseq, tt, 1)) == NULL) 860 at = &it->templates[i];
861
862 if ((seqat = asn1_do_adb(&aseq, at, 1)) == NULL)
857 goto err; 863 goto err;
858 864
859 if ((seqtt->flags & ASN1_TFLG_OPTIONAL) == 0) { 865 if ((seqat->flags & ASN1_TFLG_OPTIONAL) == 0) {
860 ASN1error(ASN1_R_FIELD_MISSING); 866 ASN1error(ASN1_R_FIELD_MISSING);
861 errtt = seqtt; 867 errat = seqat;
862 goto err; 868 goto err;
863 } 869 }
864 870
865 /* XXX - this is probably unnecessary with earlier free. */ 871 /* XXX - this is probably unnecessary with earlier free. */
866 pseqval = asn1_get_field_ptr(&aseq, seqtt); 872 pseqval = asn1_get_field_ptr(&aseq, seqat);
867 ASN1_template_free(pseqval, seqtt); 873 ASN1_template_free(pseqval, seqat);
868 } 874 }
869 875
870 if (!CBS_get_bytes(cbs, &cbs_object, CBS_offset(&cbs_seq))) 876 if (!CBS_get_bytes(cbs, &cbs_object, CBS_offset(&cbs_seq)))
@@ -888,8 +894,8 @@ asn1_item_d2i_sequence(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
888 err: 894 err:
889 ASN1_item_ex_free(&aseq, it); 895 ASN1_item_ex_free(&aseq, it);
890 896
891 if (errtt != NULL) 897 if (errat != NULL)
892 ERR_asprintf_error_data("Field=%s, Type=%s", errtt->field_name, 898 ERR_asprintf_error_data("Field=%s, Type=%s", errat->field_name,
893 it->sname); 899 it->sname);
894 else 900 else
895 ERR_asprintf_error_data("Type=%s", it->sname); 901 ERR_asprintf_error_data("Type=%s", it->sname);
@@ -975,7 +981,7 @@ asn1_item_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
975} 981}
976 982
977static void 983static void
978asn1_template_stack_of_free(STACK_OF(ASN1_VALUE) *avals, const ASN1_TEMPLATE *tt) { 984asn1_template_stack_of_free(STACK_OF(ASN1_VALUE) *avals, const ASN1_TEMPLATE *at) {
979 ASN1_VALUE *aval; 985 ASN1_VALUE *aval;
980 986
981 if (avals == NULL) 987 if (avals == NULL)
@@ -983,13 +989,13 @@ asn1_template_stack_of_free(STACK_OF(ASN1_VALUE) *avals, const ASN1_TEMPLATE *tt
983 989
984 while (sk_ASN1_VALUE_num(avals) > 0) { 990 while (sk_ASN1_VALUE_num(avals) > 0) {
985 aval = sk_ASN1_VALUE_pop(avals); 991 aval = sk_ASN1_VALUE_pop(avals);
986 ASN1_item_ex_free(&aval, tt->item); 992 ASN1_item_ex_free(&aval, at->item);
987 } 993 }
988 sk_ASN1_VALUE_free(avals); 994 sk_ASN1_VALUE_free(avals);
989} 995}
990 996
991static int 997static int
992asn1_template_stack_of_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt, 998asn1_template_stack_of_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *at,
993 char optional, int depth) 999 char optional, int depth)
994{ 1000{
995 CBS cbs_object, cbs_object_content; 1001 CBS cbs_object, cbs_object_content;
@@ -1006,17 +1012,17 @@ asn1_template_stack_of_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1006 if (pval == NULL) 1012 if (pval == NULL)
1007 return 0; 1013 return 0;
1008 1014
1009 asn1_template_stack_of_free((STACK_OF(ASN1_VALUE) *)*pval, tt); 1015 asn1_template_stack_of_free((STACK_OF(ASN1_VALUE) *)*pval, at);
1010 *pval = NULL; 1016 *pval = NULL;
1011 1017
1012 tag_number = tt->tag; 1018 tag_number = at->tag;
1013 tag_class = tt->flags & ASN1_TFLG_TAG_CLASS; 1019 tag_class = at->flags & ASN1_TFLG_TAG_CLASS;
1014 1020
1015 /* Determine the inner tag value for SET OF or SEQUENCE OF. */ 1021 /* Determine the inner tag value for SET OF or SEQUENCE OF. */
1016 if ((tt->flags & ASN1_TFLG_IMPTAG) == 0) { 1022 if ((at->flags & ASN1_TFLG_IMPTAG) == 0) {
1017 tag_number = V_ASN1_SEQUENCE; 1023 tag_number = V_ASN1_SEQUENCE;
1018 tag_class = V_ASN1_UNIVERSAL; 1024 tag_class = V_ASN1_UNIVERSAL;
1019 if ((tt->flags & ASN1_TFLG_SET_OF) != 0) 1025 if ((at->flags & ASN1_TFLG_SET_OF) != 0)
1020 tag_number = V_ASN1_SET; 1026 tag_number = V_ASN1_SET;
1021 } 1027 }
1022 1028
@@ -1055,7 +1061,7 @@ asn1_template_stack_of_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1055 eoc_needed = 0; 1061 eoc_needed = 0;
1056 break; 1062 break;
1057 } 1063 }
1058 if (!asn1_item_d2i(&aval, &cbs_object_content, tt->item, 1064 if (!asn1_item_d2i(&aval, &cbs_object_content, at->item,
1059 -1, 0, 0, depth)) { 1065 -1, 0, 0, depth)) {
1060 ASN1error(ERR_R_NESTED_ASN1_ERROR); 1066 ASN1error(ERR_R_NESTED_ASN1_ERROR);
1061 goto err; 1067 goto err;
@@ -1085,14 +1091,14 @@ asn1_template_stack_of_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1085 return 1; 1091 return 1;
1086 1092
1087 err: 1093 err:
1088 asn1_template_stack_of_free(avals, tt); 1094 asn1_template_stack_of_free(avals, at);
1089 ASN1_item_ex_free(&aval, tt->item); 1095 ASN1_item_ex_free(&aval, at->item);
1090 1096
1091 return 0; 1097 return 0;
1092} 1098}
1093 1099
1094static int 1100static int
1095asn1_template_noexp_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt, 1101asn1_template_noexp_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *at,
1096 char optional, int depth) 1102 char optional, int depth)
1097{ 1103{
1098 int tag_number, tag_class; 1104 int tag_number, tag_class;
@@ -1101,19 +1107,19 @@ asn1_template_noexp_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1101 if (pval == NULL) 1107 if (pval == NULL)
1102 return 0; 1108 return 0;
1103 1109
1104 if ((tt->flags & ASN1_TFLG_SK_MASK) != 0) 1110 if ((at->flags & ASN1_TFLG_SK_MASK) != 0)
1105 return asn1_template_stack_of_d2i(pval, cbs, tt, optional, depth); 1111 return asn1_template_stack_of_d2i(pval, cbs, at, optional, depth);
1106 1112
1107 tag_number = -1; 1113 tag_number = -1;
1108 tag_class = V_ASN1_UNIVERSAL; 1114 tag_class = V_ASN1_UNIVERSAL;
1109 1115
1110 /* See if we need to use IMPLICIT tagging. */ 1116 /* See if we need to use IMPLICIT tagging. */
1111 if ((tt->flags & ASN1_TFLG_IMPTAG) != 0) { 1117 if ((at->flags & ASN1_TFLG_IMPTAG) != 0) {
1112 tag_number = tt->tag; 1118 tag_number = at->tag;
1113 tag_class = tt->flags & ASN1_TFLG_TAG_CLASS; 1119 tag_class = at->flags & ASN1_TFLG_TAG_CLASS;
1114 } 1120 }
1115 1121
1116 ret = asn1_item_d2i(pval, cbs, tt->item, tag_number, tag_class, 1122 ret = asn1_item_d2i(pval, cbs, at->item, tag_number, tag_class,
1117 optional, depth); 1123 optional, depth);
1118 if (ret == -1) 1124 if (ret == -1)
1119 return -1; 1125 return -1;
@@ -1126,12 +1132,12 @@ asn1_template_noexp_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1126 1132
1127 err: 1133 err:
1128 /* XXX - The called function should have freed already. */ 1134 /* XXX - The called function should have freed already. */
1129 ASN1_template_free(pval, tt); 1135 ASN1_template_free(pval, at);
1130 return 0; 1136 return 0;
1131} 1137}
1132 1138
1133static int 1139static int
1134asn1_template_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt, 1140asn1_template_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *at,
1135 char optional, int depth) 1141 char optional, int depth)
1136{ 1142{
1137 CBS cbs_exp, cbs_exp_content; 1143 CBS cbs_exp, cbs_exp_content;
@@ -1143,14 +1149,14 @@ asn1_template_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1143 return 0; 1149 return 0;
1144 1150
1145 /* Check if EXPLICIT tag is expected. */ 1151 /* Check if EXPLICIT tag is expected. */
1146 if ((tt->flags & ASN1_TFLG_EXPTAG) == 0) 1152 if ((at->flags & ASN1_TFLG_EXPTAG) == 0)
1147 return asn1_template_noexp_d2i(pval, cbs, tt, optional, depth); 1153 return asn1_template_noexp_d2i(pval, cbs, at, optional, depth);
1148 1154
1149 CBS_init(&cbs_exp, CBS_data(cbs), CBS_len(cbs)); 1155 CBS_init(&cbs_exp, CBS_data(cbs), CBS_len(cbs));
1150 1156
1151 /* Read ASN.1 header for EXPLICIT tagged object. */ 1157 /* Read ASN.1 header for EXPLICIT tagged object. */
1152 ret = asn1_check_tag(&cbs_exp, &length, NULL, NULL, &indefinite, 1158 ret = asn1_check_tag(&cbs_exp, &length, NULL, NULL, &indefinite,
1153 &constructed, tt->tag, tt->flags & ASN1_TFLG_TAG_CLASS, optional); 1159 &constructed, at->tag, at->flags & ASN1_TFLG_TAG_CLASS, optional);
1154 if (ret == -1) 1160 if (ret == -1)
1155 return -1; 1161 return -1;
1156 if (ret != 1) { 1162 if (ret != 1) {
@@ -1170,7 +1176,7 @@ asn1_template_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1170 goto err; 1176 goto err;
1171 } 1177 }
1172 1178
1173 if ((ret = asn1_template_noexp_d2i(pval, &cbs_exp_content, tt, 0, 1179 if ((ret = asn1_template_noexp_d2i(pval, &cbs_exp_content, at, 0,
1174 depth)) != 1) { 1180 depth)) != 1) {
1175 ASN1error(ERR_R_NESTED_ASN1_ERROR); 1181 ASN1error(ERR_R_NESTED_ASN1_ERROR);
1176 return 0; 1182 return 0;
@@ -1194,7 +1200,7 @@ asn1_template_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt,
1194 return 1; 1200 return 1;
1195 1201
1196 err: 1202 err:
1197 ASN1_template_free(pval, tt); 1203 ASN1_template_free(pval, at);
1198 return 0; 1204 return 0;
1199} 1205}
1200 1206
@@ -1234,7 +1240,7 @@ ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long inlen,
1234 1240
1235int 1241int
1236ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 1242ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
1237 const ASN1_TEMPLATE *tt) 1243 const ASN1_TEMPLATE *at)
1238{ 1244{
1239 CBS cbs; 1245 CBS cbs;
1240 int ret; 1246 int ret;
@@ -1243,7 +1249,7 @@ ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
1243 return 0; 1249 return 0;
1244 1250
1245 CBS_init(&cbs, *in, len); 1251 CBS_init(&cbs, *in, len);
1246 if ((ret = asn1_template_d2i(pval, &cbs, tt, 0, 0)) == 1) 1252 if ((ret = asn1_template_d2i(pval, &cbs, at, 0, 0)) == 1)
1247 *in = CBS_data(&cbs); 1253 *in = CBS_data(&cbs);
1248 1254
1249 return ret; 1255 return ret;