From 942b36d00b74def734bc8127b1a3a903850af0cb Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 25 Sep 2021 19:01:16 +0530 Subject: [PATCH] src: remove usage of AllocatedBuffer from crypto_common.cc Signed-off-by: Darshan Sen --- src/crypto/crypto_common.cc | 50 ++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/crypto/crypto_common.cc b/src/crypto/crypto_common.cc index f4b7bd3ad8548a..cc03fddd464b09 100644 --- a/src/crypto/crypto_common.cc +++ b/src/crypto/crypto_common.cc @@ -26,7 +26,9 @@ namespace node { using v8::Array; +using v8::ArrayBuffer; using v8::ArrayBufferView; +using v8::BackingStore; using v8::Context; using v8::EscapableHandleScope; using v8::Integer; @@ -562,11 +564,17 @@ MaybeLocal GetPubKey(Environment* env, const RSAPointer& rsa) { int size = i2d_RSA_PUBKEY(rsa.get(), nullptr); CHECK_GE(size, 0); - AllocatedBuffer buffer = AllocatedBuffer::AllocateManaged(env, size); - unsigned char* serialized = - reinterpret_cast(buffer.data()); - i2d_RSA_PUBKEY(rsa.get(), &serialized); - return buffer.ToBuffer(); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), size); + } + + unsigned char* serialized = reinterpret_cast(bs->Data()); + CHECK_GE(i2d_RSA_PUBKEY(rsa.get(), &serialized), 0); + + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local()); } MaybeLocal GetExponentString( @@ -600,11 +608,17 @@ MaybeLocal GetModulusString( MaybeLocal GetRawDERCertificate(Environment* env, X509* cert) { int size = i2d_X509(cert, nullptr); - AllocatedBuffer buffer = AllocatedBuffer::AllocateManaged(env, size); - unsigned char* serialized = - reinterpret_cast(buffer.data()); - i2d_X509(cert, &serialized); - return buffer.ToBuffer(); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), size); + } + + unsigned char* serialized = reinterpret_cast(bs->Data()); + CHECK_GE(i2d_X509(cert, &serialized), 0); + + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local()); } MaybeLocal GetSerialNumber(Environment* env, X509* cert) { @@ -878,18 +892,26 @@ MaybeLocal ECPointToBuffer(Environment* env, if (error != nullptr) *error = "Failed to get public key length"; return MaybeLocal(); } - AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, len); + + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), len); + } + len = EC_POINT_point2oct(group, point, form, - reinterpret_cast(buf.data()), - buf.size(), + reinterpret_cast(bs->Data()), + bs->ByteLength(), nullptr); if (len == 0) { if (error != nullptr) *error = "Failed to get public key"; return MaybeLocal(); } - return buf.ToBuffer(); + + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local()); } MaybeLocal GetPeerCert(