From 2e3a21f2db190cce77baa3974e55874490d2b83c Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 11 Apr 2022 09:37:43 +0530 Subject: [PATCH] src,crypto: remove uses of AllocatedBuffer from crypto_tls.cc Refs: https://github.com/nodejs/node/pull/39941 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/42589 Reviewed-By: James M Snell --- src/crypto/crypto_tls.cc | 63 +++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/src/crypto/crypto_tls.cc b/src/crypto/crypto_tls.cc index 72b49e05f0cb0d..2d0f0c4e73555b 100644 --- a/src/crypto/crypto_tls.cc +++ b/src/crypto/crypto_tls.cc @@ -25,7 +25,6 @@ #include "crypto/crypto_util.h" #include "crypto/crypto_bio.h" #include "crypto/crypto_clienthello-inl.h" -#include "allocated_buffer-inl.h" #include "async_wrap-inl.h" #include "debug_utils-inl.h" #include "memory_tracker-inl.h" @@ -1611,9 +1610,19 @@ void TLSWrap::GetFinished(const FunctionCallbackInfo& args) { if (len == 0) return; - AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, len); - CHECK_EQ(len, SSL_get_finished(w->ssl_.get(), buf.data(), len)); - args.GetReturnValue().Set(buf.ToBuffer().FromMaybe(Local())); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), len); + } + + CHECK_EQ(bs->ByteLength(), + SSL_get_finished(w->ssl_.get(), 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 TLSWrap::GetPeerFinished(const FunctionCallbackInfo& args) { @@ -1632,9 +1641,19 @@ void TLSWrap::GetPeerFinished(const FunctionCallbackInfo& args) { if (len == 0) return; - AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, len); - CHECK_EQ(len, SSL_get_peer_finished(w->ssl_.get(), buf.data(), len)); - args.GetReturnValue().Set(buf.ToBuffer().FromMaybe(Local())); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), len); + } + + CHECK_EQ(bs->ByteLength(), + SSL_get_peer_finished(w->ssl_.get(), 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 TLSWrap::GetSession(const FunctionCallbackInfo& args) { @@ -1651,10 +1670,19 @@ void TLSWrap::GetSession(const FunctionCallbackInfo& args) { if (slen <= 0) return; // Invalid or malformed session. - AllocatedBuffer sbuf = AllocatedBuffer::AllocateManaged(env, slen); - unsigned char* p = reinterpret_cast(sbuf.data()); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), slen); + } + + unsigned char* p = static_cast(bs->Data()); CHECK_LT(0, i2d_SSL_SESSION(sess, &p)); - args.GetReturnValue().Set(sbuf.ToBuffer().FromMaybe(Local())); + + 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 TLSWrap::SetSession(const FunctionCallbackInfo& args) { @@ -1825,7 +1853,11 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo& args) { uint32_t olen = args[0].As()->Value(); Utf8Value label(env->isolate(), args[1]); - AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, olen); + std::unique_ptr bs; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + bs = ArrayBuffer::NewBackingStore(env->isolate(), olen); + } ByteSource context; bool use_context = !args[2]->IsUndefined(); @@ -1834,11 +1866,11 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo& args) { if (SSL_export_keying_material( w->ssl_.get(), - reinterpret_cast(out.data()), + static_cast(bs->Data()), olen, *label, label.length(), - reinterpret_cast(context.get()), + context.data(), context.size(), use_context) != 1) { return ThrowCryptoError( @@ -1847,7 +1879,10 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo& args) { "SSL_export_keying_material"); } - args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local())); + 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 TLSWrap::EndParser(const FunctionCallbackInfo& args) {