Skip to content

Commit 988afd0

Browse files
Reject len < -1 in ASN1_mbstring_ncopy
1 parent 0993768 commit 988afd0

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

crypto/asn1/a_mbstr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UTF8STRING)
3333
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in,
3434
ossl_ssize_t len, int inform, unsigned long mask,
3535
ossl_ssize_t minsize, ossl_ssize_t maxsize) {
36+
if (len < -1) {
37+
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_FORMAT);
38+
return -1;
39+
}
3640
if (len == -1) {
3741
len = strlen((const char *)in);
3842
}

crypto/asn1/asn1_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,17 @@ TEST(ASN1Test, MBString) {
17111711
ERR_clear_error();
17121712
EXPECT_EQ(nullptr, str);
17131713
}
1714+
1715+
// |len| values below -1 must be rejected; only -1 is special-cased to mean
1716+
// "call strlen on |in|". Any other negative value would otherwise be cast
1717+
// to a huge |size_t| by |CBS_init|.
1718+
static const uint8_t kDummy[] = {'a'};
1719+
ASN1_STRING *str = nullptr;
1720+
EXPECT_EQ(-1, ASN1_mbstring_ncopy(&str, kDummy, -2, MBSTRING_UTF8,
1721+
B_ASN1_UTF8STRING, /*minsize=*/0,
1722+
/*maxsize=*/0));
1723+
EXPECT_EQ(nullptr, str);
1724+
ERR_clear_error();
17141725
}
17151726

17161727
TEST(ASN1Test, StringByNID) {

0 commit comments

Comments
 (0)