Skip to content

Commit 0c57a37

Browse files
authored
src,crypto: remove uses of AllocatedBuffer from crypto_tls.cc
Refs: #39941 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42589 Reviewed-By: James M Snell <[email protected]>
1 parent 951dbc0 commit 0c57a37

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

src/crypto/crypto_tls.cc

+49-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "crypto/crypto_util.h"
2626
#include "crypto/crypto_bio.h"
2727
#include "crypto/crypto_clienthello-inl.h"
28-
#include "allocated_buffer-inl.h"
2928
#include "async_wrap-inl.h"
3029
#include "debug_utils-inl.h"
3130
#include "memory_tracker-inl.h"
@@ -1611,9 +1610,19 @@ void TLSWrap::GetFinished(const FunctionCallbackInfo<Value>& args) {
16111610
if (len == 0)
16121611
return;
16131612

1614-
AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, len);
1615-
CHECK_EQ(len, SSL_get_finished(w->ssl_.get(), buf.data(), len));
1616-
args.GetReturnValue().Set(buf.ToBuffer().FromMaybe(Local<Value>()));
1613+
std::unique_ptr<BackingStore> bs;
1614+
{
1615+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
1616+
bs = ArrayBuffer::NewBackingStore(env->isolate(), len);
1617+
}
1618+
1619+
CHECK_EQ(bs->ByteLength(),
1620+
SSL_get_finished(w->ssl_.get(), bs->Data(), bs->ByteLength()));
1621+
1622+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
1623+
Local<Value> buffer;
1624+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
1625+
args.GetReturnValue().Set(buffer);
16171626
}
16181627

16191628
void TLSWrap::GetPeerFinished(const FunctionCallbackInfo<Value>& args) {
@@ -1632,9 +1641,19 @@ void TLSWrap::GetPeerFinished(const FunctionCallbackInfo<Value>& args) {
16321641
if (len == 0)
16331642
return;
16341643

1635-
AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, len);
1636-
CHECK_EQ(len, SSL_get_peer_finished(w->ssl_.get(), buf.data(), len));
1637-
args.GetReturnValue().Set(buf.ToBuffer().FromMaybe(Local<Value>()));
1644+
std::unique_ptr<BackingStore> bs;
1645+
{
1646+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
1647+
bs = ArrayBuffer::NewBackingStore(env->isolate(), len);
1648+
}
1649+
1650+
CHECK_EQ(bs->ByteLength(),
1651+
SSL_get_peer_finished(w->ssl_.get(), bs->Data(), bs->ByteLength()));
1652+
1653+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
1654+
Local<Value> buffer;
1655+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
1656+
args.GetReturnValue().Set(buffer);
16381657
}
16391658

16401659
void TLSWrap::GetSession(const FunctionCallbackInfo<Value>& args) {
@@ -1651,10 +1670,19 @@ void TLSWrap::GetSession(const FunctionCallbackInfo<Value>& args) {
16511670
if (slen <= 0)
16521671
return; // Invalid or malformed session.
16531672

1654-
AllocatedBuffer sbuf = AllocatedBuffer::AllocateManaged(env, slen);
1655-
unsigned char* p = reinterpret_cast<unsigned char*>(sbuf.data());
1673+
std::unique_ptr<BackingStore> bs;
1674+
{
1675+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
1676+
bs = ArrayBuffer::NewBackingStore(env->isolate(), slen);
1677+
}
1678+
1679+
unsigned char* p = static_cast<unsigned char*>(bs->Data());
16561680
CHECK_LT(0, i2d_SSL_SESSION(sess, &p));
1657-
args.GetReturnValue().Set(sbuf.ToBuffer().FromMaybe(Local<Value>()));
1681+
1682+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
1683+
Local<Value> buffer;
1684+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
1685+
args.GetReturnValue().Set(buffer);
16581686
}
16591687

16601688
void TLSWrap::SetSession(const FunctionCallbackInfo<Value>& args) {
@@ -1825,7 +1853,11 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
18251853
uint32_t olen = args[0].As<Uint32>()->Value();
18261854
Utf8Value label(env->isolate(), args[1]);
18271855

1828-
AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, olen);
1856+
std::unique_ptr<BackingStore> bs;
1857+
{
1858+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
1859+
bs = ArrayBuffer::NewBackingStore(env->isolate(), olen);
1860+
}
18291861

18301862
ByteSource context;
18311863
bool use_context = !args[2]->IsUndefined();
@@ -1834,11 +1866,11 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
18341866

18351867
if (SSL_export_keying_material(
18361868
w->ssl_.get(),
1837-
reinterpret_cast<unsigned char*>(out.data()),
1869+
static_cast<unsigned char*>(bs->Data()),
18381870
olen,
18391871
*label,
18401872
label.length(),
1841-
reinterpret_cast<const unsigned char*>(context.get()),
1873+
context.data<unsigned char>(),
18421874
context.size(),
18431875
use_context) != 1) {
18441876
return ThrowCryptoError(
@@ -1847,7 +1879,10 @@ void TLSWrap::ExportKeyingMaterial(const FunctionCallbackInfo<Value>& args) {
18471879
"SSL_export_keying_material");
18481880
}
18491881

1850-
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
1882+
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
1883+
Local<Value> buffer;
1884+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&buffer)) return;
1885+
args.GetReturnValue().Set(buffer);
18511886
}
18521887

18531888
void TLSWrap::EndParser(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)