2017-01-03 21:16:48 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2016-12-30 23:38:06 +00:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const spawn = require('child_process').spawn;
|
2015-03-04 01:11:21 +00:00
|
|
|
|
|
|
|
if (!common.hasCrypto) {
|
2016-05-11 19:34:52 +00:00
|
|
|
common.skip('missing crypto');
|
2015-07-07 15:25:55 +00:00
|
|
|
return;
|
2015-03-04 01:11:21 +00:00
|
|
|
}
|
2016-12-30 23:38:06 +00:00
|
|
|
const tls = require('tls');
|
2015-03-04 01:11:21 +00:00
|
|
|
|
2016-12-30 23:38:06 +00:00
|
|
|
const fs = require('fs');
|
2012-02-15 18:26:43 +00:00
|
|
|
|
2014-01-18 12:18:25 +00:00
|
|
|
if (!common.opensslCli) {
|
2016-05-11 19:34:52 +00:00
|
|
|
common.skip('node compiled without OpenSSL CLI.');
|
2015-07-07 15:25:55 +00:00
|
|
|
return;
|
2014-01-18 12:18:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 18:26:43 +00:00
|
|
|
// renegotiation limits to test
|
2017-01-08 13:19:00 +00:00
|
|
|
const LIMITS = [0, 1, 2, 3, 5, 10, 16];
|
2012-02-15 18:26:43 +00:00
|
|
|
|
2016-07-12 23:47:32 +00:00
|
|
|
{
|
|
|
|
let n = 0;
|
2012-02-15 18:26:43 +00:00
|
|
|
function next() {
|
|
|
|
if (n >= LIMITS.length) return;
|
|
|
|
tls.CLIENT_RENEG_LIMIT = LIMITS[n++];
|
|
|
|
test(next);
|
|
|
|
}
|
|
|
|
next();
|
2016-07-12 23:47:32 +00:00
|
|
|
}
|
2012-02-15 18:26:43 +00:00
|
|
|
|
|
|
|
function test(next) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const options = {
|
2012-02-15 18:26:43 +00:00
|
|
|
cert: fs.readFileSync(common.fixturesDir + '/test_cert.pem'),
|
|
|
|
key: fs.readFileSync(common.fixturesDir + '/test_key.pem')
|
|
|
|
};
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let seenError = false;
|
2012-06-18 02:05:21 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = tls.createServer(options, function(conn) {
|
2012-02-15 18:26:43 +00:00
|
|
|
conn.on('error', function(err) {
|
|
|
|
console.error('Caught exception: ' + err);
|
|
|
|
assert(/TLS session renegotiation attack/.test(err));
|
|
|
|
conn.destroy();
|
2012-06-18 02:05:21 +00:00
|
|
|
seenError = true;
|
2012-02-15 18:26:43 +00:00
|
|
|
});
|
|
|
|
conn.pipe(conn);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(common.PORT, function() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const args = ('s_client -connect 127.0.0.1:' + common.PORT).split(' ');
|
|
|
|
const child = spawn(common.opensslCli, args);
|
2012-02-15 18:26:43 +00:00
|
|
|
|
2014-02-25 02:38:41 +00:00
|
|
|
//child.stdout.pipe(process.stdout);
|
|
|
|
//child.stderr.pipe(process.stderr);
|
|
|
|
|
|
|
|
child.stdout.resume();
|
|
|
|
child.stderr.resume();
|
2012-02-15 18:26:43 +00:00
|
|
|
|
|
|
|
// count handshakes, start the attack after the initial handshake is done
|
2017-01-08 13:19:00 +00:00
|
|
|
let handshakes = 0;
|
|
|
|
let renegs = 0;
|
2012-06-18 02:05:21 +00:00
|
|
|
|
2012-02-15 18:26:43 +00:00
|
|
|
child.stderr.on('data', function(data) {
|
2012-06-18 02:05:21 +00:00
|
|
|
if (seenError) return;
|
2012-02-15 18:26:43 +00:00
|
|
|
handshakes += (('' + data).match(/verify return:1/g) || []).length;
|
|
|
|
if (handshakes === 2) spam();
|
2012-06-18 02:05:21 +00:00
|
|
|
renegs += (('' + data).match(/RENEGOTIATING/g) || []).length;
|
2012-02-15 18:26:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
child.on('exit', function() {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(renegs, tls.CLIENT_RENEG_LIMIT + 1);
|
2012-02-15 18:26:43 +00:00
|
|
|
server.close();
|
|
|
|
process.nextTick(next);
|
|
|
|
});
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let closed = false;
|
2012-02-15 18:26:43 +00:00
|
|
|
child.stdin.on('error', function(err) {
|
2014-02-25 02:38:41 +00:00
|
|
|
switch (err.code) {
|
|
|
|
case 'ECONNRESET':
|
|
|
|
case 'EPIPE':
|
|
|
|
break;
|
|
|
|
default:
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(err.code, 'ECONNRESET');
|
2014-02-25 02:38:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-02-15 18:26:43 +00:00
|
|
|
closed = true;
|
|
|
|
});
|
|
|
|
child.stdin.on('close', function() {
|
|
|
|
closed = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// simulate renegotiation attack
|
|
|
|
function spam() {
|
|
|
|
if (closed) return;
|
2012-02-18 23:01:35 +00:00
|
|
|
child.stdin.write('R\n');
|
2012-06-18 02:05:21 +00:00
|
|
|
setTimeout(spam, 50);
|
2012-02-15 18:26:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|