Truly independent web browser
LibWeb: Remove the C++ CryptoKey accessors
Shannon Boothc43d4b5029c0
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rsindex d809cfde7d8..72bc19d5673 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_key.rs@@ -9,7 +9,7 @@use core::cell::{Cell, RefCell};use core::ffi::c_void;-use ak::{Span, Utf16String};+use ak::Utf16String;use libcrypto_rust::{HandleTag, KeyHandle, OkpCurve};use libjs_rust::api::{Array, CellRoot, ErrorKind, ErrorType, Gc, GcRefCell, GcWeak, IntegrityLevel, MarkedVec, Must, Object,@@ -17,9 +17,7 @@ use libjs_rust::api::{};use crate::bindings::conversions::create_uint8_array;-use crate::bindings::interface::{- Ref, Wrappable, allocate, cell_from_abi, create_dictionary_object, realm_arguments, wrap,-};+use crate::bindings::interface::{Ref, Wrappable, allocate, cell_from_abi, create_dictionary_object};use crate::bindings::setlike::WorldEntries;use crate::bindings::structured_data::{Serializable, SerializationReader, SerializationWriter};use crate::bindings::{CallContext, Exception, WebIdlResult};@@ -136,8 +134,6 @@ pub(crate) struct CryptoKey {key_usages: RefCell<Vec<KeyUsage>>,#[gc(untraced)]handle: KeyHandle,- #[gc(untraced)]- algorithm_name: RefCell<Utf16String>,cached_algorithm_objects: GcRefCell<Vec<CachedAlgorithmObject>>,cached_usages_objects: WorldEntries<Object>,}@@ -258,7 +254,6 @@ impl CryptoKey {}),key_usages: RefCell::default(),handle,- algorithm_name: RefCell::default(),cached_algorithm_objects: GcRefCell::default(),cached_usages_objects: WorldEntries::default(),})@@ -298,7 +293,6 @@ impl CryptoKey {pub(crate) fn set_algorithm(&self, algorithm: InternalAlgorithmData) {self.algorithm.replace(algorithm);- self.algorithm_name.replace(Utf16String::default());self.cached_algorithm_objects.replace(Vec::new());}@@ -363,26 +357,10 @@ impl CryptoKey {}pub(crate) fn algorithm_name(&self) -> Utf16String {- let mut algorithm_name = self.algorithm_name.borrow_mut();- if algorithm_name.is_empty() {- *algorithm_name = self.algorithm.borrow().name().clone();- }- algorithm_name.clone()+ self.algorithm.borrow().name().clone()}}-#[derive(Default)]-#[repr(C)]-struct RawAlgorithm<'a> {- r#type: u8,- name: usize,- modulus_length: u32,- public_exponent: Span<'a, u8>,- hash: usize,- named_curve: usize,- length: u32,-}-/// The identifiers by which a storage record holds a key type.const KEY_TYPES: [&[u8]; 3] = [b"public", b"private", b"secret"];@@ -453,8 +431,7 @@ impl Serializable for CryptoKey {// 3. Set serialized.[[Algorithm]] to the sub-serialization of the [[algorithm]] internal slot of value.let algorithm = self.algorithm.borrow();- let fields = algorithm.fields();- serialized.write_u8(fields.r#type);+ serialized.write_u8(algorithm.serialized_type());serialized.write_utf16_string(algorithm.name());match &*algorithm {InternalAlgorithmData::Key { .. } => {}@@ -623,224 +600,69 @@ impl Serializable for CryptoKey {public_exponent_bytes.extend_from_slice(public_exponent);key.r#type.set(Some(KEY_TYPE_VALUES[usize::from(r#type)]));key.extractable.set(extractable);- key.set_algorithm(InternalAlgorithmData::from_fields(- &RawAlgorithm {- r#type: algorithm_type,- modulus_length,- length,- ..RawAlgorithm::default()- },- name,- hash,- named_curve,- public_exponent_bytes,- ));- key.set_usages(usages);- Ok(key)- }-}--impl InternalAlgorithmData {- fn fields(&self) -> RawAlgorithm<'_> {- let mut fields = RawAlgorithm {- name: self.name().raw_identity(),- ..RawAlgorithm::default()- };- fields.r#type = match self {- Self::Key { .. } => ALGORITHM_KEY,- Self::Rsa {- modulus_length,- public_exponent,- ..- } => {- fields.modulus_length = *modulus_length;- fields.public_exponent = Span::new(public_exponent);- ALGORITHM_RSA- }- Self::RsaHashed {- modulus_length,- public_exponent,- hash,- ..- } => {- fields.modulus_length = *modulus_length;- fields.public_exponent = Span::new(public_exponent);- fields.hash = hash.raw_identity();- ALGORITHM_RSA_HASHED- }- Self::Ec { named_curve, .. } => {- fields.named_curve = named_curve.raw_identity();- ALGORITHM_EC- }- Self::Aes { length, .. } => {- fields.length = u32::from(*length);- ALGORITHM_AES- }- Self::Hmac { hash, length, .. } => {- fields.hash = hash.raw_identity();- fields.length = *length;- ALGORITHM_HMAC- }- Self::Kmac { length, .. } => {- fields.length = *length;- ALGORITHM_KMAC- }- };- fields- }-- fn from_fields(- fields: &RawAlgorithm<'_>,- name: Utf16String,- hash: Utf16String,- named_curve: Utf16String,- public_exponent: Vec<u8>,- ) -> Self {- let RawAlgorithm {- r#type,- modulus_length,- length,- ..- } = *fields;- match r#type {- ALGORITHM_KEY => Self::Key { name },- ALGORITHM_RSA => Self::Rsa {+ key.set_algorithm(match algorithm_type {+ ALGORITHM_KEY => InternalAlgorithmData::Key { name },+ ALGORITHM_RSA => InternalAlgorithmData::Rsa {name,modulus_length,- public_exponent,+ public_exponent: public_exponent_bytes,},- ALGORITHM_RSA_HASHED => Self::RsaHashed {+ ALGORITHM_RSA_HASHED => InternalAlgorithmData::RsaHashed {name,modulus_length,- public_exponent,+ public_exponent: public_exponent_bytes,hash,},- ALGORITHM_EC => Self::Ec { name, named_curve },- ALGORITHM_AES => Self::Aes {+ ALGORITHM_EC => InternalAlgorithmData::Ec { name, named_curve },+ ALGORITHM_AES => InternalAlgorithmData::Aes {name,length: u16::try_from(length).expect("an AES key has a 16-bit length"),},- ALGORITHM_HMAC => Self::Hmac { name, hash, length },- ALGORITHM_KMAC => Self::Kmac { name, length },- _ => unreachable!("the type is that of a key algorithm"),- }+ ALGORITHM_HMAC => InternalAlgorithmData::Hmac { name, hash, length },+ ALGORITHM_KMAC => InternalAlgorithmData::Kmac { name, length },+ _ => unreachable!("the type is that of an algorithm, as checked above"),+ });+ key.set_usages(usages);+ Ok(key)}}-/// # Safety-///-/// The VM, realm, and CryptoKey cell must be live.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_rust_crypto_key_wrap(- vm: *mut c_void,- realm: *mut c_void,- key: *mut c_void,-) -> *mut c_void {- // SAFETY: The caller supplies the live VM, realm, and CryptoKey cell.- let ((vm, realm), key) = unsafe { (realm_arguments(vm, realm), cell_from_abi::<CryptoKey>(key)) };- wrap(&CallContext::new(vm, realm), key).as_ptr().cast()-}--/// # Safety-///-/// The key must be a live CryptoKey cell.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_rust_crypto_key_extractable(key: *mut c_void) -> bool {- // SAFETY: LibWeb supplies a live CryptoKey cell.- unsafe { cell_from_abi::<CryptoKey>(key) }.extractable.get()+impl InternalAlgorithmData {+ fn serialized_type(&self) -> u8 {+ match self {+ Self::Key { .. } => ALGORITHM_KEY,+ Self::Rsa { .. } => ALGORITHM_RSA,+ Self::RsaHashed { .. } => ALGORITHM_RSA_HASHED,+ Self::Ec { .. } => ALGORITHM_EC,+ Self::Aes { .. } => ALGORITHM_AES,+ Self::Hmac { .. } => ALGORITHM_HMAC,+ Self::Kmac { .. } => ALGORITHM_KMAC,+ }+ }}-/// # Safety+/// Gives a key with an RsaHashedKeyAlgorithm the RsaKeyAlgorithm without its hash, which no operation creates, so+/// that tests can serialize one.///-/// The key must be a live CryptoKey cell.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_rust_crypto_key_set_extractable(key: *mut c_void, extractable: bool) {- // SAFETY: LibWeb supplies a live CryptoKey cell.- unsafe { cell_from_abi::<CryptoKey>(key) }.extractable.set(extractable);-}-/// # Safety////// The key must be a live CryptoKey cell.#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_rust_crypto_key_type(key: *mut c_void) -> u8 {- // SAFETY: LibWeb supplies a live CryptoKey cell.- unsafe { cell_from_abi::<CryptoKey>(key) }.key_type() as u8-}--/// # Safety-///-/// The key must be a live CryptoKey cell and the type must be a valid KeyType.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_rust_crypto_key_set_type(key: *mut c_void, r#type: u8) {- // SAFETY: LibWeb supplies a live CryptoKey cell.- let key = unsafe { cell_from_abi::<CryptoKey>(key) };- key.r#type.set(Some(KEY_TYPE_VALUES[usize::from(r#type)]));-}--/// # Safety-///-/// The key must be a live CryptoKey cell and fields must point to writable storage.-#[unsafe(no_mangle)]-unsafe extern "C" fn web_rust_crypto_key_get_algorithm(key: *mut c_void, fields: *mut c_void) {+pub unsafe extern "C" fn web_rust_crypto_key_remove_algorithm_hash_for_testing(key: *mut c_void) {// SAFETY: LibWeb supplies a live CryptoKey cell.let key = unsafe { cell_from_abi::<CryptoKey>(key) };- let algorithm = key.algorithm.borrow();- // SAFETY: LibWeb reads the fields before mutating the key or letting it die.- unsafe { fields.cast::<RawAlgorithm<'_>>().write(algorithm.fields()) };-}--/// # Safety-///-/// The key must be a live CryptoKey cell and fields must own their strings.-#[unsafe(no_mangle)]-unsafe extern "C" fn web_rust_crypto_key_set_algorithm(key: *mut c_void, fields: &RawAlgorithm<'_>) {- // SAFETY: LibWeb supplies a live CryptoKey cell and transfers the strings.- let (key, name, hash, named_curve) = unsafe {- (- cell_from_abi::<CryptoKey>(key),- Utf16String::from_raw_owned(fields.name),- Utf16String::from_raw_owned(fields.hash),- Utf16String::from_raw_owned(fields.named_curve),- )+ let algorithm = match &*key.internal_algorithm() {+ InternalAlgorithmData::RsaHashed {+ name,+ modulus_length,+ public_exponent,+ ..+ } => InternalAlgorithmData::Rsa {+ name: name.clone(),+ modulus_length: *modulus_length,+ public_exponent: public_exponent.clone(),+ },+ _ => panic!("the key has an RsaHashedKeyAlgorithm"),};- key.set_algorithm(InternalAlgorithmData::from_fields(- fields,- name,- hash,- named_curve,- fields.public_exponent.as_slice().to_vec(),- ));-}--/// # Safety-///-/// The key must be a live CryptoKey cell. The caller takes ownership of the returned string.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_rust_crypto_key_algorithm_name(key: *mut c_void) -> usize {- // SAFETY: LibWeb supplies a live CryptoKey cell.- unsafe { cell_from_abi::<CryptoKey>(key) }.algorithm_name().into_raw()-}--/// # Safety-///-/// The key must be a live CryptoKey cell and steps must not mutate it.-#[unsafe(no_mangle)]-unsafe extern "C" fn web_rust_crypto_key_with_usages(- key: *mut c_void,- state: *mut c_void,- steps: unsafe extern "C" fn(*mut c_void, Span<'_, KeyUsage>),-) {- // SAFETY: LibWeb supplies a live CryptoKey cell and the usages are lent for the callback.- let key = unsafe { cell_from_abi::<CryptoKey>(key) };- unsafe { steps(state, Span::new(&key.key_usages.borrow())) };-}--/// # Safety-///-/// The key must be a live CryptoKey cell and usages must contain valid KeyUsage values.-#[unsafe(no_mangle)]-unsafe extern "C" fn web_rust_crypto_key_set_usages(key: *mut c_void, usages: Span<'_, KeyUsage>) {- // SAFETY: LibWeb supplies a live CryptoKey cell and lends the usages for the call.- unsafe { cell_from_abi::<CryptoKey>(key) }.set_usages(usages.as_slice().to_vec());+ key.set_algorithm(algorithm);}diff --git a/Libraries/LibWeb/Crypto/CryptoKey.cpp b/Libraries/LibWeb/Crypto/CryptoKey.cppindex a22af17b34b..9dcdd140811 100644--- a/Libraries/LibWeb/Crypto/CryptoKey.cpp+++ b/Libraries/LibWeb/Crypto/CryptoKey.cpp@@ -5,171 +5,15 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <LibJS/HostClassBuilder.h>-#include <LibJS/Runtime/HostObject.h>-#include <LibJS/Runtime/Realm.h>-#include <LibJS/Runtime/VM.h>-#include <LibWeb/Bindings/RustSerialization.h>-#include <LibWeb/Bindings/WrapperWorld.h>#include <LibWeb/Crypto/CryptoKey.h>-#include <LibWeb/WebIDL/AbstractOperations.h>namespace Web::Crypto {-enum class SerializedKeyAlgorithmType : u8 {- Key,- Rsa,- RsaHashed,- Ec,- Aes,- Hmac,- Kmac,-};+extern "C" void web_rust_crypto_key_remove_algorithm_hash_for_testing(CryptoKey*);-struct CryptoKeyAlgorithmFields {- u8 type { 0 };- FlatPtr name { 0 };- u32 modulus_length { 0 };- ReadonlyBytes public_exponent;- FlatPtr hash { 0 };- FlatPtr named_curve { 0 };- u32 length { 0 };-};--extern "C" {--bool web_rust_crypto_key_extractable(CryptoKey const*);-u8 web_rust_crypto_key_type(CryptoKey const*);-void web_rust_crypto_key_set_extractable(CryptoKey*, bool);-void web_rust_crypto_key_set_type(CryptoKey*, u8);-void web_rust_crypto_key_get_algorithm(CryptoKey const*, CryptoKeyAlgorithmFields*);-void web_rust_crypto_key_set_algorithm(CryptoKey*, CryptoKeyAlgorithmFields const*);-FlatPtr web_rust_crypto_key_algorithm_name(CryptoKey const*);-void web_rust_crypto_key_with_usages(CryptoKey const*, void*, void (*)(void*, ReadonlySpan<KeyUsage>));-void web_rust_crypto_key_set_usages(CryptoKey*, ReadonlySpan<KeyUsage>);-}--void CryptoKey::set_algorithm(InternalAlgorithmData algorithm)-{- CryptoKeyAlgorithmFields fields;- Utf16String hash;- Utf16String named_curve;- auto name = algorithm.visit([](auto const& data) { return data.name(); });- algorithm.visit(- [&](KeyAlgorithmData const&) {- fields.type = to_underlying(SerializedKeyAlgorithmType::Key);- },- [&](RsaKeyAlgorithmData const& data) {- fields.type = to_underlying(SerializedKeyAlgorithmType::Rsa);- fields.modulus_length = data.modulus_length();- fields.public_exponent = data.public_exponent().bytes();- },- [&](RsaHashedKeyAlgorithmData const& data) {- fields.type = to_underlying(SerializedKeyAlgorithmType::RsaHashed);- fields.modulus_length = data.modulus_length();- fields.public_exponent = data.public_exponent().bytes();- hash = data.hash();- },- [&](EcKeyAlgorithmData const& data) {- fields.type = to_underlying(SerializedKeyAlgorithmType::Ec);- named_curve = data.named_curve();- },- [&](AesKeyAlgorithmData const& data) {- fields.type = to_underlying(SerializedKeyAlgorithmType::Aes);- fields.length = data.length();- },- [&](HmacKeyAlgorithmData const& data) {- fields.type = to_underlying(SerializedKeyAlgorithmType::Hmac);- hash = data.hash();- fields.length = data.length();- },- [&](KmacKeyAlgorithmData const& data) {- fields.type = to_underlying(SerializedKeyAlgorithmType::Kmac);- fields.length = data.length();- });- fields.name = move(name).into_raw();- fields.hash = move(hash).into_raw();- fields.named_curve = move(named_curve).into_raw();- web_rust_crypto_key_set_algorithm(this, &fields);-}--void CryptoKey::set_usages(Vector<KeyUsage> usages)-{- web_rust_crypto_key_set_usages(this, usages.span());-}--bool CryptoKey::extractable() const-{- return web_rust_crypto_key_extractable(this);-}--KeyType CryptoKey::type() const-{- return static_cast<KeyType>(web_rust_crypto_key_type(this));-}--void CryptoKey::set_extractable(bool extractable)-{- web_rust_crypto_key_set_extractable(this, extractable);-}--void CryptoKey::set_type(KeyType type)-{- web_rust_crypto_key_set_type(this, to_underlying(type));-}--CryptoKey::EcKeyAlgorithmData CryptoKey::ec_algorithm() const-{- CryptoKeyAlgorithmFields fields;- web_rust_crypto_key_get_algorithm(this, &fields);- VERIFY(fields.type == to_underlying(SerializedKeyAlgorithmType::Ec));- return { Utf16String::from_raw(fields.name), Utf16String::from_raw(fields.named_curve) };-}--Vector<KeyUsage> CryptoKey::internal_usages() const-{- Vector<KeyUsage> usages;- web_rust_crypto_key_with_usages(this, &usages, [](void* state, ReadonlySpan<KeyUsage> values) {- static_cast<Vector<KeyUsage>*>(state)->append(values.data(), values.size());- });- return usages;-}--Utf16String CryptoKey::algorithm_name() const-{- return Utf16String::adopt_raw(web_rust_crypto_key_algorithm_name(this));-}--static_assert(to_underlying(KeyType::Public) == 0);-static_assert(to_underlying(KeyType::Private) == 1);-static_assert(to_underlying(KeyType::Secret) == 2);-static_assert(sizeof(KeyUsage) == 1);-static_assert(to_underlying(Bindings::KeyUsage::Encrypt) == 0);-static_assert(to_underlying(Bindings::KeyUsage::Decrypt) == 1);-static_assert(to_underlying(Bindings::KeyUsage::Sign) == 2);-static_assert(to_underlying(Bindings::KeyUsage::Verify) == 3);-static_assert(to_underlying(Bindings::KeyUsage::DeriveKey) == 4);-static_assert(to_underlying(Bindings::KeyUsage::DeriveBits) == 5);-static_assert(to_underlying(Bindings::KeyUsage::WrapKey) == 6);-static_assert(to_underlying(Bindings::KeyUsage::UnwrapKey) == 7);-static_assert(to_underlying(Bindings::KeyUsage::EncapsulateKey) == 8);-static_assert(to_underlying(Bindings::KeyUsage::EncapsulateBits) == 9);-static_assert(to_underlying(Bindings::KeyUsage::DecapsulateKey) == 10);-static_assert(to_underlying(Bindings::KeyUsage::DecapsulateBits) == 11);-static_assert(to_underlying(SerializedKeyAlgorithmType::Key) == 0);-static_assert(to_underlying(SerializedKeyAlgorithmType::Kmac) == 6);--}--namespace Web::Bindings {--extern "C" JSObject* web_rust_crypto_key_wrap(JSVM*, void*, Crypto::CryptoKey*);--GC::Ref<JS::HostObject> wrap(WrapperWorld& wrapper_world, JS::Realm& preferred_realm, GC::Ref<Crypto::CryptoKey> key)+void CryptoKey::remove_algorithm_hash_for_testing(){- VERIFY(&wrapper_world == &host_defined_wrapper_world(preferred_realm));- auto* wrapper = web_rust_crypto_key_wrap(JS::HostABI::vm_to_abi(preferred_realm.vm()), &preferred_realm, key.ptr());- return as<JS::HostObject>(*JS::HostABI::object_from_abi(wrapper));+ web_rust_crypto_key_remove_algorithm_hash_for_testing(this);}}diff --git a/Libraries/LibWeb/Crypto/CryptoKey.h b/Libraries/LibWeb/Crypto/CryptoKey.hindex 6ba78824cba..a21a3b88df2 100644--- a/Libraries/LibWeb/Crypto/CryptoKey.h+++ b/Libraries/LibWeb/Crypto/CryptoKey.h@@ -6,114 +6,16 @@#pragma once-#include <LibGC/Ptr.h>-#include <LibJS/Forward.h>#include <LibWeb/Bindings/CryptoKey.h>#include <LibWeb/Bindings/RustInterface.h>-#include <LibWebCommon/WebIDL/Types.h>--namespace Web::Bindings {--enum class KeyType : u8;--}namespace Web::Crypto {-using KeyUsage = Bindings::KeyUsage;--enum class KeyType {- Public,- Private,- Secret,-};-class WEB_API CryptoKey final : public Bindings::RustInterfaceCell {WEB_RUST_INTERFACE(CryptoKey, crypto_key)public:- struct KeyAlgorithmData {- Utf16String const& name() const { return m_name; }-- Utf16String m_name;- };-- struct RsaKeyAlgorithmData {- Utf16String const& name() const { return m_name; }- u32 modulus_length() const { return m_modulus_length; }- ByteBuffer const& public_exponent() const { return m_public_exponent; }-- Utf16String m_name;- u32 m_modulus_length { 0 };- ByteBuffer m_public_exponent;- };-- struct RsaHashedKeyAlgorithmData {- Utf16String const& name() const { return m_name; }- u32 modulus_length() const { return m_modulus_length; }- ByteBuffer const& public_exponent() const { return m_public_exponent; }- Utf16String const& hash() const { return m_hash; }-- Utf16String m_name;- u32 m_modulus_length { 0 };- ByteBuffer m_public_exponent;- Utf16String m_hash;- };-- struct EcKeyAlgorithmData {- Utf16String const& name() const { return m_name; }- Utf16String const& named_curve() const { return m_named_curve; }-- Utf16String m_name;- Utf16String m_named_curve;- };-- struct AesKeyAlgorithmData {- Utf16String const& name() const { return m_name; }- u16 length() const { return m_length; }-- Utf16String m_name;- u16 m_length { 0 };- };-- struct HmacKeyAlgorithmData {- Utf16String const& name() const { return m_name; }- Utf16String const& hash() const { return m_hash; }- WebIDL::UnsignedLong length() const { return m_length; }-- Utf16String m_name;- Utf16String m_hash;- WebIDL::UnsignedLong m_length { 0 };- };-- struct KmacKeyAlgorithmData {- Utf16String const& name() const { return m_name; }- WebIDL::UnsignedLong length() const { return m_length; }-- Utf16String m_name;- WebIDL::UnsignedLong m_length { 0 };- };-- using InternalAlgorithmData = Variant<KeyAlgorithmData, RsaKeyAlgorithmData, RsaHashedKeyAlgorithmData, EcKeyAlgorithmData, AesKeyAlgorithmData, HmacKeyAlgorithmData, KmacKeyAlgorithmData>;-- bool extractable() const;- KeyType type() const;-- Vector<KeyUsage> internal_usages() const;- EcKeyAlgorithmData ec_algorithm() const;-- void set_extractable(bool);- void set_type(KeyType);- void set_algorithm(InternalAlgorithmData);- void set_usages(Vector<KeyUsage>);-- Utf16String algorithm_name() const;+ void remove_algorithm_hash_for_testing();};}--namespace Web::Bindings {--WEB_API GC::Ref<JS::HostObject> wrap(WrapperWorld&, JS::Realm&, GC::Ref<Crypto::CryptoKey>);--}diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.pyindex 1165cf028b6..e8508ef68f3 100755--- a/Meta/Linters/check_libweb_realm_mentions.py+++ b/Meta/Linters/check_libweb_realm_mentions.py@@ -49,7 +49,6 @@ ALLOWED_REALM_MENTIONS = {"Clipboard": (4, 8, "the C++ facades create the Rust objects and wrap DataTransfers in a given realm"),"ContentSecurityPolicy": (4, 10, "CSP violation/report objects and callbacks still materialize JS-facing values"),"CookieStore": (2, 3, "the C++ facade creates the Rust CookieStore in a given realm"),- "Crypto": (2, 3, "CryptoKey wrappers use the caller's realm and wrapper world"),"DOM": (9,30,diff --git a/Tests/LibWeb/StructuredSerializeTestHelpers.h b/Tests/LibWeb/StructuredSerializeTestHelpers.hindex a1b82a3e13a..86ef1cbe6e9 100644--- a/Tests/LibWeb/StructuredSerializeTestHelpers.h+++ b/Tests/LibWeb/StructuredSerializeTestHelpers.h@@ -236,6 +236,11 @@ inline double number_property(JS::Value object, StringView name)return property_of(object, name).as_double();}+inline Utf16String string_property(JS::Value object, StringView name)+{+ return property_of(object, name).as_string().utf16_string();+}+inline Web::WebIDL::ExceptionOr<JS::Value> storage_deserialize(Web::HTML::StorageSerializationRecord const& record){auto& realm = test_realm();@@ -780,22 +785,18 @@ inline Vector<CryptoHandleGolden> crypto_handle_goldens()}// Deserialize first so encode goldens use slot-consistent CryptoKeys.-inline GC::Ref<Web::Crypto::CryptoKey> build_live_crypto_key(StringView type, StringView usage, Vector<u8> const& algorithm, Vector<u8> const& handle)+inline GC::Ref<JS::Object> build_live_crypto_key(StringView type, StringView usage, Vector<u8> const& algorithm, Vector<u8> const& handle){auto value = MUST(principal_storage_deserialize(crypto_key_full_record(type, usage, algorithm, handle)));- return unwrap_wrappable<Web::Crypto::CryptoKey>(value);+ return value.as_object();}// Fabricate RsaKeyAlgorithm directly to pin the encode-only tag-1 branch.-inline GC::Ref<Web::Crypto::CryptoKey> build_live_crypto_key_with_rsa_key_algorithm(StringView type, StringView usage, StringView name, u32 modulus_length, ReadonlyBytes public_exponent, ReadonlyBytes modulus)+inline GC::Ref<JS::Object> build_live_crypto_key_with_rsa_key_algorithm(StringView type, StringView usage, StringView name, u32 modulus_length, ReadonlyBytes public_exponent, ReadonlyBytes modulus){// NB: A stored record cannot hold an RsaKeyAlgorithm, so the key is decoded with an RsaHashedKeyAlgorithm and given// the RsaKeyAlgorithm afterwards.auto key = build_live_crypto_key(type, usage, alg_rsa_hashed_key_algorithm(name, modulus_length, public_exponent, "SHA-256"sv), handle_rsa_public_key(modulus, public_exponent));- key->set_algorithm(Web::Crypto::CryptoKey::RsaKeyAlgorithmData {- Utf16String::from_utf8(name),- modulus_length,- MUST(ByteBuffer::copy(public_exponent)),- });+ unwrap_wrappable<Web::Crypto::CryptoKey>(key).remove_algorithm_hash_for_testing();return key;}diff --git a/Tests/LibWeb/TestStructuredSerializeDurability.cpp b/Tests/LibWeb/TestStructuredSerializeDurability.cppindex 08ef28bb2e8..e28cd408c5a 100644--- a/Tests/LibWeb/TestStructuredSerializeDurability.cpp+++ b/Tests/LibWeb/TestStructuredSerializeDurability.cpp@@ -87,12 +87,12 @@ TEST_CASE(crypto_key_storage_record_round_trips)ReadonlyBytes handle { handle_bytes.data(), handle_bytes.size() };auto value = MUST(principal_storage_deserialize(crypto_key_secret_record(handle, handle.size())));- auto& key = unwrap_wrappable<Web::Crypto::CryptoKey>(value);- EXPECT_EQ(key.type(), Web::Crypto::KeyType::Secret);- EXPECT_EQ(key.extractable(), false);- EXPECT_EQ(key.algorithm_name(), "HKDF"_string);- EXPECT_EQ(key.internal_usages().size(), 1u);- EXPECT_EQ(key.internal_usages()[0], Web::Bindings::KeyUsage::DeriveKey);+ expect_utf16_equals(string_property(value, "type"sv), "secret"_utf16);+ EXPECT_EQ(property_of(value, "extractable"sv).as_bool(), false);+ expect_utf16_equals(string_property(property_of(value, "algorithm"sv), "name"sv), "HKDF"_utf16);+ auto usages = property_of(value, "usages"sv);+ EXPECT_EQ(number_property(usages, "length"sv), 1.0);+ expect_utf16_equals(MUST(usages.as_object().get(0u)).as_string().utf16_string(), "deriveKey"_utf16);EXPECT_EQ(storage_serialize(GC::Ref { value.as_object() }).data.span(), crypto_key_secret_record(handle, handle.size()).data.span());}@@ -590,12 +590,10 @@ TEST_CASE(crypto_key_algorithm_variants_decode_from_frozen_bytes)for (auto const& golden : crypto_algorithm_goldens()) {auto record = crypto_key_full_record(golden.type, golden.usage, golden.algorithm, golden.handle);auto value = MUST(principal_storage_deserialize(record));- auto& key = unwrap_wrappable<Web::Crypto::CryptoKey>(value);-if (golden.tag == KeyAlgorithmTag::EcKeyAlgorithm) {- auto const& algorithm = key.ec_algorithm();- expect_utf16_equals(algorithm.name(), "ECDSA"_utf16);- expect_utf16_equals(algorithm.named_curve(), "P-256"_utf16);+ auto algorithm = property_of(value, "algorithm"sv);+ expect_utf16_equals(string_property(algorithm, "name"sv), "ECDSA"_utf16);+ expect_utf16_equals(string_property(algorithm, "namedCurve"sv), "P-256"_utf16);}}}