Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/internal/tls/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ function requestOCSP(socket, info) {

let ctx = socket._handle.sni_context;

// A context selected by the SNI callback may not have a certificate, for
// example when it only provides the CAs. The handshake then uses the
// default certificate, so report that one.
if (ctx && !(ctx.context ?? ctx).getCertificate())
ctx = undefined;

if (!ctx) {
ctx = socket.server._sharedCreds;

Expand Down Expand Up @@ -491,6 +497,9 @@ function onnewsession(sessionId, session) {
return;
once = true;

if (owner.destroyed)
return;

if (owner._handle === null)
return owner.destroy(new ERR_SOCKET_CLOSED());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

// A socket that failed during the handshake does not complete it when the
// callback of the `newSession` event is called afterwards.

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const net = require('net');
const tls = require('tls');
const { constants } = require('crypto');
const fixtures = require('../common/fixtures');

const key = fixtures.readKey('agent1-key.pem');
const cert = fixtures.readKey('agent1-cert.pem');

const server = tls.createServer({
key,
cert,
maxVersion: 'TLSv1.2',
secureOptions: constants.SSL_OP_NO_TICKET,
});

// An invalid TLS record is appended to the client chunk that follows the
// ClientHello, which is expected to carry ClientKeyExchange, ChangeCipherSpec
// and Finished in a single `data` event. The server then fails with a TLS
// error after it has requested the callback of the `newSession` event.
const invalidRecord = Buffer.from([0x17, 0x03, 0x03, 0x00, 0x05, 1, 2, 3, 4, 5]);
const proxy = net.createServer(common.mustCall((downstream) => {
const upstream = net.connect(server.address().port);
let first = true;
let injected = false;
downstream.on('data', (chunk) => {
// Handshake records have the content type 0x16.
if (!first && !injected && chunk[0] === 0x16) {
injected = true;
chunk = Buffer.concat([chunk, invalidRecord]);
}
first = false;
upstream.write(chunk);
});
upstream.pipe(downstream);
downstream.on('error', () => {});
upstream.on('error', () => {});
downstream.on('close', () => upstream.destroy());
upstream.on('close', () => downstream.destroy());
}));

let done;
server.on('secureConnection', common.mustNotCall());
server.on('newSession', common.mustCall((id, data, callback) => {
done = callback;
}));
server.on('tlsClientError', common.mustCall((err, socket) => {
assert.ok(socket.destroyed);
socket.on('secure', common.mustNotCall());
done();
setImmediate(common.mustCall(() => {
proxy.close();
server.close();
}));
}));

server.listen(0, common.mustCall(() => {
proxy.listen(0, common.mustCall(() => {
const client = tls.connect({
port: proxy.address().port,
maxVersion: 'TLSv1.2',
secureOptions: constants.SSL_OP_NO_TICKET,
rejectUnauthorized: false,
});
client.on('error', () => {});
}));
}));
46 changes: 46 additions & 0 deletions test/parallel/test-tls-ocsp-request-sni-without-certificate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

// The OCSPRequest event reports the certificate that the connection uses,
// also when the SNI context only carries a CA.

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const crypto = require('crypto');
const tls = require('tls');
const fixtures = require('../common/fixtures');

const key = fixtures.readKey('agent1-key.pem');
const cert = fixtures.readKey('agent1-cert.pem');
const ca = fixtures.readKey('ca1-cert.pem');

const server = tls.createServer({
key,
cert,
SNICallback: common.mustCall((servername, callback) => {
callback(null, tls.createSecureContext({ ca }));
}),
});

server.on('OCSPRequest', common.mustCall((certificate, issuer, callback) => {
assert.deepStrictEqual(
certificate,
new crypto.X509Certificate(cert).raw);
callback(null, null);
}));

server.listen(0, common.mustCall(() => {
const client = tls.connect({
port: server.address().port,
servername: 'sni.example.com',
requestOCSP: true,
rejectUnauthorized: false,
});

client.on('OCSPResponse', common.mustCall(() => {
client.destroy();
server.close();
}));
}));
Loading