Truly independent web browser
LibWeb: Check raw EC public keys against the named curve
Shannon Booth37b74df276ff
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rsindex 750701f16c7..c425146741a 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs@@ -7893,11 +7893,18 @@ fn ec_key_from_coordinates(vm: &Vm, x: &Bytes, y: &Bytes, coord_size: usize, d:)}-fn ec_parse_raw_public_key(vm: &Vm, key_bytes: &Bytes) -> WebIdlResult<Ref<CryptoKey>> {+fn ec_parse_raw_public_key(vm: &Vm, curve: EcCurve, key_bytes: &Bytes) -> WebIdlResult<Ref<CryptoKey>> {let Some(key) = libcrypto_rust::ec_parse_raw_public_key(key_bytes.as_slice()) else {return Err(dom_error("DataError", b"Failed to parse key"));};- Ok(CryptoKey::create(vm, key))+ let key = CryptoKey::create(vm, key);+ let Ok(is_valid) = ec_key_is_valid(curve, key) else {+ return Err(dom_error("DataError", b"Failed to verify key"));+ };+ if !is_valid {+ return Err(dom_error("DataError", b"Invalid key"));+ }+ Ok(key)}fn ec_key_is_valid(curve: EcCurve, key: Ref<CryptoKey>) -> Result<bool, BackendError> {@@ -8711,7 +8718,7 @@ impl AlgorithmMethods for Ecdsa {}// 3. If namedCurve is "P-256", "P-384" or "P-521":- if ec_curve_from_name(&normalized_algorithm.named_curve).is_some() {+ if let Some(curve) = ec_curve_from_name(&normalized_algorithm.named_curve) {// 1. Let Q be the elliptic curve point on the curve identified by the namedCurve member of// normalizedAlgorithm identified by performing the conversion steps defined in Section 2.3.4 of// [SEC1] on keyData. The uncompressed point format MUST be supported.@@ -8722,7 +8729,7 @@ impl AlgorithmMethods for Ecdsa {let ImportKeyData::Bytes(key_bytes) = key_data else {unreachable!("raw key data contains bytes")};- key = ec_parse_raw_public_key(cx.vm(), &key_bytes)?;+ key = ec_parse_raw_public_key(cx.vm(), curve, &key_bytes)?;} else {// Otherwise:// 1. Perform any key import steps defined by other applicable specifications, passing format, keyData and obtaining key.@@ -9692,7 +9699,7 @@ impl AlgorithmMethods for Ecdh {}// 3. If namedCurve is "P-256", "P-384" or "P-521":- if ec_curve_from_name(&normalized_algorithm.named_curve).is_some() {+ if let Some(curve) = ec_curve_from_name(&normalized_algorithm.named_curve) {// 1. Let Q be the Elliptic Curve public key on the curve identified by the namedCurve member of// normalizedAlgorithm identified by performing the conversion steps defined in Section 2.3.4 of// [SEC1] to keyData. The uncompressed point format MUST be supported.@@ -9703,7 +9710,7 @@ impl AlgorithmMethods for Ecdh {let ImportKeyData::Bytes(key_bytes) = key_data else {unreachable!("raw key data contains bytes")};- key = ec_parse_raw_public_key(cx.vm(), &key_bytes)?;+ key = ec_parse_raw_public_key(cx.vm(), curve, &key_bytes)?;} else {// Otherwise:// 1. Perform any key import steps defined by other applicable specifications, passing format, keyData and obtaining key.diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-EC-import-raw-named-curve.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-EC-import-raw-named-curve.txtnew file mode 100644index 00000000000..8aa05c9cf34--- /dev/null+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-EC-import-raw-named-curve.txt@@ -0,0 +1,8 @@+ECDSA, P-256 point as P-256: imported as P-256+ECDSA, P-256 point as P-384: DataError+ECDSA, P-256 point as P-521: DataError+ECDSA, off-curve point: DataError+ECDH, P-256 point as P-256: imported as P-256+ECDH, P-256 point as P-384: DataError+ECDH, P-256 point as P-521: DataError+ECDH, off-curve point: DataErrordiff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-point-width.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-point-width.txtindex 1350e1598cf..fd4ae95c507 100644--- a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-point-width.txt+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-point-width.txt@@ -1,2 +1,2 @@-trigger=OperationError-control=OperationError+trigger=DataError+control=DataErrordiff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-EC-import-raw-named-curve.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-EC-import-raw-named-curve.htmlnew file mode 100644index 00000000000..d94d465b96f--- /dev/null+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-EC-import-raw-named-curve.html@@ -0,0 +1,29 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ for (const [name, usages, generateUsages] of [+ ["ECDSA", ["verify"], ["sign", "verify"]],+ ["ECDH", [], ["deriveBits"]],+ ]) {+ const { publicKey } = await crypto.subtle.generateKey({ name, namedCurve: "P-256" }, true, generateUsages);+ const raw = new Uint8Array(await crypto.subtle.exportKey("raw", publicKey));+ const offCurve = new Uint8Array(raw);+ offCurve[raw.length - 1] ^= 1;++ for (const [description, keyData, namedCurve] of [+ ["P-256 point as P-256", raw, "P-256"],+ ["P-256 point as P-384", raw, "P-384"],+ ["P-256 point as P-521", raw, "P-521"],+ ["off-curve point", offCurve, "P-256"],+ ]) {+ try {+ const key = await crypto.subtle.importKey("raw", keyData, { name, namedCurve }, true, usages);+ println(`${name}, ${description}: imported as ${key.algorithm.namedCurve}`);+ } catch (error) {+ println(`${name}, ${description}: ${error.name}`);+ }+ }+ }+ });+</script>