Truly independent web browser
LibCrypto: Validate Ed25519 private-key length
Kyle Elliott2d4f77d252ec
diff --git a/Libraries/LibCrypto/Certificate/Certificate.cpp b/Libraries/LibCrypto/Certificate/Certificate.cppindex bf68e97c31c..d7f56a7c1f6 100644--- a/Libraries/LibCrypto/Certificate/Certificate.cpp+++ b/Libraries/LibCrypto/Certificate/Certificate.cpp@@ -385,6 +385,13 @@ ErrorOr<PrivateKey> parse_private_key_info(ASN1::Decoder& decoder, Vector<Stringreturn private_key;}+ if (private_key.algorithm.identifier.span() == ASN1::ed25519_oid.span()) {+ ASN1::Decoder private_key_decoder(value.bytes());+ auto curve_private_key = TRY(private_key_decoder.read<StringView>(ASN1::Class::Universal, ASN1::Kind::OctetString));+ if (!private_key_decoder.eof() || curve_private_key.length() != 32)+ ERROR_WITH_SCOPE(TRY(String::formatted("Invalid Ed25519 private key at {}", current_scope)));+ }+// https://datatracker.ietf.org/doc/html/rfc8410#section-9// For all of the OIDs, the parameters MUST be absent.diff --git a/Tests/LibTLS/TestTLSCertificateParser.cpp b/Tests/LibTLS/TestTLSCertificateParser.cppindex e425d7dcc29..5da16575581 100644--- a/Tests/LibTLS/TestTLSCertificateParser.cpp+++ b/Tests/LibTLS/TestTLSCertificateParser.cpp@@ -8,6 +8,27 @@#include <LibCrypto/Certificate/Certificate.h>#include <LibTest/TestCase.h>+TEST_CASE(test_ed25519_private_key_length)+{+ constexpr Array<u8, 16> invalid_key_der {+ 0x30, 0x0e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,+ 0x03, 0x2b, 0x65, 0x70, 0x04, 0x02, 0x04, 0x00+ };+ Crypto::ASN1::Decoder invalid_key_decoder(invalid_key_der);+ EXPECT(Crypto::Certificate::parse_private_key_info(invalid_key_decoder).is_error());++ constexpr Array<u8, 48> valid_key_der {+ 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,+ 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00+ };+ Crypto::ASN1::Decoder valid_key_decoder(valid_key_der);+ EXPECT(!Crypto::Certificate::parse_private_key_info(valid_key_decoder).is_error());+}+TEST_CASE(test_private_key_info_decode){constexpr auto keyder = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA5HMXMnY+RhEcYXsa"