Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit b6a9b91

Browse files
committed
Continue of "Allocate memory of Buffer with V8's allocator"
Patches few more places that should use V8's allocator. Todo: Merge the 2 commits when rebasing.
1 parent dc8fe9d commit b6a9b91

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

src/node_crypto.cc

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,7 +1804,8 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
18041804
int slen = i2d_SSL_SESSION(sess, nullptr);
18051805
CHECK_GT(slen, 0);
18061806

1807-
char* sbuf = Malloc(slen);
1807+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
1808+
char* sbuf = static_cast<char*>(allocator->AllocateUninitialized(slen));
18081809
unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
18091810
i2d_SSL_SESSION(sess, &p);
18101811
args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked());
@@ -3549,8 +3550,9 @@ bool CipherBase::Update(const char* data,
35493550
auth_tag_len_ = 0;
35503551
}
35513552

3553+
auto* allocator = env()->isolate()->GetArrayBufferAllocator();
35523554
*out_len = len + EVP_CIPHER_CTX_block_size(&ctx_);
3553-
*out = Malloc<unsigned char>(static_cast<size_t>(*out_len));
3555+
*out = static_cast<unsigned char*>(allocator->AllocateUninitialized(*out_len));
35543556
return EVP_CipherUpdate(&ctx_,
35553557
*out,
35563558
out_len,
@@ -3584,7 +3586,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
35843586
}
35853587

35863588
if (!r) {
3587-
free(out);
3589+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
3590+
allocator->Free(out, out_len);
35883591
return ThrowCryptoError(env,
35893592
ERR_get_error(),
35903593
"Trying to add data in unsupported state");
@@ -3620,8 +3623,9 @@ bool CipherBase::Final(unsigned char** out, int *out_len) {
36203623
if (!initialised_)
36213624
return false;
36223625

3623-
*out = Malloc<unsigned char>(
3624-
static_cast<size_t>(EVP_CIPHER_CTX_block_size(&ctx_)));
3626+
auto* allocator = env()->isolate()->GetArrayBufferAllocator();
3627+
*out = static_cast<unsigned char*>(allocator->AllocateUninitialized(
3628+
EVP_CIPHER_CTX_block_size(&ctx_)));
36253629
int r = EVP_CipherFinal_ex(&ctx_, *out, out_len);
36263630

36273631
if (r == 1 && kind_ == kCipher && IsAuthenticatedMode()) {
@@ -3653,7 +3657,8 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
36533657
bool r = cipher->Final(&out_value, &out_len);
36543658

36553659
if (out_len <= 0 || !r) {
3656-
free(out_value);
3660+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
3661+
allocator->Free(out_value, out_len);
36573662
out_value = nullptr;
36583663
out_len = 0;
36593664
if (!r) {
@@ -4456,7 +4461,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
44564461
template <PublicKeyCipher::Operation operation,
44574462
PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
44584463
PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
4459-
bool PublicKeyCipher::Cipher(const char* key_pem,
4464+
bool PublicKeyCipher::Cipher(Environment* env,
4465+
const char* key_pem,
44604466
int key_pem_len,
44614467
const char* passphrase,
44624468
int padding,
@@ -4469,6 +4475,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
44694475
BIO* bp = nullptr;
44704476
X509* x509 = nullptr;
44714477
bool fatal = true;
4478+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
44724479

44734480
bp = BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len);
44744481
if (bp == nullptr)
@@ -4521,7 +4528,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
45214528
if (EVP_PKEY_cipher(ctx, nullptr, out_len, data, len) <= 0)
45224529
goto exit;
45234530

4524-
*out = Malloc<unsigned char>(*out_len);
4531+
*out = static_cast<unsigned char*>(allocator->AllocateUninitialized(*out_len));
45254532

45264533
if (EVP_PKEY_cipher(ctx, *out, out_len, data, len) <= 0)
45274534
goto exit;
@@ -4566,6 +4573,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
45664573
ClearErrorOnReturn clear_error_on_return;
45674574

45684575
bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
4576+
env,
45694577
kbuf,
45704578
klen,
45714579
args.Length() >= 3 && !args[2]->IsNull() ? *passphrase : nullptr,
@@ -4576,7 +4584,8 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
45764584
&out_len);
45774585

45784586
if (out_len == 0 || !r) {
4579-
free(out_value);
4587+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
4588+
allocator->Free(out_value, out_len);
45804589
out_value = nullptr;
45814590
out_len = 0;
45824591
if (!r) {
@@ -4763,7 +4772,8 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
47634772
}
47644773

47654774
size_t size = BN_num_bytes(diffieHellman->dh->pub_key);
4766-
char* data = Malloc(size);
4775+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
4776+
char* data = static_cast<char*>(allocator->AllocateUninitialized(size));
47674777
BN_bn2bin(diffieHellman->dh->pub_key, reinterpret_cast<unsigned char*>(data));
47684778
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
47694779
}
@@ -4781,7 +4791,8 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
47814791
if (num == nullptr) return env->ThrowError(err_if_null);
47824792

47834793
size_t size = BN_num_bytes(num);
4784-
char* data = Malloc(size);
4794+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
4795+
char* data = static_cast<char*>(allocator->AllocateUninitialized(size));
47854796
BN_bn2bin(num, reinterpret_cast<unsigned char*>(data));
47864797
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
47874798
}
@@ -4832,7 +4843,8 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
48324843
}
48334844

48344845
int dataSize = DH_size(diffieHellman->dh);
4835-
char* data = Malloc(dataSize);
4846+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
4847+
char* data = static_cast<char*>(allocator->AllocateUninitialized(dataSize));
48364848

48374849
int size = DH_compute_key(reinterpret_cast<unsigned char*>(data),
48384850
key,
@@ -4844,7 +4856,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
48444856

48454857
checked = DH_check_pub_key(diffieHellman->dh, key, &checkResult);
48464858
BN_free(key);
4847-
free(data);
4859+
allocator->Free(data, dataSize);
48484860

48494861
if (!checked) {
48504862
return ThrowCryptoError(env, ERR_get_error(), "Invalid Key");
@@ -5085,7 +5097,7 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
50855097

50865098
int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr);
50875099
if (r != size) {
5088-
free(out);
5100+
allocator->Free(out, size);
50895101
return env->ThrowError("Failed to get public key");
50905102
}
50915103

@@ -5111,7 +5123,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
51115123
static_cast<unsigned char*>(allocator->AllocateUninitialized(size));
51125124

51135125
if (size != BN_bn2bin(b, out)) {
5114-
free(out);
5126+
allocator->Free(out, size);
51155127
return env->ThrowError("Failed to convert ECDH private key to Buffer");
51165128
}
51175129

@@ -5839,10 +5851,11 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
58395851
}
58405852

58415853

5842-
char* ExportPublicKey(const char* data, int len, size_t* size) {
5854+
char* ExportPublicKey(Environment* env, const char* data, int len, size_t* size) {
58435855
char* buf = nullptr;
58445856
EVP_PKEY* pkey = nullptr;
58455857
NETSCAPE_SPKI* spki = nullptr;
5858+
auto* allocator = env->isolate()->GetArrayBufferAllocator();
58465859

58475860
BIO* bio = BIO_new(BIO_s_mem());
58485861
if (bio == nullptr)
@@ -5863,7 +5876,7 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
58635876
BIO_get_mem_ptr(bio, &ptr);
58645877

58655878
*size = ptr->length;
5866-
buf = Malloc(*size);
5879+
buf = static_cast<char*>(allocator->AllocateUninitialized(*size));
58675880
memcpy(buf, ptr->data, *size);
58685881

58695882
exit:
@@ -5896,7 +5909,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
58965909
CHECK_NE(data, nullptr);
58975910

58985911
size_t pkey_size;
5899-
char* pkey = ExportPublicKey(data, length, &pkey_size);
5912+
char* pkey = ExportPublicKey(env, data, length, &pkey_size);
59005913
if (pkey == nullptr)
59015914
return args.GetReturnValue().SetEmptyString();
59025915

src/node_crypto.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ class PublicKeyCipher {
636636
template <Operation operation,
637637
EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
638638
EVP_PKEY_cipher_t EVP_PKEY_cipher>
639-
static bool Cipher(const char* key_pem,
639+
static bool Cipher(Environment* env,
640+
const char* key_pem,
640641
int key_pem_len,
641642
const char* passphrase,
642643
int padding,

0 commit comments

Comments
 (0)