mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-28 18:40:29 +00:00
LibWeb: Implement AES-CTR.exportKey
This commit is contained in:
parent
030cbef532
commit
4b2120d919
Notes:
github-actions[bot]
2024-10-31 07:16:10 +00:00
Author: https://github.com/stelar7 Commit: https://github.com/LadybirdBrowser/ladybird/commit/4b2120d919a Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2070
3 changed files with 70 additions and 0 deletions
|
@ -1517,6 +1517,74 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> AesCtr::import_key(AlgorithmPar
|
|||
return key;
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> AesCtr::export_key(Bindings::KeyFormat format, JS::NonnullGCPtr<CryptoKey> key)
|
||||
{
|
||||
// 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.
|
||||
// Note: In our impl this is always accessible
|
||||
|
||||
JS::GCPtr<JS::Object> result = nullptr;
|
||||
|
||||
// 2. If format is "raw":
|
||||
if (format == Bindings::KeyFormat::Raw) {
|
||||
// 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.
|
||||
auto data = key->handle().get<ByteBuffer>();
|
||||
|
||||
// 2. Let result be the result of creating an ArrayBuffer containing data.
|
||||
result = JS::ArrayBuffer::create(m_realm, data);
|
||||
}
|
||||
|
||||
// 2. If format is "jwk":
|
||||
else if (format == Bindings::KeyFormat::Jwk) {
|
||||
// 1. Let jwk be a new JsonWebKey dictionary.
|
||||
Bindings::JsonWebKey jwk = {};
|
||||
|
||||
// 2. Set the kty attribute of jwk to the string "oct".
|
||||
jwk.kty = "oct"_string;
|
||||
|
||||
// 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,
|
||||
// encoded according to Section 6.4 of JSON Web Algorithms [JWA].
|
||||
auto const& key_bytes = key->handle().get<ByteBuffer>();
|
||||
jwk.k = TRY_OR_THROW_OOM(m_realm->vm(), encode_base64url(key_bytes, AK::OmitPadding::Yes));
|
||||
|
||||
// 4. -> If the length attribute of key is 128:
|
||||
// Set the alg attribute of jwk to the string "A128CTR".
|
||||
// -> If the length attribute of key is 192:
|
||||
// Set the alg attribute of jwk to the string "A192CTR".
|
||||
// -> If the length attribute of key is 256:
|
||||
// Set the alg attribute of jwk to the string "A256CTR".
|
||||
auto key_bits = key_bytes.size() * 8;
|
||||
if (key_bits == 128) {
|
||||
jwk.alg = "A128CTR"_string;
|
||||
} else if (key_bits == 192) {
|
||||
jwk.alg = "A192CTR"_string;
|
||||
} else if (key_bits == 256) {
|
||||
jwk.alg = "A256CTR"_string;
|
||||
}
|
||||
|
||||
// 5. Set the key_ops attribute of jwk to the usages attribute of key.
|
||||
jwk.key_ops = Vector<String> {};
|
||||
jwk.key_ops->ensure_capacity(key->internal_usages().size());
|
||||
for (auto const& usage : key->internal_usages()) {
|
||||
jwk.key_ops->append(Bindings::idl_enum_to_string(usage));
|
||||
}
|
||||
|
||||
// 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.
|
||||
jwk.ext = key->extractable();
|
||||
|
||||
// 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].
|
||||
result = TRY(jwk.to_object(m_realm));
|
||||
}
|
||||
|
||||
// 2. Otherwise:
|
||||
else {
|
||||
// 1. throw a NotSupportedError.
|
||||
return WebIDL::NotSupportedError::create(m_realm, "Cannot export to unsupported format"_string);
|
||||
}
|
||||
|
||||
// 3. Return result.
|
||||
return JS::NonnullGCPtr { *result };
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<JS::Value> AesCtr::get_key_length(AlgorithmParams const& params)
|
||||
{
|
||||
// 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw a OperationError.
|
||||
|
|
|
@ -330,6 +330,7 @@ private:
|
|||
class AesCtr : public AlgorithmMethods {
|
||||
public:
|
||||
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
|
||||
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
|
||||
virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
|
||||
|
||||
static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AesCtr(realm)); }
|
||||
|
|
|
@ -778,6 +778,7 @@ SupportedAlgorithmsMap supported_algorithms()
|
|||
|
||||
// https://w3c.github.io/webcrypto/#aes-ctr-registration
|
||||
define_an_algorithm<AesCtr>("importKey"_string, "AES-CTR"_string);
|
||||
define_an_algorithm<AesCtr>("exportKey"_string, "AES-CTR"_string);
|
||||
define_an_algorithm<AesCtr, AesDerivedKeyParams>("get key length"_string, "AES-CTR"_string);
|
||||
|
||||
// https://w3c.github.io/webcrypto/#hkdf
|
||||
|
|
Loading…
Reference in a new issue