Преглед на файлове

LibWeb: Export UnsignedBigInts into Uint8Arrays without losing bytes

The behavior of Crypto::UnsignedBigInt::export_data unexpectedly
does not actually remove leading zero bytes when the corresponding
parameter is passed. The caller must manually adjust for the location
of the zero bytes.
Andrew Kaster преди 1 година
родител
ревизия
2599142214
променени са 2 файла, в които са добавени 6 реда и са изтрити 5 реда
  1. 2 2
      Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-generateKey.txt
  2. 4 3
      Userland/Libraries/LibWeb/Crypto/KeyAlgorithms.cpp

+ 2 - 2
Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-generateKey.txt

@@ -1,11 +1,11 @@
 generateKey with RSA-OAEP algorithm
 publicKey: [object CryptoKey]
-publicKey algorithm: {"name":"RSA-OAEP","modulusLength":512,"publicExponent":{"0":0,"1":1,"2":0},"hash":"SHA-256"}
+publicKey algorithm: {"name":"RSA-OAEP","modulusLength":512,"publicExponent":{"0":1,"1":0,"2":1},"hash":"SHA-256"}
 publicKey type: public
 publicKey extractable: true
 publicKey usages: encrypt,wrapKey
 privateKey: [object CryptoKey]
-privateKey algorithm: {"name":"RSA-OAEP","modulusLength":512,"publicExponent":{"0":0,"1":1,"2":0},"hash":"SHA-256"}
+privateKey algorithm: {"name":"RSA-OAEP","modulusLength":512,"publicExponent":{"0":1,"1":0,"2":1},"hash":"SHA-256"}
 privateKey type: private
 privateKey extractable: true
 privateKey usages: decrypt,unwrapKey

+ 4 - 3
Userland/Libraries/LibWeb/Crypto/KeyAlgorithms.cpp

@@ -90,15 +90,16 @@ WebIDL::ExceptionOr<void> RsaKeyAlgorithm::set_public_exponent(::Crypto::Unsigne
 
     auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::create_uninitialized(exponent.trimmed_byte_length()));
 
-    bool const strip_leading_zeroes = true;
-    auto data_size = exponent.export_data(bytes.span(), strip_leading_zeroes);
+    bool const remove_leading_zeroes = true;
+    auto data_size = exponent.export_data(bytes.span(), remove_leading_zeroes);
+    auto data_slice = bytes.bytes().slice(bytes.size() - data_size, data_size);
 
     // The BigInteger typedef from the WebCrypto spec requires the bytes in the Uint8Array be ordered in Big Endian
 
     Vector<u8, 32> byte_swapped_data;
     byte_swapped_data.ensure_capacity(data_size);
     for (size_t i = 0; i < data_size; ++i)
-        byte_swapped_data.append(bytes[data_size - i - 1]);
+        byte_swapped_data.append(data_slice[data_size - i - 1]);
 
     m_public_exponent = TRY(JS::Uint8Array::create(realm, byte_swapped_data.size()));
     m_public_exponent->viewed_array_buffer()->buffer().overwrite(0, byte_swapped_data.data(), byte_swapped_data.size());