@@ -1865,7 +1865,8 @@ void SSLWrap<Base>::GetFinished(const FunctionCallbackInfo<Value>& args) {
1865
1865
if (len == 0 )
1866
1866
return ;
1867
1867
1868
- char * buf = Malloc (len);
1868
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
1869
+ char * buf = static_cast <char *>(allocator->AllocateUninitialized (len));
1869
1870
CHECK_EQ (len, SSL_get_finished (w->ssl_ .get (), buf, len));
1870
1871
args.GetReturnValue ().Set (Buffer::New (env, buf, len).ToLocalChecked ());
1871
1872
}
@@ -1888,7 +1889,8 @@ void SSLWrap<Base>::GetPeerFinished(const FunctionCallbackInfo<Value>& args) {
1888
1889
if (len == 0 )
1889
1890
return ;
1890
1891
1891
- char * buf = Malloc (len);
1892
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
1893
+ char * buf = static_cast <char *>(allocator->AllocateUninitialized (len));
1892
1894
CHECK_EQ (len, SSL_get_peer_finished (w->ssl_ .get (), buf, len));
1893
1895
args.GetReturnValue ().Set (Buffer::New (env, buf, len).ToLocalChecked ());
1894
1896
}
@@ -1908,7 +1910,8 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
1908
1910
int slen = i2d_SSL_SESSION (sess, nullptr );
1909
1911
CHECK_GT (slen, 0 );
1910
1912
1911
- char * sbuf = Malloc (slen);
1913
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
1914
+ char * sbuf = static_cast <char *>(allocator->AllocateUninitialized (slen));
1912
1915
unsigned char * p = reinterpret_cast <unsigned char *>(sbuf);
1913
1916
i2d_SSL_SESSION (sess, &p);
1914
1917
args.GetReturnValue ().Set (Buffer::New (env, sbuf, slen).ToLocalChecked ());
@@ -2329,11 +2332,12 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
2329
2332
size_t len = Buffer::Length (obj);
2330
2333
2331
2334
// OpenSSL takes control of the pointer after accepting it
2332
- char * data = node::Malloc (len);
2335
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
2336
+ uint8_t * data = static_cast <uint8_t *>(allocator->AllocateUninitialized (len));
2333
2337
memcpy (data, resp, len);
2334
2338
2335
2339
if (!SSL_set_tlsext_status_ocsp_resp (s, data, len))
2336
- free (data);
2340
+ allocator-> Free (data, len );
2337
2341
w->ocsp_response_ .Reset ();
2338
2342
2339
2343
return SSL_TLSEXT_ERR_OK;
@@ -3037,7 +3041,8 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
3037
3041
return kErrorState ;
3038
3042
}
3039
3043
3040
- *out = Malloc<unsigned char >(buff_len);
3044
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3045
+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (buff_len));
3041
3046
int r = EVP_CipherUpdate (ctx_.get (),
3042
3047
*out,
3043
3048
out_len,
@@ -3080,7 +3085,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3080
3085
}
3081
3086
3082
3087
if (r != kSuccess ) {
3083
- free (out);
3088
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3089
+ allocator->Free (out, out_len);
3084
3090
if (r == kErrorState ) {
3085
3091
ThrowCryptoError (env, ERR_get_error (),
3086
3092
" Trying to add data in unsupported state" );
@@ -3119,8 +3125,9 @@ bool CipherBase::Final(unsigned char** out, int* out_len) {
3119
3125
3120
3126
const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
3121
3127
3122
- *out = Malloc<unsigned char >(
3123
- static_cast <size_t >(EVP_CIPHER_CTX_block_size (ctx_.get ())));
3128
+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3129
+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (
3130
+ EVP_CIPHER_CTX_block_size (ctx_.get ())));
3124
3131
3125
3132
if (kind_ == kDecipher && IsSupportedAuthenticatedMode (mode)) {
3126
3133
MaybePassAuthTagToOpenSSL ();
@@ -3169,7 +3176,8 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3169
3176
bool r = cipher->Final (&out_value, &out_len);
3170
3177
3171
3178
if (out_len <= 0 || !r) {
3172
- free (out_value);
3179
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3180
+ allocator->Free (out_value, out_len);
3173
3181
out_value = nullptr ;
3174
3182
out_len = 0 ;
3175
3183
if (!r) {
@@ -3818,7 +3826,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
3818
3826
template <PublicKeyCipher::Operation operation,
3819
3827
PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
3820
3828
PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
3821
- bool PublicKeyCipher::Cipher (const char * key_pem,
3829
+ bool PublicKeyCipher::Cipher (Environment* env,
3830
+ const char * key_pem,
3822
3831
int key_pem_len,
3823
3832
const char * passphrase,
3824
3833
int padding,
@@ -3827,6 +3836,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
3827
3836
unsigned char ** out,
3828
3837
size_t * out_len) {
3829
3838
EVPKeyPointer pkey;
3839
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3830
3840
3831
3841
// Check if this is a PKCS#8 or RSA public key before trying as X.509 and
3832
3842
// private key.
@@ -3859,7 +3869,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
3859
3869
if (EVP_PKEY_cipher (ctx.get (), nullptr , out_len, data, len) <= 0 )
3860
3870
return false ;
3861
3871
3862
- *out = Malloc <unsigned char >( *out_len);
3872
+ *out = static_cast <unsigned char *>(allocator-> AllocateUninitialized ( *out_len) );
3863
3873
3864
3874
if (EVP_PKEY_cipher (ctx.get (), *out, out_len, data, len) <= 0 )
3865
3875
return false ;
@@ -3893,6 +3903,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
3893
3903
ClearErrorOnReturn clear_error_on_return;
3894
3904
3895
3905
bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
3906
+ env,
3896
3907
kbuf,
3897
3908
klen,
3898
3909
args.Length () >= 4 && !args[3 ]->IsNull () ? *passphrase : nullptr ,
@@ -3903,7 +3914,8 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
3903
3914
&out_len);
3904
3915
3905
3916
if (out_len == 0 || !r) {
3906
- free (out_value);
3917
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3918
+ allocator->Free (out_value, out_len);
3907
3919
out_value = nullptr ;
3908
3920
out_len = 0 ;
3909
3921
if (!r) {
@@ -4116,7 +4128,8 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
4116
4128
const BIGNUM* pub_key;
4117
4129
DH_get0_key (diffieHellman->dh_ .get (), &pub_key, nullptr );
4118
4130
size_t size = BN_num_bytes (pub_key);
4119
- char * data = Malloc (size);
4131
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4132
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
4120
4133
BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
4121
4134
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4122
4135
}
@@ -4135,7 +4148,8 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
4135
4148
if (num == nullptr ) return env->ThrowError (err_if_null);
4136
4149
4137
4150
size_t size = BN_num_bytes (num);
4138
- char * data = Malloc (size);
4151
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4152
+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
4139
4153
BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
4140
4154
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4141
4155
}
@@ -4199,7 +4213,8 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4199
4213
Buffer::Length (args[0 ]),
4200
4214
0 ));
4201
4215
4202
- MallocedBuffer<char > data (DH_size (diffieHellman->dh_ .get ()));
4216
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4217
+ MallocedBuffer<char > data (DH_size (diffieHellman->dh_ .get ()), allocator);
4203
4218
4204
4219
int size = DH_compute_key (reinterpret_cast <unsigned char *>(data.data ),
4205
4220
key.get (),
@@ -4419,13 +4434,14 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4419
4434
}
4420
4435
4421
4436
// NOTE: field_size is in bits
4437
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4422
4438
int field_size = EC_GROUP_get_degree (ecdh->group_ );
4423
4439
size_t out_len = (field_size + 7 ) / 8 ;
4424
- char * out = node::Malloc ( out_len);
4440
+ char * out = static_cast < char *>(allocator-> AllocateUninitialized ( out_len) );
4425
4441
4426
4442
int r = ECDH_compute_key (out, out_len, pub.get (), ecdh->key_ .get (), nullptr );
4427
4443
if (!r) {
4428
- free (out);
4444
+ allocator-> Free (out, out_len );
4429
4445
return env->ThrowError (" Failed to compute ECDH key" );
4430
4446
}
4431
4447
@@ -4456,11 +4472,13 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
4456
4472
if (size == 0 )
4457
4473
return env->ThrowError (" Failed to get public key length" );
4458
4474
4459
- unsigned char * out = node::Malloc<unsigned char >(size);
4475
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4476
+ unsigned char * out =
4477
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
4460
4478
4461
4479
int r = EC_POINT_point2oct (ecdh->group_ , pub, form, out, size, nullptr );
4462
4480
if (r != size) {
4463
- free (out);
4481
+ allocator-> Free (out, size );
4464
4482
return env->ThrowError (" Failed to get public key" );
4465
4483
}
4466
4484
@@ -4480,11 +4498,13 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4480
4498
if (b == nullptr )
4481
4499
return env->ThrowError (" Failed to get ECDH private key" );
4482
4500
4501
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4483
4502
int size = BN_num_bytes (b);
4484
- unsigned char * out = node::Malloc<unsigned char >(size);
4503
+ unsigned char * out =
4504
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
4485
4505
4486
4506
if (size != BN_bn2bin (b, out)) {
4487
- free (out);
4507
+ allocator-> Free (out, size );
4488
4508
return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
4489
4509
}
4490
4510
@@ -4953,8 +4973,9 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4953
4973
}
4954
4974
4955
4975
4956
- char * ExportPublicKey (const char * data, int len, size_t * size) {
4976
+ char * ExportPublicKey (Environment* env, const char * data, int len, size_t * size) {
4957
4977
char * buf = nullptr ;
4978
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4958
4979
4959
4980
BIOPointer bio (BIO_new (BIO_s_mem ()));
4960
4981
if (!bio)
@@ -4975,7 +4996,7 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
4975
4996
BIO_get_mem_ptr (bio.get (), &ptr);
4976
4997
4977
4998
*size = ptr->length ;
4978
- buf = Malloc ( *size);
4999
+ buf = static_cast < char *>(allocator-> AllocateUninitialized ( *size) );
4979
5000
memcpy (buf, ptr->data , *size);
4980
5001
4981
5002
return buf;
@@ -4993,7 +5014,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
4993
5014
CHECK_NOT_NULL (data);
4994
5015
4995
5016
size_t pkey_size;
4996
- char * pkey = ExportPublicKey (data, length, &pkey_size);
5017
+ char * pkey = ExportPublicKey (env, data, length, &pkey_size);
4997
5018
if (pkey == nullptr )
4998
5019
return args.GetReturnValue ().SetEmptyString ();
4999
5020
@@ -5075,11 +5096,13 @@ void ConvertKey(const FunctionCallbackInfo<Value>& args) {
5075
5096
if (size == 0 )
5076
5097
return env->ThrowError (" Failed to get public key length" );
5077
5098
5078
- unsigned char * out = node::Malloc<unsigned char >(size);
5099
+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
5100
+ unsigned char * out =
5101
+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
5079
5102
5080
5103
int r = EC_POINT_point2oct (group.get (), pub.get (), form, out, size, nullptr );
5081
5104
if (r != size) {
5082
- free (out);
5105
+ allocator-> Free (out, size );
5083
5106
return env->ThrowError (" Failed to get public key" );
5084
5107
}
5085
5108
0 commit comments