Forráskód Böngészése

LibWeb: Implement usages property for CryptoKey

And set it from the only place we currently create a CryptoKey, in
importKey.
Andrew Kaster 1 éve
szülő
commit
0a6f195a71

+ 1 - 0
Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-import-PBKDF2.txt

@@ -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

+ 1 - 1
Tests/LibWeb/Text/input/Crypto/SubtleCrypto-import-PBKDF2.html

@@ -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);

+ 10 - 0
Userland/Libraries/LibWeb/Crypto/CryptoKey.cpp

@@ -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));
+    });
+}
+
 }

+ 6 - 3
Userland/Libraries/LibWeb/Crypto/CryptoKey.h

@@ -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;
 };
 

+ 9 - 2
Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp

@@ -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);