Skip to content

Commit c5b4f6b

Browse files
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 425a354 commit c5b4f6b

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

doc/api/tls.markdown

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

600+
- `secureContext`: An optional TLS context object from
601+
`tls.createSecureContext( ... )`. Could it be used for caching client
602+
certificates, key, and CA certificates.
603+
600604
- `session`: A `Buffer` instance, containing TLS session.
601605

602606
- `minDHSize`: Minimum size of DH parameter in bits to accept a TLS

lib/_tls_wrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ exports.connect = function(/* [port, host], options, cb */) {
984984
'localhost',
985985
NPN = {},
986986
ALPN = {},
987-
context = tls.createSecureContext(options);
987+
context = options.secureContext || tls.createSecureContext(options);
988988
tls.convertNPNProtocols(options.NPNProtocols, NPN);
989989
tls.convertALPNProtocols(options.ALPNProtocols, ALPN);
990990

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

0 commit comments

Comments
 (0)