From a8ddf6c2a4a95093fe4c4a5ba9687768e52b4ef8 Mon Sep 17 00:00:00 2001 From: stelar7 Date: Fri, 15 Dec 2023 20:25:35 +0100 Subject: [PATCH] LibWeb: Add the CryptoKey interface --- .../Userland/Libraries/LibWeb/Crypto/BUILD.gn | 2 + .../Userland/Libraries/LibWeb/idl_files.gni | 1 + Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../Libraries/LibWeb/Crypto/CryptoKey.cpp | 40 ++++++++++++++++ Userland/Libraries/LibWeb/Crypto/CryptoKey.h | 47 +++++++++++++++++++ .../Libraries/LibWeb/Crypto/CryptoKey.idl | 12 +++++ Userland/Libraries/LibWeb/idl_files.cmake | 1 + 7 files changed, 104 insertions(+) create mode 100644 Userland/Libraries/LibWeb/Crypto/CryptoKey.cpp create mode 100644 Userland/Libraries/LibWeb/Crypto/CryptoKey.h create mode 100644 Userland/Libraries/LibWeb/Crypto/CryptoKey.idl diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/Crypto/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/Crypto/BUILD.gn index 318af7770c..928da40f06 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/Crypto/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/Crypto/BUILD.gn @@ -4,6 +4,8 @@ source_set("Crypto") { sources = [ "Crypto.cpp", "Crypto.h", + "CryptoKey.cpp", + "CryptoKey.h", "SubtleCrypto.cpp", "SubtleCrypto.h", ] diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni b/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni index 44930902e7..56f3327270 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni @@ -26,6 +26,7 @@ standard_idl_files = [ "//Userland/Libraries/LibWeb/Animations/DocumentTimeline.idl", "//Userland/Libraries/LibWeb/Clipboard/Clipboard.idl", "//Userland/Libraries/LibWeb/Crypto/Crypto.idl", + "//Userland/Libraries/LibWeb/Crypto/CryptoKey.idl", "//Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl", "//Userland/Libraries/LibWeb/CSS/CSSConditionRule.idl", "//Userland/Libraries/LibWeb/CSS/CSSFontFaceRule.idl", diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 5e27306da1..2b158d51f1 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -24,6 +24,7 @@ set(SOURCES Bindings/PlatformObject.cpp Clipboard/Clipboard.cpp Crypto/Crypto.cpp + Crypto/CryptoKey.cpp Crypto/SubtleCrypto.cpp CSS/Angle.cpp CSS/CalculatedOr.cpp diff --git a/Userland/Libraries/LibWeb/Crypto/CryptoKey.cpp b/Userland/Libraries/LibWeb/Crypto/CryptoKey.cpp new file mode 100644 index 0000000000..516346ec86 --- /dev/null +++ b/Userland/Libraries/LibWeb/Crypto/CryptoKey.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Web::Crypto { + +JS_DEFINE_ALLOCATOR(CryptoKey); + +JS::NonnullGCPtr CryptoKey::create(JS::Realm& realm) +{ + return realm.heap().allocate(realm, realm); +} + +CryptoKey::CryptoKey(JS::Realm& realm) + : PlatformObject(realm) + , m_algorithm(Object::create(realm, nullptr)) + , m_usages(Object::create(realm, nullptr)) +{ +} + +CryptoKey::~CryptoKey() = default; + +void CryptoKey::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + set_prototype(&Bindings::ensure_web_prototype(realm, "CryptoKey"_fly_string)); +} + +void CryptoKey::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_algorithm); + visitor.visit(m_usages); +} + +} diff --git a/Userland/Libraries/LibWeb/Crypto/CryptoKey.h b/Userland/Libraries/LibWeb/Crypto/CryptoKey.h new file mode 100644 index 0000000000..a36af57511 --- /dev/null +++ b/Userland/Libraries/LibWeb/Crypto/CryptoKey.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#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: + [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&); + + virtual ~CryptoKey() override; + + bool extractable() const { return m_extractable; } + Bindings::KeyType type() const { return m_type; } + Object const* algorithm() const { return m_algorithm.ptr(); } + Object const* usages() const { return m_usages.ptr(); } + + 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(JS::NonnullGCPtr usages) { m_usages = move(usages); } + +private: + explicit CryptoKey(JS::Realm&); + 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; +}; + +} diff --git a/Userland/Libraries/LibWeb/Crypto/CryptoKey.idl b/Userland/Libraries/LibWeb/Crypto/CryptoKey.idl new file mode 100644 index 0000000000..b079ddbd21 --- /dev/null +++ b/Userland/Libraries/LibWeb/Crypto/CryptoKey.idl @@ -0,0 +1,12 @@ +enum KeyType { "public", "private", "secret" }; + +enum KeyUsage { "encrypt", "decrypt", "sign", "verify", "deriveKey", "deriveBits", "wrapKey", "unwrapKey" }; + +// https://w3c.github.io/webcrypto/#cryptokey-interface +[SecureContext,Exposed=(Window,Worker),Serializable] +interface CryptoKey { + readonly attribute KeyType type; + readonly attribute boolean extractable; + readonly attribute object algorithm; + readonly attribute object usages; +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 453e6a73e9..7f96c54c3b 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -9,6 +9,7 @@ libweb_js_bindings(Animations/DocumentTimeline) libweb_js_bindings(Animations/KeyframeEffect) libweb_js_bindings(Clipboard/Clipboard) libweb_js_bindings(Crypto/Crypto) +libweb_js_bindings(Crypto/CryptoKey) libweb_js_bindings(Crypto/SubtleCrypto) libweb_js_bindings(CSS/CSSConditionRule) libweb_js_bindings(CSS/CSSFontFaceRule)