diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/Crypto/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/Crypto/BUILD.gn index 928da40f06..e20f1f060f 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", + "CryptoBindings.cpp", + "CryptoBindings.h", "CryptoKey.cpp", "CryptoKey.h", "SubtleCrypto.cpp", diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 2b158d51f1..2164bb6699 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/CryptoBindings.cpp Crypto/CryptoKey.cpp Crypto/SubtleCrypto.cpp CSS/Angle.cpp diff --git a/Userland/Libraries/LibWeb/Crypto/CryptoBindings.cpp b/Userland/Libraries/LibWeb/Crypto/CryptoBindings.cpp new file mode 100644 index 0000000000..a04c8e5da0 --- /dev/null +++ b/Userland/Libraries/LibWeb/Crypto/CryptoBindings.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::Bindings { + +JS_DEFINE_ALLOCATOR(KeyAlgorithm); + +JS::NonnullGCPtr KeyAlgorithm::create(JS::Realm& realm) +{ + return realm.heap().allocate(realm, realm); +} + +KeyAlgorithm::KeyAlgorithm(JS::Realm& realm) + : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype()) +{ +} + +void KeyAlgorithm::initialize(JS::Realm& realm) +{ + define_native_accessor(realm, "name", name_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable); + Base::initialize(realm); +} + +static JS::ThrowCompletionOr impl_from(JS::VM& vm) +{ + auto this_value = vm.this_value(); + JS::Object* this_object = nullptr; + if (this_value.is_nullish()) + this_object = &vm.current_realm()->global_object(); + else + this_object = TRY(this_value.to_object(vm)); + + if (!is(this_object)) + return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "KeyAlgorithm"); + return static_cast(this_object); +} + +JS_DEFINE_NATIVE_FUNCTION(KeyAlgorithm::name_getter) +{ + auto* impl = TRY(impl_from(vm)); + auto name = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->name(); })); + return JS::PrimitiveString::create(vm, name); +} + +} diff --git a/Userland/Libraries/LibWeb/Crypto/CryptoBindings.h b/Userland/Libraries/LibWeb/Crypto/CryptoBindings.h new file mode 100644 index 0000000000..aed983e01c --- /dev/null +++ b/Userland/Libraries/LibWeb/Crypto/CryptoBindings.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +// FIXME: Generate these from IDL +namespace Web::Bindings { + +// https://w3c.github.io/webcrypto/#JsonWebKey-dictionary +struct RsaOtherPrimesInfo { + Optional r; + Optional d; + Optional t; +}; + +// https://w3c.github.io/webcrypto/#JsonWebKey-dictionary +struct JsonWebKey { + Optional kty; + Optional use; + Optional> key_ops; + Optional alg; + Optional ext; + Optional crv; + Optional x; + Optional y; + Optional d; + Optional n; + Optional e; + Optional p; + Optional q; + Optional dp; + Optional dq; + Optional qi; + Optional> oth; + Optional k; +}; + +// https://w3c.github.io/webcrypto/#dfn-Algorithm +struct Algorithm { + String name; +}; + +// https://w3c.github.io/webcrypto/#key-algorithm-dictionary +class KeyAlgorithm : public JS::Object { + JS_OBJECT(KeyAlgorithm, Object); + JS_DECLARE_ALLOCATOR(KeyAlgorithm); + +public: + static JS::NonnullGCPtr create(JS::Realm&); + virtual ~KeyAlgorithm() override = default; + + String const& name() const { return m_name; } + void set_name(String name) { m_name = move(name); } + +private: + KeyAlgorithm(JS::Realm&); + virtual void initialize(JS::Realm&) override; + + JS_DECLARE_NATIVE_FUNCTION(name_getter); + + String m_name; +}; + +// https://w3c.github.io/webcrypto/#pbkdf2-params +struct Pbkdf2Params { + JS::Handle salt; + u32 iterations; + Variant, String> hash; +}; + +}; diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index 01ceea5fa8..ff04be1871 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -190,7 +190,7 @@ SubtleCrypto::SupportedAlgorithmsMap& SubtleCrypto::supported_algorithms_interna return s_supported_algorithms; } -// https://w3c.github.io/webcrypto/#algorithm-normalization-internal +// https://w3c.github.io/webcrypto/#algorithm-normalization-internalS SubtleCrypto::SupportedAlgorithmsMap SubtleCrypto::supported_algorithms() { auto& internal_object = supported_algorithms_internal(); diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h index 95ef93397b..8a65fcbb87 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h @@ -10,15 +10,7 @@ #include #include #include -// FIXME: Generate these from IDL -namespace Web::Bindings { - -// https://w3c.github.io/webcrypto/#dfn-Algorithm -struct Algorithm { - String name; -}; - -}; +#include namespace Web::Crypto { diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl index 7c80b7810f..600fc27038 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl @@ -4,6 +4,42 @@ dictionary Algorithm { required DOMString name; }; +enum KeyFormat { "raw", "spki", "pkcs8", "jwk" }; + +dictionary RsaOtherPrimesInfo { + // The following fields are defined in Section 6.3.2.7 of JSON Web Algorithms + DOMString r; + DOMString d; + DOMString t; +}; + +dictionary JsonWebKey +{ + // The following fields are defined in Section 3.1 of JSON Web Key + DOMString kty; + DOMString use; + sequence key_ops; + DOMString alg; + + // The following fields are defined in JSON Web Key Parameters Registration + boolean ext; + + // The following fields are defined in Section 6 of JSON Web Algorithms + DOMString crv; + DOMString x; + DOMString y; + DOMString d; + DOMString n; + DOMString e; + DOMString p; + DOMString q; + DOMString dp; + DOMString dq; + DOMString qi; + sequence oth; + DOMString k; +}; + // https://w3c.github.io/webcrypto/#subtlecrypto-interface [SecureContext,Exposed=(Window,Worker)] interface SubtleCrypto {