Skip to content

Truly independent web browser

LibCrypto: Remove the DER boolean and string support

Shannon Booth922dc2352fc4
diff --git a/Libraries/LibCrypto/ASN1/DER.cpp b/Libraries/LibCrypto/ASN1/DER.cpp
index 95366ec3b26..d4c068825e4 100644
--- a/Libraries/LibCrypto/ASN1/DER.cpp
+++ b/Libraries/LibCrypto/ASN1/DER.cpp
@@ -7,7 +7,6 @@
#include <AK/IntegralMath.h>
#include <AK/Math.h>
#include <AK/Try.h>
-#include <AK/Utf8View.h>
#include <LibCrypto/ASN1/DER.h>
namespace Crypto::ASN1 {
@@ -87,14 +86,6 @@ ErrorOr<ReadonlyBytes> Decoder::read_bytes(size_t length)
return bytes;
}
-ErrorOr<bool> Decoder::decode_boolean(ReadonlyBytes data)
-{
- if (data.size() != 1)
- return Error::from_string_literal("ASN1::Decoder: Decoding boolean from a non boolean-sized span");
-
- return data[0] != 0;
-}
-
ErrorOr<UnsignedBigInteger> Decoder::decode_arbitrary_sized_integer(ReadonlyBytes data)
{
if (data.size() < 1)
@@ -166,15 +157,6 @@ ErrorOr<ObjectIdentifier> Decoder::decode_object_identifier(ReadonlyBytes data)
return result;
}
-ErrorOr<StringView> Decoder::decode_printable_string(ReadonlyBytes data)
-{
- Utf8View view { data };
- if (!view.validate())
- return Error::from_string_literal("ASN1::Decoder: Invalid UTF-8 in printable string");
-
- return StringView { data };
-}
-
ErrorOr<BitStringView> Decoder::decode_bit_string(ReadonlyBytes data)
{
if (data.size() < 1)
@@ -291,17 +273,6 @@ ErrorOr<void> Encoder::write_bytes(ReadonlyBytes bytes)
return {};
}
-ErrorOr<void> Encoder::write_boolean(bool value, Optional<Class> class_override, Optional<Kind> kind_override)
-{
- auto class_ = class_override.value_or(Class::Universal);
- auto type = Type::Primitive;
- auto kind = kind_override.value_or(Kind::Boolean);
-
- TRY(write_tag(class_, type, kind));
- TRY(write_length(1));
- return write_byte(value ? 0xff : 0x00);
-}
-
ErrorOr<void> Encoder::write_arbitrary_sized_integer(UnsignedBigInteger const& value, Optional<Class> class_override, Optional<Kind> kind_override)
{
auto class_ = class_override.value_or(Class::Universal);
@@ -329,21 +300,6 @@ ErrorOr<void> Encoder::write_arbitrary_sized_integer(UnsignedBigInteger const& v
return write_bytes(result);
}
-ErrorOr<void> Encoder::write_printable_string(StringView string, Optional<Class> class_override, Optional<Kind> kind_override)
-{
- Utf8View view { string };
- if (!view.validate())
- return Error::from_string_literal("ASN1::Encoder: Invalid UTF-8 in printable string");
-
- auto class_ = class_override.value_or(Class::Universal);
- auto type = Type::Primitive;
- auto kind = kind_override.value_or(Kind::PrintableString);
-
- TRY(write_tag(class_, type, kind));
- TRY(write_length(string.length()));
- return write_bytes(string.bytes());
-}
-
ErrorOr<void> Encoder::write_octet_string(ReadonlyBytes bytes, Optional<Class> class_override, Optional<Kind> kind_override)
{
auto class_ = class_override.value_or(Class::Universal);
diff --git a/Libraries/LibCrypto/ASN1/DER.h b/Libraries/LibCrypto/ASN1/DER.h
index 246e6791984..c82db1f498e 100644
--- a/Libraries/LibCrypto/ASN1/DER.h
+++ b/Libraries/LibCrypto/ASN1/DER.h
@@ -65,13 +65,6 @@ using ObjectIdentifier = Vector<u32, 12>;
ERROR_WITH_SCOPE(TRY(String::formatted("Rewrite of kind " #kind_name " failed: {}", value_name##_result.error()))); \
}
-#define DROP_OBJECT() \
- do { \
- if (auto error = decoder.drop(); error.is_error()) { \
- ERROR_WITH_SCOPE(TRY(String::formatted("Drop failed: {}", error.error()))); \
- } \
- } while (0)
-
class BitStringView {
public:
BitStringView(ReadonlyBytes data, size_t unused_bits)
@@ -87,28 +80,11 @@ public:
return m_data;
}
- bool get(size_t index) const
- {
- if (index >= 8 * m_data.size() - m_unused_bits)
- return false;
- return 0 != (m_data[index / 8] & (1u << (7 - (index % 8))));
- }
-
size_t unused_bits() const { return m_unused_bits; }
size_t byte_length() const { return m_data.size(); }
ReadonlyBytes underlying_bytes() const { return m_data; }
- // FIXME: Improve me! I am naive!
- bool operator==(BitStringView const& other) const
- {
- for (size_t bit_index = 0; bit_index < 8 * m_data.size() - m_unused_bits; ++bit_index) {
- if (get(bit_index) != other.get(bit_index))
- return false;
- }
- return true;
- }
-
private:
ReadonlyBytes m_data;
size_t m_unused_bits;
@@ -255,13 +231,9 @@ private:
if (value_or_error.is_error())
return value_or_error.release_error();
- if constexpr (IsSame<ValueType, bool> && !IsSame<DecodedType, bool>) {
- return Error::from_string_literal("ASN1::Decoder: Trying to decode a boolean from a non-boolean type");
- } else {
- auto&& value = value_or_error.value();
- if constexpr (requires { ValueType { value }; })
- return ValueType { value };
- }
+ auto&& value = value_or_error.value();
+ if constexpr (requires { ValueType { value }; })
+ return ValueType { value };
return Error::from_string_literal("ASN1::Decoder: Trying to decode a value from an incompatible type");
}
@@ -277,9 +249,6 @@ private:
if (klass != Class::Universal)
return with_type_check<ValueType>(data);
- if (kind == Kind::Boolean)
- return with_type_check<ValueType>(decode_boolean(data));
-
if (kind == Kind::Integer)
return with_type_check<ValueType>(decode_arbitrary_sized_integer(data));
@@ -292,12 +261,6 @@ private:
if (kind == Kind::ObjectIdentifier)
return with_type_check<ValueType>(decode_object_identifier(data));
- if (kind == Kind::PrintableString || kind == Kind::IA5String || kind == Kind::UTCTime)
- return with_type_check<ValueType>(decode_printable_string(data));
-
- if (kind == Kind::Utf8String)
- return with_type_check<ValueType>(StringView { data.data(), data.size() });
-
if (kind == Kind::BitString)
return with_type_check<ValueType>(decode_bit_string(data));
@@ -310,12 +273,10 @@ private:
ErrorOr<u8> read_byte();
ErrorOr<ReadonlyBytes> read_bytes(size_t length);
- static ErrorOr<bool> decode_boolean(ReadonlyBytes);
static ErrorOr<UnsignedBigInteger> decode_arbitrary_sized_integer(ReadonlyBytes);
static ErrorOr<StringView> decode_octet_string(ReadonlyBytes);
static ErrorOr<nullptr_t> decode_null(ReadonlyBytes);
static ErrorOr<ObjectIdentifier> decode_object_identifier(ReadonlyBytes);
- static ErrorOr<StringView> decode_printable_string(ReadonlyBytes);
static ErrorOr<BitStringView> decode_bit_string(ReadonlyBytes);
Vector<ReadonlyBytes> m_stack;
@@ -339,12 +300,8 @@ public:
template<typename ValueType>
ErrorOr<void> write(ValueType const& value, Optional<Class> class_override = {}, Optional<Kind> kind_override = {})
{
- if constexpr (IsSame<ValueType, bool>) {
- return write_boolean(value, class_override, kind_override);
- } else if constexpr (IsSame<ValueType, UnsignedBigInteger> || (IsIntegral<ValueType> && IsUnsigned<ValueType>)) {
+ if constexpr (IsSame<ValueType, UnsignedBigInteger> || (IsIntegral<ValueType> && IsUnsigned<ValueType> && !IsSame<ValueType, bool>)) {
return write_arbitrary_sized_integer(value, class_override, kind_override);
- } else if constexpr (IsOneOf<ValueType, StringView, String, ByteString>) {
- return write_printable_string(value, class_override, kind_override);
} else if constexpr (IsOneOf<ValueType, ReadonlyBytes, ByteBuffer>) {
return write_octet_string(value, class_override, kind_override);
} else if constexpr (IsSame<ValueType, nullptr_t>) {
@@ -390,9 +347,7 @@ private:
ErrorOr<void> write_bytes(ReadonlyBytes);
ErrorOr<void> write_byte(u8);
- ErrorOr<void> write_boolean(bool, Optional<Class>, Optional<Kind>);
ErrorOr<void> write_arbitrary_sized_integer(UnsignedBigInteger const&, Optional<Class>, Optional<Kind>);
- ErrorOr<void> write_printable_string(StringView, Optional<Class>, Optional<Kind>);
ErrorOr<void> write_octet_string(ReadonlyBytes, Optional<Class>, Optional<Kind>);
ErrorOr<void> write_null(Optional<Class>, Optional<Kind>);
ErrorOr<void> write_object_identifier(Span<u32 const>, Optional<Class>, Optional<Kind>);
diff --git a/Tests/LibCrypto/TestASN1.cpp b/Tests/LibCrypto/TestASN1.cpp
index 52c83b61098..7c45a52a414 100644
--- a/Tests/LibCrypto/TestASN1.cpp
+++ b/Tests/LibCrypto/TestASN1.cpp
@@ -20,9 +20,6 @@ TEST_CASE(test_encoder_primitives)
EXPECT_EQ(decoded, value);
};
- roundtrip_value(false);
- roundtrip_value(true);
-
roundtrip_value(Crypto::UnsignedBigInteger { 0 });
roundtrip_value(Crypto::UnsignedBigInteger { 1 });
roundtrip_value(TRY_OR_FAIL(Crypto::UnsignedBigInteger { 2 }.shift_left(128)));
@@ -32,11 +29,15 @@ TEST_CASE(test_encoder_primitives)
roundtrip_value(Crypto::ASN1::ObjectIdentifier { 1, 2, 840, 113549, 1, 1, 11 });
roundtrip_value(Crypto::ASN1::ObjectIdentifier { 1, 2, 0xfffffff });
- roundtrip_value(ByteString { "Hello, World!\n" });
-
roundtrip_value(nullptr);
- roundtrip_value(Crypto::ASN1::BitStringView { { { 0x00, 0x01, 0x02, 0x03 } }, 3 });
+ u8 const bit_string_bytes[] { 0x00, 0x01, 0x02, 0x03 };
+ Crypto::ASN1::Encoder encoder;
+ MUST(encoder.write(Crypto::ASN1::BitStringView { bit_string_bytes, 0 }));
+ auto encoded = encoder.finish();
+ Crypto::ASN1::Decoder decoder(encoded);
+ auto decoded = MUST(decoder.read<Crypto::ASN1::BitStringView>());
+ EXPECT_EQ(MUST(decoded.raw_bytes()), ReadonlyBytes { bit_string_bytes });
}
TEST_CASE(test_encoder_constructed)
@@ -86,14 +87,14 @@ TEST_CASE(test_encoder_constructed)
TEST_CASE(test_decoder_restores_state_after_failed_read)
{
- // A Boolean tag declaring a length of 5, but only 3 value bytes are present.
- u8 const data[] { 0x01, 0x05, 0x00, 0x00, 0x00 };
+ // An Integer tag declaring a length of 5, but only 3 value bytes are present.
+ u8 const data[] { 0x02, 0x05, 0x00, 0x00, 0x00 };
Crypto::ASN1::Decoder decoder({ data, sizeof(data) });
- EXPECT(decoder.read<bool>().is_error());
+ EXPECT(decoder.read<Crypto::UnsignedBigInteger>().is_error());
// The first failed read should leave the decoder state unchanged, so a second drop should have the same result.
- EXPECT(decoder.read<bool>().is_error());
+ EXPECT(decoder.read<Crypto::UnsignedBigInteger>().is_error());
}
TEST_CASE(test_decoder_restores_state_after_failed_drop)