/* * Copyright (c) 2023, stelar7 * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace Web::Crypto { class CryptoKey final : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(CryptoKey, Bindings::PlatformObject); JS_DECLARE_ALLOCATOR(CryptoKey); public: using InternalKeyData = Variant, ::Crypto::PK::RSAPrivateKey<>>; [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&, InternalKeyData); 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(JS::NonnullGCPtr algorithm) { m_algorithm = move(algorithm); } void set_usages(Vector); InternalKeyData const& handle() const { return m_key_data; } private: CryptoKey(JS::Realm&, InternalKeyData); virtual void initialize(JS::Realm&) override; virtual void visit_edges(Visitor&) override; Bindings::KeyType m_type; bool m_extractable { false }; JS::NonnullGCPtr m_algorithm; JS::NonnullGCPtr m_usages; Vector m_key_usages; InternalKeyData m_key_data; // [[handle]] }; // https://w3c.github.io/webcrypto/#ref-for-dfn-CryptoKeyPair-2 class CryptoKeyPair : public JS::Object { JS_OBJECT(CryptoKeyPair, Object); JS_DECLARE_ALLOCATOR(CryptoKeyPair); public: static JS::NonnullGCPtr create(JS::Realm&, JS::NonnullGCPtr public_key, JS::NonnullGCPtr private_key); virtual ~CryptoKeyPair() override = default; JS::NonnullGCPtr public_key() const { return m_public_key; } JS::NonnullGCPtr private_key() const { return m_private_key; } private: CryptoKeyPair(JS::Realm&, JS::NonnullGCPtr public_key, JS::NonnullGCPtr 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); JS::NonnullGCPtr m_public_key; JS::NonnullGCPtr m_private_key; }; }