Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/pk/asn1/der/sequence/der_decode_sequence_flexi.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
{
ltc_asn1_list *l;
int err;
unsigned long identifier, len, totlen, data_offset, id_len, len_len;
unsigned long identifier, len, totlen, data_offset, id_len, len_len, tlv_len;
void *realloc_tmp;

LTC_ARGCHK(in != NULL);
Expand Down Expand Up @@ -104,6 +104,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
data_offset = id_len + len_len;
s_print_err("OK", l, err, identifier, data_offset, len);
len += data_offset;
tlv_len = len; /* length of the whole element according to its header */

if (l->type == LTC_ASN1_CUSTOM_TYPE) {
/* Custom type, use the 'used' field to store the original identifier */
Expand All @@ -124,6 +125,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
*/
data_offset = 0;
len = 0;
tlv_len = 0;
}

/* now switch on type */
Expand All @@ -138,7 +140,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
l->size = 1;
l->data = XCALLOC(1, sizeof(int));

if ((err = der_decode_boolean(in, *inlen, l->data)) != CRYPT_OK) {
if ((err = der_decode_boolean(in, tlv_len, l->data)) != CRYPT_OK) {
goto error;
}

Expand Down Expand Up @@ -166,7 +168,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
}

/* decode field */
if ((err = der_decode_integer(in, *inlen, l->data)) != CRYPT_OK) {
if ((err = der_decode_integer(in, tlv_len, l->data)) != CRYPT_OK) {
goto error;
}

Expand All @@ -190,7 +192,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

if ((err = der_decode_bit_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
if ((err = der_decode_bit_string(in, tlv_len, l->data, &l->size)) != CRYPT_OK) {
goto error;
}

Expand All @@ -213,7 +215,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

if ((err = der_decode_octet_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
if ((err = der_decode_octet_string(in, tlv_len, l->data, &l->size)) != CRYPT_OK) {
goto error;
}

Expand Down Expand Up @@ -255,7 +257,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

if ((err = der_decode_object_identifier(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
if ((err = der_decode_object_identifier(in, tlv_len, l->data, &l->size)) != CRYPT_OK) {
goto error;
}

Expand Down Expand Up @@ -285,7 +287,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

if ((err = der_decode_utf8_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
if ((err = der_decode_utf8_string(in, tlv_len, l->data, &l->size)) != CRYPT_OK) {
goto error;
}

Expand All @@ -308,7 +310,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

if ((err = der_decode_printable_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
if ((err = der_decode_printable_string(in, tlv_len, l->data, &l->size)) != CRYPT_OK) {
goto error;
}

Expand All @@ -331,7 +333,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

if ((err = der_decode_teletex_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
if ((err = der_decode_teletex_string(in, tlv_len, l->data, &l->size)) != CRYPT_OK) {
goto error;
}

Expand All @@ -354,7 +356,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

if ((err = der_decode_ia5_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) {
if ((err = der_decode_ia5_string(in, tlv_len, l->data, &l->size)) != CRYPT_OK) {
goto error;
}

Expand All @@ -377,7 +379,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto error;
}

len = *inlen;
len = tlv_len;
if ((err = der_decode_utctime(in, &len, l->data)) != CRYPT_OK) {
goto error;
}
Expand Down Expand Up @@ -454,6 +456,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
in += data_offset;
*inlen -= data_offset;
len -= data_offset;
tlv_len -= data_offset;

/* save the decoded ASN.1 len */
len_len = len;
Expand Down Expand Up @@ -505,6 +508,12 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
goto outside;
}

/* the re-encoded length has to match the header, otherwise it's not DER and we would advance by the wrong amount */
if (len != tlv_len) {
err = CRYPT_INVALID_PACKET;
goto error;
}

/* advance pointers */
totlen += len;
in += len;
Expand Down
25 changes: 25 additions & 0 deletions tests/der_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,30 @@ static void s_der_utf8_continuation_lead_test(void)
SHOULD_FAIL_WITH(der_decode_utf8_string(issue_507_utf8, sizeof(issue_507_utf8), decoded, &outlen), CRYPT_INVALID_PACKET);
}

/* flexi has to advance by the length from the header, an element that re-encodes to a different length is not DER */
static void s_der_flexi_reencoded_length_test(void)
{
/* UTCTime with and without seconds, each followed by a NULL */
static const unsigned char utctime[] = { 0x17, 0x0d, '2', '6', '0', '1', '0', '1', '0', '0', '0', '0', '0', '0', 'Z', 0x05, 0x00 };
static const unsigned char utctime_no_ss[] = { 0x17, 0x0b, '2', '6', '0', '1', '0', '1', '0', '0', '0', '0', 'Z', 0x05, 0x00 };
/* SEQUENCE { UTF8String } where U+0000 is encoded twice in the overlong form c0 80 */
static const unsigned char utf8_overlong[] = { 0x30, 0x08, 0x0c, 0x06, 0xc0, 0x80, 0xc0, 0x80, 0x05, 0x00 };
ltc_asn1_list *l;
unsigned long len;

len = sizeof(utctime);
DO(der_decode_sequence_flexi(utctime, &len, &l));
ENSURE(len == sizeof(utctime));
ENSURE(l->type == LTC_ASN1_UTCTIME && l->next != NULL && l->next->type == LTC_ASN1_NULL);
der_sequence_free(l);

len = sizeof(utctime_no_ss);
SHOULD_FAIL_WITH(der_decode_sequence_flexi(utctime_no_ss, &len, &l), CRYPT_INVALID_PACKET);

len = sizeof(utf8_overlong);
SHOULD_FAIL_WITH(der_decode_sequence_flexi(utf8_overlong, &len, &l), CRYPT_INVALID_PACKET);
}

static void s_der_regression_test(void)
{
static const unsigned char s_broken_sequence[] = {
Expand Down Expand Up @@ -1464,6 +1488,7 @@ int der_test(void)
der_custom_test();

s_der_utf8_continuation_lead_test();
s_der_flexi_reencoded_length_test();
s_der_regression_test();

der_toolong_test();
Expand Down
Loading