|
| 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 assert = require('assert'); |
| 11 | +const net = require('net'); |
| 12 | +const tls = require('tls'); |
| 13 | +const fixtures = require('../common/fixtures'); |
| 14 | + |
| 15 | +const RECORD_SIZE = 512; |
| 16 | +const RECORDS = 256; |
| 17 | +const payload = Buffer.alloc(RECORD_SIZE * RECORDS); |
| 18 | +for (let i = 0; i < RECORDS; i++) |
| 19 | + payload.fill(i, i * RECORD_SIZE, (i + 1) * RECORD_SIZE); |
| 20 | + |
| 21 | +const serverOptions = { |
| 22 | + key: fixtures.readKey('agent1-key.pem'), |
| 23 | + cert: fixtures.readKey('agent1-cert.pem'), |
| 24 | +}; |
| 25 | +const clientOptions = { |
| 26 | + ca: fixtures.readKey('ca1-cert.pem'), |
| 27 | + servername: 'agent1', |
| 28 | +}; |
| 29 | + |
| 30 | +function sendPayload(socket) { |
| 31 | + assert(socket.setMaxSendFragment(RECORD_SIZE)); |
| 32 | + socket.end(payload); |
| 33 | +} |
| 34 | + |
| 35 | +// Small records are coalesced, and 'end' follows all of the data. |
| 36 | +{ |
| 37 | + const server = tls.createServer(serverOptions, common.mustCall(sendPayload)); |
| 38 | + server.listen(0, common.mustCall(() => { |
| 39 | + const chunks = []; |
| 40 | + const client = tls.connect({ ...clientOptions, port: server.address().port }); |
| 41 | + client.on('data', (chunk) => chunks.push(chunk)); |
| 42 | + client.on('end', common.mustCall(() => { |
| 43 | + assert.deepStrictEqual(Buffer.concat(chunks), payload); |
| 44 | + assert(chunks.length < RECORDS, `${chunks.length} chunks`); |
| 45 | + assert(chunks.every((chunk) => chunk.length <= 64 * 1024)); |
| 46 | + server.close(); |
| 47 | + })); |
| 48 | + })); |
| 49 | +} |
| 50 | + |
| 51 | +// An onread buffer smaller than a record still receives every byte. |
| 52 | +{ |
| 53 | + const server = tls.createServer(serverOptions, common.mustCall(sendPayload)); |
| 54 | + server.listen(0, common.mustCall(() => { |
| 55 | + const chunks = []; |
| 56 | + const client = tls.connect({ |
| 57 | + ...clientOptions, |
| 58 | + port: server.address().port, |
| 59 | + onread: { |
| 60 | + buffer: Buffer.alloc(100), |
| 61 | + callback(nread, buf) { |
| 62 | + chunks.push(Buffer.from(buf.subarray(0, nread))); |
| 63 | + }, |
| 64 | + }, |
| 65 | + }); |
| 66 | + client.on('end', common.mustCall(() => { |
| 67 | + assert.deepStrictEqual(Buffer.concat(chunks), payload); |
| 68 | + server.close(); |
| 69 | + })); |
| 70 | + })); |
| 71 | +} |
| 72 | + |
| 73 | +// A bad record that arrives together with data is reported after the data. |
| 74 | +{ |
| 75 | + const server = tls.createServer(serverOptions, common.mustCall((socket) => { |
| 76 | + socket.once('data', common.mustCall(() => sendPayload(socket))); |
| 77 | + })); |
| 78 | + server.listen(0, common.mustCall(() => { |
| 79 | + // Passes the handshake through, then holds the response until the server |
| 80 | + // ends and forwards it in one write, with the last record (close_notify) |
| 81 | + // corrupted. |
| 82 | + let holding = false; |
| 83 | + const held = []; |
| 84 | + const proxy = net.createServer(common.mustCall((socket) => { |
| 85 | + const upstream = net.connect(server.address().port); |
| 86 | + socket.pipe(upstream); |
| 87 | + socket.on('error', () => {}); |
| 88 | + upstream.on('data', (chunk) => { |
| 89 | + if (holding) |
| 90 | + held.push(chunk); |
| 91 | + else |
| 92 | + socket.write(chunk); |
| 93 | + }); |
| 94 | + upstream.on('end', common.mustCall(() => { |
| 95 | + const data = Buffer.concat(held); |
| 96 | + data[data.length - 1] ^= 1; |
| 97 | + socket.end(data); |
| 98 | + })); |
| 99 | + })); |
| 100 | + proxy.listen(0, common.mustCall(() => { |
| 101 | + const chunks = []; |
| 102 | + const client = tls.connect({ |
| 103 | + ...clientOptions, |
| 104 | + port: proxy.address().port, |
| 105 | + }, common.mustCall(() => { |
| 106 | + holding = true; |
| 107 | + client.write('go'); |
| 108 | + })); |
| 109 | + client.on('data', (chunk) => chunks.push(chunk)); |
| 110 | + client.on('error', common.mustCall((err) => { |
| 111 | + assert.strictEqual(err.code, |
| 112 | + 'ERR_SSL_DECRYPTION_FAILED_OR_BAD_RECORD_MAC'); |
| 113 | + assert.deepStrictEqual(Buffer.concat(chunks), payload); |
| 114 | + proxy.close(); |
| 115 | + server.close(); |
| 116 | + })); |
| 117 | + })); |
| 118 | + })); |
| 119 | +} |
0 commit comments