@@ -4115,9 +4115,11 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
4115
4115
4116
4116
const BIGNUM* pub_key;
4117
4117
DH_get0_key (diffieHellman->dh_ .get (), &pub_key, nullptr );
4118
- size_t size = BN_num_bytes (pub_key);
4118
+ const int size = BN_num_bytes (pub_key);
4119
+ CHECK_GE (size, 0 );
4119
4120
char * data = Malloc (size);
4120
- BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
4121
+ CHECK_EQ (size,
4122
+ BN_bn2binpad (pub_key, reinterpret_cast <unsigned char *>(data), size));
4121
4123
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4122
4124
}
4123
4125
@@ -4133,9 +4135,11 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
4133
4135
const BIGNUM* num = get_field (dh->dh_ .get ());
4134
4136
if (num == nullptr ) return env->ThrowError (err_if_null);
4135
4137
4136
- size_t size = BN_num_bytes (num);
4138
+ const int size = BN_num_bytes (num);
4139
+ CHECK_GE (size, 0 );
4137
4140
char * data = Malloc (size);
4138
- BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
4141
+ CHECK_EQ (size,
4142
+ BN_bn2binpad (num, reinterpret_cast <unsigned char *>(data), size));
4139
4143
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4140
4144
}
4141
4145
@@ -4470,13 +4474,9 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4470
4474
if (b == nullptr )
4471
4475
return env->ThrowError (" Failed to get ECDH private key" );
4472
4476
4473
- int size = BN_num_bytes (b);
4477
+ const int size = BN_num_bytes (b);
4474
4478
unsigned char * out = node::Malloc<unsigned char >(size);
4475
-
4476
- if (size != BN_bn2bin (b, out)) {
4477
- free (out);
4478
- return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
4479
- }
4479
+ CHECK_EQ (size, BN_bn2binpad (b, out, size));
4480
4480
4481
4481
Local<Object> buf =
4482
4482
Buffer::New (env, reinterpret_cast <char *>(out), size).ToLocalChecked ();
0 commit comments