Skip to content

Commit

Permalink
tls: new tls.TLSSocket() supports sec ctx options
Browse files Browse the repository at this point in the history
Add support to new tls.TLSSocket() to create a SecureContext object with
all its supported options, in the same way they are supported for all
the other APIs that need SecureContext objects.

Fix: nodejs#10538
PR-URL: nodejs#11005
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
sam-github committed Feb 20, 2017
1 parent bd947de commit 4e6efc1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,12 @@ added: v0.11.4
will be emitted on the socket before establishing a secure communication
* `secureContext`: Optional TLS context object created with
[`tls.createSecureContext()`][]. If a `secureContext` is _not_ provided, one
will be created by calling [`tls.createSecureContext()`][] with no options.
will be created by passing the entire `options` object to
`tls.createSecureContext()`. *Note*: In effect, all
[`tls.createSecureContext()`][] options can be provided, but they will be
_completely ignored_ unless the `secureContext` option is missing.
* ...: Optional [`tls.createSecureContext()`][] options can be provided, see
the `secureContext` option for more information.

Construct a new `tls.TLSSocket` object from an existing TCP socket.

Expand Down
2 changes: 1 addition & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
// Wrap socket's handle
var context = options.secureContext ||
options.credentials ||
tls.createSecureContext();
tls.createSecureContext(options);
res = tls_wrap.wrap(handle._externalStream,
context.context,
!!options.isServer);
Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-tls-socket-default-options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const common = require('../common');

// Test a directly created TLS socket supports no options, and empty options.
// Test directly created TLS sockets and options.

const assert = require('assert');
const join = require('path').join;
Expand All @@ -26,6 +26,16 @@ test({secureContext: tls.createSecureContext({ca: keys.agent1.ca})}, (err) => {
assert.ifError(err);
});

test({ca: keys.agent1.ca}, (err) => {
assert.ifError(err);
});

// Secure context options, like ca, are ignored if a sec ctx is explicitly
// provided.
test({secureContext: tls.createSecureContext(), ca: keys.agent1.ca}, (err) => {
assert.strictEqual(err.message, 'unable to verify the first certificate');
});

function test(client, callback) {
callback = common.mustCall(callback);
connect({
Expand Down

0 comments on commit 4e6efc1

Please sign in to comment.