GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — tls: coalesce buffered records into one read · nodejs/node@fc502dd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/tls
    • throughput-s2c.js
  • src/crypto
    • crypto_tls.cc
  • test/parallel
    • test-double-tls-client.js
    • test-tls-coalesce-records.js
    • test-tls-coalesce-renegotiation.js
    • test-tls-max-send-fragment.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/tls/throughput-s2c.js‎

Copy file name to clipboardExpand all lines: benchmark/tls/throughput-s2c.js
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const bench = common.createBenchmark(main, {
66
sendchunklen: [256, 32 * 1024, 128 * 1024, 16 * 1024 * 1024],
77
recvbuflen: [0, 64 * 1024, 1024 * 1024],
88
recvbufgenfn: ['true', 'false'],
9+
recordlen: [1024, 4096, 16384],
910
});
1011

1112
const fixtures = require('../../test/common/fixtures');
@@ -14,7 +15,8 @@ let recvbuf;
1415
let received = 0;
1516
const tls = require('tls');
1617

17-
function main({ dur, type, sendchunklen, recvbuflen, recvbufgenfn }) {
18+
function main({ dur, type, sendchunklen, recvbuflen, recvbufgenfn,
19+
recordlen }) {
1820
if (isFinite(recvbuflen) && recvbuflen > 0)
1921
recvbuf = Buffer.alloc(recvbuflen);
2022

@@ -74,6 +76,7 @@ function main({ dur, type, sendchunklen, recvbuflen, recvbufgenfn }) {
7476
}
7577

7678
const server = tls.createServer(options, (socket) => {
79+
socket.setMaxSendFragment(recordlen);
7780
socket.on('data', (buf) => {
7881
socket.on('drain', write);
7982
write();
Collapse file

‎src/crypto/crypto_tls.cc‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_tls.cc
+48-14Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -870,20 +870,14 @@ void TLSWrap::ClearOut() {
870870
MarkPopErrorOnReturn mark_pop_error_on_return;
871871

872872
char out[kClearOutChunkSize];
873+
int length = 0;
873874
int read;
874-
for (;;) {
875-
{
876-
SSLLibraryCallScope ssl_library_call_scope(this);
877-
read = SSL_read(ssl_.get(), out, sizeof(out));
878-
}
879-
Debug(this, "Read %d bytes of cleartext output", read);
880-
881-
if (read <= 0)
882-
break;
883875

876+
// Emits out[0, length). Returns false if JS destroyed the SSL context.
877+
auto emit = [&]() {
884878
char* current = out;
885-
while (read > 0) {
886-
int avail = read;
879+
while (length > 0) {
880+
int avail = length;
887881

888882
uv_buf_t buf = EmitAlloc(avail);
889883
if (static_cast<int>(buf.len) < avail)
@@ -896,26 +890,60 @@ void TLSWrap::ClearOut() {
896890
// check that ssl_ != nullptr afterwards.
897891
if (ssl_ == nullptr) {
898892
Debug(this, "Returning from read loop, ssl_ == nullptr");
899-
return;
893+
return false;
900894
}
901895

902-
read -= avail;
896+
length -= avail;
903897
current += avail;
904898
}
899+
return true;
900+
};
901+
902+
for (;;) {
903+
{
904+
SSLLibraryCallScope ssl_library_call_scope(this);
905+
read = SSL_read(ssl_.get(), out + length, kClearOutChunkSize - length);
906+
}
907+
Debug(this, "Read %d bytes of cleartext output", read);
908+
909+
if (read <= 0) break;
910+
length += read;
911+
912+
// SSL_read() returns at most one record. If the next record is already
913+
// buffered, decrypt it into the same chunk instead of emitting one read
914+
// per record. This is limited to application data records that are too
915+
// long to be empty: other record types can trigger callbacks, e.g. for
916+
// renegotiation, that have to run after the preceding data has been
917+
// emitted, and SSL_read() skips over empty records to whatever follows.
918+
// With read-ahead disabled, enc_in_ is at a record boundary after a
919+
// successful read. The header need not be followed by a complete record:
920+
// SSL_read() can return WANT_READ, and the path below emits the batch.
921+
size_t pending;
922+
const unsigned char* next = reinterpret_cast<const unsigned char*>(
923+
NodeBIO::FromBIO(enc_in_)->Peek(&pending));
924+
if (length < kClearOutChunkSize && pending >= SSL3_RT_HEADER_LENGTH &&
925+
next[0] == SSL3_RT_APPLICATION_DATA &&
926+
(next[3] << 8 | next[4]) > SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
927+
continue;
928+
}
929+
930+
if (!emit()) return;
905931
}
906932

907933
// We need to check whether an error occurred or the connection was
908934
// shutdown cleanly (SSL_ERROR_ZERO_RETURN) even when read == 0.
909935
// See node#1642 and SSL_read(3SSL) for details. SSL_get_error must be
910936
// called immediately after SSL_read, without calling into JS, which may
911937
// change OpenSSL's error queue, modify ssl_, or even destroy ssl_
912-
// altogether.
938+
// altogether. Data that was read before the failure is emitted once the
939+
// error has been captured, before it is reported.
913940
if (read <= 0) {
914941
HandleScope handle_scope(env()->isolate());
915942
Local<Value> error;
916943
int err = SSL_get_error(ssl_.get(), read);
917944
switch (err) {
918945
case SSL_ERROR_ZERO_RETURN:
946+
if (!emit()) return;
919947
if (!flags_.eof) {
920948
flags_.eof = true;
921949
EmitRead(UV_EOF);
@@ -965,9 +993,15 @@ void TLSWrap::ClearOut() {
965993
break;
966994

967995
default:
996+
emit();
968997
return;
969998
}
970999

1000+
if (!emit()) {
1001+
Debug(this, "Got SSL error (%d) but not calling onerror", err);
1002+
return;
1003+
}
1004+
9711005
Debug(this, "Got SSL error (%d), calling onerror", err);
9721006
// When TLS Alert are stored in wbio,
9731007
// it should be flushed to socket before destroyed.
Collapse file

‎test/parallel/test-double-tls-client.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-double-tls-client.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ const server = tls.createServer({
2323
})
2424
.listen(client)
2525
.on('secureConnection', common.mustCall((serverTlsSock) => {
26-
serverTlsSock.on('data', common.mustCallAtLeast((chunk) => {
27-
assert.strictEqual(chunk.toString(), HEAD);
26+
serverTlsSock.once('data', common.mustCall((chunk) => {
27+
assert.strictEqual(chunk.toString('latin1', 0, HEAD.length), HEAD);
28+
// The start of the inner handshake can arrive in the same chunk as
29+
// HEAD. Put it back so that the inner TLSSocket, which reads whatever
30+
// is buffered on the socket it wraps, receives it.
31+
if (chunk.length > HEAD.length) {
32+
serverTlsSock.pause();
33+
serverTlsSock.unshift(chunk.subarray(HEAD.length));
34+
}
2835
subserver.emit('connection', serverTlsSock);
2936
}));
3037
}));
Collapse file

‎test/parallel/test-tls-coalesce-records.js‎

Copy file name to clipboard
+130Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
'use strict';
2+
// TLS records that are already buffered are emitted as one chunk rather than
3+
// one chunk per record. The end of the stream, clean or not, must still be
4+
// reported after the data that precedes it.
5+
6+
const common = require('../common');
7+
if (!common.hasCrypto)
8+
common.skip('missing crypto');
9+
10+
const { isBoringSSL } = require('../common/crypto');
11+
const assert = require('assert');
12+
const net = require('net');
13+
const tls = require('tls');
14+
const fixtures = require('../common/fixtures');
15+
16+
const RECORD_SIZE = 512;
17+
const RECORDS = 256;
18+
const payload = Buffer.alloc(RECORD_SIZE * RECORDS);
19+
for (let i = 0; i < RECORDS; i++)
20+
payload.fill(i, i * RECORD_SIZE, (i + 1) * RECORD_SIZE);
21+
22+
const serverOptions = {
23+
key: fixtures.readKey('agent1-key.pem'),
24+
cert: fixtures.readKey('agent1-cert.pem'),
25+
};
26+
const clientOptions = {
27+
ca: fixtures.readKey('ca1-cert.pem'),
28+
servername: 'agent1',
29+
};
30+
31+
function sendPayload(socket) {
32+
assert(socket.setMaxSendFragment(RECORD_SIZE));
33+
socket.end(payload);
34+
}
35+
36+
// Small records are coalesced, and 'end' follows all of the data.
37+
{
38+
const server = tls.createServer(serverOptions, common.mustCall(sendPayload));
39+
server.listen(0, common.mustCall(() => {
40+
const chunks = [];
41+
const client = tls.connect({ ...clientOptions, port: server.address().port });
42+
client.on('data', (chunk) => chunks.push(chunk));
43+
client.on('end', common.mustCall(() => {
44+
assert.deepStrictEqual(Buffer.concat(chunks), payload);
45+
assert(chunks.length < RECORDS, `${chunks.length} chunks`);
46+
assert(chunks.every((chunk) => chunk.length <= 16 * 1024));
47+
server.close();
48+
}));
49+
}));
50+
}
51+
52+
// An onread buffer smaller than a record still receives every byte.
53+
{
54+
const server = tls.createServer(serverOptions, common.mustCall(sendPayload));
55+
server.listen(0, common.mustCall(() => {
56+
const chunks = [];
57+
const client = tls.connect({
58+
...clientOptions,
59+
port: server.address().port,
60+
onread: {
61+
buffer: Buffer.alloc(100),
62+
callback(nread, buf) {
63+
chunks.push(Buffer.from(buf.subarray(0, nread)));
64+
},
65+
},
66+
});
67+
client.on('end', common.mustCall(() => {
68+
assert.deepStrictEqual(Buffer.concat(chunks), payload);
69+
server.close();
70+
}));
71+
}));
72+
}
73+
74+
// A bad record that arrives together with data is reported after the data.
75+
{
76+
// Small enough to arrive in one read and to fit into one batch.
77+
const data = payload.subarray(0, 8 * RECORD_SIZE);
78+
const server = tls.createServer(serverOptions, common.mustCall((socket) => {
79+
socket.once('data', common.mustCall(() => {
80+
assert(socket.setMaxSendFragment(RECORD_SIZE));
81+
socket.end(data);
82+
}));
83+
}));
84+
server.listen(0, common.mustCall(() => {
85+
// Passes the handshake through, then holds the response until the server
86+
// ends and forwards it in one write, with the last data record (the one
87+
// before close_notify) corrupted.
88+
let holding = false;
89+
const held = [];
90+
const proxy = net.createServer(common.mustCall((socket) => {
91+
const upstream = net.connect(server.address().port);
92+
socket.pipe(upstream);
93+
socket.on('error', () => {});
94+
upstream.on('data', (chunk) => {
95+
if (holding)
96+
held.push(chunk);
97+
else
98+
socket.write(chunk);
99+
});
100+
upstream.on('end', common.mustCall(() => {
101+
const response = Buffer.concat(held);
102+
let closeNotify = 0;
103+
for (let i = 0; i < response.length; i += 5 + response.readUInt16BE(i + 3))
104+
closeNotify = i;
105+
response[closeNotify - 1] ^= 1;
106+
socket.end(response);
107+
}));
108+
}));
109+
proxy.listen(0, common.mustCall(() => {
110+
const chunks = [];
111+
const client = tls.connect({
112+
...clientOptions,
113+
port: proxy.address().port,
114+
}, common.mustCall(() => {
115+
holding = true;
116+
client.write('go');
117+
}));
118+
client.on('data', (chunk) => chunks.push(chunk));
119+
client.on('error', common.mustCall((err) => {
120+
assert.strictEqual(err.code, isBoringSSL ?
121+
'ERR_SSL_BAD_DECRYPT' :
122+
'ERR_SSL_DECRYPTION_FAILED_OR_BAD_RECORD_MAC');
123+
assert.deepStrictEqual(Buffer.concat(chunks),
124+
data.subarray(0, -RECORD_SIZE));
125+
proxy.close();
126+
server.close();
127+
}));
128+
}));
129+
}));
130+
}
Collapse file

‎test/parallel/test-tls-coalesce-renegotiation.js‎

Copy file name to clipboard
+82Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
const { isBoringSSL } = require('../common/crypto');
6+
if (isBoringSSL)
7+
common.skip('BoringSSL does not support renegotiation');
8+
9+
const assert = require('assert');
10+
const net = require('net');
11+
const tls = require('tls');
12+
const fixtures = require('../common/fixtures');
13+
14+
// Deliver application data and a TLS 1.2 ClientHello in one transport write.
15+
// The data handler must run before renegotiation starts, so it can disable it.
16+
const payload = Buffer.from('disable renegotiation');
17+
let holding = false;
18+
let client;
19+
20+
const server = tls.createServer({
21+
key: fixtures.readKey('agent1-key.pem'),
22+
cert: fixtures.readKey('agent1-cert.pem'),
23+
maxVersion: 'TLSv1.2',
24+
}, common.mustCall((socket) => {
25+
socket.on('data', common.mustCall((data) => {
26+
assert.deepStrictEqual(data, payload);
27+
socket.disableRenegotiation();
28+
}));
29+
socket.on('secure', common.mustNotCall('renegotiation completed'));
30+
socket.on('error', common.mustCall((err) => {
31+
assert.strictEqual(err.code, 'ERR_TLS_RENEGOTIATION_DISABLED');
32+
client.destroy();
33+
socket.destroy();
34+
server.close();
35+
proxy.close();
36+
}));
37+
}));
38+
39+
const proxy = net.createServer(common.mustCall((socket) => {
40+
const upstream = net.connect(server.address().port);
41+
upstream.pipe(socket);
42+
socket.on('end', () => upstream.end());
43+
socket.on('close', () => upstream.destroy());
44+
const held = [];
45+
let pending = Buffer.alloc(0);
46+
socket.on('data', (data) => {
47+
if (!holding) {
48+
upstream.write(data);
49+
return;
50+
}
51+
held.push(data);
52+
pending = Buffer.concat([pending, data]);
53+
while (pending.length >= 5) {
54+
const length = pending.readUInt16BE(3);
55+
if (pending.length < 5 + length)
56+
break;
57+
if (pending[0] === 22) { // handshake
58+
holding = false;
59+
upstream.write(Buffer.concat(held));
60+
return;
61+
}
62+
pending = pending.subarray(5 + length);
63+
}
64+
});
65+
}));
66+
67+
server.listen(0, common.mustCall(() => {
68+
proxy.listen(0, common.mustCall(() => {
69+
client = tls.connect({
70+
port: proxy.address().port,
71+
ca: fixtures.readKey('ca1-cert.pem'),
72+
servername: 'agent1',
73+
maxVersion: 'TLSv1.2',
74+
}, common.mustCall(() => {
75+
holding = true;
76+
client.write(payload, common.mustCall(() => {
77+
assert(client.renegotiate({},
78+
common.mustNotCall('renegotiation completed')));
79+
}));
80+
}));
81+
}));
82+
}));

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.