This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
tls: wrap tls inside tls using legacy API #6261
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,9 @@ var util = require('util'); | |
var Timer = process.binding('timer_wrap').Timer; | ||
var tls_wrap = process.binding('tls_wrap'); | ||
|
||
// Lazy load | ||
var tls_legacy; | ||
|
||
var debug = util.debuglog('tls'); | ||
|
||
function onhandshakestart() { | ||
|
@@ -145,6 +148,9 @@ function onnewsession(key, session) { | |
*/ | ||
|
||
function TLSSocket(socket, options) { | ||
// Disallow wrapping TLSSocket in TLSSocket | ||
assert(!(socket instanceof TLSSocket)); | ||
|
||
net.Socket.call(this, socket && { | ||
handle: socket._handle, | ||
allowHalfOpen: socket.allowHalfOpen, | ||
|
@@ -645,6 +651,28 @@ function normalizeConnectArgs(listArgs) { | |
return (cb) ? [options, cb] : [options]; | ||
} | ||
|
||
function legacyConnect(hostname, options, NPN, credentials) { | ||
assert(options.socket); | ||
if (!tls_legacy) | ||
tls_legacy = require('_tls_legacy'); | ||
|
||
var pair = tls_legacy.createSecurePair(credentials, | ||
false, | ||
true, | ||
!!options.rejectUnauthorized, | ||
{ | ||
NPNProtocols: NPN.NPNProtocols, | ||
servername: hostname | ||
}); | ||
tls_legacy.pipe(pair, options.socket); | ||
pair.cleartext._controlReleased = true; | ||
pair.on('error', function(err) { | ||
pair.cleartext.emit('error', err); | ||
}); | ||
|
||
return pair; | ||
} | ||
|
||
exports.connect = function(/* [port, host], options, cb */) { | ||
var args = normalizeConnectArgs(arguments); | ||
var options = args[0]; | ||
|
@@ -656,57 +684,96 @@ exports.connect = function(/* [port, host], options, cb */) { | |
options = util._extend(defaults, options || {}); | ||
|
||
var hostname = options.servername || options.host || 'localhost', | ||
NPN = {}; | ||
NPN = {}, | ||
credentials = crypto.createCredentials(options); | ||
tls.convertNPNProtocols(options.NPNProtocols, NPN); | ||
|
||
var socket = new TLSSocket(options.socket, { | ||
credentials: crypto.createCredentials(options), | ||
isServer: false, | ||
requestCert: true, | ||
rejectUnauthorized: options.rejectUnauthorized, | ||
NPNProtocols: NPN.NPNProtocols | ||
}); | ||
// Wrapping TLS socket inside another TLS socket was requested - | ||
// create legacy secure pair | ||
var socket; | ||
var legacy; | ||
var result; | ||
if (options.socket instanceof TLSSocket) { | ||
console.log('legacy connect'); | ||
legacy = true; | ||
socket = legacyConnect(hostname, options, NPN, credentials); | ||
result = socket.cleartext; | ||
} else { | ||
legacy = false; | ||
socket = new TLSSocket(options.socket, { | ||
credentials: credentials, | ||
isServer: false, | ||
requestCert: true, | ||
rejectUnauthorized: options.rejectUnauthorized, | ||
NPNProtocols: NPN.NPNProtocols | ||
}); | ||
result = socket; | ||
} | ||
|
||
if (socket._handle) | ||
onHandle(); | ||
else | ||
socket.once('connect', onHandle); | ||
|
||
if (cb) | ||
result.once('secureConnect', cb); | ||
|
||
if (!options.socket) { | ||
assert(!legacy); | ||
var connect_opt = (options.path && !options.port) ? {path: options.path} : { | ||
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. For readability's sake, can you change this into if/then/else statements? |
||
port: options.port, | ||
host: options.host, | ||
localAddress: options.localAddress | ||
}; | ||
socket.connect(connect_opt); | ||
} | ||
|
||
return result; | ||
|
||
function onHandle() { | ||
socket._releaseControl(); | ||
if (!legacy) | ||
socket._releaseControl(); | ||
|
||
if (options.session) | ||
socket.setSession(options.session); | ||
|
||
if (options.servername) | ||
socket.setServername(options.servername); | ||
if (!legacy) { | ||
if (options.servername) | ||
socket.setServername(options.servername); | ||
|
||
socket._start(); | ||
socket._start(); | ||
} | ||
socket.on('secure', function() { | ||
var verifyError = socket.ssl.verifyError(); | ||
|
||
// Verify that server's identity matches it's certificate's names | ||
if (!verifyError) { | ||
var validCert = tls.checkServerIdentity(hostname, | ||
socket.getPeerCertificate()); | ||
var cert = result.getPeerCertificate(); | ||
var validCert = tls.checkServerIdentity(hostname, cert); | ||
if (!validCert) { | ||
verifyError = new Error('Hostname/IP doesn\'t match certificate\'s ' + | ||
'altnames'); | ||
} | ||
} | ||
|
||
if (verifyError) { | ||
socket.authorizationError = verifyError.message; | ||
result.authorized = false; | ||
result.authorizationError = verifyError.message; | ||
|
||
if (options.rejectUnauthorized) { | ||
socket.emit('error', verifyError); | ||
socket.destroy(); | ||
result.emit('error', verifyError); | ||
result.destroy(); | ||
return; | ||
} else { | ||
socket.emit('secureConnect'); | ||
result.emit('secureConnect'); | ||
} | ||
} else { | ||
socket.authorized = true; | ||
socket.emit('secureConnect'); | ||
result.authorized = true; | ||
result.emit('secureConnect'); | ||
} | ||
|
||
// Uncork incoming data | ||
socket.removeListener('end', onHangUp); | ||
result.removeListener('end', onHangUp); | ||
}); | ||
|
||
function onHangUp() { | ||
|
@@ -719,24 +786,6 @@ exports.connect = function(/* [port, host], options, cb */) { | |
socket.emit('error', error); | ||
} | ||
} | ||
socket.once('end', onHangUp); | ||
} | ||
if (socket._handle) | ||
onHandle(); | ||
else | ||
socket.once('connect', onHandle); | ||
|
||
if (cb) | ||
socket.once('secureConnect', cb); | ||
|
||
if (!options.socket) { | ||
var connect_opt = (options.path && !options.port) ? {path: options.path} : { | ||
port: options.port, | ||
host: options.host, | ||
localAddress: options.localAddress | ||
}; | ||
socket.connect(connect_opt); | ||
result.once('end', onHangUp); | ||
} | ||
|
||
return socket; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
if (!process.versions.openssl) { | ||
console.error('Skipping because node compiled without OpenSSL.'); | ||
process.exit(0); | ||
} | ||
|
||
var common = require('../common'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var net = require('net'); | ||
var tls = require('tls'); | ||
var assert = require('assert'); | ||
|
||
var options, a, b, portA, portB; | ||
var gotHello = false; | ||
|
||
options = { | ||
key: fs.readFileSync(path.join(common.fixturesDir, 'test_key.pem')), | ||
cert: fs.readFileSync(path.join(common.fixturesDir, 'test_cert.pem')) | ||
}; | ||
|
||
// the "proxy" server | ||
a = tls.createServer(options, function (socket) { | ||
var options = { | ||
host: '127.0.0.1', | ||
port: b.address().port, | ||
rejectUnauthorized: false | ||
}; | ||
var dest = net.connect(options); | ||
dest.pipe(socket); | ||
socket.pipe(dest); | ||
}); | ||
|
||
// the "target" server | ||
b = tls.createServer(options, function (socket) { | ||
socket.end('hello'); | ||
}); | ||
|
||
process.on('exit', function () { | ||
assert(gotHello); | ||
}); | ||
|
||
a.listen(common.PORT, function () { | ||
b.listen(common.PORT + 1, function () { | ||
options = { | ||
host: '127.0.0.1', | ||
port: a.address().port, | ||
rejectUnauthorized: false | ||
}; | ||
var socket = tls.connect(options); | ||
var ssl; | ||
ssl = tls.connect({ | ||
socket: socket, | ||
rejectUnauthorized: false | ||
}); | ||
ssl.setEncoding('utf8'); | ||
ssl.once('data', function (data) { | ||
assert.equal('hello', data); | ||
gotHello = true; | ||
}); | ||
ssl.on('end', function () { | ||
ssl.end(); | ||
a.close(); | ||
b.close(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
remove
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.
or change to a
debug()
call...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.
That was supposed to be
debug()
call, forgot to change it back :)