Skip to content

Truly independent web browser

LibWeb: Normalize algorithms before reading the decapsulation ciphertext

Shannon Booth7d08e6fff52f
diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
index fef5867109d..49e7eeba99c 100644
--- a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
@@ -1326,28 +1326,28 @@ GC::Ref<WebIDL::Promise> SubtleCrypto::decapsulate_key(JS::Realm& realm, Algorit
// decapsulationAlgorithm, decapsulationKey, sharedKeyAlgorithm, extractable and keyUsages parameters passed to
// the decapsulateKey() method, respectively.
- // 2. Let ciphertext be the result of getting a copy of the bytes held by the ciphertext parameter passed to the
- // decapsulateKey() method.
- auto cipher_text = MUST(WebIDL::get_buffer_source_copy(WebIDL::BufferSource { ciphertext }));
-
- // 3. Let normalizedDecapsulationAlgorithm be the result of normalizing an algorithm, with alg set to
+ // 2. Let normalizedDecapsulationAlgorithm be the result of normalizing an algorithm, with alg set to
// decapsulationAlgorithm and op set to "decapsulate".
auto normalized_decapsulation_algorithm = normalize_an_algorithm(realm, decapsulation_algorithm, "decapsulate"_string);
- // 4. If an error occurred, return a Promise rejected with normalizedDecapsulationAlgorithm.
+ // 3. If an error occurred, return a Promise rejected with normalizedDecapsulationAlgorithm.
if (normalized_decapsulation_algorithm.is_error()) {
return WebIDL::create_rejected_promise_from_exception(realm, normalized_decapsulation_algorithm.release_error());
}
- // 5. Let normalizedSharedKeyAlgorithm be the result of normalizing an algorithm, with alg
+ // 4. Let normalizedSharedKeyAlgorithm be the result of normalizing an algorithm, with alg
// set to sharedKeyAlgorithm and op set to "importKey".
auto normalized_shared_key_algorithm = normalize_an_algorithm(realm, shared_key_algorithm, "importKey"_string);
- // 6. If an error occurred, return a Promise rejected with normalizedSharedKeyAlgorithm.
+ // 5. If an error occurred, return a Promise rejected with normalizedSharedKeyAlgorithm.
if (normalized_shared_key_algorithm.is_error()) {
return WebIDL::create_rejected_promise_from_exception(realm, normalized_shared_key_algorithm.release_error());
}
+ // 6. Let ciphertext be the result of getting a copy of the bytes held by the ciphertext parameter passed to the
+ // decapsulateKey() method.
+ auto cipher_text = MUST(WebIDL::get_buffer_source_copy(WebIDL::BufferSource { ciphertext }));
+
// 7. Let realm be the relevant realm of this.
// 8. Let promise be a new Promise.
@@ -1432,19 +1432,19 @@ GC::Ref<WebIDL::Promise> SubtleCrypto::decapsulate_bits(JS::Realm& realm, Algori
// 1. Let decapsulationAlgorithm and decapsulationKey be the decapsulationAlgorithm and decapsulationKey
// parameters passed to the decapsulateBits() method, respectively.
- // 2. Let ciphertext be the result of getting a copy of the bytes held by the ciphertext parameter passed to the
- // decapsulateBits() method.
- auto cipher_text = MUST(WebIDL::get_buffer_source_copy(WebIDL::BufferSource { ciphertext }));
-
- // 3. Let normalizedDecapsulationAlgorithm be the result of normalizing an algorithm, with alg set to
+ // 2. Let normalizedDecapsulationAlgorithm be the result of normalizing an algorithm, with alg set to
// decapsulationAlgorithm and op set to "decapsulate".
auto normalized_decapsulation_algorithm = normalize_an_algorithm(realm, decapsulation_algorithm, "decapsulate"_string);
- // 4. If an error occurred, return a Promise rejected with normalizedDecapsulationAlgorithm.
+ // 3. If an error occurred, return a Promise rejected with normalizedDecapsulationAlgorithm.
if (normalized_decapsulation_algorithm.is_error()) {
return WebIDL::create_rejected_promise_from_exception(realm, normalized_decapsulation_algorithm.release_error());
}
+ // 4. Let ciphertext be the result of getting a copy of the bytes held by the ciphertext parameter passed to the
+ // decapsulateBits() method.
+ auto cipher_text = MUST(WebIDL::get_buffer_source_copy(WebIDL::BufferSource { ciphertext }));
+
// 5. Let realm be the relevant realm of this.
// 6. Let promise be a new Promise.
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-decapsulate-ciphertext-altered-during-call.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-decapsulate-ciphertext-altered-during-call.txt
new file mode 100644
index 00000000000..ac346cea169
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-decapsulate-ciphertext-altered-during-call.txt
@@ -0,0 +1,2 @@
+decapsulateBits used the altered ciphertext: true
+decapsulateKey used the altered ciphertext: true
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-decapsulate-ciphertext-altered-during-call.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-decapsulate-ciphertext-altered-during-call.html
new file mode 100644
index 00000000000..c8ccb650160
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-decapsulate-ciphertext-altered-during-call.html
@@ -0,0 +1,39 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ async function check(label, promise) {
+ try {
+ await promise;
+ println(`${label}: ok`);
+ } catch (error) {
+ println(`${label}: ${error.name}`);
+ }
+ }
+
+ const equal = (a, b) => a.byteLength === b.byteLength && new Uint8Array(a).every((byte, i) => byte === new Uint8Array(b)[i]);
+
+ const { publicKey, privateKey } = await crypto.subtle.generateKey("ML-KEM-768", false, ["encapsulateBits", "decapsulateBits", "decapsulateKey"]);
+
+ const encapsulated = await crypto.subtle.encapsulateBits("ML-KEM-768", publicKey);
+ const zeroed = await crypto.subtle.decapsulateBits("ML-KEM-768", privateKey, new Uint8Array(encapsulated.ciphertext.byteLength));
+
+ const bits = await crypto.subtle.decapsulateBits({
+ get name() {
+ new Uint8Array(encapsulated.ciphertext).fill(0);
+ return "ML-KEM-768";
+ },
+ }, privateKey, encapsulated.ciphertext);
+ println(`decapsulateBits used the altered ciphertext: ${equal(bits, zeroed)}`);
+
+ const reencapsulated = await crypto.subtle.encapsulateBits("ML-KEM-768", publicKey);
+ const sharedKey = await crypto.subtle.decapsulateKey("ML-KEM-768", privateKey, reencapsulated.ciphertext, {
+ get name() {
+ new Uint8Array(reencapsulated.ciphertext).fill(0);
+ return "HMAC";
+ },
+ hash: "SHA-256",
+ }, true, ["sign"]);
+ println(`decapsulateKey used the altered ciphertext: ${equal(await crypto.subtle.exportKey("raw", sharedKey), zeroed)}`);
+ });
+</script>