@@ -477,48 +477,11 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
477
477
}
478
478
}
479
479
480
- namespace {
481
- ByteSource StatelessDiffieHellmanThreadsafe (const EVPKeyPointer& our_key,
482
- const EVPKeyPointer& their_key) {
483
- auto dp = DHPointer::stateless (our_key, their_key);
484
- if (!dp) return {};
485
-
486
- return ByteSource::Allocated (dp.release ());
487
- }
488
-
489
- void Stateless (const FunctionCallbackInfo<Value>& args) {
490
- Environment* env = Environment::GetCurrent (args);
491
-
492
- CHECK (args[0 ]->IsObject () && args[1 ]->IsObject ());
493
- KeyObjectHandle* our_key_object;
494
- ASSIGN_OR_RETURN_UNWRAP (&our_key_object, args[0 ].As <Object>());
495
- CHECK_EQ (our_key_object->Data ().GetKeyType (), kKeyTypePrivate );
496
- KeyObjectHandle* their_key_object;
497
- ASSIGN_OR_RETURN_UNWRAP (&their_key_object, args[1 ].As <Object>());
498
- CHECK_NE (their_key_object->Data ().GetKeyType (), kKeyTypeSecret );
499
-
500
- const auto & our_key = our_key_object->Data ().GetAsymmetricKey ();
501
- const auto & their_key = their_key_object->Data ().GetAsymmetricKey ();
502
-
503
- Local<Value> out;
504
- if (!StatelessDiffieHellmanThreadsafe (our_key, their_key)
505
- .ToBuffer (env)
506
- .ToLocal (&out)) return ;
507
-
508
- if (Buffer::Length (out) == 0 )
509
- return ThrowCryptoError (env, ERR_get_error (), " diffieHellman failed" );
510
-
511
- args.GetReturnValue ().Set (out);
512
- }
513
- } // namespace
514
-
515
480
Maybe<void > DHBitsTraits::AdditionalConfig (
516
481
CryptoJobMode mode,
517
482
const FunctionCallbackInfo<Value>& args,
518
483
unsigned int offset,
519
484
DHBitsConfig* params) {
520
- Environment* env = Environment::GetCurrent (args);
521
-
522
485
CHECK (args[offset]->IsObject ()); // public key
523
486
CHECK (args[offset + 1 ]->IsObject ()); // private key
524
487
@@ -528,11 +491,8 @@ Maybe<void> DHBitsTraits::AdditionalConfig(
528
491
ASSIGN_OR_RETURN_UNWRAP (&public_key, args[offset], Nothing<void >());
529
492
ASSIGN_OR_RETURN_UNWRAP (&private_key, args[offset + 1 ], Nothing<void >());
530
493
531
- if (private_key->Data ().GetKeyType () != kKeyTypePrivate ||
532
- public_key->Data ().GetKeyType () != kKeyTypePublic ) {
533
- THROW_ERR_CRYPTO_INVALID_KEYTYPE (env);
534
- return Nothing<void >();
535
- }
494
+ CHECK (private_key->Data ().GetKeyType () == kKeyTypePrivate );
495
+ CHECK (public_key->Data ().GetKeyType () != kKeyTypeSecret );
536
496
537
497
params->public_key = public_key->Data ().addRef ();
538
498
params->private_key = private_key->Data ().addRef ();
@@ -550,8 +510,20 @@ bool DHBitsTraits::DeriveBits(
550
510
Environment* env,
551
511
const DHBitsConfig& params,
552
512
ByteSource* out) {
553
- *out = StatelessDiffieHellmanThreadsafe (params.private_key .GetAsymmetricKey (),
554
- params.public_key .GetAsymmetricKey ());
513
+ auto dp = DHPointer::stateless (params.private_key .GetAsymmetricKey (),
514
+ params.public_key .GetAsymmetricKey ());
515
+ if (!dp) {
516
+ bool can_throw =
517
+ per_process::v8_initialized && Isolate::TryGetCurrent () != nullptr ;
518
+ if (can_throw) {
519
+ unsigned long err = ERR_get_error (); // NOLINT(runtime/int)
520
+ if (err) ThrowCryptoError (env, err, " diffieHellman failed" );
521
+ }
522
+ return false ;
523
+ }
524
+
525
+ *out = ByteSource::Allocated (dp.release ());
526
+ CHECK (!out->empty ());
555
527
return true ;
556
528
}
557
529
@@ -604,7 +576,6 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
604
576
make (FIXED_ONE_BYTE_STRING (env->isolate (), " DiffieHellmanGroup" ),
605
577
DiffieHellmanGroup);
606
578
607
- SetMethodNoSideEffect (context, target, " statelessDH" , Stateless);
608
579
DHKeyPairGenJob::Initialize (env, target);
609
580
DHKeyExportJob::Initialize (env, target);
610
581
DHBitsJob::Initialize (env, target);
@@ -625,7 +596,6 @@ void DiffieHellman::RegisterExternalReferences(
625
596
registry->Register (SetPrivateKey);
626
597
627
598
registry->Register (Check);
628
- registry->Register (Stateless);
629
599
630
600
DHKeyPairGenJob::RegisterExternalReferences (registry);
631
601
DHKeyExportJob::RegisterExternalReferences (registry);
0 commit comments