@@ -1890,7 +1890,8 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
1890
1890
int slen = i2d_SSL_SESSION (sess, nullptr );
1891
1891
CHECK_GT (slen, 0 );
1892
1892
1893
- char * sbuf = Malloc (slen);
1893
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
1894
+ char * sbuf = static_cast <char *>(allocator->AllocateUninitialized (slen));
1894
1895
unsigned char * p = reinterpret_cast <unsigned char *>(sbuf);
1895
1896
i2d_SSL_SESSION (sess, &p);
1896
1897
args.GetReturnValue ().Set (Buffer::New (env, sbuf, slen).ToLocalChecked ());
@@ -3011,7 +3012,8 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
3011
3012
return kErrorState ;
3012
3013
}
3013
3014
3014
- *out = Malloc<unsigned char >(buff_len);
3015
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3016
+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (buff_len));
3015
3017
int r = EVP_CipherUpdate (ctx_.get (),
3016
3018
*out,
3017
3019
out_len,
@@ -3053,7 +3055,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3053
3055
}
3054
3056
3055
3057
if (r != kSuccess ) {
3056
- free (out);
3058
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3059
+ allocator->Free (out, out_len);
3057
3060
if (r == kErrorState ) {
3058
3061
ThrowCryptoError (env, ERR_get_error (),
3059
3062
" Trying to add data in unsupported state" );
@@ -3091,8 +3094,9 @@ bool CipherBase::Final(unsigned char** out, int* out_len) {
3091
3094
3092
3095
const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
3093
3096
3094
- *out = Malloc<unsigned char >(
3095
- static_cast <size_t >(EVP_CIPHER_CTX_block_size (ctx_.get ())));
3097
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3098
+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (
3099
+ EVP_CIPHER_CTX_block_size (ctx_.get ())));
3096
3100
3097
3101
// In CCM mode, final() only checks whether authentication failed in update().
3098
3102
// EVP_CipherFinal_ex must not be called and will fail.
@@ -3135,7 +3139,8 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3135
3139
bool r = cipher->Final (&out_value, &out_len);
3136
3140
3137
3141
if (out_len <= 0 || !r) {
3138
- free (out_value);
3142
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3143
+ allocator->Free (out_value, out_len);
3139
3144
out_value = nullptr ;
3140
3145
out_len = 0 ;
3141
3146
if (!r) {
@@ -3781,7 +3786,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
3781
3786
template <PublicKeyCipher::Operation operation,
3782
3787
PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
3783
3788
PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
3784
- bool PublicKeyCipher::Cipher (const char * key_pem,
3789
+ bool PublicKeyCipher::Cipher (Environment* env,
3790
+ const char * key_pem,
3785
3791
int key_pem_len,
3786
3792
const char * passphrase,
3787
3793
int padding,
@@ -3790,6 +3796,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
3790
3796
unsigned char ** out,
3791
3797
size_t * out_len) {
3792
3798
EVPKeyPointer pkey;
3799
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3793
3800
3794
3801
BIOPointer bp (BIO_new_mem_buf (const_cast <char *>(key_pem), key_pem_len));
3795
3802
if (!bp)
@@ -3837,7 +3844,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
3837
3844
if (EVP_PKEY_cipher (ctx.get (), nullptr , out_len, data, len) <= 0 )
3838
3845
return false ;
3839
3846
3840
- *out = Malloc <unsigned char >( *out_len);
3847
+ *out = static_cast <unsigned char *>(allocator-> AllocateUninitialized ( *out_len) );
3841
3848
3842
3849
if (EVP_PKEY_cipher (ctx.get (), *out, out_len, data, len) <= 0 )
3843
3850
return false ;
@@ -3870,6 +3877,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
3870
3877
ClearErrorOnReturn clear_error_on_return;
3871
3878
3872
3879
bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
3880
+ env,
3873
3881
kbuf,
3874
3882
klen,
3875
3883
args.Length () >= 3 && !args[2 ]->IsNull () ? *passphrase : nullptr ,
@@ -3880,7 +3888,8 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
3880
3888
&out_len);
3881
3889
3882
3890
if (out_len == 0 || !r) {
3883
- free (out_value);
3891
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3892
+ allocator->Free (out_value, out_len);
3884
3893
out_value = nullptr ;
3885
3894
out_len = 0 ;
3886
3895
if (!r) {
@@ -4085,7 +4094,8 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
4085
4094
const BIGNUM* pub_key;
4086
4095
DH_get0_key (diffieHellman->dh_ .get (), &pub_key, nullptr );
4087
4096
size_t size = BN_num_bytes (pub_key);
4088
- char * data = Malloc (size);
4097
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4098
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
4089
4099
BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
4090
4100
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4091
4101
}
@@ -4104,7 +4114,8 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
4104
4114
if (num == nullptr ) return env->ThrowError (err_if_null);
4105
4115
4106
4116
size_t size = BN_num_bytes (num);
4107
- char * data = Malloc (size);
4117
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4118
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
4108
4119
BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
4109
4120
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4110
4121
}
@@ -4168,7 +4179,8 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4168
4179
Buffer::Length (args[0 ]),
4169
4180
0 ));
4170
4181
4171
- MallocedBuffer<char > data (DH_size (diffieHellman->dh_ .get ()));
4182
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4183
+ MallocedBuffer<char > data (DH_size (diffieHellman->dh_ .get ()), allocator);
4172
4184
4173
4185
int size = DH_compute_key (reinterpret_cast <unsigned char *>(data.data ),
4174
4186
key.get (),
@@ -4388,13 +4400,14 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4388
4400
}
4389
4401
4390
4402
// NOTE: field_size is in bits
4403
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4391
4404
int field_size = EC_GROUP_get_degree (ecdh->group_ );
4392
4405
size_t out_len = (field_size + 7 ) / 8 ;
4393
- char * out = node::Malloc ( out_len);
4406
+ char * out = static_cast < char *>(allocator-> AllocateUninitialized ( out_len) );
4394
4407
4395
4408
int r = ECDH_compute_key (out, out_len, pub.get (), ecdh->key_ .get (), nullptr );
4396
4409
if (!r) {
4397
- free (out);
4410
+ allocator-> Free (out, out_len );
4398
4411
return env->ThrowError (" Failed to compute ECDH key" );
4399
4412
}
4400
4413
@@ -4424,11 +4437,13 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
4424
4437
if (size == 0 )
4425
4438
return env->ThrowError (" Failed to get public key length" );
4426
4439
4427
- unsigned char * out = node::Malloc<unsigned char >(size);
4440
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4441
+ unsigned char * out =
4442
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
4428
4443
4429
4444
int r = EC_POINT_point2oct (ecdh->group_ , pub, form, out, size, nullptr );
4430
4445
if (r != size) {
4431
- free (out);
4446
+ allocator-> Free (out, size );
4432
4447
return env->ThrowError (" Failed to get public key" );
4433
4448
}
4434
4449
@@ -4448,11 +4463,13 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4448
4463
if (b == nullptr )
4449
4464
return env->ThrowError (" Failed to get ECDH private key" );
4450
4465
4466
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4451
4467
int size = BN_num_bytes (b);
4452
- unsigned char * out = node::Malloc<unsigned char >(size);
4468
+ unsigned char * out =
4469
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
4453
4470
4454
4471
if (size != BN_bn2bin (b, out)) {
4455
- free (out);
4472
+ allocator-> Free (out, size );
4456
4473
return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
4457
4474
}
4458
4475
@@ -4572,7 +4589,7 @@ class PBKDF2Request : public AsyncWrap, public ThreadPoolWork {
4572
4589
success_(false ),
4573
4590
pass_(std::move(pass)),
4574
4591
salt_(std::move(salt)),
4575
- key_(keylen),
4592
+ key_(keylen, env-> isolate ()->GetArrayBufferAllocator() ),
4576
4593
iteration_count_(iteration_count) {
4577
4594
}
4578
4595
@@ -4634,6 +4651,7 @@ void PBKDF2Request::AfterThreadPoolWork(int status) {
4634
4651
4635
4652
void PBKDF2 (const FunctionCallbackInfo<Value>& args) {
4636
4653
Environment* env = Environment::GetCurrent (args);
4654
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4637
4655
4638
4656
const EVP_MD* digest = nullptr ;
4639
4657
int keylen = -1 ;
@@ -4642,12 +4660,12 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4642
4660
4643
4661
int passlen = Buffer::Length (args[0 ]);
4644
4662
4645
- MallocedBuffer<char > pass (passlen);
4663
+ MallocedBuffer<char > pass (passlen, allocator );
4646
4664
memcpy (pass.data , Buffer::Data (args[0 ]), passlen);
4647
4665
4648
4666
int saltlen = Buffer::Length (args[1 ]);
4649
4667
4650
- MallocedBuffer<char > salt (saltlen);
4668
+ MallocedBuffer<char > salt (saltlen, allocator );
4651
4669
memcpy (salt.data , Buffer::Data (args[1 ]), saltlen);
4652
4670
4653
4671
iteration_count = args[2 ]->Int32Value (env->context ()).FromJust ();
@@ -4724,9 +4742,10 @@ class RandomBytesRequest : public AsyncWrap, public ThreadPoolWork {
4724
4742
}
4725
4743
4726
4744
inline void release () {
4745
+ size_t free_size = size_;
4727
4746
size_ = 0 ;
4728
4747
if (free_mode_ == FREE_DATA) {
4729
- free ( data_);
4748
+ env ()-> isolate ()-> GetArrayBufferAllocator ()-> Free ( data_, free_size );
4730
4749
data_ = nullptr ;
4731
4750
}
4732
4751
}
@@ -4840,7 +4859,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
4840
4859
4841
4860
Local<Object> obj = env->randombytes_constructor_template ()->
4842
4861
NewInstance (env->context ()).ToLocalChecked ();
4843
- char * data = node::Malloc (size);
4862
+ char * data = static_cast <char *>(
4863
+ env->isolate ()->GetArrayBufferAllocator ()->AllocateUninitialized (size));
4844
4864
std::unique_ptr<RandomBytesRequest> req (
4845
4865
new RandomBytesRequest (env,
4846
4866
obj,
@@ -5015,8 +5035,9 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5015
5035
}
5016
5036
5017
5037
5018
- char * ExportPublicKey (const char * data, int len, size_t * size) {
5038
+ char * ExportPublicKey (Environment* env, const char * data, int len, size_t * size) {
5019
5039
char * buf = nullptr ;
5040
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5020
5041
5021
5042
BIOPointer bio (BIO_new (BIO_s_mem ()));
5022
5043
if (!bio)
@@ -5037,7 +5058,7 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
5037
5058
BIO_get_mem_ptr (bio.get (), &ptr);
5038
5059
5039
5060
*size = ptr->length ;
5040
- buf = Malloc ( *size);
5061
+ buf = static_cast < char *>(allocator-> AllocateUninitialized ( *size) );
5041
5062
memcpy (buf, ptr->data , *size);
5042
5063
5043
5064
return buf;
@@ -5055,7 +5076,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
5055
5076
CHECK_NE (data, nullptr );
5056
5077
5057
5078
size_t pkey_size;
5058
- char * pkey = ExportPublicKey (data, length, &pkey_size);
5079
+ char * pkey = ExportPublicKey (env, data, length, &pkey_size);
5059
5080
if (pkey == nullptr )
5060
5081
return args.GetReturnValue ().SetEmptyString ();
5061
5082
0 commit comments