@@ -4186,9 +4186,11 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
4186
4186
4187
4187
const BIGNUM* pub_key;
4188
4188
DH_get0_key (diffieHellman->dh_ .get (), &pub_key, nullptr );
4189
- size_t size = BN_num_bytes (pub_key);
4189
+ const int size = BN_num_bytes (pub_key);
4190
+ CHECK_GE (size, 0 );
4190
4191
char * data = Malloc (size);
4191
- BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
4192
+ CHECK_EQ (size,
4193
+ BN_bn2binpad (pub_key, reinterpret_cast <unsigned char *>(data), size));
4192
4194
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4193
4195
}
4194
4196
@@ -4204,9 +4206,11 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
4204
4206
const BIGNUM* num = get_field (dh->dh_ .get ());
4205
4207
if (num == nullptr ) return env->ThrowError (err_if_null);
4206
4208
4207
- size_t size = BN_num_bytes (num);
4209
+ const int size = BN_num_bytes (num);
4210
+ CHECK_GE (size, 0 );
4208
4211
char * data = Malloc (size);
4209
- BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
4212
+ CHECK_EQ (size,
4213
+ BN_bn2binpad (num, reinterpret_cast <unsigned char *>(data), size));
4210
4214
args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
4211
4215
}
4212
4216
@@ -4542,13 +4546,9 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
4542
4546
if (b == nullptr )
4543
4547
return env->ThrowError (" Failed to get ECDH private key" );
4544
4548
4545
- int size = BN_num_bytes (b);
4549
+ const int size = BN_num_bytes (b);
4546
4550
unsigned char * out = node::Malloc<unsigned char >(size);
4547
-
4548
- if (size != BN_bn2bin (b, out)) {
4549
- free (out);
4550
- return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
4551
- }
4551
+ CHECK_EQ (size, BN_bn2binpad (b, out, size));
4552
4552
4553
4553
Local<Object> buf =
4554
4554
Buffer::New (env, reinterpret_cast <char *>(out), size).ToLocalChecked ();
0 commit comments