Skip to content

Truly independent web browser

LibWeb: Implement SubtleCrypto.getPublicKey()

Jelle Raaijmakers5e1e6a74fc1c
diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
index 66fac146541..a00e7763e53 100644
--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
@@ -1467,6 +1467,12 @@ WebIDL::ExceptionOr<ExportKeyResult> RSAOAEP::export_key(JS::Realm& realm, KeyFo
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> RSAOAEP::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::Encrypt, KeyUsage::WrapKey };
+ return usages.span();
+}
+
// https://w3c.github.io/webcrypto/#rsa-pss-operations-generate-key
WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> RSAPSS::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)
{
@@ -2048,6 +2054,12 @@ WebIDL::ExceptionOr<ExportKeyResult> RSAPSS::export_key(JS::Realm& realm, KeyFor
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> RSAPSS::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::Verify };
+ return usages.span();
+}
+
// https://w3c.github.io/webcrypto/#rsassa-pkcs1-operations-generate-key
WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> RSASSAPKCS1::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)
{
@@ -2623,6 +2635,12 @@ WebIDL::ExceptionOr<ExportKeyResult> RSASSAPKCS1::export_key(JS::Realm& realm, K
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> RSASSAPKCS1::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::Verify };
+ return usages.span();
+}
+
// https://w3c.github.io/webcrypto/#aes-cbc-operations-encrypt
WebIDL::ExceptionOr<ByteBuffer> AesCbc::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)
{
@@ -4960,6 +4978,12 @@ WebIDL::ExceptionOr<ExportKeyResult> ECDSA::export_key(JS::Realm&, KeyFormat for
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> ECDSA::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::Verify };
+ return usages.span();
+}
+
// https://w3c.github.io/webcrypto/#ecdh-operations-generate-key
WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ECDH::generate_key(JS::Realm&, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)
{
@@ -5892,6 +5916,11 @@ WebIDL::ExceptionOr<ExportKeyResult> ECDH::export_key(JS::Realm&, KeyFormat form
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> ECDH::public_key_usages() const
+{
+ return ReadonlySpan<KeyUsage> {};
+}
+
// https://w3c.github.io/webcrypto/#ed25519-operations-generate-key
WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ED25519::generate_key(JS::Realm&, [[maybe_unused]] AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)
{
@@ -6325,6 +6354,12 @@ WebIDL::ExceptionOr<ExportKeyResult> ED25519::export_key(JS::Realm& realm, KeyFo
return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
}
+Optional<ReadonlySpan<KeyUsage>> ED25519::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::Verify };
+ return usages.span();
+}
+
// https://w3c.github.io/webcrypto/#ed25519-operations-sign
WebIDL::ExceptionOr<ByteBuffer> ED25519::sign(JS::Realm& realm, [[maybe_unused]] AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message)
{
@@ -6810,6 +6845,12 @@ WebIDL::ExceptionOr<ExportKeyResult> ED448::export_key(JS::Realm& realm, KeyForm
return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
}
+Optional<ReadonlySpan<KeyUsage>> ED448::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::Verify };
+ return usages.span();
+}
+
// https://wicg.github.io/webcrypto-secure-curves/#ed448-operations
WebIDL::ExceptionOr<ByteBuffer> ED448::sign(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message)
{
@@ -7533,6 +7574,11 @@ WebIDL::ExceptionOr<ExportKeyResult> X25519::export_key(JS::Realm& realm, KeyFor
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> X25519::public_key_usages() const
+{
+ return ReadonlySpan<KeyUsage> {};
+}
+
// https://wicg.github.io/webcrypto-secure-curves/#x448-operations
WebIDL::ExceptionOr<ByteBuffer> X448::derive_bits(JS::Realm& realm,
AlgorithmParams const& params,
@@ -7766,6 +7812,11 @@ WebIDL::ExceptionOr<ExportKeyResult> X448::export_key(JS::Realm& realm, KeyForma
return WebIDL::NotSupportedError::create("Invalid key format"_utf16);
}
+Optional<ReadonlySpan<KeyUsage>> X448::public_key_usages() const
+{
+ return ReadonlySpan<KeyUsage> {};
+}
+
// https://wicg.github.io/webcrypto-secure-curves/#x448-operations
WebIDL::ExceptionOr<GC::Ref<CryptoKey>> X448::import_key(JS::Realm& realm,
AlgorithmParams const&,
@@ -9028,6 +9079,12 @@ WebIDL::ExceptionOr<ExportKeyResult> MLDSA::export_key(JS::Realm& realm, KeyForm
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> MLDSA::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::Verify };
+ return usages.span();
+}
+
// https://wicg.github.io/webcrypto-modern-algos/#ml-kem-operations-generate-key
WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> MLKEM::generate_key(JS::Realm&, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& usages)
{
@@ -9452,6 +9509,12 @@ WebIDL::ExceptionOr<ExportKeyResult> MLKEM::export_key(JS::Realm& realm, KeyForm
return result.release_value();
}
+Optional<ReadonlySpan<KeyUsage>> MLKEM::public_key_usages() const
+{
+ static constexpr Array usages { KeyUsage::EncapsulateKey, KeyUsage::EncapsulateBits };
+ return usages.span();
+}
+
// https://wicg.github.io/webcrypto-modern-algos/#ml-kem-operations-encapsulate
WebIDL::ExceptionOr<EncapsulatedBits> MLKEM::encapsulate(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key)
{
diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h
index 7a28fe3c434..abaec19bcae 100644
--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h
+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h
@@ -376,6 +376,8 @@ public:
return WebIDL::NotSupportedError::create("decalpsulate is not supported"_utf16);
}
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const { return {}; }
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AlgorithmMethods); }
protected:
@@ -392,6 +394,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new RSAOAEP); }
private:
@@ -408,6 +412,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new RSAPSS); }
private:
@@ -424,6 +430,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new RSASSAPKCS1); }
private:
@@ -533,6 +541,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ECDSA); }
private:
@@ -546,6 +556,8 @@ public:
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
virtual WebIDL::ExceptionOr<ByteBuffer> derive_bits(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ECDH); }
private:
@@ -561,6 +573,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ED25519); }
private:
@@ -576,6 +590,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ED448); }
private:
@@ -589,6 +605,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new X25519); }
private:
@@ -602,6 +620,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new X448); }
private:
@@ -631,6 +651,8 @@ public:
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new MLDSA); }
private:
@@ -645,6 +667,8 @@ public:
virtual WebIDL::ExceptionOr<EncapsulatedBits> encapsulate(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>) override;
virtual WebIDL::ExceptionOr<ByteBuffer> decapsulate(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;
+ virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;
+
static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new MLKEM); }
private:
diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
index 21907a6418e..42bb6f219a7 100644
--- a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
@@ -8,6 +8,8 @@
#include <AK/ByteBuffer.h>
#include <AK/NeverDestroyed.h>
+#include <LibCrypto/Curves/EdwardsCurve.h>
+#include <LibCrypto/Curves/SECPxxxr1.h>
#include <LibCrypto/Hash/HashManager.h>
#include <LibGC/Heap.h>
#include <LibJS/Runtime/ArrayBuffer.h>
@@ -1500,6 +1502,149 @@ GC::Ref<WebIDL::Promise> SubtleCrypto::decapsulate_bits(JS::Realm& realm, Algori
return promise;
}
+static ErrorOr<CryptoKey::InternalKeyData> public_key_for_private_key(CryptoKey const& key)
+{
+ return key.handle().visit(
+ [](::Crypto::PK::RSAPrivateKey const& private_key) -> ErrorOr<CryptoKey::InternalKeyData> {
+ return ::Crypto::PK::RSAPublicKey { private_key.modulus(), private_key.public_exponent() };
+ },
+ [&](::Crypto::PK::ECPrivateKey const& private_key) -> ErrorOr<CryptoKey::InternalKeyData> {
+ if (private_key.public_key().has_value())
+ return *private_key.public_key();
+
+ auto const& named_curve = key.ec_algorithm().named_curve();
+ auto point = TRY([&] -> ErrorOr<::Crypto::Curves::SECPxxxr1Point> {
+ if (named_curve == "P-256"_utf16)
+ return ::Crypto::Curves::SECP256r1 {}.generate_public_key(private_key.d());
+ if (named_curve == "P-384"_utf16)
+ return ::Crypto::Curves::SECP384r1 {}.generate_public_key(private_key.d());
+ if (named_curve == "P-521"_utf16)
+ return ::Crypto::Curves::SECP521r1 {}.generate_public_key(private_key.d());
+ return Error::from_string_literal("Unsupported named curve");
+ }());
+ return ::Crypto::PK::ECPublicKey { move(point) };
+ },
+ [](::Crypto::PK::MLDSAPrivateKey const& private_key) -> ErrorOr<CryptoKey::InternalKeyData> {
+ return ::Crypto::PK::MLDSAPublicKey { private_key.public_key() };
+ },
+ [](::Crypto::PK::MLKEMPrivateKey const& private_key) -> ErrorOr<CryptoKey::InternalKeyData> {
+ return ::Crypto::PK::MLKEMPublicKey { private_key.public_key() };
+ },
+ [&](OKPPrivateKey const& private_key) -> ErrorOr<CryptoKey::InternalKeyData> {
+ auto const& name = key.algorithm_name();
+ auto public_key = TRY([&] -> ErrorOr<ByteBuffer> {
+ if (name == "Ed25519"_utf16)
+ return ::Crypto::Curves::Ed25519 {}.generate_public_key(private_key.bytes);
+ if (name == "Ed448"_utf16)
+ return ::Crypto::Curves::Ed448 {}.generate_public_key(private_key.bytes);
+ if (name == "X25519"_utf16)
+ return ::Crypto::Curves::X25519 {}.generate_public_key(private_key.bytes);
+ if (name == "X448"_utf16)
+ return ::Crypto::Curves::X448 {}.generate_public_key(private_key.bytes);
+ return Error::from_string_literal("Unsupported algorithm");
+ }());
+ return OKPPublicKey { move(public_key) };
+ },
+ [](auto const&) -> ErrorOr<CryptoKey::InternalKeyData> {
+ return Error::from_string_literal("Key is not a private key");
+ });
+}
+
+// https://wicg.github.io/webcrypto-modern-algos/#dfn-SubtleCrypto-method-getPublicKey
+GC::Ref<WebIDL::Promise> SubtleCrypto::get_public_key(JS::Realm& realm, GC::Ref<CryptoKey> key, Vector<KeyUsage> key_usages)
+{
+ auto& global = realm.global_object();
+ auto& heap = GC::Heap::the();
+
+ // 1. Let key and usages be the key and keyUsages parameters passed to the getPublicKey() method, respectively.
+
+ // 2. Let algorithm be the [[algorithm]] internal slot of key.
+ // NB: Every algorithm that supports deriving a public key registers an export key operation, which lets us find
+ // the algorithm's methods.
+ auto normalized_algorithm = normalize_an_algorithm(realm, key->algorithm_name(), "exportKey"_string);
+
+ // 3. If the cryptographic algorithm identified by algorithm does not support deriving a public key from a private
+ // key, then return a Promise rejected with a NotSupportedError.
+ Optional<ReadonlySpan<KeyUsage>> public_key_usages;
+ if (!normalized_algorithm.is_error())
+ public_key_usages = normalized_algorithm.value().methods->public_key_usages();
+ if (!public_key_usages.has_value()) {
+ auto error = WebIDL::NotSupportedError::create(Utf16String::formatted("Algorithm '{}' does not support deriving a public key", key->algorithm_name()));
+ return WebIDL::create_rejected_promise_from_exception(realm, error);
+ }
+
+ // 4. Let realm be the relevant realm of this.
+
+ // 5. Let promise be a new Promise.
+ auto promise = WebIDL::create_promise(realm);
+
+ // 6. Return promise and perform the remaining steps in parallel.
+ Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(heap, [&realm, &global, &heap, promise, key, key_usages = move(key_usages), public_key_usages = *public_key_usages]() -> void {
+ HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::No);
+
+ // 7. If the following steps or referenced procedures say to throw an error, queue a global task on the crypto
+ // task source, given realm's global object, to reject promise with the returned error; and then terminate
+ // the algorithm.
+ auto const throw_in_this_context = [&realm, &global, &heap, &promise](JS::Completion completion) {
+ HTML::queue_global_task(HTML::Task::Source::Crypto, global, GC::create_function(heap, [&realm, promise, value = completion.value()] {
+ HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
+ WebIDL::reject_promise(promise, value);
+ }));
+ };
+
+ // 8. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
+ if (key->type() != KeyType::Private) {
+ throw_in_this_context(throw_completion(realm, WebIDL::InvalidAccessError::create("Key is not a private key"_utf16)));
+ return;
+ }
+
+ // 9. If usages contains an entry which is not supported for a public key by the algorithm identified by
+ // algorithm, then throw a SyntaxError.
+ for (auto usage : key_usages) {
+ if (!public_key_usages.contains_slow(usage)) {
+ throw_in_this_context(throw_completion(realm, WebIDL::SyntaxError::create("Invalid key usage for a public key"_utf16)));
+ return;
+ }
+ }
+
+ // 10. Let publicKey be a new CryptoKey representing the public key corresponding to the private key represented
+ // by the [[handle]] internal slot of key.
+ auto public_key_data = public_key_for_private_key(key);
+
+ // 11. If an error occurred, then throw a OperationError.
+ if (public_key_data.is_error()) {
+ throw_in_this_context(throw_completion(realm, WebIDL::OperationError::create("Failed to derive the public key"_utf16)));
+ return;
+ }
+ auto public_key = CryptoKey::create(public_key_data.release_value());
+
+ // 12. Set the [[type]] internal slot of publicKey to "public".
+ public_key->set_type(KeyType::Public);
+
+ // 13. Set the [[algorithm]] internal slot of publicKey to algorithm.
+ public_key->set_algorithm(key->internal_algorithm());
+
+ // 14. Set the [[extractable]] internal slot of publicKey to true.
+ public_key->set_extractable(true);
+
+ // 15. Set the [[usages]] internal slot of publicKey to the normalized value of usages.
+ public_key->set_usages(normalized_key_usages(key_usages));
+
+ // 16. Queue a global task on the crypto task source, given realm's global object, to perform the remaining
+ // steps.
+ HTML::queue_global_task(HTML::Task::Source::Crypto, global, GC::create_function(heap, [&realm, promise, public_key] {
+ HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
+
+ // 17. Let result be the result of converting publicKey to an ECMAScript Object in realm, as defined by
+ // [WebIDL].
+ // 18. Resolve promise with result.
+ resolve_crypto_key_promise(realm, promise, public_key);
+ }));
+ }));
+
+ return promise;
+}
+
SupportedAlgorithmsMap& supported_algorithms_internal()
{
static NeverDestroyed<SupportedAlgorithmsMap> supported_algorithms;
diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.h b/Libraries/LibWeb/Crypto/SubtleCrypto.h
index 40b22694afd..3644b2d4abd 100644
--- a/Libraries/LibWeb/Crypto/SubtleCrypto.h
+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.h
@@ -50,6 +50,8 @@ public:
GC::Ref<WebIDL::Promise> decapsulate_key(JS::Realm&, AlgorithmIdentifier decapsulation_algorithm, GC::Ref<CryptoKey> decapsulation_key, WebIDL::BufferSourceVariant const& ciphertext, AlgorithmIdentifier shared_key_algorithm, bool extractable, Vector<KeyUsage> const& usages);
GC::Ref<WebIDL::Promise> decapsulate_bits(JS::Realm&, AlgorithmIdentifier decapsulation_algorithm, GC::Ref<CryptoKey> decapsulation_key, WebIDL::BufferSourceVariant const& ciphertext);
+ GC::Ref<WebIDL::Promise> get_public_key(JS::Realm&, GC::Ref<CryptoKey> key, Vector<KeyUsage> key_usages);
+
private:
SubtleCrypto();
};
diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.idl b/Libraries/LibWeb/Crypto/SubtleCrypto.idl
index efff0b4226e..190dfef3924 100644
--- a/Libraries/LibWeb/Crypto/SubtleCrypto.idl
+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.idl
@@ -83,4 +83,5 @@ interface SubtleCrypto {
[NeedsCallerRealm] Promise<EncapsulatedBits> encapsulateBits(AlgorithmIdentifier encapsulationAlgorithm, CryptoKey encapsulationKey);
[NeedsCallerRealm] Promise<CryptoKey> decapsulateKey(AlgorithmIdentifier decapsulationAlgorithm, CryptoKey decapsulationKey, BufferSource ciphertext, AlgorithmIdentifier sharedKeyAlgorithm, boolean extractable, sequence<KeyUsage> keyUsages);
[NeedsCallerRealm] Promise<ArrayBuffer> decapsulateBits(AlgorithmIdentifier decapsulationAlgorithm, CryptoKey decapsulationKey, BufferSource ciphertext);
+ [NeedsCallerRealm] Promise<CryptoKey> getPublicKey(CryptoKey key, sequence<KeyUsage> keyUsages);
};
diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.py
index b69af9b6489..9e282c98327 100755
--- a/Meta/Linters/check_libweb_realm_mentions.py
+++ b/Meta/Linters/check_libweb_realm_mentions.py
@@ -40,7 +40,7 @@ ALLOWED_REALM_MENTIONS = {
"Clipboard": (4, 16, "clipboard item/data callbacks still materialize JS values in caller-selected realms"),
"ContentSecurityPolicy": (4, 10, "CSP violation/report objects and callbacks still materialize JS-facing values"),
"CookieStore": (2, 19, "cookie-store async callbacks and promise/value materialization still thread realms"),
- "Crypto": (8, 369, "WebCrypto algorithms still use realms for buffer/key/promise materialization"),
+ "Crypto": (8, 371, "WebCrypto algorithms still use realms for buffer/key/promise materialization"),
"DOM": (10, 33, "DOM abort plumbing and node helpers still have callback/materialization realm use"),
"Fetch": (
20,
diff --git a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/getPublicKey.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/getPublicKey.https.any.txt
index 31cf7a31d60..3f5882fad1e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/getPublicKey.https.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/getPublicKey.https.any.txt
@@ -2,47 +2,48 @@ Harness status: OK
Found 43 tests
-43 Fail
-Fail getPublicKey works for ECDH
-Fail getPublicKey works for ECDSA
-Fail getPublicKey works for Ed25519
-Fail getPublicKey works for Ed448
-Fail getPublicKey works for RSA-OAEP
-Fail getPublicKey works for RSA-PSS
-Fail getPublicKey works for RSASSA-PKCS1-v1_5
-Fail getPublicKey works for X25519
-Fail getPublicKey works for X448
-Fail getPublicKey works for ML-DSA-44
-Fail getPublicKey works for ML-DSA-65
-Fail getPublicKey works for ML-DSA-87
-Fail getPublicKey works for ML-KEM-512
-Fail getPublicKey works for ML-KEM-768
-Fail getPublicKey works for ML-KEM-1024
+37 Pass
+6 Fail
+Pass getPublicKey works for ECDH
+Pass getPublicKey works for ECDSA
+Pass getPublicKey works for Ed25519
+Pass getPublicKey works for Ed448
+Pass getPublicKey works for RSA-OAEP
+Pass getPublicKey works for RSA-PSS
+Pass getPublicKey works for RSASSA-PKCS1-v1_5
+Pass getPublicKey works for X25519
+Pass getPublicKey works for X448
+Pass getPublicKey works for ML-DSA-44
+Pass getPublicKey works for ML-DSA-65
+Pass getPublicKey works for ML-DSA-87
+Pass getPublicKey works for ML-KEM-512
+Pass getPublicKey works for ML-KEM-768
+Pass getPublicKey works for ML-KEM-1024
Fail getPublicKey works for MLKEM768-P256
Fail getPublicKey works for MLKEM768-X25519
Fail getPublicKey works for MLKEM1024-P384
-Fail Derived public key is functionally equivalent to original public key
-Fail getPublicKey works with empty usages for ECDH
-Fail getPublicKey works with empty usages for ECDSA
-Fail getPublicKey works with empty usages for Ed25519
-Fail getPublicKey works with empty usages for Ed448
-Fail getPublicKey works with empty usages for RSA-OAEP
-Fail getPublicKey works with empty usages for RSA-PSS
-Fail getPublicKey works with empty usages for RSASSA-PKCS1-v1_5
-Fail getPublicKey works with empty usages for X25519
-Fail getPublicKey works with empty usages for X448
-Fail getPublicKey works with empty usages for ML-DSA-44
-Fail getPublicKey works with empty usages for ML-DSA-65
-Fail getPublicKey works with empty usages for ML-DSA-87
-Fail getPublicKey works with empty usages for ML-KEM-512
-Fail getPublicKey works with empty usages for ML-KEM-768
-Fail getPublicKey works with empty usages for ML-KEM-1024
+Pass Derived public key is functionally equivalent to original public key
+Pass getPublicKey works with empty usages for ECDH
+Pass getPublicKey works with empty usages for ECDSA
+Pass getPublicKey works with empty usages for Ed25519
+Pass getPublicKey works with empty usages for Ed448
+Pass getPublicKey works with empty usages for RSA-OAEP
+Pass getPublicKey works with empty usages for RSA-PSS
+Pass getPublicKey works with empty usages for RSASSA-PKCS1-v1_5
+Pass getPublicKey works with empty usages for X25519
+Pass getPublicKey works with empty usages for X448
+Pass getPublicKey works with empty usages for ML-DSA-44
+Pass getPublicKey works with empty usages for ML-DSA-65
+Pass getPublicKey works with empty usages for ML-DSA-87
+Pass getPublicKey works with empty usages for ML-KEM-512
+Pass getPublicKey works with empty usages for ML-KEM-768
+Pass getPublicKey works with empty usages for ML-KEM-1024
Fail getPublicKey works with empty usages for MLKEM768-P256
Fail getPublicKey works with empty usages for MLKEM768-X25519
Fail getPublicKey works with empty usages for MLKEM1024-P384
-Fail getPublicKey rejects with InvalidAccessError when given a public key
-Fail getPublicKey rejects with NotSupportedError for AES-GCM symmetric keys
-Fail getPublicKey rejects with NotSupportedError for HMAC symmetric keys
-Fail getPublicKey rejects with SyntaxError for invalid usages
-Fail getPublicKey rejects with SyntaxError when any usage is invalid for the algorithm
-Fail getPublicKey method is available
\ No newline at end of file
+Pass getPublicKey rejects with InvalidAccessError when given a public key
+Pass getPublicKey rejects with NotSupportedError for AES-GCM symmetric keys
+Pass getPublicKey rejects with NotSupportedError for HMAC symmetric keys
+Pass getPublicKey rejects with SyntaxError for invalid usages
+Pass getPublicKey rejects with SyntaxError when any usage is invalid for the algorithm
+Pass getPublicKey method is available
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/idlharness.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/idlharness.https.any.txt
index 51da42179ab..550f51f16c5 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/idlharness.https.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/modern-algos/idlharness.https.any.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 28 tests
-21 Pass
-7 Fail
+24 Pass
+4 Fail
Pass idl_test setup
Pass idl_test validation
Pass Partial interface SubtleCrypto: original interface defined
@@ -15,7 +15,7 @@ Pass SubtleCrypto interface: operation encapsulateKey(AlgorithmIdentifier, Crypt
Pass SubtleCrypto interface: operation encapsulateBits(AlgorithmIdentifier, CryptoKey)
Pass SubtleCrypto interface: operation decapsulateKey(AlgorithmIdentifier, CryptoKey, BufferSource, AlgorithmIdentifier, boolean, sequence<KeyUsage>)
Pass SubtleCrypto interface: operation decapsulateBits(AlgorithmIdentifier, CryptoKey, BufferSource)
-Fail SubtleCrypto interface: operation getPublicKey(CryptoKey, sequence<KeyUsage>)
+Pass SubtleCrypto interface: operation getPublicKey(CryptoKey, sequence<KeyUsage>)
Fail SubtleCrypto interface: operation supports(DOMString, AlgorithmIdentifier, optional unsigned long?)
Fail SubtleCrypto interface: operation supports(DOMString, AlgorithmIdentifier, AlgorithmIdentifier)
Pass SubtleCrypto interface: crypto.subtle must inherit property "encapsulateKey(AlgorithmIdentifier, CryptoKey, AlgorithmIdentifier, boolean, sequence<KeyUsage>)" with the proper type
@@ -26,8 +26,8 @@ Pass SubtleCrypto interface: crypto.subtle must inherit property "decapsulateKey
Pass SubtleCrypto interface: calling decapsulateKey(AlgorithmIdentifier, CryptoKey, BufferSource, AlgorithmIdentifier, boolean, sequence<KeyUsage>) on crypto.subtle with too few arguments must throw TypeError
Pass SubtleCrypto interface: crypto.subtle must inherit property "decapsulateBits(AlgorithmIdentifier, CryptoKey, BufferSource)" with the proper type
Pass SubtleCrypto interface: calling decapsulateBits(AlgorithmIdentifier, CryptoKey, BufferSource) on crypto.subtle with too few arguments must throw TypeError
-Fail SubtleCrypto interface: crypto.subtle must inherit property "getPublicKey(CryptoKey, sequence<KeyUsage>)" with the proper type
-Fail SubtleCrypto interface: calling getPublicKey(CryptoKey, sequence<KeyUsage>) on crypto.subtle with too few arguments must throw TypeError
+Pass SubtleCrypto interface: crypto.subtle must inherit property "getPublicKey(CryptoKey, sequence<KeyUsage>)" with the proper type
+Pass SubtleCrypto interface: calling getPublicKey(CryptoKey, sequence<KeyUsage>) on crypto.subtle with too few arguments must throw TypeError
Pass SubtleCrypto interface: crypto.subtle must inherit property "supports(DOMString, AlgorithmIdentifier, optional unsigned long?)" with the proper type
Fail SubtleCrypto interface: calling supports(DOMString, AlgorithmIdentifier, optional unsigned long?) on crypto.subtle with too few arguments must throw TypeError
Pass SubtleCrypto interface: crypto.subtle must inherit property "supports(DOMString, AlgorithmIdentifier, AlgorithmIdentifier)" with the proper type