Skip to content

Commit f37ab79

Browse files
indutnyaddaleax
authored andcommitted
tls: do not crash on STARTTLS when OCSP requested
`TLSSocket` should not have a hard dependency on `tls.Server`, since it may be running without it in cases like `STARTTLS`. Fix: #10704 PR-URL: #10706 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent d301367 commit f37ab79

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/_tls_wrap.js

+7
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ function requestOCSP(self, hello, ctx, cb) {
110110

111111
if (!ctx)
112112
ctx = self.server._sharedCreds;
113+
114+
// TLS socket is using a `net.Server` instead of a tls.TLSServer.
115+
// Some TLS properties like `server._sharedCreds` will not be present
116+
if (!ctx)
117+
return cb(null);
118+
119+
// TODO(indutny): eventually disallow raw `SecureContext`
113120
if (ctx.context)
114121
ctx = ctx.context;
115122

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
// Test asynchronous SNI+OCSP on TLSSocket created with `server` set to
4+
// `net.Server` instead of `tls.Server`
5+
6+
const common = require('../common');
7+
8+
if (!common.hasCrypto) {
9+
common.skip('missing crypto');
10+
return;
11+
}
12+
13+
const assert = require('assert');
14+
const fs = require('fs');
15+
const net = require('net');
16+
const tls = require('tls');
17+
18+
const key = fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem');
19+
const cert = fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem');
20+
21+
const server = net.createServer(common.mustCall((s) => {
22+
const tlsSocket = new tls.TLSSocket(s, {
23+
isServer: true,
24+
server: server,
25+
26+
secureContext: tls.createSecureContext({
27+
key: key,
28+
cert: cert
29+
}),
30+
31+
SNICallback: common.mustCall((hostname, callback) => {
32+
assert.strictEqual(hostname, 'test.test');
33+
34+
callback(null, null);
35+
})
36+
});
37+
38+
tlsSocket.on('secure', common.mustCall(() => {
39+
tlsSocket.end();
40+
server.close();
41+
}));
42+
})).listen(0, () => {
43+
const opts = {
44+
servername: 'test.test',
45+
port: server.address().port,
46+
rejectUnauthorized: false,
47+
requestOCSP: true
48+
};
49+
50+
tls.connect(opts, function() {
51+
this.end();
52+
});
53+
});

0 commit comments

Comments
 (0)