Skip to content

Commit 0a3d27c

Browse files
committed
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.
1 parent 1d70306 commit 0a3d27c

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ static ::Crypto::UnsignedBigInteger big_integer_from_api_big_integer(JS::GCPtr<J
5757
return result;
5858
}
5959

60+
AlgorithmParams::~AlgorithmParams() = default;
61+
6062
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AlgorithmParams::from_value(JS::VM& vm, JS::Value value)
6163
{
6264
auto& object = value.as_object();
6365

6466
auto name = TRY(object.get("name"));
6567
auto name_string = TRY(name.to_string(vm));
6668

67-
return adopt_own(*new AlgorithmParams { .name = name_string });
69+
return adopt_own(*new AlgorithmParams { name_string });
6870
}
6971

72+
PBKDF2Params::~PBKDF2Params() = default;
73+
7074
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> PBKDF2Params::from_value(JS::VM& vm, JS::Value value)
7175
{
7276
auto& realm = *vm.current_realm();
@@ -96,9 +100,11 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> PBKDF2Params::from_value(J
96100
hash = HashAlgorithmIdentifier { hash_object };
97101
}
98102

99-
return adopt_own<AlgorithmParams>(*new PBKDF2Params { { name }, salt, iterations, hash.downcast<HashAlgorithmIdentifier>() });
103+
return adopt_own<AlgorithmParams>(*new PBKDF2Params { name, salt, iterations, hash.downcast<HashAlgorithmIdentifier>() });
100104
}
101105

106+
RsaKeyGenParams::~RsaKeyGenParams() = default;
107+
102108
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaKeyGenParams::from_value(JS::VM& vm, JS::Value value)
103109
{
104110
auto& object = value.as_object();
@@ -117,9 +123,11 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaKeyGenParams::from_valu
117123

118124
public_exponent = static_cast<JS::Uint8Array&>(public_exponent_value.as_object());
119125

120-
return adopt_own<AlgorithmParams>(*new RsaKeyGenParams { { name }, modulus_length, big_integer_from_api_big_integer(public_exponent) });
126+
return adopt_own<AlgorithmParams>(*new RsaKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent) });
121127
}
122128

129+
RsaHashedKeyGenParams::~RsaHashedKeyGenParams() = default;
130+
123131
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaHashedKeyGenParams::from_value(JS::VM& vm, JS::Value value)
124132
{
125133
auto& object = value.as_object();
@@ -148,7 +156,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaHashedKeyGenParams::fro
148156
hash = HashAlgorithmIdentifier { hash_object };
149157
}
150158

151-
return adopt_own<AlgorithmParams>(*new RsaHashedKeyGenParams { { { name }, modulus_length, big_integer_from_api_big_integer(public_exponent) }, hash.get<HashAlgorithmIdentifier>() });
159+
return adopt_own<AlgorithmParams>(*new RsaHashedKeyGenParams { name, modulus_length, big_integer_from_api_big_integer(public_exponent), hash.get<HashAlgorithmIdentifier>() });
152160
}
153161

154162
// https://w3c.github.io/webcrypto/#rsa-oaep-operations

Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,28 @@ using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, Bindings::JsonWebK
2525

2626
// https://w3c.github.io/webcrypto/#algorithm-overview
2727
struct AlgorithmParams {
28+
virtual ~AlgorithmParams();
29+
explicit AlgorithmParams(String name)
30+
: name(move(name))
31+
{
32+
}
33+
2834
String name;
2935

3036
static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
3137
};
3238

3339
// https://w3c.github.io/webcrypto/#pbkdf2-params
3440
struct PBKDF2Params : public AlgorithmParams {
41+
virtual ~PBKDF2Params() override;
42+
PBKDF2Params(String name, JS::Handle<WebIDL::BufferSource> salt, u32 iterations, HashAlgorithmIdentifier hash)
43+
: AlgorithmParams(move(name))
44+
, salt(move(salt))
45+
, iterations(iterations)
46+
, hash(move(hash))
47+
{
48+
}
49+
3550
JS::Handle<WebIDL::BufferSource> salt;
3651
u32 iterations;
3752
HashAlgorithmIdentifier hash;
@@ -41,6 +56,15 @@ struct PBKDF2Params : public AlgorithmParams {
4156

4257
// https://w3c.github.io/webcrypto/#dfn-RsaKeyGenParams
4358
struct RsaKeyGenParams : public AlgorithmParams {
59+
virtual ~RsaKeyGenParams() override;
60+
61+
RsaKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent)
62+
: AlgorithmParams(move(name))
63+
, modulus_length(modulus_length)
64+
, public_exponent(move(public_exponent))
65+
{
66+
}
67+
4468
u32 modulus_length;
4569
// NOTE that the raw data is going to be in Big Endian u8[] format
4670
::Crypto::UnsignedBigInteger public_exponent;
@@ -50,6 +74,14 @@ struct RsaKeyGenParams : public AlgorithmParams {
5074

5175
// https://w3c.github.io/webcrypto/#dfn-RsaHashedKeyGenParams
5276
struct RsaHashedKeyGenParams : public RsaKeyGenParams {
77+
virtual ~RsaHashedKeyGenParams() override;
78+
79+
RsaHashedKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent, HashAlgorithmIdentifier hash)
80+
: RsaKeyGenParams(move(name), modulus_length, move(public_exponent))
81+
, hash(move(hash))
82+
{
83+
}
84+
5385
HashAlgorithmIdentifier hash;
5486

5587
static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);

0 commit comments

Comments
 (0)