|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +// Tests that calling disableRenegotiation on a TLSSocket stops renegotiation. |
| 7 | + |
| 8 | +if (!common.hasCrypto) { |
| 9 | + common.skip('missing crypto'); |
| 10 | + return; |
| 11 | +} |
| 12 | +const tls = require('tls'); |
| 13 | + |
| 14 | +const options = { |
| 15 | + key: fs.readFileSync(`${common.fixturesDir}/keys/agent1-key.pem`), |
| 16 | + cert: fs.readFileSync(`${common.fixturesDir}/keys/agent1-cert.pem`) |
| 17 | +}; |
| 18 | + |
| 19 | +const server = tls.Server(options, common.mustCall((socket) => { |
| 20 | + socket.on('error', common.mustCall((err) => { |
| 21 | + assert.strictEqual( |
| 22 | + err.message, |
| 23 | + 'TLS session renegotiation disabled for this socket'); |
| 24 | + socket.destroy(); |
| 25 | + server.close(); |
| 26 | + })); |
| 27 | + // Disable renegotiation after the first chunk of data received. |
| 28 | + // Demonstrates that renegotiation works successfully up until |
| 29 | + // disableRenegotiation is called. |
| 30 | + socket.on('data', common.mustCall((chunk) => { |
| 31 | + socket.write(chunk); |
| 32 | + socket.disableRenegotiation(); |
| 33 | + })); |
| 34 | + socket.on('secure', common.mustCall(() => { |
| 35 | + assert(socket._handle.handshakes < 2, |
| 36 | + `Too many handshakes [${socket._handle.handshakes}]`); |
| 37 | + })); |
| 38 | +})); |
| 39 | + |
| 40 | + |
| 41 | +server.listen(0, common.mustCall(() => { |
| 42 | + const port = server.address().port; |
| 43 | + const client = |
| 44 | + tls.connect({rejectUnauthorized: false, port: port}, common.mustCall(() => { |
| 45 | + client.write(''); |
| 46 | + // Negotiation is still permitted for this first |
| 47 | + // attempt. This should succeed. |
| 48 | + client.renegotiate( |
| 49 | + {rejectUnauthorized: false}, |
| 50 | + common.mustCall(() => { |
| 51 | + // Once renegotiation completes, we write some |
| 52 | + // data to the socket, which triggers the on |
| 53 | + // data event on the server. After that data |
| 54 | + // is received, disableRenegotiation is called. |
| 55 | + client.write('data', common.mustCall(() => { |
| 56 | + client.write(''); |
| 57 | + // This second renegotiation attempt should fail |
| 58 | + // and the callback should never be invoked. The |
| 59 | + // server will simply drop the connection after |
| 60 | + // emitting the error. |
| 61 | + client.renegotiate( |
| 62 | + {rejectUnauthorized: false}, |
| 63 | + common.mustNotCall()); |
| 64 | + })); |
| 65 | + })); |
| 66 | + })); |
| 67 | +})); |
0 commit comments