From 0a3d27c41d1f8eb376beab5facff6b2f317409e1 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 13 Mar 2024 21:26:00 -0600 Subject: [PATCH] LibWeb: Make SubtleCrypto AlgorithmParams classes virtual This allows us to properly destroy the child classes through a pointer to the base class, avoiding ASAN/UBSAN errors. --- .../LibWeb/Crypto/CryptoAlgorithms.cpp | 16 +++++++--- .../LibWeb/Crypto/CryptoAlgorithms.h | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp index b3d5370c4c..5152b6f8fc 100644 --- a/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp +++ b/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp @@ -57,6 +57,8 @@ static ::Crypto::UnsignedBigInteger big_integer_from_api_big_integer(JS::GCPtr> AlgorithmParams::from_value(JS::VM& vm, JS::Value value) { auto& object = value.as_object(); @@ -64,9 +66,11 @@ JS::ThrowCompletionOr> AlgorithmParams::from_valu auto name = TRY(object.get("name")); auto name_string = TRY(name.to_string(vm)); - return adopt_own(*new AlgorithmParams { .name = name_string }); + return adopt_own(*new AlgorithmParams { name_string }); } +PBKDF2Params::~PBKDF2Params() = default; + JS::ThrowCompletionOr> PBKDF2Params::from_value(JS::VM& vm, JS::Value value) { auto& realm = *vm.current_realm(); @@ -96,9 +100,11 @@ JS::ThrowCompletionOr> PBKDF2Params::from_value(J hash = HashAlgorithmIdentifier { hash_object }; } - return adopt_own(*new PBKDF2Params { { name }, salt, iterations, hash.downcast() }); + return adopt_own(*new PBKDF2Params { name, salt, iterations, hash.downcast() }); } +RsaKeyGenParams::~RsaKeyGenParams() = default; + JS::ThrowCompletionOr> RsaKeyGenParams::from_value(JS::VM& vm, JS::Value value) { auto& object = value.as_object(); @@ -117,9 +123,11 @@ JS::ThrowCompletionOr> RsaKeyGenParams::from_valu public_exponent = static_cast(public_exponent_value.as_object()); - return adopt_own(*new RsaKeyGenParams { { name }, modulus_length, big_integer_from_api_big_integer(public_exponent) }); + return adopt_own(*new RsaKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent) }); } +RsaHashedKeyGenParams::~RsaHashedKeyGenParams() = default; + JS::ThrowCompletionOr> RsaHashedKeyGenParams::from_value(JS::VM& vm, JS::Value value) { auto& object = value.as_object(); @@ -148,7 +156,7 @@ JS::ThrowCompletionOr> RsaHashedKeyGenParams::fro hash = HashAlgorithmIdentifier { hash_object }; } - return adopt_own(*new RsaHashedKeyGenParams { { { name }, modulus_length, big_integer_from_api_big_integer(public_exponent) }, hash.get() }); + return adopt_own(*new RsaHashedKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent), hash.get() }); } // https://w3c.github.io/webcrypto/#rsa-oaep-operations diff --git a/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.h b/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.h index 0ab4297a34..a5f801e455 100644 --- a/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.h +++ b/Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.h @@ -25,6 +25,12 @@ using KeyDataType = Variant, Bindings::JsonWebK // https://w3c.github.io/webcrypto/#algorithm-overview struct AlgorithmParams { + virtual ~AlgorithmParams(); + explicit AlgorithmParams(String name) + : name(move(name)) + { + } + String name; static JS::ThrowCompletionOr> from_value(JS::VM&, JS::Value); @@ -32,6 +38,15 @@ struct AlgorithmParams { // https://w3c.github.io/webcrypto/#pbkdf2-params struct PBKDF2Params : public AlgorithmParams { + virtual ~PBKDF2Params() override; + PBKDF2Params(String name, JS::Handle salt, u32 iterations, HashAlgorithmIdentifier hash) + : AlgorithmParams(move(name)) + , salt(move(salt)) + , iterations(iterations) + , hash(move(hash)) + { + } + JS::Handle salt; u32 iterations; HashAlgorithmIdentifier hash; @@ -41,6 +56,15 @@ struct PBKDF2Params : public AlgorithmParams { // https://w3c.github.io/webcrypto/#dfn-RsaKeyGenParams struct RsaKeyGenParams : public AlgorithmParams { + virtual ~RsaKeyGenParams() override; + + RsaKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent) + : AlgorithmParams(move(name)) + , modulus_length(modulus_length) + , public_exponent(move(public_exponent)) + { + } + u32 modulus_length; // NOTE that the raw data is going to be in Big Endian u8[] format ::Crypto::UnsignedBigInteger public_exponent; @@ -50,6 +74,14 @@ struct RsaKeyGenParams : public AlgorithmParams { // https://w3c.github.io/webcrypto/#dfn-RsaHashedKeyGenParams struct RsaHashedKeyGenParams : public RsaKeyGenParams { + virtual ~RsaHashedKeyGenParams() override; + + RsaHashedKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent, HashAlgorithmIdentifier hash) + : RsaKeyGenParams(move(name), modulus_length, move(public_exponent)) + , hash(move(hash)) + { + } + HashAlgorithmIdentifier hash; static JS::ThrowCompletionOr> from_value(JS::VM&, JS::Value);