Skip to content

Commit

Permalink
test: remove s_client from test-tls-ci-reneg-attack
Browse files Browse the repository at this point in the history
Rewrite test-tls-ci-reneg-attack to use tls.renegotiate() instead of
external (and potentially unpredictable/quirky/buggy) s_client.

Refs: #25676 (comment)

PR-URL: #25700
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
Trott authored and addaleax committed Jan 28, 2019
1 parent 190c063 commit 271126a
Showing 1 changed file with 21 additions and 36 deletions.
57 changes: 21 additions & 36 deletions test/pummel/test-tls-ci-reneg-attack.js
Expand Up @@ -28,7 +28,6 @@ if (!common.opensslCli)
common.skip('node compiled without OpenSSL CLI.'); common.skip('node compiled without OpenSSL CLI.');


const assert = require('assert'); const assert = require('assert');
const spawn = require('child_process').spawn;
const tls = require('tls'); const tls = require('tls');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');


Expand All @@ -51,63 +50,49 @@ function test(next) {
key: fixtures.readSync('test_key.pem') key: fixtures.readSync('test_key.pem')
}; };


let seenError = false;

const server = tls.createServer(options, function(conn) { const server = tls.createServer(options, function(conn) {
conn.on('error', function(err) { conn.on('error', function(err) {
console.error(`Caught exception: ${err}`); console.error(`Caught exception: ${err}`);
assert(/TLS session renegotiation attack/.test(err)); assert(/TLS session renegotiation attack/.test(err));
conn.destroy(); conn.destroy();
seenError = true;
}); });
conn.pipe(conn); conn.pipe(conn);
}); });


server.listen(common.PORT, function() { server.listen(0, function() {
const args = (`s_client -connect 127.0.0.1:${common.PORT}`).split(' '); const options = {
const child = spawn(common.opensslCli, args); host: server.address().host,

port: server.address().port,
child.stdout.resume(); rejectUnauthorized: false
child.stderr.resume(); };
const client = tls.connect(options, spam);


// Count handshakes, start the attack after the initial handshake is done
let handshakes = 0;
let renegs = 0; let renegs = 0;


child.stderr.on('data', function(data) { client.on('close', function() {
if (seenError) return;
handshakes += ((String(data)).match(/verify return:1/g) || []).length;
if (handshakes === 2) spam();
renegs += ((String(data)).match(/RENEGOTIATING/g) || []).length;
});

child.on('exit', function() {
assert.strictEqual(renegs, tls.CLIENT_RENEG_LIMIT + 1); assert.strictEqual(renegs, tls.CLIENT_RENEG_LIMIT + 1);
server.close(); server.close();
process.nextTick(next); process.nextTick(next);
}); });


let closed = false; client.on('error', function(err) {
child.stdin.on('error', function(err) { console.log('CLIENT ERR', err);
switch (err.code) { throw err;
case 'ECONNRESET':
case 'EPIPE':
break;
default:
assert.strictEqual(err.code, 'ECONNRESET');
break;
}
closed = true;
}); });
child.stdin.on('close', function() {
closed = true; client.on('close', function(hadErr) {
assert.strictEqual(hadErr, false);
}); });


// simulate renegotiation attack // simulate renegotiation attack
function spam() { function spam() {
if (closed) return; client.write('');
child.stdin.write('R\n'); client.renegotiate({}, (err) => {
setTimeout(spam, 50); assert.ifError(err);
assert.ok(renegs <= tls.CLIENT_RENEG_LIMIT);
spam();
});
renegs++;
} }
}); });
} }

0 comments on commit 271126a

Please sign in to comment.