Skip to content

Truly independent web browser

LibWeb: Clear private CryptoKey storage before freeing it

Shannon Booth861da4a34ddb
diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
index 79730e5fcb8..bd1dd02ca4b 100644
--- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
+++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
@@ -11,6 +11,7 @@
#include <AK/BuiltinWrappers.h>
#include <AK/FloatingPoint.h>
+#include <AK/Memory.h>
#include <AK/StringBuilder.h>
#include <AK/Utf16StringBuilder.h>
#include <LibCrypto/BigInt/Tommath.h>
@@ -105,6 +106,15 @@ UnsignedBigInteger::~UnsignedBigInteger()
mp_clear(&m_mp);
}
+void UnsignedBigInteger::clear_sensitive_data()
+{
+ if (m_mp.dp)
+ secure_zero(m_mp.dp, static_cast<size_t>(m_mp.alloc) * sizeof(mp_digit));
+ m_mp.used = 0;
+ m_mp.sign = MP_ZPOS;
+ m_hash.clear();
+}
+
Bytes UnsignedBigInteger::export_data(Bytes data) const
{
size_t written = 0;
diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
index 5d0172e6a5c..c4826014fd2 100644
--- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
+++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
@@ -43,6 +43,8 @@ public:
UnsignedBigInteger();
~UnsignedBigInteger();
+ void clear_sensitive_data();
+
[[nodiscard]] static UnsignedBigInteger import_data(ReadonlyBytes data) { return UnsignedBigInteger(data); }
// Exports in big-endian (msb stored first), trimmed (no leading zeros) format
diff --git a/Libraries/LibCrypto/PK/EC.cpp b/Libraries/LibCrypto/PK/EC.cpp
index 5df64ba782e..fb0b8d74da2 100644
--- a/Libraries/LibCrypto/PK/EC.cpp
+++ b/Libraries/LibCrypto/PK/EC.cpp
@@ -14,6 +14,11 @@
namespace Crypto::PK {
+void ECPrivateKey::clear_sensitive_data()
+{
+ m_d.clear_sensitive_data();
+}
+
ErrorOr<ByteBuffer> ECPrivateKey::export_as_der() const
{
ASN1::Encoder encoder;
diff --git a/Libraries/LibCrypto/PK/EC.h b/Libraries/LibCrypto/PK/EC.h
index dd0d12b6855..3885d5e93ae 100644
--- a/Libraries/LibCrypto/PK/EC.h
+++ b/Libraries/LibCrypto/PK/EC.h
@@ -77,6 +77,8 @@ public:
ECPrivateKey() = default;
+ void clear_sensitive_data();
+
UnsignedBigInteger const& d() const { return m_d; }
ErrorOr<ByteBuffer> d_bytes() const
{
diff --git a/Libraries/LibCrypto/PK/MLDSA.cpp b/Libraries/LibCrypto/PK/MLDSA.cpp
index 5aaedddc08f..5e8b93eb43e 100644
--- a/Libraries/LibCrypto/PK/MLDSA.cpp
+++ b/Libraries/LibCrypto/PK/MLDSA.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Memory.h>
#include <LibCrypto/PK/MLDSA.h>
#include <LibCrypto/Curves/SECPxxxr1.h>
@@ -15,6 +16,12 @@
namespace Crypto::PK {
+void MLDSAPrivateKey::clear_sensitive_data()
+{
+ secure_zero(m_seed.data(), m_seed.size());
+ secure_zero(m_private_key.data(), m_private_key.size());
+}
+
namespace {
char const* mldsa_size_to_openssl_name(MLDSASize size)
diff --git a/Libraries/LibCrypto/PK/MLDSA.h b/Libraries/LibCrypto/PK/MLDSA.h
index 803f84a355b..cc9e5cbbc23 100644
--- a/Libraries/LibCrypto/PK/MLDSA.h
+++ b/Libraries/LibCrypto/PK/MLDSA.h
@@ -43,6 +43,8 @@ public:
MLDSAPrivateKey() = default;
+ void clear_sensitive_data();
+
ByteBuffer const& seed() const { return m_seed; }
ByteBuffer const& public_key() const { return m_public_key; }
ByteBuffer const& private_key() const { return m_private_key; }
diff --git a/Libraries/LibCrypto/PK/MLKEM.cpp b/Libraries/LibCrypto/PK/MLKEM.cpp
index ad7d00ef6da..7d28a07b3f5 100644
--- a/Libraries/LibCrypto/PK/MLKEM.cpp
+++ b/Libraries/LibCrypto/PK/MLKEM.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Memory.h>
#include <LibCrypto/PK/MLKEM.h>
#include <LibCrypto/ASN1/DER.h>
@@ -16,6 +17,12 @@
namespace Crypto::PK {
+void MLKEMPrivateKey::clear_sensitive_data()
+{
+ secure_zero(m_seed.data(), m_seed.size());
+ secure_zero(m_private_key.data(), m_private_key.size());
+}
+
static char const* mlkem_size_to_openssl_name(MLKEMSize size)
{
switch (size) {
diff --git a/Libraries/LibCrypto/PK/MLKEM.h b/Libraries/LibCrypto/PK/MLKEM.h
index 1cad6794e6f..aaea350f796 100644
--- a/Libraries/LibCrypto/PK/MLKEM.h
+++ b/Libraries/LibCrypto/PK/MLKEM.h
@@ -43,6 +43,8 @@ public:
MLKEMPrivateKey() = default;
+ void clear_sensitive_data();
+
ByteBuffer const& seed() const { return m_seed; }
ByteBuffer const& public_key() const { return m_public_key; }
ByteBuffer const& private_key() const { return m_private_key; }
diff --git a/Libraries/LibCrypto/PK/RSA.cpp b/Libraries/LibCrypto/PK/RSA.cpp
index fc275fc1bbe..5a46868771d 100644
--- a/Libraries/LibCrypto/PK/RSA.cpp
+++ b/Libraries/LibCrypto/PK/RSA.cpp
@@ -20,6 +20,16 @@
namespace Crypto::PK {
+void RSAPrivateKey::clear_sensitive_data()
+{
+ m_private_exponent.clear_sensitive_data();
+ m_prime_1.clear_sensitive_data();
+ m_prime_2.clear_sensitive_data();
+ m_exponent_1.clear_sensitive_data();
+ m_exponent_2.clear_sensitive_data();
+ m_coefficient.clear_sensitive_data();
+}
+
ErrorOr<RSA::KeyPairType> RSA::parse_rsa_key(ReadonlyBytes der, bool is_private, Vector<StringView> current_scope)
{
KeyPairType keypair;
diff --git a/Libraries/LibCrypto/PK/RSA.h b/Libraries/LibCrypto/PK/RSA.h
index ef98c9d679e..323475a4cb0 100644
--- a/Libraries/LibCrypto/PK/RSA.h
+++ b/Libraries/LibCrypto/PK/RSA.h
@@ -81,6 +81,8 @@ public:
RSAPrivateKey() = default;
UnsignedBigInteger const& modulus() const { return m_modulus; }
+ void clear_sensitive_data();
+
UnsignedBigInteger const& private_exponent() const { return m_private_exponent; }
UnsignedBigInteger const& public_exponent() const { return m_public_exponent; }
UnsignedBigInteger const& prime1() const { return m_prime_1; }
diff --git a/Libraries/LibWeb/Crypto/CryptoKey.cpp b/Libraries/LibWeb/Crypto/CryptoKey.cpp
index 7ddc28a016e..0827f5bb5f8 100644
--- a/Libraries/LibWeb/Crypto/CryptoKey.cpp
+++ b/Libraries/LibWeb/Crypto/CryptoKey.cpp
@@ -81,7 +81,11 @@ void web_crypto_key_destroy_handle(CryptoKey::InternalKeyData* handle)
[](ByteBuffer& data) { secure_zero(data.data(), data.size()); },
[](OKPPublicKey& data) { secure_zero(data.bytes.data(), data.bytes.size()); },
[](OKPPrivateKey& data) { secure_zero(data.bytes.data(), data.bytes.size()); },
- [](auto& data) { secure_zero(reinterpret_cast<u8*>(&data), sizeof(data)); });
+ [](::Crypto::PK::RSAPrivateKey& data) { data.clear_sensitive_data(); },
+ [](::Crypto::PK::ECPrivateKey& data) { data.clear_sensitive_data(); },
+ [](::Crypto::PK::MLDSAPrivateKey& data) { data.clear_sensitive_data(); },
+ [](::Crypto::PK::MLKEMPrivateKey& data) { data.clear_sensitive_data(); },
+ [](auto&) { });
delete handle;
}
diff --git a/Tests/LibCrypto/TestBigInteger.cpp b/Tests/LibCrypto/TestBigInteger.cpp
index f3d72d6de0f..f5a2e70e0cc 100644
--- a/Tests/LibCrypto/TestBigInteger.cpp
+++ b/Tests/LibCrypto/TestBigInteger.cpp
@@ -52,6 +52,17 @@ BENCHMARK_CASE(bench_bigint_fib100000)
(void)res;
}
+TEST_CASE(test_unsigned_bigint_clear_sensitive_data)
+{
+ auto value = MUST(Crypto::UnsignedBigInteger::from_base(16, "fedcba98765432100123456789abcdef"sv));
+ (void)value.hash();
+
+ value.clear_sensitive_data();
+
+ EXPECT(value.is_zero());
+ EXPECT_EQ(value.hash(), Crypto::UnsignedBigInteger(0).hash());
+}
+
TEST_CASE(test_unsigned_bigint_addition_initialization)
{
Crypto::UnsignedBigInteger num1;