@@ -4881,44 +4881,40 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
4881
4881
}
4882
4882
4883
4883
4884
- void DiffieHellman::SetPublicKey (const FunctionCallbackInfo<Value>& args) {
4885
- DiffieHellman* diffieHellman;
4886
- ASSIGN_OR_RETURN_UNWRAP (&diffieHellman, args.Holder ());
4887
- Environment* env = diffieHellman->env ();
4884
+ void DiffieHellman::SetKey (const v8::FunctionCallbackInfo<v8::Value>& args,
4885
+ BIGNUM* (DH::*field), const char* what) {
4886
+ Environment* env = Environment::GetCurrent (args);
4888
4887
4889
- if (!diffieHellman->initialised_ ) {
4890
- return ThrowCryptoError (env, ERR_get_error (), " Not initialized" );
4891
- }
4888
+ DiffieHellman* dh;
4889
+ ASSIGN_OR_RETURN_UNWRAP (&dh, args.Holder ());
4890
+ if (!dh->initialised_ ) return env->ThrowError (" Not initialized" );
4891
+
4892
+ BIGNUM** num = &((dh->dh )->*field);
4893
+ char errmsg[64 ];
4892
4894
4893
4895
if (args.Length () == 0 ) {
4894
- return env->ThrowError (" Public key argument is mandatory" );
4895
- } else {
4896
- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Public key" );
4897
- diffieHellman->dh ->pub_key = BN_bin2bn (
4898
- reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
4899
- Buffer::Length (args[0 ]), 0 );
4896
+ snprintf (errmsg, sizeof (errmsg), " %s argument is mandatory" , what);
4897
+ return env->ThrowError (errmsg);
4898
+ }
4899
+
4900
+ if (!Buffer::HasInstance (args[0 ])) {
4901
+ snprintf (errmsg, sizeof (errmsg), " %s must be a buffer" , what);
4902
+ return env->ThrowTypeError (errmsg);
4900
4903
}
4904
+
4905
+ *num = BN_bin2bn (reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
4906
+ Buffer::Length (args[0 ]), *num);
4907
+ CHECK_NE (*num, nullptr );
4901
4908
}
4902
4909
4903
4910
4904
- void DiffieHellman::SetPrivateKey (const FunctionCallbackInfo<Value>& args) {
4905
- DiffieHellman* diffieHellman;
4906
- ASSIGN_OR_RETURN_UNWRAP (&diffieHellman, args.Holder ());
4907
- Environment* env = diffieHellman->env ();
4911
+ void DiffieHellman::SetPublicKey (const FunctionCallbackInfo<Value>& args) {
4912
+ SetKey (args, &DH::pub_key, " Public key" );
4913
+ }
4908
4914
4909
- if (!diffieHellman->initialised_ ) {
4910
- return ThrowCryptoError (env, ERR_get_error (), " Not initialized" );
4911
- }
4912
4915
4913
- if (args.Length () == 0 ) {
4914
- return env->ThrowError (" Private key argument is mandatory" );
4915
- } else {
4916
- THROW_AND_RETURN_IF_NOT_BUFFER (args[0 ], " Private key" );
4917
- diffieHellman->dh ->priv_key = BN_bin2bn (
4918
- reinterpret_cast <unsigned char *>(Buffer::Data (args[0 ])),
4919
- Buffer::Length (args[0 ]),
4920
- 0 );
4921
- }
4916
+ void DiffieHellman::SetPrivateKey (const FunctionCallbackInfo<Value>& args) {
4917
+ SetKey (args, &DH::priv_key, " Private key" );
4922
4918
}
4923
4919
4924
4920
0 commit comments