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.
This commit is contained in:
Andrew Kaster 2024-03-13 21:26:00 -06:00 committed by Andrew Kaster
parent 1d70306c41
commit 0a3d27c41d
Notes: sideshowbarker 2024-07-17 02:29:45 +09:00
2 changed files with 44 additions and 4 deletions

View File

@ -57,6 +57,8 @@ static ::Crypto::UnsignedBigInteger big_integer_from_api_big_integer(JS::GCPtr<J
return result;
}
AlgorithmParams::~AlgorithmParams() = default;
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AlgorithmParams::from_value(JS::VM& vm, JS::Value value)
{
auto& object = value.as_object();
@ -64,9 +66,11 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> 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<NonnullOwnPtr<AlgorithmParams>> PBKDF2Params::from_value(JS::VM& vm, JS::Value value)
{
auto& realm = *vm.current_realm();
@ -96,9 +100,11 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> PBKDF2Params::from_value(J
hash = HashAlgorithmIdentifier { hash_object };
}
return adopt_own<AlgorithmParams>(*new PBKDF2Params { { name }, salt, iterations, hash.downcast<HashAlgorithmIdentifier>() });
return adopt_own<AlgorithmParams>(*new PBKDF2Params { name, salt, iterations, hash.downcast<HashAlgorithmIdentifier>() });
}
RsaKeyGenParams::~RsaKeyGenParams() = default;
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaKeyGenParams::from_value(JS::VM& vm, JS::Value value)
{
auto& object = value.as_object();
@ -117,9 +123,11 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaKeyGenParams::from_valu
public_exponent = static_cast<JS::Uint8Array&>(public_exponent_value.as_object());
return adopt_own<AlgorithmParams>(*new RsaKeyGenParams { { name }, modulus_length, big_integer_from_api_big_integer(public_exponent) });
return adopt_own<AlgorithmParams>(*new RsaKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent) });
}
RsaHashedKeyGenParams::~RsaHashedKeyGenParams() = default;
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaHashedKeyGenParams::from_value(JS::VM& vm, JS::Value value)
{
auto& object = value.as_object();
@ -148,7 +156,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaHashedKeyGenParams::fro
hash = HashAlgorithmIdentifier { hash_object };
}
return adopt_own<AlgorithmParams>(*new RsaHashedKeyGenParams { { { name }, modulus_length, big_integer_from_api_big_integer(public_exponent) }, hash.get<HashAlgorithmIdentifier>() });
return adopt_own<AlgorithmParams>(*new RsaHashedKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent), hash.get<HashAlgorithmIdentifier>() });
}
// https://w3c.github.io/webcrypto/#rsa-oaep-operations

View File

@ -25,6 +25,12 @@ using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, 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<NonnullOwnPtr<AlgorithmParams>> 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<WebIDL::BufferSource> salt, u32 iterations, HashAlgorithmIdentifier hash)
: AlgorithmParams(move(name))
, salt(move(salt))
, iterations(iterations)
, hash(move(hash))
{
}
JS::Handle<WebIDL::BufferSource> 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<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);