Skip to content

Truly independent web browser

LibCrypto: Reject DER lengths not encoded in the fewest bytes

Shannon Boothfe1f3c1ad728
diff --git a/Libraries/LibCrypto/ASN1/DER.cpp b/Libraries/LibCrypto/ASN1/DER.cpp
index c44ffac2517..b1e2c6fd99e 100644
--- a/Libraries/LibCrypto/ASN1/DER.cpp
+++ b/Libraries/LibCrypto/ASN1/DER.cpp
@@ -50,6 +50,10 @@ ErrorOr<size_t> Decoder::read_length()
for (auto&& byte : data)
length = (length << 8) | (size_t)byte;
+
+ // DER encodes a length in the short form when it fits, and otherwise in as few bytes as possible.
+ if (data[0] == 0 || length < 0x80)
+ return Error::from_string_literal("ASN1::Decoder: Length is not encoded in the fewest bytes possible");
}
return length;
diff --git a/Tests/LibCrypto/TestASN1.cpp b/Tests/LibCrypto/TestASN1.cpp
index 979fa502481..87c904a84d6 100644
--- a/Tests/LibCrypto/TestASN1.cpp
+++ b/Tests/LibCrypto/TestASN1.cpp
@@ -300,3 +300,23 @@ TEST_CASE(test_decode_object_identifier_rejects_invalid_encodings)
u8 const too_large[] { 0x06, 0x06, 0x2a, 0x81, 0x80, 0x80, 0x80, 0x00 };
expect_decode_failure({ too_large, sizeof(too_large) });
}
+
+TEST_CASE(test_decoder_lengths_in_the_fewest_bytes_possible)
+{
+ auto read_octet_string_of_length = [](Vector<u8> length_bytes, size_t length) {
+ Vector<u8> der { 0x04 };
+ der.extend(length_bytes);
+ for (size_t i = 0; i < length; ++i)
+ der.append(0xaa);
+ Crypto::ASN1::Decoder decoder { der };
+ return decoder.read<StringView>();
+ };
+
+ EXPECT_EQ(TRY_OR_FAIL(read_octet_string_of_length({ 0x05 }, 5)).length(), 5u);
+ EXPECT_EQ(TRY_OR_FAIL(read_octet_string_of_length({ 0x81, 0x80 }, 128)).length(), 128u);
+ EXPECT_EQ(TRY_OR_FAIL(read_octet_string_of_length({ 0x82, 0x01, 0x00 }, 256)).length(), 256u);
+
+ EXPECT(read_octet_string_of_length({ 0x81, 0x05 }, 5).is_error());
+ EXPECT(read_octet_string_of_length({ 0x82, 0x00, 0x80 }, 128).is_error());
+ EXPECT(read_octet_string_of_length({ 0x83, 0x00, 0x01, 0x00 }, 256).is_error());
+}