@@ -94,10 +94,25 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
9494 break ;
9595 }
9696
97+ /* handle context specific tags - just skip the tag + len bytes */
98+ z = 0 ;
99+ if (list [i ].tag > 0 && list [i ].tag == in [x + z ++ ]) {
100+ if (in [x + z ] & 0x80 ) {
101+ y = in [x + z ++ ] & 0x7F ;
102+ if (y == 0 || y > 2 ) { return CRYPT_INVALID_PACKET ; }
103+ z += y ;
104+ } else {
105+ z ++ ;
106+ }
107+ x += z ;
108+ inlen -= z ;
109+ }
110+
97111 switch (type ) {
98112 case LTC_ASN1_BOOLEAN :
99113 z = inlen ;
100114 if ((err = der_decode_boolean (in + x , z , ((int * )data ))) != CRYPT_OK ) {
115+ if (!ordered || list [i ].optional ) { continue ; }
101116 goto LBL_ERR ;
102117 }
103118 if ((err = der_length_boolean (& z )) != CRYPT_OK ) {
@@ -108,7 +123,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
108123 case LTC_ASN1_INTEGER :
109124 z = inlen ;
110125 if ((err = der_decode_integer (in + x , z , data )) != CRYPT_OK ) {
111- if (!ordered ) { continue ; }
126+ if (!ordered || list [ i ]. optional ) { continue ; }
112127 goto LBL_ERR ;
113128 }
114129 if ((err = der_length_integer (data , & z )) != CRYPT_OK ) {
@@ -119,7 +134,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
119134 case LTC_ASN1_SHORT_INTEGER :
120135 z = inlen ;
121136 if ((err = der_decode_short_integer (in + x , z , data )) != CRYPT_OK ) {
122- if (!ordered ) { continue ; }
137+ if (!ordered || list [ i ]. optional ) { continue ; }
123138 goto LBL_ERR ;
124139 }
125140 if ((err = der_length_short_integer (((unsigned long * )data )[0 ], & z )) != CRYPT_OK ) {
@@ -131,7 +146,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
131146 case LTC_ASN1_BIT_STRING :
132147 z = inlen ;
133148 if ((err = der_decode_bit_string (in + x , z , data , & size )) != CRYPT_OK ) {
134- if (!ordered ) { continue ; }
149+ if (!ordered || list [ i ]. optional ) { continue ; }
135150 goto LBL_ERR ;
136151 }
137152 list [i ].size = size ;
@@ -143,7 +158,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
143158 case LTC_ASN1_RAW_BIT_STRING :
144159 z = inlen ;
145160 if ((err = der_decode_raw_bit_string (in + x , z , data , & size )) != CRYPT_OK ) {
146- if (!ordered ) { continue ; }
161+ if (!ordered || list [ i ]. optional ) { continue ; }
147162 goto LBL_ERR ;
148163 }
149164 list [i ].size = size ;
@@ -155,7 +170,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
155170 case LTC_ASN1_OCTET_STRING :
156171 z = inlen ;
157172 if ((err = der_decode_octet_string (in + x , z , data , & size )) != CRYPT_OK ) {
158- if (!ordered ) { continue ; }
173+ if (!ordered || list [ i ]. optional ) { continue ; }
159174 goto LBL_ERR ;
160175 }
161176 list [i ].size = size ;
@@ -166,7 +181,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
166181
167182 case LTC_ASN1_NULL :
168183 if (inlen < 2 || in [x ] != 0x05 || in [x + 1 ] != 0x00 ) {
169- if (!ordered ) { continue ; }
184+ if (!ordered || list [ i ]. optional ) { continue ; }
170185 err = CRYPT_INVALID_PACKET ;
171186 goto LBL_ERR ;
172187 }
@@ -176,7 +191,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
176191 case LTC_ASN1_OBJECT_IDENTIFIER :
177192 z = inlen ;
178193 if ((err = der_decode_object_identifier (in + x , z , data , & size )) != CRYPT_OK ) {
179- if (!ordered ) { continue ; }
194+ if (!ordered || list [ i ]. optional ) { continue ; }
180195 goto LBL_ERR ;
181196 }
182197 list [i ].size = size ;
@@ -188,7 +203,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
188203 case LTC_ASN1_TELETEX_STRING :
189204 z = inlen ;
190205 if ((err = der_decode_teletex_string (in + x , z , data , & size )) != CRYPT_OK ) {
191- if (!ordered ) { continue ; }
206+ if (!ordered || list [ i ]. optional ) { continue ; }
192207 goto LBL_ERR ;
193208 }
194209 list [i ].size = size ;
@@ -200,7 +215,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
200215 case LTC_ASN1_IA5_STRING :
201216 z = inlen ;
202217 if ((err = der_decode_ia5_string (in + x , z , data , & size )) != CRYPT_OK ) {
203- if (!ordered ) { continue ; }
218+ if (!ordered || list [ i ]. optional ) { continue ; }
204219 goto LBL_ERR ;
205220 }
206221 list [i ].size = size ;
@@ -213,7 +228,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
213228 case LTC_ASN1_PRINTABLE_STRING :
214229 z = inlen ;
215230 if ((err = der_decode_printable_string (in + x , z , data , & size )) != CRYPT_OK ) {
216- if (!ordered ) { continue ; }
231+ if (!ordered || list [ i ]. optional ) { continue ; }
217232 goto LBL_ERR ;
218233 }
219234 list [i ].size = size ;
@@ -225,7 +240,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
225240 case LTC_ASN1_UTF8_STRING :
226241 z = inlen ;
227242 if ((err = der_decode_utf8_string (in + x , z , data , & size )) != CRYPT_OK ) {
228- if (!ordered ) { continue ; }
243+ if (!ordered || list [ i ]. optional ) { continue ; }
229244 goto LBL_ERR ;
230245 }
231246 list [i ].size = size ;
@@ -237,7 +252,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
237252 case LTC_ASN1_UTCTIME :
238253 z = inlen ;
239254 if ((err = der_decode_utctime (in + x , & z , data )) != CRYPT_OK ) {
240- if (!ordered ) { continue ; }
255+ if (!ordered || list [ i ]. optional ) { continue ; }
241256 goto LBL_ERR ;
242257 }
243258 break ;
@@ -253,7 +268,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
253268 case LTC_ASN1_SET :
254269 z = inlen ;
255270 if ((err = der_decode_set (in + x , z , data , size )) != CRYPT_OK ) {
256- if (!ordered ) { continue ; }
271+ if (!ordered || list [ i ]. optional ) { continue ; }
257272 goto LBL_ERR ;
258273 }
259274 if ((err = der_length_sequence (data , size , & z )) != CRYPT_OK ) {
@@ -271,7 +286,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
271286
272287 z = inlen ;
273288 if ((err = der_decode_sequence (in + x , z , data , size )) != CRYPT_OK ) {
274- if (!ordered ) { continue ; }
289+ if (!ordered || list [ i ]. optional ) { continue ; }
275290 goto LBL_ERR ;
276291 }
277292 if ((err = der_length_sequence (data , size , & z )) != CRYPT_OK ) {
@@ -283,7 +298,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
283298 case LTC_ASN1_CHOICE :
284299 z = inlen ;
285300 if ((err = der_decode_choice (in + x , & z , data , size )) != CRYPT_OK ) {
286- if (!ordered ) { continue ; }
301+ if (!ordered || list [ i ]. optional ) { continue ; }
287302 goto LBL_ERR ;
288303 }
289304 break ;
@@ -304,7 +319,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
304319 }
305320
306321 for (i = 0 ; i < (int )outlen ; i ++ ) {
307- if (list [i ].used == 0 ) {
322+ if (list [i ].used == 0 && list [ i ]. optional == 0 ) {
308323 err = CRYPT_INVALID_PACKET ;
309324 goto LBL_ERR ;
310325 }
0 commit comments