Skip to content

Commit 0f41e99

Browse files
authored
Merge pull request #10024 from embhorn/zd21390
Fix DecodeAltNames length check
2 parents 994a1fb + 8ffb096 commit 0f41e99

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

tests/api/test_asn.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,111 @@ int test_wc_DecodeRsaPssParams(void)
920920
#endif /* WC_RSA_PSS && !NO_RSA && !NO_ASN */
921921
return EXPECT_RESULT();
922922
}
923+
924+
/* Test that DecodeAltNames rejects a SAN entry whose length exceeds the
925+
* remaining SEQUENCE length (integer underflow on the length tracker). */
926+
int test_DecodeAltNames_length_underflow(void)
927+
{
928+
EXPECT_DECLS;
929+
930+
#if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_ASN)
931+
/* Self-signed DER certificate with a well-formed SAN extension.
932+
* Byte at offset 418 is the SAN SEQUENCE length (0x06). The negative
933+
* test below copies this cert and shrinks that byte to 0x03 so the
934+
* DNS entry length exceeds the SEQUENCE bounds. */
935+
static const unsigned char good_san_cert[] = {
936+
0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
937+
0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
938+
0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
939+
0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
940+
0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
941+
0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
942+
0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
943+
0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
944+
0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
945+
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
946+
0x0d, 0x00, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa8,
947+
0x8a, 0x5e, 0x26, 0x23, 0x1b, 0x31, 0xd3, 0x37, 0x1a, 0x70, 0xb2, 0xec,
948+
0x3f, 0x74, 0xd4, 0xb4, 0x44, 0xe3, 0x7a, 0xa5, 0xc0, 0xf5, 0xaa, 0x97,
949+
0x26, 0x9a, 0x04, 0xff, 0xda, 0xbe, 0xe5, 0x09, 0x03, 0x98, 0x3d, 0xb5,
950+
0xbf, 0x01, 0x2c, 0x9a, 0x0a, 0x3a, 0xfb, 0xbc, 0x3c, 0xe7, 0xbe, 0x83,
951+
0x5c, 0xb3, 0x70, 0xe8, 0x5c, 0xe3, 0xd1, 0x83, 0xc3, 0x94, 0x08, 0xcd,
952+
0x1a, 0x87, 0xe5, 0xe0, 0x5b, 0x9c, 0x5c, 0x6e, 0xb0, 0x7d, 0xe2, 0x58,
953+
0x6c, 0xc3, 0xb5, 0xc8, 0x9d, 0x11, 0xf1, 0x5d, 0x96, 0x0d, 0x66, 0x1e,
954+
0x56, 0x7f, 0x8f, 0x59, 0xa7, 0xa5, 0xe1, 0xc5, 0xe7, 0x81, 0x4c, 0x09,
955+
0x9d, 0x5e, 0x96, 0xf0, 0x9a, 0xc2, 0x8b, 0x70, 0xd5, 0xab, 0x79, 0x58,
956+
0x5d, 0xb7, 0x58, 0xaa, 0xfd, 0x75, 0x52, 0xaa, 0x4b, 0xa7, 0x25, 0x68,
957+
0x76, 0x59, 0x00, 0xee, 0x78, 0x2b, 0x91, 0xc6, 0x59, 0x91, 0x99, 0x38,
958+
0x3e, 0xa1, 0x76, 0xc3, 0xf5, 0x23, 0x6b, 0xe6, 0x07, 0xea, 0x63, 0x1c,
959+
0x97, 0x49, 0xef, 0xa0, 0xfe, 0xfd, 0x13, 0xc9, 0xa9, 0x9f, 0xc2, 0x0b,
960+
0xe6, 0x87, 0x92, 0x5b, 0xcc, 0xf5, 0x42, 0x95, 0x4a, 0xa4, 0x6d, 0x64,
961+
0xba, 0x7d, 0xce, 0xcb, 0x04, 0xd0, 0xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
962+
0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
963+
0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
964+
0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
965+
0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
966+
0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
967+
0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
968+
0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
969+
0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
970+
/* SAN extension: correct SEQUENCE length 0x06 */
971+
0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
972+
0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
973+
0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
974+
0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
975+
0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
976+
0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
977+
0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
978+
0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
979+
0x03, 0x82, 0x01, 0x01, 0x00, 0x3f, 0xd5, 0x37, 0x2f, 0xc7, 0xf8, 0x8b,
980+
0x39, 0x1c, 0xe3, 0xdf, 0x77, 0xee, 0xc6, 0x4b, 0x5f, 0x84, 0xcf, 0xfa,
981+
0x33, 0x2c, 0xb2, 0xb5, 0x4b, 0x09, 0xee, 0x56, 0xc0, 0xf2, 0xf0, 0xeb,
982+
0xad, 0x1c, 0x02, 0xef, 0xae, 0x09, 0x53, 0xc0, 0x06, 0xad, 0x4e, 0xfd,
983+
0x3e, 0x8c, 0x13, 0xb3, 0xbf, 0x80, 0x05, 0x36, 0xb5, 0x3f, 0x2b, 0xc7,
984+
0x60, 0x53, 0x14, 0xbf, 0x33, 0x63, 0x47, 0xc3, 0xc6, 0x28, 0xda, 0x10,
985+
0x12, 0xe2, 0xc4, 0xeb, 0xc5, 0x64, 0x66, 0xc0, 0xcc, 0x6b, 0x84, 0xda,
986+
0x0c, 0xe9, 0xf6, 0xe3, 0xf8, 0x8e, 0x3d, 0x95, 0x5f, 0xba, 0x9f, 0xe1,
987+
0xc7, 0xed, 0x6e, 0x97, 0xcc, 0xbd, 0x7d, 0xe5, 0x4e, 0xab, 0xbc, 0x1b,
988+
0xf1, 0x3a, 0x09, 0x33, 0x09, 0xe1, 0xcc, 0xec, 0x21, 0x16, 0x8e, 0xb1,
989+
0x74, 0x9e, 0xc8, 0x13, 0x7c, 0xdf, 0x07, 0xaa, 0xeb, 0x70, 0xd7, 0x91,
990+
0x5c, 0xc4, 0xef, 0x83, 0x88, 0xc3, 0xe4, 0x97, 0xfa, 0xe4, 0xdf, 0xd7,
991+
0x0d, 0xff, 0xba, 0x78, 0x22, 0xfc, 0x3f, 0xdc, 0xd8, 0x02, 0x8d, 0x93,
992+
0x57, 0xf9, 0x9e, 0x39, 0x3a, 0x77, 0x00, 0xd9, 0x19, 0xaa, 0x68, 0xa1,
993+
0xe6, 0x9e, 0x13, 0xeb, 0x37, 0x16, 0xf5, 0x77, 0xa4, 0x0b, 0x40, 0x04,
994+
0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
995+
0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
996+
0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
997+
0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
998+
0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
999+
0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
1000+
0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
1001+
};
1002+
1003+
/* Offset of the SAN SEQUENCE length byte inside good_san_cert. */
1004+
#define SAN_SEQ_LEN_OFFSET 418
1005+
1006+
DecodedCert cert;
1007+
unsigned char bad_san_cert[sizeof(good_san_cert)];
1008+
1009+
/* Control: the original cert with correct SAN SEQUENCE length should
1010+
* parse successfully (signature won't verify, but NO_VERIFY skips that). */
1011+
wc_InitDecodedCert(&cert, good_san_cert, (word32)sizeof(good_san_cert),
1012+
NULL);
1013+
ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL), 0);
1014+
wc_FreeDecodedCert(&cert);
1015+
1016+
/* Build a malformed variant: shrink the SAN SEQUENCE length from 6 to 3
1017+
* so the DNS entry length (4) exceeds the SEQUENCE bounds. Without a
1018+
* bounds check DecodeAltNames would underflow the length tracker. */
1019+
XMEMCPY(bad_san_cert, good_san_cert, sizeof(good_san_cert));
1020+
bad_san_cert[SAN_SEQ_LEN_OFFSET] = 0x03;
1021+
1022+
wc_InitDecodedCert(&cert, bad_san_cert, (word32)sizeof(bad_san_cert),
1023+
NULL);
1024+
ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL),
1025+
WC_NO_ERR_TRACE(ASN_PARSE_E));
1026+
wc_FreeDecodedCert(&cert);
1027+
1028+
#endif /* !NO_CERTS && !NO_RSA && !NO_ASN */
1029+
return EXPECT_RESULT();
1030+
}

tests/api/test_asn.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ int test_GetSetShortInt(void);
2929
int test_wc_IndexSequenceOf(void);
3030
int test_wolfssl_local_MatchBaseName(void);
3131
int test_wc_DecodeRsaPssParams(void);
32+
int test_DecodeAltNames_length_underflow(void);
3233

3334
#define TEST_ASN_DECLS \
3435
TEST_DECL_GROUP("asn", test_SetAsymKeyDer), \
3536
TEST_DECL_GROUP("asn", test_GetSetShortInt), \
3637
TEST_DECL_GROUP("asn", test_wc_IndexSequenceOf), \
3738
TEST_DECL_GROUP("asn", test_wolfssl_local_MatchBaseName), \
38-
TEST_DECL_GROUP("asn", test_wc_DecodeRsaPssParams)
39+
TEST_DECL_GROUP("asn", test_wc_DecodeRsaPssParams), \
40+
TEST_DECL_GROUP("asn", test_DecodeAltNames_length_underflow)
3941

4042
#endif /* WOLFCRYPT_TEST_ASN_H */

wolfcrypt/src/asn.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20826,6 +20826,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2082620826

2082720827
AddAltName(cert, dnsEntry);
2082820828

20829+
if (strLen > length) {
20830+
return ASN_PARSE_E;
20831+
}
2082920832
length -= strLen;
2083020833
idx += (word32)strLen;
2083120834
}
@@ -20867,6 +20870,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2086720870
dirEntry->next = cert->altDirNames;
2086820871
cert->altDirNames = dirEntry;
2086920872

20873+
if (strLen > length) {
20874+
return ASN_PARSE_E;
20875+
}
2087020876
length -= strLen;
2087120877
idx += (word32)strLen;
2087220878
}
@@ -20902,6 +20908,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2090220908
emailEntry->next = cert->altEmailNames;
2090320909
cert->altEmailNames = emailEntry;
2090420910

20911+
if (strLen > length) {
20912+
return ASN_PARSE_E;
20913+
}
2090520914
length -= strLen;
2090620915
idx += (word32)strLen;
2090720916
}
@@ -20981,6 +20990,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2098120990

2098220991
AddAltName(cert, uriEntry);
2098320992

20993+
if (strLen > length) {
20994+
return ASN_PARSE_E;
20995+
}
2098420996
length -= strLen;
2098520997
idx += (word32)strLen;
2098620998
}
@@ -21027,6 +21039,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2102721039
}
2102821040
AddAltName(cert, ipAddr);
2102921041

21042+
if (strLen > length) {
21043+
return ASN_PARSE_E;
21044+
}
2103021045
length -= strLen;
2103121046
idx += (word32)strLen;
2103221047
}
@@ -21075,6 +21090,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2107521090

2107621091
AddAltName(cert, rid);
2107721092

21093+
if (strLen > length) {
21094+
return ASN_PARSE_E;
21095+
}
2107821096
length -= strLen;
2107921097
idx += (word32)strLen;
2108021098
}
@@ -21092,6 +21110,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2109221110
return ASN_PARSE_E;
2109321111
}
2109421112
/* Consume the rest of this sequence. */
21113+
if ((int)((word32)strLen + idx - lenStartIdx) > length) {
21114+
return ASN_PARSE_E;
21115+
}
2109521116
length -= (int)(((word32)strLen + idx - lenStartIdx));
2109621117

2109721118
if (GetObjectId(input, &idx, &oid, oidCertAltNameType, sz) < 0) {
@@ -21144,6 +21165,9 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
2114421165
WOLFSSL_MSG("\tfail: unsupported name length");
2114521166
return ASN_PARSE_E;
2114621167
}
21168+
if ((int)((word32)strLen + idx - lenStartIdx) > length) {
21169+
return ASN_PARSE_E;
21170+
}
2114721171
length -= (int)((word32)strLen + idx - lenStartIdx);
2114821172
idx += (word32)strLen;
2114921173
}

0 commit comments

Comments
 (0)