Skip to content

Truly independent web browser

LibWeb: Reject unsupported key formats in ECDH exportKey

Shannon Booth01737b344ee6
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
index 04a0650833a..dab1f624fd3 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
@@ -9754,10 +9754,8 @@ impl AlgorithmMethods for Ecdh {
// 2. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
// NB: The key handle is always accessible.
- let mut result = None;
-
// 3. If format is "spki":
- if format == KeyFormat::Spki {
+ let result = if format == KeyFormat::Spki {
// 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
if key.key_type() != KeyType::Public {
return Err(dom_error("InvalidAccessError", b"Key is not a public key"));
@@ -9808,7 +9806,7 @@ impl AlgorithmMethods for Ecdh {
// * Set the subjectPublicKey field to keyData
// AD-HOC: The draft omits the result assignment for this DER format.
- result = Some(ExportKeyResult::Bytes(data));
+ ExportKeyResult::Bytes(data)
}
// 3. If format is "pkcs8":
else if format == KeyFormat::Pkcs8 {
@@ -9869,7 +9867,7 @@ impl AlgorithmMethods for Ecdh {
// * Set the privateKey field to keyData.
// AD-HOC: The draft omits the result assignment for this DER format.
- result = Some(ExportKeyResult::Bytes(data));
+ ExportKeyResult::Bytes(data)
}
// 3. If format is "jwk":
else if format == KeyFormat::Jwk {
@@ -9946,7 +9944,7 @@ impl AlgorithmMethods for Ecdh {
jwk.ext = Some(key.is_extractable());
// 6. Let result be jwk.
- result = Some(ExportKeyResult::JsonWebKey(jwk));
+ ExportKeyResult::JsonWebKey(jwk)
}
// 3. If format is "raw":
// https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat
@@ -9982,11 +9980,15 @@ impl AlgorithmMethods for Ecdh {
};
// 3. Let result be data.
- result = Some(ExportKeyResult::Bytes(data));
+ ExportKeyResult::Bytes(data)
}
+ // AD-HOC: The specification leaves result unset for unsupported formats.
+ else {
+ return Err(dom_error("NotSupportedError", b"Invalid key format"));
+ };
// 4. Return result.
- Ok(result.expect("the operation has a result"))
+ Ok(result)
}
fn public_key_usages(&self) -> Option<Vec<KeyUsage>> {
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-export-unsupported-format.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-export-unsupported-format.txt
new file mode 100644
index 00000000000..1186a6b263f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-export-unsupported-format.txt
@@ -0,0 +1,6 @@
+public key, raw-private: NotSupportedError
+public key, raw-seed: NotSupportedError
+public key, raw-secret: NotSupportedError
+private key, raw-private: NotSupportedError
+private key, raw-seed: NotSupportedError
+private key, raw-secret: NotSupportedError
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-export-unsupported-format.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-export-unsupported-format.html
new file mode 100644
index 00000000000..0b0a612f185
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-export-unsupported-format.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const { publicKey, privateKey } = await crypto.subtle.generateKey(
+ { name: "ECDH", namedCurve: "P-256" },
+ true,
+ ["deriveBits"]
+ );
+ for (const [description, key] of [
+ ["public", publicKey],
+ ["private", privateKey],
+ ]) {
+ for (const format of ["raw-private", "raw-seed", "raw-secret"]) {
+ try {
+ await crypto.subtle.exportKey(format, key);
+ println(`${description} key, ${format}: exported`);
+ } catch (error) {
+ println(`${description} key, ${format}: ${error.name}`);
+ }
+ }
+ }
+ });
+</script>