@@ -1937,7 +1937,8 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
1937
1937
int slen = i2d_SSL_SESSION (sess, nullptr );
1938
1938
CHECK_GT (slen, 0 );
1939
1939
1940
- char * sbuf = Malloc (slen);
1940
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
1941
+ char * sbuf = static_cast <char *>(allocator->AllocateUninitialized (slen));
1941
1942
unsigned char * p = reinterpret_cast <unsigned char *>(sbuf);
1942
1943
i2d_SSL_SESSION (sess, &p);
1943
1944
args.GetReturnValue ().Set (Buffer::New (env, sbuf, slen).ToLocalChecked ());
@@ -3000,8 +3001,9 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
3000
3001
auth_tag_set_ = true ;
3001
3002
}
3002
3003
3004
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3003
3005
*out_len = len + EVP_CIPHER_CTX_block_size (ctx_);
3004
- *out = Malloc <unsigned char >( static_cast < size_t > (*out_len));
3006
+ *out = static_cast <unsigned char *>(allocator-> AllocateUninitialized (*out_len));
3005
3007
int r = EVP_CipherUpdate (ctx_,
3006
3008
*out,
3007
3009
out_len,
@@ -3041,7 +3043,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3041
3043
}
3042
3044
3043
3045
if (r != kSuccess ) {
3044
- free (out);
3046
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3047
+ allocator->Free (out, out_len);
3045
3048
if (r == kErrorState ) {
3046
3049
ThrowCryptoError (env, ERR_get_error (),
3047
3050
" Trying to add data in unsupported state" );
@@ -3079,8 +3082,9 @@ bool CipherBase::Final(unsigned char** out, int *out_len) {
3079
3082
3080
3083
const int mode = EVP_CIPHER_CTX_mode (ctx_);
3081
3084
3082
- *out = Malloc<unsigned char >(
3083
- static_cast <size_t >(EVP_CIPHER_CTX_block_size (ctx_)));
3085
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3086
+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (
3087
+ EVP_CIPHER_CTX_block_size (ctx_)));
3084
3088
3085
3089
// In CCM mode, final() only checks whether authentication failed in update().
3086
3090
// EVP_CipherFinal_ex must not be called and will fail.
@@ -3125,7 +3129,8 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3125
3129
bool r = cipher->Final (&out_value, &out_len);
3126
3130
3127
3131
if (out_len <= 0 || !r) {
3128
- free (out_value);
3132
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3133
+ allocator->Free (out_value, out_len);
3129
3134
out_value = nullptr ;
3130
3135
out_len = 0 ;
3131
3136
if (!r) {
@@ -3837,7 +3842,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
3837
3842
template <PublicKeyCipher::Operation operation,
3838
3843
PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
3839
3844
PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
3840
- bool PublicKeyCipher::Cipher (const char * key_pem,
3845
+ bool PublicKeyCipher::Cipher (Environment* env,
3846
+ const char * key_pem,
3841
3847
int key_pem_len,
3842
3848
const char * passphrase,
3843
3849
int padding,
@@ -3850,6 +3856,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
3850
3856
BIO* bp = nullptr ;
3851
3857
X509* x509 = nullptr ;
3852
3858
bool fatal = true ;
3859
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3853
3860
3854
3861
bp = BIO_new_mem_buf (const_cast <char *>(key_pem), key_pem_len);
3855
3862
if (bp == nullptr )
@@ -3902,7 +3909,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
3902
3909
if (EVP_PKEY_cipher (ctx, nullptr , out_len, data, len) <= 0 )
3903
3910
goto exit ;
3904
3911
3905
- *out = Malloc <unsigned char >( *out_len);
3912
+ *out = static_cast <unsigned char *>(allocator-> AllocateUninitialized ( *out_len) );
3906
3913
3907
3914
if (EVP_PKEY_cipher (ctx, *out, out_len, data, len) <= 0 )
3908
3915
goto exit ;
@@ -3947,6 +3954,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
3947
3954
ClearErrorOnReturn clear_error_on_return;
3948
3955
3949
3956
bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
3957
+ env,
3950
3958
kbuf,
3951
3959
klen,
3952
3960
args.Length () >= 3 && !args[2 ]->IsNull () ? *passphrase : nullptr ,
@@ -3957,7 +3965,8 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
3957
3965
&out_len);
3958
3966
3959
3967
if (out_len == 0 || !r) {
3960
- free (out_value);
3968
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3969
+ allocator->Free (out_value, out_len);
3961
3970
out_value = nullptr ;
3962
3971
out_len = 0 ;
3963
3972
if (!r) {
@@ -4162,7 +4171,8 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
4162
4171
const BIGNUM* pub_key;
4163
4172
DH_get0_key (diffieHellman->dh , &pub_key, nullptr );
4164
4173
size_t size = BN_num_bytes (pub_key);
4165
- char * data = Malloc (size);
4174
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4175
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
4166
4176
BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
4167
4177
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4168
4178
}
@@ -4181,7 +4191,8 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
4181
4191
if (num == nullptr ) return env->ThrowError (err_if_null);
4182
4192
4183
4193
size_t size = BN_num_bytes (num);
4184
- char * data = Malloc (size);
4194
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4195
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
4185
4196
BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
4186
4197
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4187
4198
}
@@ -4247,7 +4258,8 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4247
4258
}
4248
4259
4249
4260
int dataSize = DH_size (diffieHellman->dh );
4250
- char * data = Malloc (dataSize);
4261
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4262
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (dataSize));
4251
4263
4252
4264
int size = DH_compute_key (reinterpret_cast <unsigned char *>(data),
4253
4265
key,
@@ -4259,7 +4271,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4259
4271
4260
4272
checked = DH_check_pub_key (diffieHellman->dh , key, &checkResult);
4261
4273
BN_free (key);
4262
- free (data);
4274
+ allocator-> Free (data, dataSize );
4263
4275
4264
4276
if (!checked) {
4265
4277
return ThrowCryptoError (env, ERR_get_error (), " Invalid Key" );
@@ -4470,14 +4482,15 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4470
4482
}
4471
4483
4472
4484
// NOTE: field_size is in bits
4485
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4473
4486
int field_size = EC_GROUP_get_degree (ecdh->group_ );
4474
4487
size_t out_len = (field_size + 7 ) / 8 ;
4475
- char * out = node::Malloc ( out_len);
4488
+ char * out = static_cast < char *>(allocator-> AllocateUninitialized ( out_len) );
4476
4489
4477
4490
int r = ECDH_compute_key (out, out_len, pub, ecdh->key_ , nullptr );
4478
4491
EC_POINT_free (pub);
4479
4492
if (!r) {
4480
- free (out);
4493
+ allocator-> Free (out, out_len );
4481
4494
return env->ThrowError (" Failed to compute ECDH key" );
4482
4495
}
4483
4496
@@ -4507,11 +4520,13 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
4507
4520
if (size == 0 )
4508
4521
return env->ThrowError (" Failed to get public key length" );
4509
4522
4510
- unsigned char * out = node::Malloc<unsigned char >(size);
4523
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4524
+ unsigned char * out =
4525
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
4511
4526
4512
4527
int r = EC_POINT_point2oct (ecdh->group_ , pub, form, out, size, nullptr );
4513
4528
if (r != size) {
4514
- free (out);
4529
+ allocator-> Free (out, size );
4515
4530
return env->ThrowError (" Failed to get public key" );
4516
4531
}
4517
4532
@@ -4531,11 +4546,13 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4531
4546
if (b == nullptr )
4532
4547
return env->ThrowError (" Failed to get ECDH private key" );
4533
4548
4549
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4534
4550
int size = BN_num_bytes (b);
4535
- unsigned char * out = node::Malloc<unsigned char >(size);
4551
+ unsigned char * out =
4552
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
4536
4553
4537
4554
if (size != BN_bn2bin (b, out)) {
4538
- free (out);
4555
+ allocator-> Free (out, size );
4539
4556
return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
4540
4557
}
4541
4558
@@ -4666,7 +4683,8 @@ class PBKDF2Request : public AsyncWrap {
4666
4683
saltlen_(saltlen),
4667
4684
salt_(salt),
4668
4685
keylen_(keylen),
4669
- key_(node::Malloc(keylen)),
4686
+ key_(static_cast <char *>(env->isolate ()->GetArrayBufferAllocator()->
4687
+ AllocateUninitialized(keylen))),
4670
4688
iter_(iter) {
4671
4689
Wrap (object, this );
4672
4690
}
@@ -4680,7 +4698,7 @@ class PBKDF2Request : public AsyncWrap {
4680
4698
salt_ = nullptr ;
4681
4699
saltlen_ = 0 ;
4682
4700
4683
- free ( key_);
4701
+ env ()-> isolate ()-> GetArrayBufferAllocator ()-> Free ( key_, keylen_ );
4684
4702
key_ = nullptr ;
4685
4703
keylen_ = 0 ;
4686
4704
@@ -4870,9 +4888,10 @@ class RandomBytesRequest : public AsyncWrap {
4870
4888
}
4871
4889
4872
4890
inline void release () {
4891
+ size_t free_size = size_;
4873
4892
size_ = 0 ;
4874
4893
if (free_mode_ == FREE_DATA) {
4875
- free ( data_);
4894
+ env ()-> isolate ()-> GetArrayBufferAllocator ()-> Free ( data_, free_size );
4876
4895
data_ = nullptr ;
4877
4896
}
4878
4897
}
@@ -4989,7 +5008,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
4989
5008
4990
5009
Local<Object> obj = env->randombytes_constructor_template ()->
4991
5010
NewInstance (env->context ()).ToLocalChecked ();
4992
- char * data = node::Malloc (size);
5011
+ char * data = static_cast <char *>(
5012
+ env->isolate ()->GetArrayBufferAllocator ()->AllocateUninitialized (size));
4993
5013
std::unique_ptr<RandomBytesRequest> req (
4994
5014
new RandomBytesRequest (env,
4995
5015
obj,
@@ -5189,10 +5209,11 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5189
5209
}
5190
5210
5191
5211
5192
- char * ExportPublicKey (const char * data, int len, size_t * size) {
5212
+ char * ExportPublicKey (Environment* env, const char * data, int len, size_t * size) {
5193
5213
char * buf = nullptr ;
5194
5214
EVP_PKEY* pkey = nullptr ;
5195
5215
NETSCAPE_SPKI* spki = nullptr ;
5216
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5196
5217
5197
5218
BIO* bio = BIO_new (BIO_s_mem ());
5198
5219
if (bio == nullptr )
@@ -5213,7 +5234,7 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
5213
5234
BIO_get_mem_ptr (bio, &ptr);
5214
5235
5215
5236
*size = ptr->length ;
5216
- buf = Malloc ( *size);
5237
+ buf = static_cast < char *>(allocator-> AllocateUninitialized ( *size) );
5217
5238
memcpy (buf, ptr->data , *size);
5218
5239
5219
5240
exit :
@@ -5241,7 +5262,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
5241
5262
CHECK_NE (data, nullptr );
5242
5263
5243
5264
size_t pkey_size;
5244
- char * pkey = ExportPublicKey (data, length, &pkey_size);
5265
+ char * pkey = ExportPublicKey (env, data, length, &pkey_size);
5245
5266
if (pkey == nullptr )
5246
5267
return args.GetReturnValue ().SetEmptyString ();
5247
5268
0 commit comments