Truly independent web browser
LibWeb+LibCrypto: Reject ML-DSA public keys of another parameter set
Shannon Booth60f5686ed2d6
diff --git a/Libraries/LibCrypto/Rust/src/ffi.rs b/Libraries/LibCrypto/Rust/src/ffi.rsindex e40fcb3e042..8a3b51e60f4 100644--- a/Libraries/LibCrypto/Rust/src/ffi.rs+++ b/Libraries/LibCrypto/Rust/src/ffi.rs@@ -379,7 +379,7 @@ unsafe extern "C" {oid: &mut Span<'_, u32>,) -> ();pub(super) fn crypto_mldsa_key_info_read_private_key(key_info: *const c_void) -> *mut c_void;- pub(super) fn crypto_mldsa_read_public_key(bytes: Span<'_, u8>) -> *mut c_void;+ pub(super) fn crypto_mldsa_read_public_key(size: MlDsaSize, bytes: Span<'_, u8>) -> *mut c_void;pub(super) fn crypto_mldsa_public_bytes(key: *const c_void, bytes: &mut Span<'_, u8>) -> ();pub(super) fn crypto_mldsa_seed_bytes(key: *const c_void, state: *mut c_void, receive: ReceiveBytes) -> bool;pub(super) fn crypto_mldsa_export_der(diff --git a/Libraries/LibCrypto/Rust/src/lib.rs b/Libraries/LibCrypto/Rust/src/lib.rsindex 9716557c1b6..cad8869fec4 100644--- a/Libraries/LibCrypto/Rust/src/lib.rs+++ b/Libraries/LibCrypto/Rust/src/lib.rs@@ -845,9 +845,9 @@ pub fn mldsa_generate_key_pair(size: MlDsaSize, seed: &[u8]) -> Result<KeyPair,Ok(unsafe { KeyPair::from_raw(public_key, private_key) })}-pub fn mldsa_read_public_key(bytes: &[u8]) -> Option<KeyHandle> {+pub fn mldsa_read_public_key(size: MlDsaSize, bytes: &[u8]) -> Option<KeyHandle> {// SAFETY: The input is borrowed for the call.- let raw = unsafe { ffi::crypto_mldsa_read_public_key(Span::new(bytes)) };+ let raw = unsafe { ffi::crypto_mldsa_read_public_key(size, Span::new(bytes)) };// SAFETY: A non-null result transfers ownership of the key.unsafe { KeyHandle::from_raw(raw) }}diff --git a/Libraries/LibCrypto/RustBindings.cpp b/Libraries/LibCrypto/RustBindings.cppindex 9cfe7c762e0..8dccd6a4dc9 100644--- a/Libraries/LibCrypto/RustBindings.cpp+++ b/Libraries/LibCrypto/RustBindings.cpp@@ -765,7 +765,7 @@ bool crypto_mldsa_verify(::Crypto::PK::MLDSASize, KeyHandle const*, ReadonlyByteFlatPtr crypto_mldsa_generate_key_pair(::Crypto::PK::MLDSASize, ReadonlyBytes, KeyHandle**, KeyHandle**);void crypto_asn1_key_info_identifier(Asn1Structure, void const*, ReadonlySpan<u32>*);KeyHandle* crypto_mldsa_key_info_read_private_key(void const*);-KeyHandle* crypto_mldsa_read_public_key(ReadonlyBytes);+KeyHandle* crypto_mldsa_read_public_key(::Crypto::PK::MLDSASize, ReadonlyBytes);void crypto_mldsa_public_bytes(KeyHandle const*, ReadonlyBytes*);bool crypto_mldsa_seed_bytes(KeyHandle const*, void*, ReceiveEncodedBytes);bool crypto_mldsa_export_der(KeyHandle const*, bool, ReadonlySpan<u32>, void*, ReceiveEncodedBytes);@@ -810,10 +810,10 @@ KeyHandle* crypto_mldsa_key_info_read_private_key(void const* key_info)return new KeyHandle(static_cast<PrivateKeyStorage const*>(key_info)->value.mldsa);}-KeyHandle* crypto_mldsa_read_public_key(ReadonlyBytes bytes)+KeyHandle* crypto_mldsa_read_public_key(::Crypto::PK::MLDSASize size, ReadonlyBytes bytes){auto public_key = ::Crypto::PK::MLDSAPublicKey::from_bytes(bytes);- if (public_key.is_error())+ if (public_key.is_error() || public_key.value().size() != size)return nullptr;return new KeyHandle(public_key.release_value());}diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rsindex 3e398eba736..709260cdb02 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs@@ -12412,8 +12412,21 @@ const ML_DSA_44_OID: [u32; 9] = [2, 16, 840, 1, 101, 3, 4, 3, 17];const ML_DSA_65_OID: [u32; 9] = [2, 16, 840, 1, 101, 3, 4, 3, 18];const ML_DSA_87_OID: [u32; 9] = [2, 16, 840, 1, 101, 3, 4, 3, 19];-fn mldsa_read_public_key(vm: &Vm, bytes: &[u8]) -> WebIdlResult<Ref<CryptoKey>> {- let Some(key) = libcrypto_rust::mldsa_read_public_key(bytes) else {+fn mldsa_size(name: &Utf16String) -> MlDsaSize {+ if matches!(name.as_units(), Utf16StringUnits::Ascii(b"ML-DSA-44")) {+ MlDsaSize::MlDsa44+ } else if matches!(name.as_units(), Utf16StringUnits::Ascii(b"ML-DSA-65")) {+ MlDsaSize::MlDsa65+ } else if matches!(name.as_units(), Utf16StringUnits::Ascii(b"ML-DSA-87")) {+ MlDsaSize::MlDsa87+ } else {+ unreachable!("the name identifies an ML-DSA parameter set")+ }+}++// NB: A public key whose length belongs to another parameter set is not a key for this one.+fn mldsa_read_public_key(vm: &Vm, size: MlDsaSize, bytes: &[u8]) -> WebIdlResult<Ref<CryptoKey>> {+ let Some(key) = libcrypto_rust::mldsa_read_public_key(size, bytes) else {return Err(dom_error("DataError", b"Invalid key"));};Ok(CryptoKey::create(vm, key))@@ -12736,7 +12749,7 @@ impl AlgorithmMethods for MlDsa {let public_key = spki.raw_key();// 8. Let key be a new CryptoKey that represents publicKey.- key = mldsa_read_public_key(cx.vm(), public_key)?;+ key = mldsa_read_public_key(cx.vm(), mldsa_size(¶ms.name), public_key)?;// 9. Set the [[type]] internal slot of key to "public"key.set_type(KeyType::Public);@@ -12851,7 +12864,7 @@ impl AlgorithmMethods for MlDsa {let ImportKeyData::Bytes(key_data) = key_data else {unreachable!("raw key data contains bytes")};- key = mldsa_read_public_key(cx.vm(), key_data.as_slice())?;+ key = mldsa_read_public_key(cx.vm(), mldsa_size(¶ms.name), key_data.as_slice())?;// 5. Set the [[type]] internal slot of key to "public"key.set_type(KeyType::Public);@@ -13007,7 +13020,7 @@ impl AlgorithmMethods for MlDsa {// 2. Let key be a new CryptoKey object that represents the ML-DSA public key// identified by interpreting the pub attribute of jwk as a base64url encoded public key.- key = mldsa_read_public_key(cx.vm(), public_key.as_slice())?;+ key = mldsa_read_public_key(cx.vm(), mldsa_size(¶ms.name), public_key.as_slice())?;// 3. Set the [[type]] internal slot of key to "public".key.set_type(KeyType::Public);diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-MLDSA-import-raw-public-parameter-set.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-MLDSA-import-raw-public-parameter-set.txtnew file mode 100644index 00000000000..b64fa35fb81--- /dev/null+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-MLDSA-import-raw-public-parameter-set.txt@@ -0,0 +1,2 @@+ML-DSA-44 public key imported as ML-DSA-65: DataError+ML-DSA-44 public key imported as ML-DSA-44: importeddiff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-MLDSA-import-raw-public-parameter-set.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-MLDSA-import-raw-public-parameter-set.htmlnew file mode 100644index 00000000000..fdd6f674264--- /dev/null+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-MLDSA-import-raw-public-parameter-set.html@@ -0,0 +1,16 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const key = await crypto.subtle.generateKey({ name: "ML-DSA-44" }, true, ["sign", "verify"]);+ const raw = await crypto.subtle.exportKey("raw-public", key.publicKey);++ for (const name of ["ML-DSA-65", "ML-DSA-44"]) {+ const result = await crypto.subtle.importKey("raw-public", raw, { name }, true, ["verify"]).then(+ () => "imported",+ error => error.name+ );+ println(`ML-DSA-44 public key imported as ${name}: ${result}`);+ }+ });+</script>