|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Verify that servername (SNI) is preserved on resumed TLS sessions. |
| 4 | +// Regression test for https://github.com/nodejs/node/issues/59202 |
| 5 | + |
| 6 | +const common = require('../common'); |
| 7 | +if (!common.hasCrypto) |
| 8 | + common.skip('missing crypto'); |
| 9 | + |
| 10 | +// The fix relies on SSL_SESSION_get0_hostname which BoringSSL may not support. |
| 11 | +if (process.features.openssl_is_boringssl) |
| 12 | + common.skip('BoringSSL does not support SSL_SESSION_get0_hostname'); |
| 13 | + |
| 14 | +const assert = require('assert'); |
| 15 | +const tls = require('tls'); |
| 16 | +const fixtures = require('../common/fixtures'); |
| 17 | + |
| 18 | +const SERVERNAME = 'agent1.example.com'; |
| 19 | + |
| 20 | +function test(minVersion, maxVersion) { |
| 21 | + const serverOptions = { |
| 22 | + key: fixtures.readKey('agent1-key.pem'), |
| 23 | + cert: fixtures.readKey('agent1-cert.pem'), |
| 24 | + minVersion, |
| 25 | + maxVersion, |
| 26 | + }; |
| 27 | + |
| 28 | + let serverConns = 0; |
| 29 | + const server = tls.createServer(serverOptions, common.mustCall((socket) => { |
| 30 | + assert.strictEqual(socket.servername, SERVERNAME); |
| 31 | + serverConns++; |
| 32 | + // Don't end the socket immediately on the first connection — the client |
| 33 | + // needs time to receive the TLS 1.3 NewSessionTicket message. |
| 34 | + if (serverConns === 2) |
| 35 | + socket.end(); |
| 36 | + }, 2)); |
| 37 | + |
| 38 | + server.listen(0, common.mustCall(function() { |
| 39 | + const port = server.address().port; |
| 40 | + |
| 41 | + // First connection: establish a session with an SNI servername. |
| 42 | + const client1 = tls.connect({ |
| 43 | + port, |
| 44 | + servername: SERVERNAME, |
| 45 | + rejectUnauthorized: false, |
| 46 | + minVersion, |
| 47 | + maxVersion, |
| 48 | + }, common.mustCall()); |
| 49 | + |
| 50 | + client1.once('session', common.mustCall((session) => { |
| 51 | + client1.end(); |
| 52 | + |
| 53 | + // Second connection: resume the session and verify the servername is |
| 54 | + // preserved on the server side. |
| 55 | + const client2 = tls.connect({ |
| 56 | + port, |
| 57 | + servername: SERVERNAME, |
| 58 | + rejectUnauthorized: false, |
| 59 | + session, |
| 60 | + minVersion, |
| 61 | + maxVersion, |
| 62 | + }, common.mustCall(() => { |
| 63 | + assert.strictEqual(client2.isSessionReused(), true); |
| 64 | + client2.end(); |
| 65 | + })); |
| 66 | + |
| 67 | + client2.on('close', common.mustCall(() => server.close())); |
| 68 | + })); |
| 69 | + |
| 70 | + client1.resume(); |
| 71 | + })); |
| 72 | +} |
| 73 | + |
| 74 | +// TLS 1.3 — the primary bug: SSL_get_servername() returns NULL on |
| 75 | +// server-side resumed sessions. |
| 76 | +test('TLSv1.3', 'TLSv1.3'); |
| 77 | + |
| 78 | +// TLS 1.2 — OpenSSL handles this natively, but verify the fallback path |
| 79 | +// doesn't break it. |
| 80 | +test('TLSv1.2', 'TLSv1.2'); |
0 commit comments