Skip to content

Commit e5f6644

Browse files
committed
src: move more crypto_dh.cc code to ncrypto
nodejs/node#54459
1 parent 1510d8a commit e5f6644

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

patches/node/support_v8_sandboxed_pointers.patch

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,38 @@ index 5fc1b6f2446d7c786024eb60800e2edab613dcd1..f59abcb21d64b910d8d42eb23c03109f
2525
void* NodeArrayBufferAllocator::Allocate(size_t size) {
2626
void* ret;
2727
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers)
28+
diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc
29+
index c9e32ee754cec137f3e7673956e6af1360cb767f..9851b90aad27084504e5b4b0bfd861097d57b180 100644
30+
--- a/src/crypto/crypto_dh.cc
31+
+++ b/src/crypto/crypto_dh.cc
32+
@@ -51,6 +51,19 @@ void DiffieHellman::MemoryInfo(MemoryTracker* tracker) const {
33+
namespace {
34+
MaybeLocal<Value> DataPointerToBuffer(Environment* env,
35+
ncrypto::DataPointer&& data) {
36+
+#if defined(V8_ENABLE_SANDBOX)
37+
+ std::unique_ptr<ArrayBuffer::Allocator> allocator(ArrayBuffer::Allocator::NewDefaultAllocator());
38+
+ void* v8_data = allocator->Allocate(data.size());
39+
+ CHECK(v8_data);
40+
+ memcpy(v8_data, data.get(), data.size());
41+
+ std::unique_ptr<v8::BackingStore> backing = ArrayBuffer::NewBackingStore(
42+
+ v8_data,
43+
+ data.size(),
44+
+ [](void* data, size_t length, void*) {
45+
+ std::unique_ptr<ArrayBuffer::Allocator> allocator(ArrayBuffer::Allocator::NewDefaultAllocator());
46+
+ allocator->Free(data, length);
47+
+ }, nullptr);
48+
+#else
49+
auto backing = ArrayBuffer::NewBackingStore(
50+
data.get(),
51+
data.size(),
52+
@@ -59,6 +72,7 @@ MaybeLocal<Value> DataPointerToBuffer(Environment* env,
53+
},
54+
nullptr);
55+
data.release();
56+
+#endif
57+
58+
auto ab = ArrayBuffer::New(env->isolate(), std::move(backing));
59+
return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Value>());
2860
diff --git a/src/crypto/crypto_util.cc b/src/crypto/crypto_util.cc
2961
index ee81048caab4ccfe26ea9e677782c9c955d162a9..643c9d31dc2737faa2ec51684ffceb35a1014a58 100644
3062
--- a/src/crypto/crypto_util.cc
@@ -117,6 +149,39 @@ index 922e77091d72172ed6cfc5e8477901e3608396c5..16a236c69caed6d60248c7973531a95a
117149

118150
v8::Local<v8::ArrayBuffer> ToArrayBuffer(Environment* env);
119151

152+
diff --git a/src/crypto/crypto_x509.cc b/src/crypto/crypto_x509.cc
153+
index af2f953f0388dbce326b0c519de3883552f8f009..7fb34057a486914dd886ec4d3d23be90cccb4fea 100644
154+
--- a/src/crypto/crypto_x509.cc
155+
+++ b/src/crypto/crypto_x509.cc
156+
@@ -174,6 +174,19 @@ MaybeLocal<Value> ToV8Value(Local<Context> context, const BIOPointer& bio) {
157+
MaybeLocal<Value> ToBuffer(Environment* env, BIOPointer* bio) {
158+
if (bio == nullptr || !*bio) return {};
159+
BUF_MEM* mem = *bio;
160+
+#if defined(V8_ENABLE_SANDBOX)
161+
+ std::unique_ptr<ArrayBuffer::Allocator> allocator(ArrayBuffer::Allocator::NewDefaultAllocator());
162+
+ void* v8_data = allocator->Allocate(mem->length);
163+
+ CHECK(v8_data);
164+
+ memcpy(v8_data, mem->data, mem->length);
165+
+ std::unique_ptr<v8::BackingStore> backing = ArrayBuffer::NewBackingStore(
166+
+ v8_data,
167+
+ mem->length,
168+
+ [](void* data, size_t length, void*) {
169+
+ std::unique_ptr<ArrayBuffer::Allocator> allocator(ArrayBuffer::Allocator::NewDefaultAllocator());
170+
+ allocator->Free(data, length);
171+
+ }, nullptr);
172+
+#else
173+
auto backing = ArrayBuffer::NewBackingStore(
174+
mem->data,
175+
mem->length,
176+
@@ -181,6 +194,8 @@ MaybeLocal<Value> ToBuffer(Environment* env, BIOPointer* bio) {
177+
BIOPointer free_me(static_cast<BIO*>(data));
178+
},
179+
bio->release());
180+
+#endif
181+
+
182+
auto ab = ArrayBuffer::New(env->isolate(), std::move(backing));
183+
Local<Value> ret;
184+
if (!Buffer::New(env, ab, 0, ab->ByteLength()).ToLocal(&ret)) return {};
120185
diff --git a/src/node_i18n.cc b/src/node_i18n.cc
121186
index 43bb68351bf0a68285e464601013bbdbd5d5df5f..4126bbff080548c91154a6dcc437359c82a6c771 100644
122187
--- a/src/node_i18n.cc

0 commit comments

Comments
 (0)