@@ -88,6 +88,8 @@ using v8::Local;
8888using v8::Null;
8989using v8::Object;
9090using v8::Persistent;
91+ using v8::PropertyAttribute;
92+ using v8::PropertyCallbackInfo;
9193using v8::String;
9294using v8::ThrowException;
9395using v8::V8;
@@ -3158,6 +3160,9 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
31583160void DiffieHellman::Initialize (Environment* env, Handle<Object> target) {
31593161 Local<FunctionTemplate> t = FunctionTemplate::New (New);
31603162
3163+ static enum PropertyAttribute attributes =
3164+ static_cast <PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
3165+
31613166 t->InstanceTemplate ()->SetInternalFieldCount (1 );
31623167
31633168 NODE_SET_PROTOTYPE_METHOD (t, " generateKeys" , GenerateKeys);
@@ -3169,6 +3174,13 @@ void DiffieHellman::Initialize(Environment* env, Handle<Object> target) {
31693174 NODE_SET_PROTOTYPE_METHOD (t, " setPublicKey" , SetPublicKey);
31703175 NODE_SET_PROTOTYPE_METHOD (t, " setPrivateKey" , SetPrivateKey);
31713176
3177+ t->InstanceTemplate ()->SetAccessor (env->verify_error_string (),
3178+ DiffieHellman::VerifyErrorGetter,
3179+ NULL ,
3180+ Handle<Value>(),
3181+ v8::DEFAULT,
3182+ attributes);
3183+
31723184 target->Set (FIXED_ONE_BYTE_STRING (env->isolate (), " DiffieHellman" ),
31733185 t->GetFunction ());
31743186
@@ -3182,14 +3194,21 @@ void DiffieHellman::Initialize(Environment* env, Handle<Object> target) {
31823194 NODE_SET_PROTOTYPE_METHOD (t2, " getPublicKey" , GetPublicKey);
31833195 NODE_SET_PROTOTYPE_METHOD (t2, " getPrivateKey" , GetPrivateKey);
31843196
3197+ t2->InstanceTemplate ()->SetAccessor (env->verify_error_string (),
3198+ DiffieHellman::VerifyErrorGetter,
3199+ NULL ,
3200+ Handle<Value>(),
3201+ v8::DEFAULT,
3202+ attributes);
3203+
31853204 target->Set (FIXED_ONE_BYTE_STRING (env->isolate (), " DiffieHellmanGroup" ),
31863205 t2->GetFunction ());
31873206}
31883207
31893208
3190- bool DiffieHellman::Init (int primeLength) {
3209+ bool DiffieHellman::Init (int primeLength, int g ) {
31913210 dh = DH_new ();
3192- DH_generate_parameters_ex (dh, primeLength, DH_GENERATOR_2 , 0 );
3211+ DH_generate_parameters_ex (dh, primeLength, g , 0 );
31933212 bool result = VerifyContext ();
31943213 if (!result)
31953214 return false ;
@@ -3198,11 +3217,11 @@ bool DiffieHellman::Init(int primeLength) {
31983217}
31993218
32003219
3201- bool DiffieHellman::Init (const char * p, int p_len) {
3220+ bool DiffieHellman::Init (const char * p, int p_len, int g ) {
32023221 dh = DH_new ();
32033222 dh->p = BN_bin2bn (reinterpret_cast <const unsigned char *>(p), p_len, 0 );
32043223 dh->g = BN_new ();
3205- if (!BN_set_word (dh->g , 2 ))
3224+ if (!BN_set_word (dh->g , g ))
32063225 return false ;
32073226 bool result = VerifyContext ();
32083227 if (!result)
@@ -3216,6 +3235,9 @@ bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
32163235 dh = DH_new ();
32173236 dh->p = BN_bin2bn (reinterpret_cast <const unsigned char *>(p), p_len, 0 );
32183237 dh->g = BN_bin2bn (reinterpret_cast <const unsigned char *>(g), g_len, 0 );
3238+ bool result = VerifyContext ();
3239+ if (!result)
3240+ return false ;
32193241 initialised_ = true ;
32203242 return true ;
32213243}
@@ -3232,17 +3254,21 @@ void DiffieHellman::DiffieHellmanGroup(
32323254 return ThrowError (" No group name given" );
32333255 }
32343256
3257+ bool initialized = false ;
3258+
32353259 const String::Utf8Value group_name (args[0 ]);
32363260 for (unsigned int i = 0 ; i < ARRAY_SIZE (modp_groups); ++i) {
32373261 const modp_group* it = modp_groups + i;
32383262
32393263 if (strcasecmp (*group_name, it->name ) != 0 )
32403264 continue ;
32413265
3242- diffieHellman->Init (it->prime ,
3243- it->prime_size ,
3244- it->gen ,
3245- it->gen_size );
3266+ initialized = diffieHellman->Init (it->prime ,
3267+ it->prime_size ,
3268+ it->gen ,
3269+ it->gen_size );
3270+ if (!initialized)
3271+ ThrowError (" Initialization failed" );
32463272 return ;
32473273 }
32483274
@@ -3258,12 +3284,23 @@ void DiffieHellman::New(const FunctionCallbackInfo<Value>& args) {
32583284 new DiffieHellman (env, args.This ());
32593285 bool initialized = false ;
32603286
3261- if (args.Length () > 0 ) {
3287+ if (args.Length () == 2 ) {
32623288 if (args[0 ]->IsInt32 ()) {
3263- initialized = diffieHellman->Init (args[0 ]->Int32Value ());
3289+ if (args[1 ]->IsInt32 ()) {
3290+ initialized = diffieHellman->Init (args[0 ]->Int32Value (),
3291+ args[1 ]->Int32Value ());
3292+ }
32643293 } else {
3265- initialized = diffieHellman->Init (Buffer::Data (args[0 ]),
3266- Buffer::Length (args[0 ]));
3294+ if (args[1 ]->IsInt32 ()) {
3295+ initialized = diffieHellman->Init (Buffer::Data (args[0 ]),
3296+ Buffer::Length (args[0 ]),
3297+ args[1 ]->Int32Value ());
3298+ } else {
3299+ initialized = diffieHellman->Init (Buffer::Data (args[0 ]),
3300+ Buffer::Length (args[0 ]),
3301+ Buffer::Data (args[1 ]),
3302+ Buffer::Length (args[1 ]));
3303+ }
32673304 }
32683305 }
32693306
@@ -3490,18 +3527,24 @@ void DiffieHellman::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
34903527}
34913528
34923529
3530+ void DiffieHellman::VerifyErrorGetter (Local<String> property,
3531+ const PropertyCallbackInfo<Value>& args) {
3532+ HandleScope scope (args.GetIsolate ());
3533+
3534+ DiffieHellman* diffieHellman = Unwrap<DiffieHellman>(args.This ());
3535+
3536+ if (!diffieHellman->initialised_ )
3537+ return ThrowError (" Not initialized" );
3538+
3539+ args.GetReturnValue ().Set (diffieHellman->verifyError_ );
3540+ }
3541+
3542+
34933543bool DiffieHellman::VerifyContext () {
34943544 int codes;
34953545 if (!DH_check (dh, &codes))
34963546 return false ;
3497- if (codes & DH_CHECK_P_NOT_SAFE_PRIME)
3498- return false ;
3499- if (codes & DH_CHECK_P_NOT_PRIME)
3500- return false ;
3501- if (codes & DH_UNABLE_TO_CHECK_GENERATOR)
3502- return false ;
3503- if (codes & DH_NOT_SUITABLE_GENERATOR)
3504- return false ;
3547+ verifyError_ = codes;
35053548 return true ;
35063549}
35073550
0 commit comments