mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-10 19:46:03 +00:00
LibWeb: Add {,de}serialization steps for CryptoKey
This commit is contained in:
parent
ca54691ecf
commit
52f056503d
Notes:
sideshowbarker
2024-07-17 01:28:15 +09:00
Author: https://github.com/kennethmyhra
Commit: 52f056503d
Pull-request: https://github.com/SerenityOS/serenity/pull/23636
5 changed files with 97 additions and 1 deletions
|
@ -20,3 +20,8 @@ instanceOf DOMRectReadOnly: true
|
|||
DOMRectReadOnly: {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10}
|
||||
instanceOf DOMRect: true
|
||||
DOMRect: {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10}
|
||||
instanceOf CryptoKey: true
|
||||
CryptoKey.type: "secret"
|
||||
CryptoKey.extractable: false
|
||||
CryptoKey.algorithm: {"name":"PBKDF2"}
|
||||
CryptoKey.usages: ["deriveKey","deriveBits"]
|
||||
|
|
|
@ -43,6 +43,20 @@
|
|||
println(`instanceOf DOMRect: ${domRect instanceof DOMRect}`);
|
||||
println(`DOMRect: ${JSON.stringify(domRect)}`);
|
||||
|
||||
let cryptoKey = await window.crypto.subtle.importKey(
|
||||
"raw",
|
||||
new TextEncoder().encode("password"),
|
||||
{ name: "PBKDF2" },
|
||||
false,
|
||||
["deriveBits", "deriveKey"]
|
||||
);
|
||||
let clonedCryptoKey = structuredClone(cryptoKey);
|
||||
println(`instanceOf CryptoKey: ${clonedCryptoKey instanceof CryptoKey}`);
|
||||
println(`CryptoKey.type: ${JSON.stringify(clonedCryptoKey.type)}`);
|
||||
println(`CryptoKey.extractable: ${JSON.stringify(clonedCryptoKey.extractable)}`);
|
||||
println(`CryptoKey.algorithm: ${JSON.stringify(clonedCryptoKey.algorithm)}`);
|
||||
println(`CryptoKey.usages: ${JSON.stringify(clonedCryptoKey.usages)}`);
|
||||
|
||||
done();
|
||||
});
|
||||
</script>
|
||||
|
|
|
@ -20,6 +20,11 @@ JS::NonnullGCPtr<CryptoKey> CryptoKey::create(JS::Realm& realm, InternalKeyData
|
|||
return realm.heap().allocate<CryptoKey>(realm, realm, move(key_data));
|
||||
}
|
||||
|
||||
JS::NonnullGCPtr<CryptoKey> CryptoKey::create(JS::Realm& realm)
|
||||
{
|
||||
return realm.heap().allocate<CryptoKey>(realm, realm);
|
||||
}
|
||||
|
||||
CryptoKey::CryptoKey(JS::Realm& realm, InternalKeyData key_data)
|
||||
: PlatformObject(realm)
|
||||
, m_algorithm(Object::create(realm, nullptr))
|
||||
|
@ -28,6 +33,14 @@ CryptoKey::CryptoKey(JS::Realm& realm, InternalKeyData key_data)
|
|||
{
|
||||
}
|
||||
|
||||
CryptoKey::CryptoKey(JS::Realm& realm)
|
||||
: PlatformObject(realm)
|
||||
, m_algorithm(Object::create(realm, nullptr))
|
||||
, m_usages(Object::create(realm, nullptr))
|
||||
, m_key_data(MUST(ByteBuffer::create_uninitialized(0)))
|
||||
{
|
||||
}
|
||||
|
||||
CryptoKey::~CryptoKey()
|
||||
{
|
||||
m_key_data.visit(
|
||||
|
@ -110,4 +123,55 @@ JS_DEFINE_NATIVE_FUNCTION(CryptoKeyPair::private_key_getter)
|
|||
return TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->private_key(); }));
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> CryptoKey::serialization_steps(HTML::SerializationRecord& serialized, bool for_storage, HTML::SerializationMemory& memory)
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
|
||||
// 1. Set serialized.[[Type]] to the [[type]] internal slot of value.
|
||||
HTML::serialize_primitive_type(serialized, static_cast<u32>(m_type));
|
||||
|
||||
// 2. Set serialized.[[Extractable]] to the [[extractable]] internal slot of value.
|
||||
HTML::serialize_primitive_type(serialized, m_extractable);
|
||||
|
||||
// 3. Set serialized.[[Algorithm]] to the sub-serialization of the [[algorithm]] internal slot of value.
|
||||
auto serialized_algorithm = TRY(HTML::structured_serialize_internal(vm, m_algorithm, for_storage, memory));
|
||||
serialized.extend(move(serialized_algorithm));
|
||||
|
||||
// 4. Set serialized.[[Usages]] to the sub-serialization of the [[usages]] internal slot of value.
|
||||
auto serialized_usages = TRY(HTML::structured_serialize_internal(vm, m_usages, for_storage, memory));
|
||||
serialized.extend(move(serialized_usages));
|
||||
|
||||
// FIXME: 5. Set serialized.[[Handle]] to the [[handle]] internal slot of value.
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> CryptoKey::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory& memory)
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
auto& realm = this->realm();
|
||||
|
||||
// 1. Initialize the [[type]] internal slot of value to serialized.[[Type]].
|
||||
m_type = static_cast<Bindings::KeyType>(HTML::deserialize_primitive_type<u32>(serialized, position));
|
||||
|
||||
// 2. Initialize the [[extractable]] internal slot of value to serialized.[[Extractable]].
|
||||
m_extractable = HTML::deserialize_primitive_type<bool>(serialized, position);
|
||||
|
||||
// 3. Initialize the [[algorithm]] internal slot of value to the sub-deserialization of serialized.[[Algorithm]].
|
||||
auto deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position));
|
||||
if (deserialized_record.value.has_value())
|
||||
m_algorithm = deserialized_record.value.release_value().as_object();
|
||||
position = deserialized_record.position;
|
||||
|
||||
// 4. Initialize the [[usages]] internal slot of value to the sub-deserialization of serialized.[[Usages]].
|
||||
deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position));
|
||||
if (deserialized_record.value.has_value())
|
||||
m_usages = deserialized_record.value.release_value().as_object();
|
||||
position = deserialized_record.position;
|
||||
|
||||
// FIXME: 5. Initialize the [[handle]] internal slot of value to serialized.[[Handle]].
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,11 +12,14 @@
|
|||
#include <LibWeb/Bindings/CryptoKeyPrototype.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/Bindings/Serializable.h>
|
||||
#include <LibWeb/Crypto/CryptoBindings.h>
|
||||
|
||||
namespace Web::Crypto {
|
||||
|
||||
class CryptoKey final : public Bindings::PlatformObject {
|
||||
class CryptoKey final
|
||||
: public Bindings::PlatformObject
|
||||
, public Bindings::Serializable {
|
||||
WEB_PLATFORM_OBJECT(CryptoKey, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(CryptoKey);
|
||||
|
||||
|
@ -24,6 +27,7 @@ public:
|
|||
using InternalKeyData = Variant<ByteBuffer, Bindings::JsonWebKey, ::Crypto::PK::RSAPublicKey<>, ::Crypto::PK::RSAPrivateKey<>>;
|
||||
|
||||
[[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&, InternalKeyData);
|
||||
[[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&);
|
||||
|
||||
virtual ~CryptoKey() override;
|
||||
|
||||
|
@ -41,8 +45,14 @@ public:
|
|||
|
||||
InternalKeyData const& handle() const { return m_key_data; }
|
||||
|
||||
virtual StringView interface_name() const override { return "CryptoKey"sv; }
|
||||
virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override;
|
||||
virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&) override;
|
||||
|
||||
private:
|
||||
CryptoKey(JS::Realm&, InternalKeyData);
|
||||
explicit CryptoKey(JS::Realm&);
|
||||
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/Serializable.h>
|
||||
#include <LibWeb/Bindings/Transferable.h>
|
||||
#include <LibWeb/Crypto/CryptoKey.h>
|
||||
#include <LibWeb/FileAPI/Blob.h>
|
||||
#include <LibWeb/FileAPI/File.h>
|
||||
#include <LibWeb/Geometry/DOMMatrix.h>
|
||||
|
@ -984,6 +985,8 @@ private:
|
|||
return Geometry::DOMRectReadOnly::create(realm);
|
||||
if (interface_name == "DOMRect"sv)
|
||||
return Geometry::DOMRect::create(realm);
|
||||
if (interface_name == "CryptoKey"sv)
|
||||
return Crypto::CryptoKey::create(realm);
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue