@@ -2114,7 +2114,8 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
2114
2114
int slen = i2d_SSL_SESSION (sess, nullptr );
2115
2115
CHECK_GT (slen, 0 );
2116
2116
2117
- char * sbuf = Malloc (slen);
2117
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
2118
+ char * sbuf = static_cast <char *>(allocator->AllocateUninitialized (slen));
2118
2119
unsigned char * p = reinterpret_cast <unsigned char *>(sbuf);
2119
2120
i2d_SSL_SESSION (sess, &p);
2120
2121
args.GetReturnValue ().Set (Buffer::New (env, sbuf, slen).ToLocalChecked ());
@@ -3894,8 +3895,9 @@ bool CipherBase::Update(const char* data,
3894
3895
auth_tag_len_ = 0 ;
3895
3896
}
3896
3897
3898
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3897
3899
*out_len = len + EVP_CIPHER_CTX_block_size (ctx_);
3898
- *out = Malloc <unsigned char >( static_cast < size_t > (*out_len));
3900
+ *out = static_cast <unsigned char *>(allocator-> AllocateUninitialized (*out_len));
3899
3901
return EVP_CipherUpdate (ctx_,
3900
3902
*out,
3901
3903
out_len,
@@ -3929,7 +3931,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3929
3931
}
3930
3932
3931
3933
if (!r) {
3932
- free (out);
3934
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3935
+ allocator->Free (out, out_len);
3933
3936
return ThrowCryptoError (env,
3934
3937
ERR_get_error (),
3935
3938
" Trying to add data in unsupported state" );
@@ -3965,8 +3968,9 @@ bool CipherBase::Final(unsigned char** out, int *out_len) {
3965
3968
if (ctx_ == nullptr )
3966
3969
return false ;
3967
3970
3968
- *out = Malloc<unsigned char >(
3969
- static_cast <size_t >(EVP_CIPHER_CTX_block_size (ctx_)));
3971
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3972
+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (
3973
+ EVP_CIPHER_CTX_block_size (ctx_)));
3970
3974
int r = EVP_CipherFinal_ex (ctx_, *out, out_len);
3971
3975
3972
3976
if (r == 1 && kind_ == kCipher && IsAuthenticatedMode ()) {
@@ -3998,7 +4002,8 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3998
4002
bool r = cipher->Final (&out_value, &out_len);
3999
4003
4000
4004
if (out_len <= 0 || !r) {
4001
- free (out_value);
4005
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4006
+ allocator->Free (out_value, out_len);
4002
4007
out_value = nullptr ;
4003
4008
out_len = 0 ;
4004
4009
if (!r) {
@@ -4712,7 +4717,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
4712
4717
template <PublicKeyCipher::Operation operation,
4713
4718
PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
4714
4719
PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
4715
- bool PublicKeyCipher::Cipher (const char * key_pem,
4720
+ bool PublicKeyCipher::Cipher (Environment* env,
4721
+ const char * key_pem,
4716
4722
int key_pem_len,
4717
4723
const char * passphrase,
4718
4724
int padding,
@@ -4725,6 +4731,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
4725
4731
BIO* bp = nullptr ;
4726
4732
X509* x509 = nullptr ;
4727
4733
bool fatal = true ;
4734
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4728
4735
4729
4736
bp = BIO_new_mem_buf (const_cast <char *>(key_pem), key_pem_len);
4730
4737
if (bp == nullptr )
@@ -4777,7 +4784,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
4777
4784
if (EVP_PKEY_cipher (ctx, nullptr , out_len, data, len) <= 0 )
4778
4785
goto exit ;
4779
4786
4780
- *out = Malloc <unsigned char >( *out_len);
4787
+ *out = static_cast <unsigned char *>(allocator-> AllocateUninitialized ( *out_len) );
4781
4788
4782
4789
if (EVP_PKEY_cipher (ctx, *out, out_len, data, len) <= 0 )
4783
4790
goto exit ;
@@ -4822,6 +4829,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
4822
4829
ClearErrorOnReturn clear_error_on_return;
4823
4830
4824
4831
bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
4832
+ env,
4825
4833
kbuf,
4826
4834
klen,
4827
4835
args.Length () >= 3 && !args[2 ]->IsNull () ? *passphrase : nullptr ,
@@ -4832,7 +4840,8 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
4832
4840
&out_len);
4833
4841
4834
4842
if (out_len == 0 || !r) {
4835
- free (out_value);
4843
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4844
+ allocator->Free (out_value, out_len);
4836
4845
out_value = nullptr ;
4837
4846
out_len = 0 ;
4838
4847
if (!r) {
@@ -5037,7 +5046,8 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
5037
5046
const BIGNUM* pub_key;
5038
5047
DH_get0_key (diffieHellman->dh , &pub_key, nullptr );
5039
5048
size_t size = BN_num_bytes (pub_key);
5040
- char * data = Malloc (size);
5049
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5050
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
5041
5051
BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
5042
5052
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
5043
5053
}
@@ -5056,7 +5066,8 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
5056
5066
if (num == nullptr ) return env->ThrowError (err_if_null);
5057
5067
5058
5068
size_t size = BN_num_bytes (num);
5059
- char * data = Malloc (size);
5069
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5070
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
5060
5071
BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
5061
5072
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
5062
5073
}
@@ -5121,7 +5132,8 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
5121
5132
}
5122
5133
5123
5134
int dataSize = DH_size (diffieHellman->dh );
5124
- char * data = Malloc (dataSize);
5135
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5136
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (dataSize));
5125
5137
5126
5138
int size = DH_compute_key (reinterpret_cast <unsigned char *>(data),
5127
5139
key,
@@ -5133,7 +5145,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
5133
5145
5134
5146
checked = DH_check_pub_key (diffieHellman->dh , key, &checkResult);
5135
5147
BN_free (key);
5136
- free (data);
5148
+ allocator-> Free (data, dataSize );
5137
5149
5138
5150
if (!checked) {
5139
5151
return ThrowCryptoError (env, ERR_get_error (), " Invalid Key" );
@@ -5338,14 +5350,15 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
5338
5350
return ;
5339
5351
5340
5352
// NOTE: field_size is in bits
5353
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5341
5354
int field_size = EC_GROUP_get_degree (ecdh->group_ );
5342
5355
size_t out_len = (field_size + 7 ) / 8 ;
5343
- char * out = node::Malloc ( out_len);
5356
+ char * out = static_cast < char *>(allocator-> AllocateUninitialized ( out_len) );
5344
5357
5345
5358
int r = ECDH_compute_key (out, out_len, pub, ecdh->key_ , nullptr );
5346
5359
EC_POINT_free (pub);
5347
5360
if (!r) {
5348
- free (out);
5361
+ allocator-> Free (out, out_len );
5349
5362
return env->ThrowError (" Failed to compute ECDH key" );
5350
5363
}
5351
5364
@@ -5375,11 +5388,13 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
5375
5388
if (size == 0 )
5376
5389
return env->ThrowError (" Failed to get public key length" );
5377
5390
5378
- unsigned char * out = node::Malloc<unsigned char >(size);
5391
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5392
+ unsigned char * out =
5393
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
5379
5394
5380
5395
int r = EC_POINT_point2oct (ecdh->group_ , pub, form, out, size, nullptr );
5381
5396
if (r != size) {
5382
- free (out);
5397
+ allocator-> Free (out, size );
5383
5398
return env->ThrowError (" Failed to get public key" );
5384
5399
}
5385
5400
@@ -5399,11 +5414,13 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
5399
5414
if (b == nullptr )
5400
5415
return env->ThrowError (" Failed to get ECDH private key" );
5401
5416
5417
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5402
5418
int size = BN_num_bytes (b);
5403
- unsigned char * out = node::Malloc<unsigned char >(size);
5419
+ unsigned char * out =
5420
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
5404
5421
5405
5422
if (size != BN_bn2bin (b, out)) {
5406
- free (out);
5423
+ allocator-> Free (out, size );
5407
5424
return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
5408
5425
}
5409
5426
@@ -5532,7 +5549,8 @@ class PBKDF2Request : public AsyncWrap {
5532
5549
saltlen_(saltlen),
5533
5550
salt_(salt),
5534
5551
keylen_(keylen),
5535
- key_(node::Malloc(keylen)),
5552
+ key_(static_cast <char *>(env->isolate ()->GetArrayBufferAllocator()->
5553
+ AllocateUninitialized(keylen))),
5536
5554
iter_(iter) {
5537
5555
Wrap (object, this );
5538
5556
}
@@ -5546,7 +5564,7 @@ class PBKDF2Request : public AsyncWrap {
5546
5564
salt_ = nullptr ;
5547
5565
saltlen_ = 0 ;
5548
5566
5549
- free ( key_);
5567
+ env ()-> isolate ()-> GetArrayBufferAllocator ()-> Free ( key_, keylen_ );
5550
5568
key_ = nullptr ;
5551
5569
keylen_ = 0 ;
5552
5570
@@ -5738,9 +5756,10 @@ class RandomBytesRequest : public AsyncWrap {
5738
5756
}
5739
5757
5740
5758
inline void release () {
5759
+ size_t free_size = size_;
5741
5760
size_ = 0 ;
5742
5761
if (free_mode_ == FREE_DATA) {
5743
- free ( data_);
5762
+ env ()-> isolate ()-> GetArrayBufferAllocator ()-> Free ( data_, free_size );
5744
5763
data_ = nullptr ;
5745
5764
}
5746
5765
}
@@ -5857,7 +5876,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
5857
5876
5858
5877
Local<Object> obj = env->randombytes_constructor_template ()->
5859
5878
NewInstance (env->context ()).ToLocalChecked ();
5860
- char * data = node::Malloc (size);
5879
+ char * data = static_cast <char *>(
5880
+ env->isolate ()->GetArrayBufferAllocator ()->AllocateUninitialized (size));
5861
5881
std::unique_ptr<RandomBytesRequest> req (
5862
5882
new RandomBytesRequest (env,
5863
5883
obj,
@@ -6057,10 +6077,11 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
6057
6077
}
6058
6078
6059
6079
6060
- char * ExportPublicKey (const char * data, int len, size_t * size) {
6080
+ char * ExportPublicKey (Environment* env, const char * data, int len, size_t * size) {
6061
6081
char * buf = nullptr ;
6062
6082
EVP_PKEY* pkey = nullptr ;
6063
6083
NETSCAPE_SPKI* spki = nullptr ;
6084
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
6064
6085
6065
6086
BIO* bio = BIO_new (BIO_s_mem ());
6066
6087
if (bio == nullptr )
@@ -6081,7 +6102,7 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
6081
6102
BIO_get_mem_ptr (bio, &ptr);
6082
6103
6083
6104
*size = ptr->length ;
6084
- buf = Malloc ( *size);
6105
+ buf = static_cast < char *>(allocator-> AllocateUninitialized ( *size) );
6085
6106
memcpy (buf, ptr->data , *size);
6086
6107
6087
6108
exit :
@@ -6109,7 +6130,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
6109
6130
CHECK_NE (data, nullptr );
6110
6131
6111
6132
size_t pkey_size;
6112
- char * pkey = ExportPublicKey (data, length, &pkey_size);
6133
+ char * pkey = ExportPublicKey (env, data, length, &pkey_size);
6113
6134
if (pkey == nullptr )
6114
6135
return args.GetReturnValue ().SetEmptyString ();
6115
6136
0 commit comments