11
11
12
12
namespace node {
13
13
14
+ using v8::ArrayBuffer;
14
15
using v8::ConstructorBehavior;
15
16
using v8::DontDelete;
16
17
using v8::FunctionCallback;
@@ -28,6 +29,7 @@ using v8::ReadOnly;
28
29
using v8::SideEffectType;
29
30
using v8::Signature;
30
31
using v8::String;
32
+ using v8::Uint8Array;
31
33
using v8::Value;
32
34
33
35
namespace crypto {
@@ -539,41 +541,9 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
539
541
}
540
542
541
543
namespace {
542
- AllocatedBuffer StatelessDiffieHellman (
543
- Environment* env,
544
- ManagedEVPPKey our_key,
545
- ManagedEVPPKey their_key) {
546
- size_t out_size;
547
-
548
- EVPKeyCtxPointer ctx (EVP_PKEY_CTX_new (our_key.get (), nullptr ));
549
- if (!ctx ||
550
- EVP_PKEY_derive_init (ctx.get ()) <= 0 ||
551
- EVP_PKEY_derive_set_peer (ctx.get (), their_key.get ()) <= 0 ||
552
- EVP_PKEY_derive (ctx.get (), nullptr , &out_size) <= 0 )
553
- return AllocatedBuffer ();
554
-
555
- AllocatedBuffer result = AllocatedBuffer::AllocateManaged (env, out_size);
556
- CHECK_NOT_NULL (result.data ());
557
-
558
- unsigned char * data = reinterpret_cast <unsigned char *>(result.data ());
559
- if (EVP_PKEY_derive (ctx.get (), data, &out_size) <= 0 )
560
- return AllocatedBuffer ();
561
-
562
- ZeroPadDiffieHellmanSecret (out_size, &result);
563
- return result;
564
- }
565
-
566
- // The version of StatelessDiffieHellman that returns an AllocatedBuffer
567
- // is not threadsafe because of the AllocatedBuffer allocation of a
568
- // v8::BackingStore (it'll cause much crashing if we call it from a
569
- // libuv worker thread). This version allocates a ByteSource instead,
570
- // which we can convert into a v8::BackingStore later.
571
- // TODO(@jasnell): Eliminate the code duplication between these two
572
- // versions of the function.
573
544
ByteSource StatelessDiffieHellmanThreadsafe (
574
- Environment* env,
575
- ManagedEVPPKey our_key,
576
- ManagedEVPPKey their_key) {
545
+ const ManagedEVPPKey& our_key,
546
+ const ManagedEVPPKey& their_key) {
577
547
size_t out_size;
578
548
579
549
EVPKeyCtxPointer ctx (EVP_PKEY_CTX_new (our_key.get (), nullptr ));
@@ -612,11 +582,18 @@ void DiffieHellman::Stateless(const FunctionCallbackInfo<Value>& args) {
612
582
ManagedEVPPKey our_key = our_key_object->Data ()->GetAsymmetricKey ();
613
583
ManagedEVPPKey their_key = their_key_object->Data ()->GetAsymmetricKey ();
614
584
615
- AllocatedBuffer out = StatelessDiffieHellman (env, our_key, their_key);
616
- if (out.size () == 0 )
585
+ Local<Value> out;
586
+ {
587
+ Local<ArrayBuffer> ab = StatelessDiffieHellmanThreadsafe (our_key, their_key)
588
+ .ToArrayBuffer (env);
589
+ out = Buffer::New (env, ab, 0 , ab->ByteLength ())
590
+ .FromMaybe (Local<Uint8Array>());
591
+ }
592
+
593
+ if (Buffer::Length (out) == 0 )
617
594
return ThrowCryptoError (env, ERR_get_error (), " diffieHellman failed" );
618
595
619
- args.GetReturnValue ().Set (out. ToBuffer (). FromMaybe (Local<Value>()) );
596
+ args.GetReturnValue ().Set (out);
620
597
}
621
598
622
599
Maybe<bool > DHBitsTraits::AdditionalConfig (
@@ -661,7 +638,6 @@ bool DHBitsTraits::DeriveBits(
661
638
const DHBitsConfig& params,
662
639
ByteSource* out) {
663
640
*out = StatelessDiffieHellmanThreadsafe (
664
- env,
665
641
params.private_key ->GetAsymmetricKey (),
666
642
params.public_key ->GetAsymmetricKey ());
667
643
return true ;
0 commit comments