mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-23 09:22:30 +00:00
Our structured serialization implementation had its own bespoke encoder and decoder to serialize JS values. It also used a u32 buffer under the hood, which made using its structures a bit awkward. We had previously worked around its data structures in transferable streams, which nested transfers of MessagePort instances. We basically had to add hooks into the MessagePort to route to the correct transfer receiving steps, and we could not invoke the correct AOs directly as the spec dictates. We now use IPC mechanics to encode and decode data. This works because, although we are encoding JS values, we are only ultimately encoding primitive and basic AK types. The resulting data structures actually enforce that we implement transferable streams exactly as the spec is worded (I had planned to do that in a separate commit, but the fallout of this patch actually required that change).
95 lines
3.4 KiB
C++
95 lines
3.4 KiB
C++
/*
|
|
* Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibCrypto/PK/EC.h>
|
|
#include <LibCrypto/PK/RSA.h>
|
|
#include <LibGC/Ptr.h>
|
|
#include <LibJS/Forward.h>
|
|
#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
|
|
, public Bindings::Serializable {
|
|
WEB_PLATFORM_OBJECT(CryptoKey, Bindings::PlatformObject);
|
|
GC_DECLARE_ALLOCATOR(CryptoKey);
|
|
|
|
public:
|
|
using InternalKeyData = Variant<ByteBuffer, Bindings::JsonWebKey, ::Crypto::PK::RSAPublicKey, ::Crypto::PK::RSAPrivateKey, ::Crypto::PK::ECPublicKey, ::Crypto::PK::ECPrivateKey>;
|
|
|
|
[[nodiscard]] static GC::Ref<CryptoKey> create(JS::Realm&, InternalKeyData);
|
|
[[nodiscard]] static GC::Ref<CryptoKey> create(JS::Realm&);
|
|
|
|
virtual ~CryptoKey() override;
|
|
|
|
bool extractable() const { return m_extractable; }
|
|
Bindings::KeyType type() const { return m_type; }
|
|
JS::Object const* algorithm() const { return m_algorithm; }
|
|
JS::Object const* usages() const { return m_usages; }
|
|
|
|
Vector<Bindings::KeyUsage> internal_usages() const { return m_key_usages; }
|
|
|
|
void set_extractable(bool extractable) { m_extractable = extractable; }
|
|
void set_type(Bindings::KeyType type) { m_type = type; }
|
|
void set_algorithm(GC::Ref<Object> algorithm) { m_algorithm = move(algorithm); }
|
|
void set_usages(Vector<Bindings::KeyUsage>);
|
|
|
|
InternalKeyData const& handle() const { return m_key_data; }
|
|
String algorithm_name() const;
|
|
|
|
virtual HTML::SerializeType serialize_type() const override { return HTML::SerializeType::CryptoKey; }
|
|
virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::TransferDataEncoder&, bool for_storage, HTML::SerializationMemory&) override;
|
|
virtual WebIDL::ExceptionOr<void> deserialization_steps(HTML::TransferDataDecoder&, HTML::DeserializationMemory&) override;
|
|
|
|
private:
|
|
CryptoKey(JS::Realm&, InternalKeyData);
|
|
explicit CryptoKey(JS::Realm&);
|
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
Bindings::KeyType m_type;
|
|
bool m_extractable { false };
|
|
GC::Ref<Object> m_algorithm;
|
|
GC::Ref<Object> m_usages;
|
|
|
|
Vector<Bindings::KeyUsage> m_key_usages;
|
|
InternalKeyData m_key_data; // [[handle]]
|
|
mutable String m_algorithm_name;
|
|
};
|
|
|
|
// https://w3c.github.io/webcrypto/#ref-for-dfn-CryptoKeyPair-2
|
|
class CryptoKeyPair : public JS::Object {
|
|
JS_OBJECT(CryptoKeyPair, JS::Object);
|
|
GC_DECLARE_ALLOCATOR(CryptoKeyPair);
|
|
|
|
public:
|
|
static GC::Ref<CryptoKeyPair> create(JS::Realm&, GC::Ref<CryptoKey> public_key, GC::Ref<CryptoKey> private_key);
|
|
virtual ~CryptoKeyPair() override = default;
|
|
|
|
GC::Ref<CryptoKey> public_key() const { return m_public_key; }
|
|
GC::Ref<CryptoKey> private_key() const { return m_private_key; }
|
|
|
|
private:
|
|
CryptoKeyPair(JS::Realm&, GC::Ref<CryptoKey> public_key, GC::Ref<CryptoKey> private_key);
|
|
virtual void initialize(JS::Realm&) override;
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(public_key_getter);
|
|
JS_DECLARE_NATIVE_FUNCTION(private_key_getter);
|
|
|
|
GC::Ref<CryptoKey> m_public_key;
|
|
GC::Ref<CryptoKey> m_private_key;
|
|
};
|
|
|
|
}
|