@@ -39,11 +39,12 @@ static int _new_element(ltc_asn1_list **l)
39
39
@param in The input buffer
40
40
@param inlen [in/out] The length of the input buffer and on output the amount of decoded data
41
41
@param out [out] A pointer to the linked list
42
+ @param depth The depth/level of decoding recursion we've already reached
42
43
@return CRYPT_OK on success.
43
44
*/
44
- int der_decode_sequence_flexi (const unsigned char * in , unsigned long * inlen , ltc_asn1_list * * out )
45
+ static int _der_decode_sequence_flexi (const unsigned char * in , unsigned long * inlen , ltc_asn1_list * * out , unsigned long depth )
45
46
{
46
- ltc_asn1_list * l , * t ;
47
+ ltc_asn1_list * l ;
47
48
unsigned long err , identifier , len , totlen , data_offset , id_len , len_len ;
48
49
void * realloc_tmp ;
49
50
@@ -428,6 +429,12 @@ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc
428
429
}
429
430
}
430
431
432
+ /* check that we don't go over the recursion limit */
433
+ if (depth > LTC_DER_MAX_RECURSION ) {
434
+ err = CRYPT_PK_ASN1_ERROR ;
435
+ goto error ;
436
+ }
437
+
431
438
if ((l -> data = XMALLOC (len )) == NULL ) {
432
439
err = CRYPT_MEM ;
433
440
goto error ;
@@ -446,7 +453,7 @@ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc
446
453
len_len = len ;
447
454
448
455
/* Sequence elements go as child */
449
- if ((err = der_decode_sequence_flexi (in , & len , & (l -> child ))) != CRYPT_OK ) {
456
+ if ((err = _der_decode_sequence_flexi (in , & len , & (l -> child ), depth + 1 )) != CRYPT_OK ) {
450
457
goto error ;
451
458
}
452
459
if (len_len != len ) {
@@ -463,17 +470,6 @@ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc
463
470
l -> child -> parent = l ;
464
471
}
465
472
466
- t = l ;
467
- len_len = 0 ;
468
- while ((t != NULL ) && (t -> child != NULL )) {
469
- len_len ++ ;
470
- t = t -> child ;
471
- }
472
- if (len_len > LTC_DER_MAX_RECURSION ) {
473
- err = CRYPT_PK_ASN1_ERROR ;
474
- goto error ;
475
- }
476
-
477
473
break ;
478
474
479
475
case 0x80 : /* Context-specific */
@@ -535,6 +531,18 @@ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc
535
531
return err ;
536
532
}
537
533
534
+ /**
535
+ ASN.1 DER Flexi(ble) decoder will decode arbitrary DER packets and create a linked list of the decoded elements.
536
+ @param in The input buffer
537
+ @param inlen [in/out] The length of the input buffer and on output the amount of decoded data
538
+ @param out [out] A pointer to the linked list
539
+ @return CRYPT_OK on success.
540
+ */
541
+ int der_decode_sequence_flexi (const unsigned char * in , unsigned long * inlen , ltc_asn1_list * * out )
542
+ {
543
+ return _der_decode_sequence_flexi (in , inlen , out , 0 );
544
+ }
545
+
538
546
#endif
539
547
540
548
0 commit comments