diff --git a/crypto/bytestring/bytestring_test.cc b/crypto/bytestring/bytestring_test.cc index ca3d8350..66e9c1e0 100644 --- a/crypto/bytestring/bytestring_test.cc +++ b/crypto/bytestring/bytestring_test.cc @@ -596,8 +596,10 @@ static const ASN1InvalidUint64Test kASN1InvalidUint64Tests[] = { {"\x02\x00", 2}, // Negative number. {"\x02\x01\x80", 3}, - // Overflow + // Overflow. {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11}, + // Leading zeros. + {"\x02\x02\x00\x01", 4}, }; static bool TestASN1Uint64() { diff --git a/crypto/bytestring/cbs.c b/crypto/bytestring/cbs.c index 36d64d80..bd94ccec 100644 --- a/crypto/bytestring/cbs.c +++ b/crypto/bytestring/cbs.c @@ -291,7 +291,12 @@ int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) { } if ((data[0] & 0x80) != 0) { - /* negative number */ + /* Negative number. */ + return 0; + } + + if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) { + /* Extra leading zeros. */ return 0; }