|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// A resumed session skips authentication, so it may only be resumed for the |
| 4 | +// server name it was authenticated for. Otherwise a client trusted by one host |
| 5 | +// could resume into another. Asking for a mismatched name gets a full |
| 6 | +// handshake, which authenticates the client from scratch like any other. |
| 7 | + |
| 8 | +const common = require('../common'); |
| 9 | + |
| 10 | +if (!common.hasCrypto) |
| 11 | + common.skip('missing crypto'); |
| 12 | + |
| 13 | +const assert = require('node:assert'); |
| 14 | +const { isBoringSSL } = require('../common/crypto'); |
| 15 | +const tls = require('node:tls'); |
| 16 | +const fixtures = require('../common/fixtures'); |
| 17 | + |
| 18 | +// What the server reported for each connection it accepted, in order. |
| 19 | +const seen = []; |
| 20 | + |
| 21 | +const server = tls.createServer({ |
| 22 | + key: fixtures.readKey('agent2-key.pem'), |
| 23 | + cert: fixtures.readKey('agent2-cert.pem'), |
| 24 | +}, (socket) => { |
| 25 | + seen.push({ servername: socket.servername, reused: socket.isSessionReused() }); |
| 26 | + socket.end(); |
| 27 | +}); |
| 28 | + |
| 29 | +// Connects TLS with options, resolves with the raw issued session when the |
| 30 | +// conn closes. We need the raw internal session so we can trick Node into |
| 31 | +// (ab)using this for the wrong hostname on resumed connections later. |
| 32 | +function connect(options) { |
| 33 | + return new Promise((resolve) => { |
| 34 | + let session; |
| 35 | + const socket = tls.connect({ |
| 36 | + port: server.address().port, |
| 37 | + rejectUnauthorized: false, |
| 38 | + ...options, |
| 39 | + }); |
| 40 | + const { onnewsession } = socket._handle; |
| 41 | + socket._handle.onnewsession = function(id, raw) { |
| 42 | + session ??= raw; |
| 43 | + return onnewsession.call(this, id, raw); |
| 44 | + }; |
| 45 | + socket.on('session', () => {}); |
| 46 | + socket.on('close', () => resolve(session)); |
| 47 | + socket.resume(); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +// Connecting by IP sends no server name. |
| 52 | +const noName = { host: '127.0.0.1' }; |
| 53 | + |
| 54 | +server.listen(0, common.mustCall(async () => { |
| 55 | + for (const maxVersion of ['TLSv1.2', 'TLSv1.3']) { |
| 56 | + seen.length = 0; |
| 57 | + const named = await connect({ servername: 'agent1.example', maxVersion }); |
| 58 | + const unnamed = await connect({ ...noName, maxVersion }); |
| 59 | + |
| 60 | + // Resuming as the same name, or with no name both times, is fine. |
| 61 | + await connect({ servername: 'agent1.example', maxVersion, session: named }); |
| 62 | + await connect({ ...noName, maxVersion, session: unnamed }); |
| 63 | + |
| 64 | + // Asking for a different name gets a full handshake instead. |
| 65 | + await connect({ servername: 'agent3.example', maxVersion, session: named }); |
| 66 | + // Going from no SNI to any name gets a full handshake instead. |
| 67 | + await connect({ servername: 'agent1.example', maxVersion, session: unnamed }); |
| 68 | + |
| 69 | + assert.deepStrictEqual(seen, [ |
| 70 | + { servername: 'agent1.example', reused: false }, |
| 71 | + { servername: false, reused: false }, |
| 72 | + { servername: 'agent1.example', reused: true }, |
| 73 | + { servername: false, reused: true }, |
| 74 | + { servername: 'agent3.example', reused: false }, |
| 75 | + { servername: 'agent1.example', reused: false }, |
| 76 | + ], maxVersion); |
| 77 | + } |
| 78 | + |
| 79 | + // BoringSSL doesn't support TLS 1.3 external PSK. |
| 80 | + if (!isBoringSSL) { |
| 81 | + // Make sure fresh PSK & PSK resumption work correctly too - this is risky as |
| 82 | + // TLS1.3 PSK works by pretending to be resumption (but it's not). |
| 83 | + const secret = Buffer.alloc(32, 1); |
| 84 | + const ciphers = 'TLS_AES_128_GCM_SHA256'; |
| 85 | + const psk = tls.createServer({ ciphers, pskCallback: () => secret }, |
| 86 | + common.mustCall((socket) => { |
| 87 | + assert.strictEqual(socket.servername, 'psk.example'); |
| 88 | + socket.end(); |
| 89 | + }, 2)); |
| 90 | + await new Promise((resolve) => psk.listen(0, resolve)); |
| 91 | + const pskOptions = { |
| 92 | + port: psk.address().port, |
| 93 | + servername: 'psk.example', |
| 94 | + ciphers, |
| 95 | + pskCallback: () => ({ psk: secret, identity: 'client' }), |
| 96 | + }; |
| 97 | + const ticket = await connect(pskOptions); |
| 98 | + assert.ok(ticket); |
| 99 | + await connect({ ...pskOptions, session: ticket }); |
| 100 | + psk.close(); |
| 101 | + } |
| 102 | + server.close(); |
| 103 | +})); |
0 commit comments