Truly independent web browser
LibWeb: Keep the ECDH public key alive in normalized parameters
Tim Ledbetterb7a449a1ef00
diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h b/Libraries/LibWeb/Crypto/CryptoAlgorithms.hindex be35396dae8..d78afce91d2 100644--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h@@ -692,7 +692,7 @@ struct EcdhKeyDeriveParams : public AlgorithmParams {{}- GC::Ref<CryptoKey> public_key;+ GC::Root<CryptoKey> public_key;static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);};diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-public-key-lifetime.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-public-key-lifetime.txtnew file mode 100644index 00000000000..c01ed8c5ada--- /dev/null+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-public-key-lifetime.txt@@ -0,0 +1,4 @@+ECDH deriveBits: same secret+X25519 deriveBits: same secret+X448 deriveBits: same secret+ECDH deriveKey: same secretdiff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-public-key-lifetime.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-public-key-lifetime.htmlnew file mode 100644index 00000000000..32d5e9b3948--- /dev/null+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-public-key-lifetime.html@@ -0,0 +1,49 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ // The public key passed to deriveBits() and deriveKey() must stay alive until the derivation runs, even when the+ // page keeps no other reference to it.+ const filler = await crypto.subtle.generateKey({ name: "HMAC", hash: "SHA-256" }, true, ["sign"]);++ function hex(buffer) {+ return Array.from(new Uint8Array(buffer), byte => byte.toString(16).padStart(2, "0")).join("");+ }++ async function check(label, algorithm, derive) {+ const ownKeyPair = await crypto.subtle.generateKey(algorithm, false, ["deriveBits", "deriveKey"]);+ const peerKeyPair = await crypto.subtle.generateKey(algorithm, true, ["deriveBits", "deriveKey"]);+ const expected = hex(await crypto.subtle.deriveBits({ name: algorithm.name, public: peerKeyPair.publicKey }, ownKeyPair.privateKey, 128));++ // The getter returns a copy of the public key that nothing else refers to.+ const params = {+ name: algorithm.name,+ get public() {+ return structuredClone(peerKeyPair.publicKey);+ },+ };+ const promise = derive(params, ownKeyPair.privateKey);++ // Collect the copy if nothing keeps it alive, then reuse its memory for other keys.+ internals.gc();+ const fillerCopies = Array.from({ length: 1024 }, () => structuredClone(filler));++ try {+ println(`${label}: ${hex(await promise) === expected ? "same secret" : "different secret"}`);+ } catch (error) {+ println(`${label}: ${error.name}`);+ }+ }++ const deriveBits = (params, key) => crypto.subtle.deriveBits(params, key, 128);+ const deriveKey = async (params, key) => {+ const derivedKey = await crypto.subtle.deriveKey(params, key, { name: "HMAC", hash: "SHA-256", length: 128 }, true, ["sign"]);+ return crypto.subtle.exportKey("raw", derivedKey);+ };++ await check("ECDH deriveBits", { name: "ECDH", namedCurve: "P-256" }, deriveBits);+ await check("X25519 deriveBits", { name: "X25519" }, deriveBits);+ await check("X448 deriveBits", { name: "X448" }, deriveBits);+ await check("ECDH deriveKey", { name: "ECDH", namedCurve: "P-256" }, deriveKey);+ });+</script>