|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// A server with no session id context of its own - possible when creating a |
| 4 | +// TLSSocket directly - can't tell which application a session belongs to. One |
| 5 | +// that verifies client certificates mustn't resume any, or a session from an |
| 6 | +// application sharing its secure context (and so its ticket keys) would skip |
| 7 | +// the client's certificate check. It gets a full handshake instead. |
| 8 | + |
| 9 | +const common = require('../common'); |
| 10 | + |
| 11 | +if (!common.hasCrypto) |
| 12 | + common.skip('missing crypto'); |
| 13 | + |
| 14 | +const assert = require('node:assert'); |
| 15 | +const net = require('node:net'); |
| 16 | +const tls = require('node:tls'); |
| 17 | +const fixtures = require('../common/fixtures'); |
| 18 | + |
| 19 | +const secureContext = tls.createSecureContext({ |
| 20 | + key: fixtures.readKey('agent2-key.pem'), |
| 21 | + cert: fixtures.readKey('agent2-cert.pem'), |
| 22 | +}); |
| 23 | + |
| 24 | +// Whether the server resumed each connection it accepted, in order. |
| 25 | +const reused = []; |
| 26 | + |
| 27 | +async function listen(requestCert) { |
| 28 | + const server = net.createServer((socket) => { |
| 29 | + const tlsSocket = new tls.TLSSocket(socket, { |
| 30 | + isServer: true, |
| 31 | + secureContext, |
| 32 | + requestCert, |
| 33 | + }); |
| 34 | + tlsSocket.on('secure', () => { |
| 35 | + reused.push(tlsSocket.isSessionReused()); |
| 36 | + tlsSocket.end(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + await new Promise((resolve) => server.listen(0, resolve)); |
| 40 | + return server; |
| 41 | +} |
| 42 | + |
| 43 | +// Resolves with the first session issued, once the connection closes. This is |
| 44 | +// the raw session from the TLS library: on BoringSSL the 'session' event's |
| 45 | +// TLS 1.3 session can't be resumed. |
| 46 | +function connect(server, options) { |
| 47 | + return new Promise((resolve) => { |
| 48 | + let session; |
| 49 | + const socket = tls.connect({ |
| 50 | + port: server.address().port, |
| 51 | + rejectUnauthorized: false, |
| 52 | + ...options, |
| 53 | + }); |
| 54 | + const { onnewsession } = socket._handle; |
| 55 | + socket._handle.onnewsession = function(id, raw) { |
| 56 | + session ??= raw; |
| 57 | + return onnewsession.call(this, id, raw); |
| 58 | + }; |
| 59 | + socket.on('session', () => {}); |
| 60 | + socket.on('close', () => resolve(session)); |
| 61 | + socket.resume(); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +(async () => { |
| 66 | + const open = await listen(false); |
| 67 | + const verifying = await listen(true); |
| 68 | + |
| 69 | + for (const maxVersion of ['TLSv1.2', 'TLSv1.3']) { |
| 70 | + reused.length = 0; |
| 71 | + const session = await connect(open, { maxVersion }); |
| 72 | + assert.ok(session); |
| 73 | + await connect(open, { maxVersion, session }); |
| 74 | + await connect(verifying, { maxVersion, session }); |
| 75 | + assert.deepStrictEqual(reused, [false, true, false], maxVersion); |
| 76 | + } |
| 77 | + |
| 78 | + open.close(); |
| 79 | + verifying.close(); |
| 80 | +})().then(common.mustCall()); |
0 commit comments