diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index fce5f418f81..c0f5840076c 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -26,6 +26,17 @@ static void normalize_key_usages(Vector& key_usages) { quick_sort(key_usages); } +struct RegisteredAlgorithm { + NonnullOwnPtr (*create_methods)(JS::Realm&) = nullptr; + JS::ThrowCompletionOr> (*parameter_from_value)(JS::VM&, JS::Value) = nullptr; +}; +using SupportedAlgorithmsMap = HashMap>; + +static SupportedAlgorithmsMap& supported_algorithms_internal(); +static SupportedAlgorithmsMap supported_algorithms(); + +template +static void define_an_algorithm(String op, String algorithm); JS_DEFINE_ALLOCATOR(SubtleCrypto); @@ -48,10 +59,9 @@ void SubtleCrypto::initialize(JS::Realm& realm) } // https://w3c.github.io/webcrypto/#dfn-normalize-an-algorithm -WebIDL::ExceptionOr SubtleCrypto::normalize_an_algorithm(AlgorithmIdentifier const& algorithm, String operation) +WebIDL::ExceptionOr normalize_an_algorithm(JS::Realm& realm, AlgorithmIdentifier const& algorithm, String operation) { - auto& realm = this->realm(); - auto& vm = this->vm(); + auto& vm = realm.vm(); // If alg is an instance of a DOMString: if (algorithm.has()) { @@ -60,7 +70,7 @@ WebIDL::ExceptionOr SubtleCrypto: auto dictionary = JS::make_handle(JS::Object::create(realm, realm.intrinsics().object_prototype())); TRY(dictionary->create_data_property("name", JS::PrimitiveString::create(vm, algorithm.get()))); - return normalize_an_algorithm(dictionary, operation); + return normalize_an_algorithm(realm, dictionary, operation); } // If alg is an object: @@ -123,7 +133,7 @@ JS::NonnullGCPtr SubtleCrypto::digest(AlgorithmIdentifier const& al auto data_buffer = data_buffer_or_error.release_value(); // 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest". - auto normalized_algorithm = normalize_an_algorithm(algorithm, "digest"_string); + auto normalized_algorithm = normalize_an_algorithm(realm, algorithm, "digest"_string); // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm. // FIXME: Spec bug: link to https://webidl.spec.whatwg.org/#a-promise-rejected-with @@ -164,7 +174,7 @@ JS::ThrowCompletionOr> SubtleCrypto::generate_key( // 2. Let normalizedAlgorithm be the result of normalizing an algorithm, // with alg set to algorithm and op set to "generateKey". - auto normalized_algorithm = normalize_an_algorithm(algorithm, "generateKey"_string); + auto normalized_algorithm = normalize_an_algorithm(realm, algorithm, "generateKey"_string); // 3. If an error occurred, return a Promise rejected with normalizedAlgorithm. if (normalized_algorithm.is_error()) @@ -246,7 +256,7 @@ JS::ThrowCompletionOr> SubtleCrypto::import_key(Bi // NOTE: The spec jumps to 5 here for some reason? // 5. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "importKey". - auto normalized_algorithm = normalize_an_algorithm(algorithm, "importKey"_string); + auto normalized_algorithm = normalize_an_algorithm(realm, algorithm, "importKey"_string); // 6. If an error occurred, return a Promise rejected with normalizedAlgorithm. if (normalized_algorithm.is_error()) @@ -300,7 +310,7 @@ JS::ThrowCompletionOr> SubtleCrypto::export_key(Bi auto promise = WebIDL::create_promise(realm); // 3. Return promise and perform the remaining steps in parallel. - Platform::EventLoopPlugin::the().deferred_invoke([&realm, key, this, promise, format]() -> void { + Platform::EventLoopPlugin::the().deferred_invoke([&realm, key, promise, format]() -> void { HTML::TemporaryExecutionContext context(Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes); // 4. If the following steps or referenced procedures say to throw an error, reject promise with the returned error and then terminate the algorithm. @@ -309,7 +319,7 @@ JS::ThrowCompletionOr> SubtleCrypto::export_key(Bi // Note: Handled by the base AlgorithmMethods implementation auto& algorithm = verify_cast(*key->algorithm()); // FIXME: Stash the AlgorithmMethods on the KeyAlgorithm - auto normalized_algorithm_or_error = normalize_an_algorithm(algorithm.name(), "exportKey"_string); + auto normalized_algorithm_or_error = normalize_an_algorithm(realm, algorithm.name(), "exportKey"_string); if (normalized_algorithm_or_error.is_error()) { WebIDL::reject_promise(realm, promise, Bindings::dom_exception_to_throw_completion(realm.vm(), normalized_algorithm_or_error.release_error()).release_value().value()); return; @@ -336,14 +346,14 @@ JS::ThrowCompletionOr> SubtleCrypto::export_key(Bi return verify_cast(*promise->promise()); } -SubtleCrypto::SupportedAlgorithmsMap& SubtleCrypto::supported_algorithms_internal() +SupportedAlgorithmsMap& supported_algorithms_internal() { - static SubtleCrypto::SupportedAlgorithmsMap s_supported_algorithms; + static SupportedAlgorithmsMap s_supported_algorithms; return s_supported_algorithms; } // https://w3c.github.io/webcrypto/#algorithm-normalization-internalS -SubtleCrypto::SupportedAlgorithmsMap SubtleCrypto::supported_algorithms() +SupportedAlgorithmsMap supported_algorithms() { auto& internal_object = supported_algorithms_internal(); @@ -394,7 +404,7 @@ SubtleCrypto::SupportedAlgorithmsMap SubtleCrypto::supported_algorithms() // https://w3c.github.io/webcrypto/#concept-define-an-algorithm template -void SubtleCrypto::define_an_algorithm(AK::String op, AK::String algorithm) +void define_an_algorithm(AK::String op, AK::String algorithm) { auto& internal_object = supported_algorithms_internal(); diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h index cd1ed265b1d..13c6b607333 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h @@ -22,12 +22,6 @@ class SubtleCrypto final : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(SubtleCrypto, Bindings::PlatformObject); JS_DECLARE_ALLOCATOR(SubtleCrypto); - struct RegisteredAlgorithm { - NonnullOwnPtr (*create_methods)(JS::Realm&) = nullptr; - JS::ThrowCompletionOr> (*parameter_from_value)(JS::VM&, JS::Value) = nullptr; - }; - using SupportedAlgorithmsMap = HashMap>; - public: [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&); @@ -43,18 +37,12 @@ public: private: explicit SubtleCrypto(JS::Realm&); virtual void initialize(JS::Realm&) override; - - struct NormalizedAlgorithmAndParameter { - NonnullOwnPtr methods; - NonnullOwnPtr parameter; - }; - WebIDL::ExceptionOr normalize_an_algorithm(AlgorithmIdentifier const& algorithm, String operation); - - static SubtleCrypto::SupportedAlgorithmsMap& supported_algorithms_internal(); - static SubtleCrypto::SupportedAlgorithmsMap supported_algorithms(); - - template - static void define_an_algorithm(String op, String algorithm); }; +struct NormalizedAlgorithmAndParameter { + NonnullOwnPtr methods; + NonnullOwnPtr parameter; +}; +WebIDL::ExceptionOr normalize_an_algorithm(JS::Realm&, AlgorithmIdentifier const& algorithm, String operation); + }