-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -344,7 +344,7 @@ TLSSocket.prototype._wrapHandle = function(wrap) { | |
// Wrap socket's handle | ||
var context = options.secureContext || | ||
options.credentials || | ||
tls.createSecureContext(); | ||
tls.createSecureContext(options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semver-major? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
})); | ||
}); | ||
} |
There was a problem hiding this comment.
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.