LibWeb: Implement usages property for CryptoKey

And set it from the only place we currently create a CryptoKey, in
importKey.
This commit is contained in:
Andrew Kaster 2024-03-06 19:11:08 -07:00 committed by Andrew Kaster
parent 2d59d6c98c
commit 0a6f195a71
Notes: sideshowbarker 2024-07-17 02:29:45 +09:00
5 changed files with 27 additions and 6 deletions

View file

@ -2,4 +2,5 @@ imported key: [object CryptoKey]
imported key.type: secret
imported key.extractable: false
imported key.algorithm: {"name":"PBKDF2"}
imported key.usages: deriveKey,deriveBits
SHA-256 digest: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

View file

@ -19,7 +19,7 @@
println(`imported key.type: ${key.type}`);
println(`imported key.extractable: ${key.extractable}`);
println(`imported key.algorithm: ${JSON.stringify(key.algorithm)}`);
// FIXME: Implement usages println(`imported key.usages: ${key.usages}`);
println(`imported key.usages: ${key.usages}`);
let message = "Hello, world!";
let encoded_message = enc.encode(message);

View file

@ -6,6 +6,7 @@
*/
#include <AK/Memory.h>
#include <LibJS/Runtime/Array.h>
#include <LibWeb/Crypto/CryptoKey.h>
namespace Web::Crypto {
@ -45,4 +46,13 @@ void CryptoKey::visit_edges(Visitor& visitor)
visitor.visit(m_usages);
}
void CryptoKey::set_usages(Vector<Bindings::KeyUsage> usages)
{
m_key_usages = move(usages);
auto& realm = this->realm();
m_usages = JS::Array::create_from<Bindings::KeyUsage>(realm, m_key_usages.span(), [&](auto& key_usage) -> JS::Value {
return JS::PrimitiveString::create(realm.vm(), Bindings::idl_enum_to_string(key_usage));
});
}
}

View file

@ -28,13 +28,15 @@ public:
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(); }
JS::Object const* algorithm() const { return m_algorithm; }
JS::Object const* usages() const { return m_usages; }
Vector<Bindings::KeyUsage> 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<Object> algorithm) { m_algorithm = move(algorithm); }
void set_usages(JS::NonnullGCPtr<Object> usages) { m_usages = move(usages); }
void set_usages(Vector<Bindings::KeyUsage>);
private:
CryptoKey(JS::Realm&, InternalKeyData);
@ -46,6 +48,7 @@ private:
JS::NonnullGCPtr<Object> m_algorithm;
JS::NonnullGCPtr<Object> m_usages;
Vector<Bindings::KeyUsage> m_key_usages;
InternalKeyData m_key_data;
};

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/QuickSort.h>
#include <LibCrypto/Hash/HashManager.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Promise.h>
@ -20,6 +21,11 @@
namespace Web::Crypto {
static void normalize_key_usages(Vector<Bindings::KeyUsage>& key_usages)
{
quick_sort(key_usages);
}
JS_DEFINE_ALLOCATOR(SubtleCrypto);
JS::NonnullGCPtr<SubtleCrypto> SubtleCrypto::create(JS::Realm& realm)
@ -190,7 +196,7 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> SubtleCrypto::import_key(Bi
auto promise = WebIDL::create_promise(realm);
// 8. Return promise and perform the remaining steps in parallel.
Platform::EventLoopPlugin::the().deferred_invoke([&realm, real_key_data = move(real_key_data), normalized_algorithm = normalized_algorithm.release_value(), promise, format, extractable, key_usages = move(key_usages), algorithm = move(algorithm)]() -> void {
Platform::EventLoopPlugin::the().deferred_invoke([&realm, real_key_data = move(real_key_data), normalized_algorithm = normalized_algorithm.release_value(), promise, format, extractable, key_usages = move(key_usages), algorithm = move(algorithm)]() mutable -> void {
HTML::TemporaryExecutionContext context(Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
// 9. If the following steps or referenced procedures say to throw an error, reject promise with the returned error and then terminate the algorithm.
@ -214,7 +220,8 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> SubtleCrypto::import_key(Bi
result->set_extractable(extractable);
// 13. Set the [[usages]] internal slot of result to the normalized value of usages.
// FIXME: result->set_usages(key_usages);
normalize_key_usages(key_usages);
result->set_usages(key_usages);
// 14. Resolve promise with result.
WebIDL::resolve_promise(realm, promise, result);