diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc index b6ef5e5b1e004e..6cbb73d5adb004 100644 --- a/src/crypto/crypto_dh.cc +++ b/src/crypto/crypto_dh.cc @@ -11,6 +11,8 @@ namespace node { +using v8::ArrayBuffer; +using v8::BackingStore; using v8::ConstructorBehavior; using v8::DontDelete; using v8::FunctionCallback; @@ -48,10 +50,6 @@ static void ZeroPadDiffieHellmanSecret(size_t remainder_size, memset(data, 0, padding); } } -static void ZeroPadDiffieHellmanSecret(size_t remainder_size, - AllocatedBuffer* ret) { - ZeroPadDiffieHellmanSecret(remainder_size, ret->data(), ret->size()); -} } // namespace DiffieHellman::DiffieHellman(Environment* env, Local wrap) @@ -273,13 +271,24 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo& args) { const BIGNUM* pub_key; DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr); - const int size = BN_num_bytes(pub_key); - CHECK_GE(size, 0); - AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size); - CHECK_EQ(size, - BN_bn2binpad( - pub_key, reinterpret_cast(data.data()), size)); - args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local())); + + std::unique_ptr bs; + { + const int size = BN_num_bytes(pub_key); + CHECK_GE(size, 0); + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), size); + } + + CHECK_EQ(static_cast(bs->ByteLength()), + BN_bn2binpad(pub_key, + static_cast(bs->Data()), + bs->ByteLength())); + + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + Local buffer; + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return; + args.GetReturnValue().Set(buffer); } @@ -295,13 +304,23 @@ void DiffieHellman::GetField(const FunctionCallbackInfo& args, if (num == nullptr) return THROW_ERR_CRYPTO_INVALID_STATE(env, err_if_null); - const int size = BN_num_bytes(num); - CHECK_GE(size, 0); - AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size); - CHECK_EQ( - size, - BN_bn2binpad(num, reinterpret_cast(data.data()), size)); - args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local())); + std::unique_ptr bs; + { + const int size = BN_num_bytes(num); + CHECK_GE(size, 0); + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), size); + } + + CHECK_EQ(static_cast(bs->ByteLength()), + BN_bn2binpad(num, + static_cast(bs->Data()), + bs->ByteLength())); + + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + Local buffer; + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return; + args.GetReturnValue().Set(buffer); } void DiffieHellman::GetPrime(const FunctionCallbackInfo& args) { @@ -350,10 +369,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo& args) { return THROW_ERR_OUT_OF_RANGE(env, "secret is too big"); BignumPointer key(BN_bin2bn(key_buf.data(), key_buf.size(), nullptr)); - AllocatedBuffer ret = - AllocatedBuffer::AllocateManaged(env, DH_size(diffieHellman->dh_.get())); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), + DH_size(diffieHellman->dh_.get())); + } - int size = DH_compute_key(reinterpret_cast(ret.data()), + int size = DH_compute_key(static_cast(bs->Data()), key.get(), diffieHellman->dh_.get()); @@ -381,9 +404,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo& args) { } CHECK_GE(size, 0); - ZeroPadDiffieHellmanSecret(static_cast(size), &ret); - - args.GetReturnValue().Set(ret.ToBuffer().FromMaybe(Local())); + ZeroPadDiffieHellmanSecret(size, + static_cast(bs->Data()), + bs->ByteLength()); + + Local ab = ArrayBuffer::New(env->isolate(), std::move(bs)); + Local buffer; + if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return; + args.GetReturnValue().Set(buffer); } void DiffieHellman::SetKey(const FunctionCallbackInfo& args,