@@ -483,49 +483,11 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
483
483
}
484
484
}
485
485
486
- namespace {
487
- ByteSource StatelessDiffieHellmanThreadsafe (const EVPKeyPointer& our_key,
488
- const EVPKeyPointer& their_key) {
489
- auto dp = DHPointer::stateless (our_key, their_key);
490
- if (!dp) return {};
491
- DCHECK (!dp.isSecure ());
492
-
493
- return ByteSource::Allocated (dp.release ());
494
- }
495
-
496
- void Stateless (const FunctionCallbackInfo<Value>& args) {
497
- Environment* env = Environment::GetCurrent (args);
498
-
499
- CHECK (args[0 ]->IsObject () && args[1 ]->IsObject ());
500
- KeyObjectHandle* our_key_object;
501
- ASSIGN_OR_RETURN_UNWRAP (&our_key_object, args[0 ].As <Object>());
502
- CHECK_EQ (our_key_object->Data ().GetKeyType (), kKeyTypePrivate );
503
- KeyObjectHandle* their_key_object;
504
- ASSIGN_OR_RETURN_UNWRAP (&their_key_object, args[1 ].As <Object>());
505
- CHECK_NE (their_key_object->Data ().GetKeyType (), kKeyTypeSecret );
506
-
507
- const auto & our_key = our_key_object->Data ().GetAsymmetricKey ();
508
- const auto & their_key = their_key_object->Data ().GetAsymmetricKey ();
509
-
510
- Local<Value> out;
511
- if (!StatelessDiffieHellmanThreadsafe (our_key, their_key)
512
- .ToBuffer (env)
513
- .ToLocal (&out)) return ;
514
-
515
- if (Buffer::Length (out) == 0 )
516
- return ThrowCryptoError (env, ERR_get_error (), " diffieHellman failed" );
517
-
518
- args.GetReturnValue ().Set (out);
519
- }
520
- } // namespace
521
-
522
486
Maybe<void > DHBitsTraits::AdditionalConfig (
523
487
CryptoJobMode mode,
524
488
const FunctionCallbackInfo<Value>& args,
525
489
unsigned int offset,
526
490
DHBitsConfig* params) {
527
- Environment* env = Environment::GetCurrent (args);
528
-
529
491
CHECK (args[offset]->IsObject ()); // public key
530
492
CHECK (args[offset + 1 ]->IsObject ()); // private key
531
493
@@ -535,11 +497,8 @@ Maybe<void> DHBitsTraits::AdditionalConfig(
535
497
ASSIGN_OR_RETURN_UNWRAP (&public_key, args[offset], Nothing<void >());
536
498
ASSIGN_OR_RETURN_UNWRAP (&private_key, args[offset + 1 ], Nothing<void >());
537
499
538
- if (private_key->Data ().GetKeyType () != kKeyTypePrivate ||
539
- public_key->Data ().GetKeyType () != kKeyTypePublic ) {
540
- THROW_ERR_CRYPTO_INVALID_KEYTYPE (env);
541
- return Nothing<void >();
542
- }
500
+ CHECK (private_key->Data ().GetKeyType () == kKeyTypePrivate );
501
+ CHECK (public_key->Data ().GetKeyType () != kKeyTypeSecret );
543
502
544
503
params->public_key = public_key->Data ().addRef ();
545
504
params->private_key = private_key->Data ().addRef ();
@@ -557,8 +516,20 @@ bool DHBitsTraits::DeriveBits(
557
516
Environment* env,
558
517
const DHBitsConfig& params,
559
518
ByteSource* out) {
560
- *out = StatelessDiffieHellmanThreadsafe (params.private_key .GetAsymmetricKey (),
561
- params.public_key .GetAsymmetricKey ());
519
+ auto dp = DHPointer::stateless (params.private_key .GetAsymmetricKey (),
520
+ params.public_key .GetAsymmetricKey ());
521
+ if (!dp) {
522
+ bool can_throw =
523
+ per_process::v8_initialized && Isolate::TryGetCurrent () != nullptr ;
524
+ if (can_throw) {
525
+ unsigned long err = ERR_get_error (); // NOLINT(runtime/int)
526
+ if (err) ThrowCryptoError (env, err, " diffieHellman failed" );
527
+ }
528
+ return false ;
529
+ }
530
+
531
+ *out = ByteSource::Allocated (dp.release ());
532
+ CHECK (!out->empty ());
562
533
return true ;
563
534
}
564
535
@@ -611,7 +582,6 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
611
582
make (FIXED_ONE_BYTE_STRING (env->isolate (), " DiffieHellmanGroup" ),
612
583
DiffieHellmanGroup);
613
584
614
- SetMethodNoSideEffect (context, target, " statelessDH" , Stateless);
615
585
DHKeyPairGenJob::Initialize (env, target);
616
586
DHKeyExportJob::Initialize (env, target);
617
587
DHBitsJob::Initialize (env, target);
@@ -632,7 +602,6 @@ void DiffieHellman::RegisterExternalReferences(
632
602
registry->Register (SetPrivateKey);
633
603
634
604
registry->Register (Check);
635
- registry->Register (Stateless);
636
605
637
606
DHKeyPairGenJob::RegisterExternalReferences (registry);
638
607
DHKeyExportJob::RegisterExternalReferences (registry);
0 commit comments