@@ -4211,9 +4211,11 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
4211
4211
4212
4212
const BIGNUM* pub_key;
4213
4213
DH_get0_key (diffieHellman->dh_ .get (), &pub_key, nullptr );
4214
- size_t size = BN_num_bytes (pub_key);
4214
+ const int size = BN_num_bytes (pub_key);
4215
+ CHECK_GE (size, 0 );
4215
4216
char * data = Malloc (size);
4216
- BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
4217
+ CHECK_EQ (size,
4218
+ BN_bn2binpad (pub_key, reinterpret_cast <unsigned char *>(data), size));
4217
4219
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4218
4220
}
4219
4221
@@ -4229,9 +4231,11 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
4229
4231
const BIGNUM* num = get_field (dh->dh_ .get ());
4230
4232
if (num == nullptr ) return env->ThrowError (err_if_null);
4231
4233
4232
- size_t size = BN_num_bytes (num);
4234
+ const int size = BN_num_bytes (num);
4235
+ CHECK_GE (size, 0 );
4233
4236
char * data = Malloc (size);
4234
- BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
4237
+ CHECK_EQ (size,
4238
+ BN_bn2binpad (num, reinterpret_cast <unsigned char *>(data), size));
4235
4239
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4236
4240
}
4237
4241
@@ -4567,13 +4571,9 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4567
4571
if (b == nullptr )
4568
4572
return env->ThrowError (" Failed to get ECDH private key" );
4569
4573
4570
- int size = BN_num_bytes (b);
4574
+ const int size = BN_num_bytes (b);
4571
4575
unsigned char * out = node::Malloc<unsigned char >(size);
4572
-
4573
- if (size != BN_bn2bin (b, out)) {
4574
- free (out);
4575
- return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
4576
- }
4576
+ CHECK_EQ (size, BN_bn2binpad (b, out, size));
4577
4577
4578
4578
Local<Object> buf =
4579
4579
Buffer::New (env, reinterpret_cast <char *>(out), size).ToLocalChecked ();
0 commit comments