From 635ad9e9b8468805c92159bff529cef88bbf2481 Mon Sep 17 00:00:00 2001 From: stelar7 Date: Thu, 14 Dec 2023 20:56:30 +0100 Subject: [PATCH] LibWeb: Use `AlgorithmIdentifier` in SubtleCrypto IDL --- Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp | 14 +++++++++++--- Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h | 2 +- Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl | 9 +++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index 021edd1646..09eda7db37 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -36,7 +36,7 @@ void SubtleCrypto::initialize(JS::Realm& realm) } // https://w3c.github.io/webcrypto/#dfn-SubtleCrypto-method-digest -JS::NonnullGCPtr SubtleCrypto::digest(String const& algorithm, JS::Handle const& data) +JS::NonnullGCPtr SubtleCrypto::digest(Variant, String> const& algorithm, JS::Handle const& data) { auto& realm = this->realm(); @@ -54,8 +54,16 @@ JS::NonnullGCPtr SubtleCrypto::digest(String const& algorithm, JS:: // 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest". // FIXME: This is way more generic than it needs to be right now, so we simplify it. + if (!algorithm.has()) { + auto promise = JS::Promise::create(realm); + auto error = WebIDL::OperationError::create(realm, "LibJS does not support non-string parameters to digest()"_fly_string); + promise->reject(error.ptr()); + return promise; + } + ::Crypto::Hash::HashKind hash_kind; - auto algorithm_as_string_view = algorithm.bytes_as_string_view(); + auto algorithm_name = algorithm.get(); + auto algorithm_as_string_view = algorithm_name.bytes_as_string_view(); if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-1"sv)) { hash_kind = ::Crypto::Hash::HashKind::SHA1; } else if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-256"sv)) { @@ -67,7 +75,7 @@ JS::NonnullGCPtr SubtleCrypto::digest(String const& algorithm, JS:: } // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm. else { - auto error = WebIDL::NotSupportedError::create(realm, MUST(String::formatted("Invalid hash function '{}'", algorithm))); + auto error = WebIDL::NotSupportedError::create(realm, MUST(String::formatted("Invalid hash function '{}'", algorithm_name))); auto promise = JS::Promise::create(realm); promise->reject(error.ptr()); return promise; diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h index 697693d523..cb2234a997 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h @@ -20,7 +20,7 @@ public: virtual ~SubtleCrypto() override; - JS::NonnullGCPtr digest(String const& algorithm, JS::Handle const& data); + JS::NonnullGCPtr digest(Variant, String> const& algorithm, JS::Handle const& data); private: explicit SubtleCrypto(JS::Realm&); diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl index def868c00e..618bbdc939 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl @@ -1,3 +1,9 @@ +typedef (object or DOMString) AlgorithmIdentifier; + +dictionary Algorithm { + required DOMString name; +}; + // https://w3c.github.io/webcrypto/#subtlecrypto-interface [SecureContext,Exposed=(Window,Worker)] interface SubtleCrypto { @@ -6,8 +12,7 @@ interface SubtleCrypto { // FIXME: Promise sign(AlgorithmIdentifier algorithm, CryptoKey key, BufferSource data); // FIXME: Promise verify(AlgorithmIdentifier algorithm, CryptoKey key, BufferSource signature, BufferSource data); - // FIXME: Add support for AlgorithmIdentifier ("typedef (object or DOMString)") - Promise digest(DOMString algorithm, BufferSource data); + Promise digest(AlgorithmIdentifier algorithm, BufferSource data); // FIXME: Promise generateKey(AlgorithmIdentifier algorithm, boolean extractable, sequence keyUsages); // FIXME: Promise deriveKey(AlgorithmIdentifier algorithm, CryptoKey baseKey, AlgorithmIdentifier derivedKeyType, boolean extractable, sequence keyUsages );