Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: remove usage of AllocatedBuffer from crypto_common.cc #40213

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/crypto/crypto_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -562,11 +564,17 @@ MaybeLocal<Object> 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<unsigned char*>(buffer.data());
i2d_RSA_PUBKEY(rsa.get(), &serialized);
return buffer.ToBuffer();
std::unique_ptr<BackingStore> bs;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(env->isolate(), size);
}

unsigned char* serialized = reinterpret_cast<unsigned char*>(bs->Data());
CHECK_GE(i2d_RSA_PUBKEY(rsa.get(), &serialized), 0);

Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Object>());
}

MaybeLocal<Value> GetExponentString(
Expand Down Expand Up @@ -600,11 +608,17 @@ MaybeLocal<Value> GetModulusString(
MaybeLocal<Object> GetRawDERCertificate(Environment* env, X509* cert) {
int size = i2d_X509(cert, nullptr);

AllocatedBuffer buffer = AllocatedBuffer::AllocateManaged(env, size);
unsigned char* serialized =
reinterpret_cast<unsigned char*>(buffer.data());
i2d_X509(cert, &serialized);
return buffer.ToBuffer();
std::unique_ptr<BackingStore> bs;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(env->isolate(), size);
}

unsigned char* serialized = reinterpret_cast<unsigned char*>(bs->Data());
CHECK_GE(i2d_X509(cert, &serialized), 0);

Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Object>());
}

MaybeLocal<Value> GetSerialNumber(Environment* env, X509* cert) {
Expand Down Expand Up @@ -878,18 +892,26 @@ MaybeLocal<Object> ECPointToBuffer(Environment* env,
if (error != nullptr) *error = "Failed to get public key length";
return MaybeLocal<Object>();
}
AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, len);

std::unique_ptr<BackingStore> bs;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(env->isolate(), len);
}

len = EC_POINT_point2oct(group,
point,
form,
reinterpret_cast<unsigned char*>(buf.data()),
buf.size(),
reinterpret_cast<unsigned char*>(bs->Data()),
bs->ByteLength(),
nullptr);
if (len == 0) {
if (error != nullptr) *error = "Failed to get public key";
return MaybeLocal<Object>();
}
return buf.ToBuffer();

Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Object>());
}

MaybeLocal<Value> GetPeerCert(
Expand Down