@@ -38,135 +38,8 @@ int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
3838 LTC_ARGCHK (outlen != NULL );
3939
4040 /* get size of output that will be required */
41- y = 0 ;
42- for (i = 0 ; i < inlen ; i ++ ) {
43- type = list [i ].type ;
44- size = list [i ].size ;
45- data = list [i ].data ;
46-
47- if (type == LTC_ASN1_EOL ) {
48- break ;
49- }
50-
51- switch (type ) {
52- case LTC_ASN1_BOOLEAN :
53- if ((err = der_length_boolean (& x )) != CRYPT_OK ) {
54- goto LBL_ERR ;
55- }
56- y += x ;
57- break ;
58-
59- case LTC_ASN1_INTEGER :
60- if ((err = der_length_integer (data , & x )) != CRYPT_OK ) {
61- goto LBL_ERR ;
62- }
63- y += x ;
64- break ;
65-
66- case LTC_ASN1_SHORT_INTEGER :
67- if ((err = der_length_short_integer (* ((unsigned long * )data ), & x )) != CRYPT_OK ) {
68- goto LBL_ERR ;
69- }
70- y += x ;
71- break ;
72-
73- case LTC_ASN1_BIT_STRING :
74- case LTC_ASN1_RAW_BIT_STRING :
75- if ((err = der_length_bit_string (size , & x )) != CRYPT_OK ) {
76- goto LBL_ERR ;
77- }
78- y += x ;
79- break ;
80-
81- case LTC_ASN1_OCTET_STRING :
82- if ((err = der_length_octet_string (size , & x )) != CRYPT_OK ) {
83- goto LBL_ERR ;
84- }
85- y += x ;
86- break ;
87-
88- case LTC_ASN1_NULL :
89- y += 2 ;
90- break ;
91-
92- case LTC_ASN1_OBJECT_IDENTIFIER :
93- if ((err = der_length_object_identifier (data , size , & x )) != CRYPT_OK ) {
94- goto LBL_ERR ;
95- }
96- y += x ;
97- break ;
98-
99- case LTC_ASN1_IA5_STRING :
100- if ((err = der_length_ia5_string (data , size , & x )) != CRYPT_OK ) {
101- goto LBL_ERR ;
102- }
103- y += x ;
104- break ;
105-
106- case LTC_ASN1_PRINTABLE_STRING :
107- if ((err = der_length_printable_string (data , size , & x )) != CRYPT_OK ) {
108- goto LBL_ERR ;
109- }
110- y += x ;
111- break ;
112-
113- case LTC_ASN1_UTF8_STRING :
114- if ((err = der_length_utf8_string (data , size , & x )) != CRYPT_OK ) {
115- goto LBL_ERR ;
116- }
117- y += x ;
118- break ;
119-
120- case LTC_ASN1_UTCTIME :
121- if ((err = der_length_utctime (data , & x )) != CRYPT_OK ) {
122- goto LBL_ERR ;
123- }
124- y += x ;
125- break ;
126-
127- case LTC_ASN1_GENERALIZEDTIME :
128- if ((err = der_length_generalizedtime (data , & x )) != CRYPT_OK ) {
129- goto LBL_ERR ;
130- }
131- y += x ;
132- break ;
133-
134- case LTC_ASN1_SET :
135- case LTC_ASN1_SETOF :
136- case LTC_ASN1_SEQUENCE :
137- if ((err = der_length_sequence (data , size , & x )) != CRYPT_OK ) {
138- goto LBL_ERR ;
139- }
140- y += x ;
141- break ;
142-
143- case LTC_ASN1_CHOICE :
144- case LTC_ASN1_CONSTRUCTED :
145- case LTC_ASN1_CONTEXT_SPECIFIC :
146- case LTC_ASN1_EOL :
147- case LTC_ASN1_TELETEX_STRING :
148- err = CRYPT_INVALID_ARG ;
149- goto LBL_ERR ;
150- }
151- }
152-
153- /* calc header size */
154- z = y ;
155- if (y < 128 ) {
156- y += 2 ;
157- } else if (y < 256 ) {
158- /* 0x30 0x81 LL */
159- y += 3 ;
160- } else if (y < 65536UL ) {
161- /* 0x30 0x82 LL LL */
162- y += 4 ;
163- } else if (y < 16777216UL ) {
164- /* 0x30 0x83 LL LL LL */
165- y += 5 ;
166- } else {
167- err = CRYPT_INVALID_ARG ;
168- goto LBL_ERR ;
169- }
41+ y = 0 ; z = 0 ;
42+ if ((err = der_length_sequence_ex (list , inlen , & y , & z )) != CRYPT_OK ) return CRYPT_INVALID_ARG ;
17043
17144 /* too big ? */
17245 if (* outlen < y ) {
@@ -212,140 +85,110 @@ int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
21285 if ((err = der_encode_boolean (* ((int * )data ), out + x , & z )) != CRYPT_OK ) {
21386 goto LBL_ERR ;
21487 }
215- x += z ;
216- * outlen -= z ;
21788 break ;
21889
21990 case LTC_ASN1_INTEGER :
22091 z = * outlen ;
22192 if ((err = der_encode_integer (data , out + x , & z )) != CRYPT_OK ) {
22293 goto LBL_ERR ;
22394 }
224- x += z ;
225- * outlen -= z ;
22695 break ;
22796
22897 case LTC_ASN1_SHORT_INTEGER :
22998 z = * outlen ;
23099 if ((err = der_encode_short_integer (* ((unsigned long * )data ), out + x , & z )) != CRYPT_OK ) {
231100 goto LBL_ERR ;
232101 }
233- x += z ;
234- * outlen -= z ;
235102 break ;
236103
237104 case LTC_ASN1_BIT_STRING :
238105 z = * outlen ;
239106 if ((err = der_encode_bit_string (data , size , out + x , & z )) != CRYPT_OK ) {
240107 goto LBL_ERR ;
241108 }
242- x += z ;
243- * outlen -= z ;
244109 break ;
245110
246111 case LTC_ASN1_RAW_BIT_STRING :
247112 z = * outlen ;
248113 if ((err = der_encode_raw_bit_string (data , size , out + x , & z )) != CRYPT_OK ) {
249114 goto LBL_ERR ;
250115 }
251- x += z ;
252- * outlen -= z ;
253116 break ;
254117
255118 case LTC_ASN1_OCTET_STRING :
256119 z = * outlen ;
257120 if ((err = der_encode_octet_string (data , size , out + x , & z )) != CRYPT_OK ) {
258121 goto LBL_ERR ;
259122 }
260- x += z ;
261- * outlen -= z ;
262123 break ;
263124
264125 case LTC_ASN1_NULL :
265- out [x ++ ] = 0x05 ;
266- out [x ++ ] = 0x00 ;
267- * outlen - = 2 ;
126+ out [x ] = 0x05 ;
127+ out [x + 1 ] = 0x00 ;
128+ z = 2 ;
268129 break ;
269130
270131 case LTC_ASN1_OBJECT_IDENTIFIER :
271132 z = * outlen ;
272133 if ((err = der_encode_object_identifier (data , size , out + x , & z )) != CRYPT_OK ) {
273134 goto LBL_ERR ;
274135 }
275- x += z ;
276- * outlen -= z ;
277136 break ;
278137
279138 case LTC_ASN1_IA5_STRING :
280139 z = * outlen ;
281140 if ((err = der_encode_ia5_string (data , size , out + x , & z )) != CRYPT_OK ) {
282141 goto LBL_ERR ;
283142 }
284- x += z ;
285- * outlen -= z ;
286143 break ;
287144
288145 case LTC_ASN1_PRINTABLE_STRING :
289146 z = * outlen ;
290147 if ((err = der_encode_printable_string (data , size , out + x , & z )) != CRYPT_OK ) {
291148 goto LBL_ERR ;
292149 }
293- x += z ;
294- * outlen -= z ;
295150 break ;
296151
297152 case LTC_ASN1_UTF8_STRING :
298153 z = * outlen ;
299154 if ((err = der_encode_utf8_string (data , size , out + x , & z )) != CRYPT_OK ) {
300155 goto LBL_ERR ;
301156 }
302- x += z ;
303- * outlen -= z ;
304157 break ;
305158
306159 case LTC_ASN1_UTCTIME :
307160 z = * outlen ;
308161 if ((err = der_encode_utctime (data , out + x , & z )) != CRYPT_OK ) {
309162 goto LBL_ERR ;
310163 }
311- x += z ;
312- * outlen -= z ;
313164 break ;
314165
315166 case LTC_ASN1_GENERALIZEDTIME :
316167 z = * outlen ;
317168 if ((err = der_encode_generalizedtime (data , out + x , & z )) != CRYPT_OK ) {
318169 goto LBL_ERR ;
319170 }
320- x += z ;
321- * outlen -= z ;
322171 break ;
323172
324173 case LTC_ASN1_SET :
325174 z = * outlen ;
326175 if ((err = der_encode_set (data , size , out + x , & z )) != CRYPT_OK ) {
327176 goto LBL_ERR ;
328177 }
329- x += z ;
330- * outlen -= z ;
331178 break ;
332179
333180 case LTC_ASN1_SETOF :
334181 z = * outlen ;
335182 if ((err = der_encode_setof (data , size , out + x , & z )) != CRYPT_OK ) {
336183 goto LBL_ERR ;
337184 }
338- x += z ;
339- * outlen -= z ;
340185 break ;
341186
342187 case LTC_ASN1_SEQUENCE :
343188 z = * outlen ;
344189 if ((err = der_encode_sequence_ex (data , size , out + x , & z , type )) != CRYPT_OK ) {
345190 goto LBL_ERR ;
346191 }
347- x += z ;
348- * outlen -= z ;
349192 break ;
350193
351194 case LTC_ASN1_CHOICE :
@@ -356,6 +199,9 @@ int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
356199 err = CRYPT_INVALID_ARG ;
357200 goto LBL_ERR ;
358201 }
202+
203+ x += z ;
204+ * outlen -= z ;
359205 }
360206 * outlen = x ;
361207 err = CRYPT_OK ;
0 commit comments