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.
This commit is contained in:
Andrew Kaster 2024-03-13 21:15:14 -06:00 committed by Andrew Kaster
parent 0a3d27c41d
commit 2599142214
Notes: sideshowbarker 2024-07-17 05:23:40 +09:00
2 changed files with 6 additions and 5 deletions

View file

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

View file

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