Debian Codegraph @

src/ltc/pk/asn1/der/sequence/der_decode_sequence_multi.c

raw
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5
6 /**
7 @file der_decode_sequence_multi.c
8 ASN.1 DER, decode a SEQUENCE, Tom St Denis
9 */
10
11 #ifdef LTC_DER
12
13 /**
14 Decode a SEQUENCE type using a VA list
15 @param in Input buffer
16 @param inlen Length of input in octets
17 @param a1 Initialized argument list #1
18 @param a2 Initialized argument list #2 (copy of #1)
19 @param flags c.f. enum ltc_der_seq
20 @return CRYPT_OK on success
21 */
22 static int s_der_decode_sequence_va(const unsigned char *in, unsigned long inlen, va_list a1, va_list a2, unsigned int flags)
23 {
24 int err;
25 ltc_asn1_type type;
26 unsigned long size, x;
27 void *data;
28 ltc_asn1_list *list;
29
30 LTC_ARGCHK(in != NULL);
31
32 /* get size of output that will be required */
33 x = 0;
34 for (;;) {
35 type = (ltc_asn1_type)va_arg(a1, int);
36
37 if (type == LTC_ASN1_EOL) {
38 break;
39 }
40
41 size = va_arg(a1, unsigned long);
42 data = va_arg(a1, void*);
43 LTC_UNUSED_PARAM(size);
44 LTC_UNUSED_PARAM(data);
45
46 switch (type) {
47 case LTC_ASN1_BOOLEAN:
48 case LTC_ASN1_INTEGER:
49 case LTC_ASN1_SHORT_INTEGER:
50 case LTC_ASN1_BIT_STRING:
51 case LTC_ASN1_OCTET_STRING:
52 case LTC_ASN1_NULL:
53 case LTC_ASN1_OBJECT_IDENTIFIER:
54 case LTC_ASN1_IA5_STRING:
55 case LTC_ASN1_PRINTABLE_STRING:
56 case LTC_ASN1_UTF8_STRING:
57 case LTC_ASN1_UTCTIME:
58 case LTC_ASN1_SET:
59 case LTC_ASN1_SETOF:
60 case LTC_ASN1_SEQUENCE:
61 case LTC_ASN1_CHOICE:
62 case LTC_ASN1_RAW_BIT_STRING:
63 case LTC_ASN1_TELETEX_STRING:
64 case LTC_ASN1_GENERALIZEDTIME:
65 ++x;
66 break;
67
68 case LTC_ASN1_EOL:
69 case LTC_ASN1_CUSTOM_TYPE:
70 return CRYPT_INVALID_ARG;
71 }
72 }
73
74 /* allocate structure for x elements */
75 if (x == 0) {
76 return CRYPT_NOP;
77 }
78
79 list = XCALLOC(x, sizeof(*list));
80 if (list == NULL) {
81 return CRYPT_MEM;
82 }
83
84 /* fill in the structure */
85 x = 0;
86 for (;;) {
87 type = (ltc_asn1_type)va_arg(a2, int);
88 size = va_arg(a2, unsigned long);
89 data = va_arg(a2, void*);
90
91 if (type == LTC_ASN1_EOL) {
92 break;
93 }
94
95 switch (type) {
96 case LTC_ASN1_BOOLEAN:
97 case LTC_ASN1_INTEGER:
98 case LTC_ASN1_SHORT_INTEGER:
99 case LTC_ASN1_BIT_STRING:
100 case LTC_ASN1_OCTET_STRING:
101 case LTC_ASN1_NULL:
102 case LTC_ASN1_OBJECT_IDENTIFIER:
103 case LTC_ASN1_IA5_STRING:
104 case LTC_ASN1_PRINTABLE_STRING:
105 case LTC_ASN1_UTF8_STRING:
106 case LTC_ASN1_UTCTIME:
107 case LTC_ASN1_SEQUENCE:
108 case LTC_ASN1_SET:
109 case LTC_ASN1_SETOF:
110 case LTC_ASN1_CHOICE:
111 case LTC_ASN1_RAW_BIT_STRING:
112 case LTC_ASN1_TELETEX_STRING:
113 case LTC_ASN1_GENERALIZEDTIME:
114 LTC_SET_ASN1(list, x++, type, data, size);
115 break;
116 /* coverity[dead_error_line] */
117 case LTC_ASN1_EOL:
118 case LTC_ASN1_CUSTOM_TYPE:
119 break;
120 }
121 }
122
123 err = der_decode_sequence_ex(in, inlen, list, x, flags);
124 XFREE(list);
125 return err;
126 }
127
128 /**
129 Decode a SEQUENCE type using a VA list
130 @param in Input buffer
131 @param inlen Length of input in octets
132 @remark <...> is of the form <type, size, data> (int, unsigned long, void*)
133 @return CRYPT_OK on success
134 */
135 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...)
136 {
137 va_list a1, a2;
138 int err;
139
140 LTC_ARGCHK(in != NULL);
141
142 va_start(a1, inlen);
143 va_start(a2, inlen);
144
145 err = s_der_decode_sequence_va(in, inlen, a1, a2, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_RELAXED);
146
147 va_end(a2);
148 va_end(a1);
149
150 return err;
151 }
152
153 /**
154 Decode a SEQUENCE type using a VA list
155 @param in Input buffer
156 @param inlen Length of input in octets
157 @param flags c.f. enum ltc_der_seq
158 @remark <...> is of the form <type, size, data> (int, unsigned long, void*)
159 @return CRYPT_OK on success
160 */
161 int der_decode_sequence_multi_ex(const unsigned char *in, unsigned long inlen, unsigned int flags, ...)
162 {
163 va_list a1, a2;
164 int err;
165
166 LTC_ARGCHK(in != NULL);
167
168 va_start(a1, flags);
169 va_start(a2, flags);
170
171 err = s_der_decode_sequence_va(in, inlen, a1, a2, flags);
172
173 va_end(a2);
174 va_end(a1);
175
176 return err;
177 }
178
179 #endif
180