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