Skip to content

Commit

Permalink
src: remove unnecessary uses of Buffer from crypto::KeylogCallback()
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
  • Loading branch information
RaisinTen committed Sep 4, 2021
1 parent f26c2ce commit 5060505
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/crypto/crypto_tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::Context;
using v8::DontDelete;
using v8::EscapableHandleScope;
Expand All @@ -59,6 +61,7 @@ using v8::Signature;
using v8::String;
using v8::True;
using v8::Uint32;
using v8::Uint8Array;
using v8::Value;

namespace crypto {
Expand Down Expand Up @@ -132,8 +135,11 @@ void KeylogCallback(const SSL* s, const char* line) {
int NewSessionCallback(SSL* s, SSL_SESSION* sess) {
TLSWrap* w = static_cast<TLSWrap*>(SSL_get_app_data(s));
Environment* env = w->env();
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Isolate* isolate = env->isolate();
HandleScope handle_scope(isolate);
Local<Context> context = env->context();
Context::Scope context_scope(context);
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());

if (!w->has_session_callbacks())
return 0;
Expand All @@ -144,32 +150,47 @@ int NewSessionCallback(SSL* s, SSL_SESSION* sess) {
return 0;

// Serialize session
// TODO(@jasnell): An AllocatedBuffer or BackingStore would be better
// here to start eliminating unnecessary uses of Buffer where an ordinary
// Uint8Array would do just fine.
Local<Object> session = Buffer::New(env, size).FromMaybe(Local<Object>());
if (UNLIKELY(session.IsEmpty()))

Local<Object> buffer_prototype_object = env->buffer_prototype_object();
if (UNLIKELY(buffer_prototype_object.IsEmpty()))
return 0;

unsigned char* session_data =
reinterpret_cast<unsigned char*>(Buffer::Data(session));
Local<Object> session;
{
std::unique_ptr<BackingStore> bs =
ArrayBuffer::NewBackingStore(isolate, size);

unsigned char* session_data = reinterpret_cast<unsigned char*>(bs->Data());
i2d_SSL_SESSION(sess, &session_data);

Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, std::move(bs));
Local<Uint8Array> ui = Uint8Array::New(ab, 0, ab->ByteLength());
if (UNLIKELY(ui->SetPrototype(context,
buffer_prototype_object).IsNothing()))
return 0;

memset(session_data, 0, size);
i2d_SSL_SESSION(sess, &session_data);
session = ui;
}

unsigned int session_id_length;
const unsigned char* session_id_data =
SSL_SESSION_get_id(sess, &session_id_length);

// TODO(@jasnell): An AllocatedBuffer or BackingStore would be better
// here to start eliminating unnecessary uses of Buffer where an ordinary
// Uint8Array would do just fine
Local<Object> session_id = Buffer::Copy(
env,
reinterpret_cast<const char*>(session_id_data),
session_id_length).FromMaybe(Local<Object>());
if (UNLIKELY(session_id.IsEmpty()))
return 0;
Local<Object> session_id;
{
std::unique_ptr<BackingStore> bs =
ArrayBuffer::NewBackingStore(isolate, session_id_length);

memcpy(bs->Data(), session_id_data, session_id_length);

Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, std::move(bs));
Local<Uint8Array> ui = Uint8Array::New(ab, 0, ab->ByteLength());
if (UNLIKELY(ui->SetPrototype(context,
buffer_prototype_object).IsNothing()))
return 0;

session_id = ui;
}

Local<Value> argv[] = {
session_id,
Expand Down

0 comments on commit 5060505

Please sign in to comment.