summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2023-05-07 14:20:46 +0200
committerAndreas Kling <kling@serenityos.org>2023-05-08 06:43:28 +0200
commitac5cef1b662e95ce1e64a83b5885dae16ebaf12a (patch)
treeb23564afa074ca75d5e4b66999e1d027d50213a0
parentf37f081f152f14a83e4897f59d5a7af68f8a1d18 (diff)
downloadserenity-ac5cef1b662e95ce1e64a83b5885dae16ebaf12a.zip
LibCrypto+LibTLS: Avoid crashing on invalid input
Fixes #18307.
-rw-r--r--Userland/Libraries/LibCrypto/ASN1/DER.h5
-rw-r--r--Userland/Libraries/LibTLS/Certificate.cpp6
2 files changed, 6 insertions, 5 deletions
diff --git a/Userland/Libraries/LibCrypto/ASN1/DER.h b/Userland/Libraries/LibCrypto/ASN1/DER.h
index 8138461e38..56473207f7 100644
--- a/Userland/Libraries/LibCrypto/ASN1/DER.h
+++ b/Userland/Libraries/LibCrypto/ASN1/DER.h
@@ -22,9 +22,10 @@ public:
{
}
- ReadonlyBytes raw_bytes() const
+ ErrorOr<ReadonlyBytes> raw_bytes() const
{
- VERIFY(m_unused_bits == 0);
+ if (m_unused_bits != 0)
+ return Error::from_string_literal("ASN1::Decoder: BitStringView contains unexpected partial bytes");
return m_data;
}
diff --git a/Userland/Libraries/LibTLS/Certificate.cpp b/Userland/Libraries/LibTLS/Certificate.cpp
index 3a831b262a..dcb5b66369 100644
--- a/Userland/Libraries/LibTLS/Certificate.cpp
+++ b/Userland/Libraries/LibTLS/Certificate.cpp
@@ -365,10 +365,10 @@ static ErrorOr<SubjectPublicKey> parse_subject_public_key_info(Crypto::ASN1::Dec
READ_OBJECT(BitString, Crypto::ASN1::BitStringView, value);
POP_SCOPE();
- public_key.raw_key = TRY(ByteBuffer::copy(value.raw_bytes()));
+ public_key.raw_key = TRY(ByteBuffer::copy(TRY(value.raw_bytes())));
if (public_key.algorithm.identifier.span() == rsa_encryption_oid.span()) {
- auto key = Crypto::PK::RSA::parse_rsa_key(value.raw_bytes());
+ auto key = Crypto::PK::RSA::parse_rsa_key(TRY(value.raw_bytes()));
if (!key.public_key.length()) {
return Error::from_string_literal("Invalid RSA key");
}
@@ -773,7 +773,7 @@ ErrorOr<Certificate> Certificate::parse_certificate(ReadonlyBytes buffer, bool)
PUSH_SCOPE("signature"sv);
READ_OBJECT(BitString, Crypto::ASN1::BitStringView, signature);
- certificate.signature_value = TRY(ByteBuffer::copy(signature.raw_bytes()));
+ certificate.signature_value = TRY(ByteBuffer::copy(TRY(signature.raw_bytes())));
POP_SCOPE();
if (!decoder.eof()) {