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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tls: wrap tls inside tls using legacy API
Allow wrapping TLSSocket inside another TLSSocket, emulate it using SecurePair in legacy APIs. fix #6204
- Loading branch information
Showing
4 changed files
with
242 additions
and
43 deletions.
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, | ||
{ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
indutny
Author
Member
|
||
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,101 @@ 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) { | ||
debug('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; | ||
if (options.path && !options.port) { | ||
connect_opt = { path: options.path }; | ||
} else { | ||
connect_opt = { | ||
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 +791,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(); | ||
}); | ||
}); | ||
}); |
createSecurePair
have only 4 arguments. fifth argument is ignored (!)Yes, you mean
options
innew SecurePair()
, butcreateSecurePair
do not pass it. It will be nice if you fixcreateSecurePair
.P.S. Do you know how I can fix MY code that use SecurePair obsolete API? what is equivalent?