/* * Copyright (c) 2023, stelar7 * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include 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, ::Crypto::PK::RSAPrivateKey<>, ::Crypto::PK::ECPublicKey<>, ::Crypto::PK::ECPrivateKey<>>; [[nodiscard]] static GC::Ref create(JS::Realm&, InternalKeyData); [[nodiscard]] static GC::Ref 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 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 algorithm) { m_algorithm = move(algorithm); } void set_usages(Vector); InternalKeyData const& handle() const { return m_key_data; } String algorithm_name() const; virtual StringView interface_name() const override { return "CryptoKey"sv; } virtual WebIDL::ExceptionOr serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override; virtual WebIDL::ExceptionOr deserialization_steps(ReadonlySpan 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; Bindings::KeyType m_type; bool m_extractable { false }; GC::Ref m_algorithm; GC::Ref m_usages; Vector 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 create(JS::Realm&, GC::Ref public_key, GC::Ref private_key); virtual ~CryptoKeyPair() override = default; GC::Ref public_key() const { return m_public_key; } GC::Ref private_key() const { return m_private_key; } private: CryptoKeyPair(JS::Realm&, GC::Ref public_key, GC::Ref 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 m_public_key; GC::Ref m_private_key; }; }