Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
4f41e4f
n-api: implement date object
jarrodconnolly Feb 4, 2019
f862276
n-api: make func argument of napi_create_threadsafe_function optional
legendecas May 21, 2019
859d475
src: reduce platform worker barrier lifetime
ofrobots Oct 11, 2018
a3eda28
doc: fixup changelog for v10.16.3
andrewhughes101 Aug 16, 2019
7735824
crypto: increase maxmem range from 32 to 53 bits
tniessen Jul 21, 2019
c74c6a5
n-api: mark version 5 N-APIs as stable
Sep 5, 2019
36a0e9a
http2: do not crash on stream listener removal w/ destroyed session
addaleax Sep 5, 2019
69b0212
http2: do not start reading after write if new write is on wire
addaleax Sep 1, 2019
15c2eb0
doc: update N-API version matrix
Sep 21, 2019
7f48519
deps: do not link against librt
sam-github Sep 26, 2019
56a834a
doc,test: clarify that Http2Stream is destroyed after data is read
mildsunrise May 26, 2019
f78ecc3
test: fix race in test-http2-origin
mildsunrise Jul 30, 2019
fe58bca
tls: group chunks into TLS segments
mildsunrise May 24, 2019
ccf2823
http: makes response.writeHead return the response
qubyte Feb 6, 2019
9258496
http2: makes response.writeHead return the response
qubyte Feb 6, 2019
2afbb3e
test,win: cleanup exec-timeout processes
joaocgreis Jul 16, 2019
cef5010
doc: describe tls.DEFAULT_MIN_VERSION/_MAX_VERSION
ckarande Jul 23, 2019
c285e69
doc: fix the links tls default version sections
ckarande Jul 24, 2019
b43d7e8
process: add --unhandled-rejections flag
BridgeAR Mar 11, 2019
2eae030
worker: add missing return value in case of fatal exceptions
BridgeAR Sep 24, 2019
9c393f1
deps: upgrade openssl sources to 1.1.1d
sam-github Oct 10, 2019
7202792
deps: update archs files for OpenSSL-1.1.1d
sam-github Oct 10, 2019
13d8549
test: well-defined DH groups now verify clean
sam-github Sep 13, 2019
69bf5b7
net: treat ENOTCONN at shutdown as success
addaleax Oct 10, 2019
85ce8ef
fs: remove experimental warning for fs.promises
addaleax Mar 11, 2019
333963e
deps: dlloads node static linked executable
lal12 Jun 4, 2019
ddb5152
stream: implement Readable.from async iterator utility
guybedford May 12, 2019
e8c90bf
zlib: do not coalesce multiple `.flush()` calls
addaleax Jul 3, 2019
66387cd
http2: send out pending data earlier
addaleax Sep 1, 2019
f1a5a36
build: update Windows icon to Feb 2016 rebrand
mikemaccana Jul 3, 2019
d6c998a
process: use public readableFlowing property
ckarande Sep 8, 2019
65e68d1
doc: add documentation for stream readableFlowing
ckarande Sep 9, 2019
1bb5102
src: use more explicit return type in Sign::SignFinal()
addaleax Oct 20, 2018
18b140a
src: use maybe version v8::Function::Call
oyyd Oct 23, 2018
00831f0
stream: make Symbol.asyncIterator support stable
mcollina Mar 29, 2019
79f3844
readline: make Symbol.asyncIterator support stable
mcollina Apr 1, 2019
fa27aac
dns: remove dns.promises experimental warning
cjihrig Mar 11, 2019
fcc22d3
dns: make dns.promises enumerable
cjihrig Mar 27, 2019
90fb146
doc: move dns.promises to stable status
cjihrig Mar 27, 2019
4f0f12c
crypto: fix rsa key gen with non-default exponent
sam-github Apr 4, 2019
03b6966
deps: upgrade npm to 6.10.0
Jul 3, 2019
e2291cf
deps: upgrade npm to 6.10.2
isaacs Jul 25, 2019
55cd01c
deps: update npm to 6.10.3
isaacs Aug 6, 2019
e53dbba
deps: update npm to 6.11.3
claudiahdz Sep 3, 2019
63de2ad
crypto: add support for chacha20-poly1305 for AEAD
chux0519 Nov 4, 2018
5dae3ef
2019-10-22, Version 10.17.0 'Dubnium' (LTS)
BethGriggs Oct 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tls: group chunks into TLS segments
TLSWrap::DoWrite() now concatenates data chunks and makes a single
call to SSL_write(). Grouping data into a single segment:

- reduces network overhead: by factors of even 2 or 3 in usages
  like `http2` or `form-data`

- improves security: segment lengths can reveal lots of info, i.e.
  with `form-data`, how many fields are sent and the approximate length
  of every individual field and its headers

- reduces encryption overhead: a quick benchmark showed a ~30% CPU time
  decrease for an extreme case, see
  #27573 (comment)

Fixes: #27573

Backport-PR-URL: #28904
PR-URL: #27861
Reviewed-By: Fedor Indutny <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
mildsunrise authored and BethGriggs committed Oct 7, 2019
commit fe58bca878ee1925199a2a86ea4ff0fb3deecf71
86 changes: 42 additions & 44 deletions src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void TLSWrap::EncOut() {
// No encrypted output ready to write to the underlying stream.
if (BIO_pending(enc_out_) == 0) {
Debug(this, "No pending encrypted output");
if (pending_cleartext_input_.empty())
if (pending_cleartext_input_.size() == 0)
InvokeQueued(0);
return;
}
Expand Down Expand Up @@ -509,28 +509,21 @@ void TLSWrap::ClearIn() {
return;
}

std::vector<uv_buf_t> buffers;
buffers.swap(pending_cleartext_input_);
if (pending_cleartext_input_.size() == 0) {
Debug(this, "Returning from ClearIn(), no pending data");
return;
}

std::vector<char> data = std::move(pending_cleartext_input_);
crypto::MarkPopErrorOnReturn mark_pop_error_on_return;

size_t i;
int written = 0;
for (i = 0; i < buffers.size(); ++i) {
size_t avail = buffers[i].len;
char* data = buffers[i].base;
written = SSL_write(ssl_.get(), data, avail);
Debug(this, "Writing %zu bytes, written = %d", avail, written);
CHECK(written == -1 || written == static_cast<int>(avail));
if (written == -1)
break;
}
int written = SSL_write(ssl_.get(), data.data(), data.size());
Debug(this, "Writing %zu bytes, written = %d", data.size(), written);
CHECK(written == -1 || written == static_cast<int>(data.size()));

// All written
if (i == buffers.size()) {
if (written != -1) {
Debug(this, "Successfully wrote all data to SSL");
// We wrote all the buffers, so no writes failed (written < 0 on failure).
CHECK_GE(written, 0);
return;
}

Expand All @@ -548,13 +541,10 @@ void TLSWrap::ClearIn() {
// possible.
InvokeQueued(UV_EPROTO, error_str.c_str());
} else {
Debug(this, "Pushing back %zu buffers", buffers.size() - i);
// Push back the not-yet-written pending buffers into their queue.
// This can be skipped in the error case because no further writes
// would succeed anyway.
pending_cleartext_input_.insert(pending_cleartext_input_.end(),
buffers.begin() + i,
buffers.end());
Debug(this, "Pushing data back");
// Push back the not-yet-written data. This can be skipped in the error
// case because no further writes would succeed anyway.
pending_cleartext_input_ = std::move(data);
}

return;
Expand Down Expand Up @@ -640,14 +630,10 @@ int TLSWrap::DoWrite(WriteWrap* w,
return UV_EPROTO;
}

bool empty = true;
size_t length = 0;
size_t i;
for (i = 0; i < count; i++) {
if (bufs[i].len > 0) {
empty = false;
break;
}
}
for (i = 0; i < count; i++)
length += bufs[i].len;

// We want to trigger a Write() on the underlying stream to drive the stream
// system, but don't want to encrypt empty buffers into a TLS frame, so see
Expand All @@ -659,7 +645,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
// stream. Since the bufs are empty, it won't actually write non-TLS data
// onto the socket, we just want the side-effects. After, make sure the
// WriteWrap was accepted by the stream, or that we call Done() on it.
if (empty) {
if (length == 0) {
Debug(this, "Empty write");
ClearOut();
if (BIO_pending(enc_out_) == 0) {
Expand All @@ -683,23 +669,36 @@ int TLSWrap::DoWrite(WriteWrap* w,
current_write_ = w;

// Write encrypted data to underlying stream and call Done().
if (empty) {
if (length == 0) {
EncOut();
return 0;
}

std::vector<char> data;
crypto::MarkPopErrorOnReturn mark_pop_error_on_return;

int written = 0;
for (i = 0; i < count; i++) {
written = SSL_write(ssl_.get(), bufs[i].base, bufs[i].len);
CHECK(written == -1 || written == static_cast<int>(bufs[i].len));
Debug(this, "Writing %zu bytes, written = %d", bufs[i].len, written);
if (written == -1)
break;
if (count != 1) {
data = std::vector<char>(length);
size_t offset = 0;
for (i = 0; i < count; i++) {
memcpy(data.data() + offset, bufs[i].base, bufs[i].len);
offset += bufs[i].len;
}
written = SSL_write(ssl_.get(), data.data(), length);
} else {
// Only one buffer: try to write directly, only store if it fails
written = SSL_write(ssl_.get(), bufs[0].base, bufs[0].len);
if (written == -1) {
data = std::vector<char>(length);
memcpy(data.data(), bufs[0].base, bufs[0].len);
}
}

if (i != count) {
CHECK(written == -1 || written == static_cast<int>(length));
Debug(this, "Writing %zu bytes, written = %d", length, written);

if (written == -1) {
int err;
Local<Value> arg = GetSSLError(written, &err, &error_);

Expand All @@ -710,11 +709,10 @@ int TLSWrap::DoWrite(WriteWrap* w,
return UV_EPROTO;
}

Debug(this, "Saving %zu buffers for later write", count - i);
Debug(this, "Saving data for later write");
// Otherwise, save unwritten data so it can be written later by ClearIn().
pending_cleartext_input_.insert(pending_cleartext_input_.end(),
&bufs[i],
&bufs[count]);
CHECK_EQ(pending_cleartext_input_.size(), 0);
pending_cleartext_input_ = std::move(data);
}

// Write any encrypted/handshake output that may be ready.
Expand Down
2 changes: 1 addition & 1 deletion src/tls_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class TLSWrap : public AsyncWrap,
BIO* enc_in_ = nullptr; // StreamListener fills this for SSL_read().
BIO* enc_out_ = nullptr; // SSL_write()/handshake fills this for EncOut().
// Waiting for ClearIn() to pass to SSL_write().
std::vector<uv_buf_t> pending_cleartext_input_;
std::vector<char> pending_cleartext_input_;
size_t write_size_ = 0;
WriteWrap* current_write_ = nullptr;
WriteWrap* current_empty_write_ = nullptr;
Expand Down