Skip to content

Commit cddbdf5

Browse files
committed
LibWeb: Implement skeleton of RSA-OAEP decrypt for SubtleCrypto
The actual Crypto algorithm part isn't implemented yet, so we just copy the ciphertext and claim that's the plaintext :^)
1 parent 29b68a1 commit cddbdf5

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,34 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> RSAOAEP::encrypt(Algorith
373373
return JS::ArrayBuffer::create(realm, move(ciphertext));
374374
}
375375

376+
// https://w3c.github.io/webcrypto/#rsa-oaep-operations
377+
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> RSAOAEP::decrypt(AlgorithmParams const& params, JS::NonnullGCPtr<CryptoKey> key, AK::ByteBuffer const& ciphertext)
378+
{
379+
auto& realm = m_realm;
380+
auto& vm = realm.vm();
381+
auto const& normalized_algorithm = static_cast<RsaOaepParams const&>(params);
382+
383+
// 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
384+
if (key->type() != Bindings::KeyType::Private)
385+
return WebIDL::InvalidAccessError::create(realm, "Key is not a private key"_fly_string);
386+
387+
// 2. Let label be the contents of the label member of normalizedAlgorithm or the empty octet string if the label member of normalizedAlgorithm is not present.
388+
[[maybe_unused]] auto const& label = normalized_algorithm.label;
389+
390+
// 3. Perform the decryption operation defined in Section 7.1 of [RFC3447] with the key represented by key as the recipient's RSA private key,
391+
// the contents of ciphertext as the ciphertext to be decrypted, C, and label as the label, L, and with the hash function specified by the hash attribute
392+
// of the [[algorithm]] internal slot of key as the Hash option and MGF1 (defined in Section B.2.1 of [RFC3447]) as the MGF option.
393+
394+
// 4. If performing the operation results in an error, then throw an OperationError.
395+
396+
// 5. Let plaintext the value M that results from performing the operation.
397+
// FIXME: Actually decrypt the data
398+
auto plaintext = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(ciphertext));
399+
400+
// 6. Return the result of creating an ArrayBuffer containing plaintext.
401+
return JS::ArrayBuffer::create(realm, move(plaintext));
402+
}
403+
376404
// https://w3c.github.io/webcrypto/#rsa-oaep-operations
377405
WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> RSAOAEP::generate_key(AlgorithmParams const& params, bool extractable, Vector<Bindings::KeyUsage> const& key_usages)
378406
{

Userland/Libraries/LibWeb/Crypto/CryptoAlgorithms.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class AlgorithmMethods {
165165
class RSAOAEP : public AlgorithmMethods {
166166
public:
167167
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
168+
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
168169

169170
virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
170171

Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ SupportedAlgorithmsMap supported_algorithms()
516516
define_an_algorithm<RSAOAEP>("exportKey"_string, "RSA-OAEP"_string);
517517
define_an_algorithm<RSAOAEP, RsaHashedImportParams>("importKey"_string, "RSA-OAEP"_string);
518518
define_an_algorithm<RSAOAEP, RsaOaepParams>("encrypt"_string, "RSA-OAEP"_string);
519-
// FIXME: decrypt
519+
define_an_algorithm<RSAOAEP, RsaOaepParams>("decrypt"_string, "RSA-OAEP"_string);
520520

521521
return internal_object;
522522
}

0 commit comments

Comments
 (0)