Skip to content

Commit 9049c1f

Browse files
indutnyMyles Borins
authored and
Myles Borins
committed
tls: introduce secureContext for tls.connect
Add `secureContext` option to `tls.connect`. It is useful for caching client certificates, key, and CA certificates. PR-URL: #4246 Reviewed-By: James M Snell <[email protected]>
1 parent 2efe64e commit 9049c1f

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

doc/api/tls.md

+4
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,10 @@ Creates a new client connection to the given `port` and `host` (old API) or
694694
SSL version 3. The possible values depend on your installation of
695695
OpenSSL and are defined in the constant [SSL_METHODS][].
696696

697+
- `secureContext`: An optional TLS context object from
698+
`tls.createSecureContext( ... )`. Could it be used for caching client
699+
certificates, key, and CA certificates.
700+
697701
- `session`: A `Buffer` instance, containing TLS session.
698702

699703
The `callback` parameter will be added as a listener for the

lib/_tls_wrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ exports.connect = function(/* [port, host], options, cb */) {
973973
(options.socket && options.socket._host) ||
974974
'localhost';
975975
const NPN = {};
976-
const context = tls.createSecureContext(options);
976+
const context = options.secureContext || tls.createSecureContext(options);
977977
tls.convertNPNProtocols(options.NPNProtocols, NPN);
978978

979979
var socket = new TLSSocket(options.socket, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (!common.hasCrypto) {
5+
console.log('1..0 # Skipped: missing crypto');
6+
return;
7+
}
8+
const tls = require('tls');
9+
10+
const fs = require('fs');
11+
const path = require('path');
12+
13+
const keysDir = path.join(common.fixturesDir, 'keys');
14+
15+
const ca = fs.readFileSync(path.join(keysDir, 'ca1-cert.pem'));
16+
const cert = fs.readFileSync(path.join(keysDir, 'agent1-cert.pem'));
17+
const key = fs.readFileSync(path.join(keysDir, 'agent1-key.pem'));
18+
19+
const server = tls.createServer({
20+
cert: cert,
21+
key: key
22+
}, function(c) {
23+
c.end();
24+
}).listen(common.PORT, function() {
25+
const secureContext = tls.createSecureContext({
26+
ca: ca
27+
});
28+
29+
const socket = tls.connect({
30+
secureContext: secureContext,
31+
servername: 'agent1',
32+
port: common.PORT
33+
}, common.mustCall(function() {
34+
server.close();
35+
socket.end();
36+
}));
37+
});

0 commit comments

Comments
 (0)