Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: new tls.TLSSocket() supports sec ctx options #11005

Merged
merged 2 commits into from
Feb 20, 2017
Merged
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
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"when the secureContext option is set"? Maybe it's because it's 11 PM but I found it hard to parse.

* ...: 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semver-major?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we always treat new properties in options objects as semver-major? If someone was passing unsupported option values to an API, and then the API started supporting those option values, it would feel major to them, but I don't know if that is our standard. What our API is is under-defined ATM.

res = tls_wrap.wrap(handle._externalStream,
context.context,
!!options.isServer);
Expand Down
102 changes: 58 additions & 44 deletions test/parallel/test-tls-socket-default-options.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,69 @@
'use strict';
const common = require('../common');

// Test directly created TLS sockets and options.

const assert = require('assert');
const join = require('path').join;
const {
connect, keys, tls
} = require(join(common.fixturesDir, 'tls-connect'));

if (!common.hasCrypto) {
common.skip('missing crypto');
return;
process.exit(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary change.

}
const tls = require('tls');

const fs = require('fs');

const sent = 'hello world';

const serverOptions = {
isServer: true,
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};

function testSocketOptions(socket, socketOptions) {
let received = '';
const server = tls.createServer(serverOptions, function(s) {
s.on('data', function(chunk) {
received += chunk;
});

s.on('end', function() {
server.close();
s.destroy();
assert.strictEqual(received, sent);
setImmediate(runTests);
});
}).listen(0, function() {
const c = new tls.TLSSocket(socket, socketOptions);
c.connect(this.address().port, function() {
c.end(sent);
});
});

}
test(undefined, (err) => {
assert.strictEqual(err.message, 'unable to verify the first certificate');
});

const testArgs = [
[],
[undefined, {}]
];
test({}, (err) => {
assert.strictEqual(err.message, 'unable to verify the first certificate');
});

let n = 0;
function runTests() {
if (n++ < testArgs.length) {
testSocketOptions.apply(null, testArgs[n]);
}
}
test({secureContext: tls.createSecureContext({ca: keys.agent1.ca})}, (err) => {
assert.ifError(err);
});

runTests();
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({
server: {
key: keys.agent1.key,
cert: keys.agent1.cert,
},
}, function(err, pair, cleanup) {
assert.strictEqual(err.message, 'unable to verify the first certificate');
let recv = '';
pair.server.server.once('secureConnection', common.mustCall((conn) => {
conn.on('data', (data) => recv += data);
conn.on('end', common.mustCall(() => {
// Server sees nothing wrong with connection, even though the client's
// authentication of the server cert failed.
assert.strictEqual(recv, 'hello');
cleanup();
}));
}));

// Client doesn't support the 'secureConnect' event, and doesn't error if
// authentication failed. Caller must explicitly check for failure.
(new tls.TLSSocket(null, client)).connect(pair.server.server.address().port)
.on('connect', common.mustCall(function() {
this.end('hello');
}))
.on('secure', common.mustCall(function() {
callback(this.ssl.verifyError());
}));
});
}