2011-01-02 10:54:19 +01:00
|
|
|
if (!process.versions.openssl) {
|
|
|
|
console.error("Skipping because node compiled without OpenSSL.");
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-19 22:28:04 +01:00
|
|
|
var common = require('../common');
|
2010-12-05 20:15:30 +01:00
|
|
|
var assert = require('assert');
|
2010-11-19 22:28:04 +01:00
|
|
|
|
|
|
|
var join = require('path').join;
|
|
|
|
var net = require('net');
|
|
|
|
var fs = require('fs');
|
|
|
|
var crypto = require('crypto');
|
2010-12-06 03:19:18 +01:00
|
|
|
var tls = require('tls');
|
2010-11-19 22:28:04 +01:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
var connections = 0;
|
2010-12-04 23:45:52 +01:00
|
|
|
var key = fs.readFileSync(join(common.fixturesDir, 'agent.key')).toString();
|
|
|
|
var cert = fs.readFileSync(join(common.fixturesDir, 'agent.crt')).toString();
|
2010-11-19 22:28:04 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
function log(a) {
|
2010-11-24 03:30:52 +01:00
|
|
|
console.error('***server*** ' + a);
|
2010-11-19 22:28:04 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
var server = net.createServer(function(socket) {
|
2010-11-19 22:28:04 +01:00
|
|
|
connections++;
|
2010-11-24 03:30:52 +01:00
|
|
|
log('connection fd=' + socket.fd);
|
2010-11-19 22:28:04 +01:00
|
|
|
var sslcontext = crypto.createCredentials({key: key, cert: cert});
|
|
|
|
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
|
|
|
|
|
2010-12-06 03:19:18 +01:00
|
|
|
var pair = tls.createSecurePair(sslcontext, true);
|
2010-11-19 22:28:04 +01:00
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
assert.ok(pair.encrypted.writable);
|
|
|
|
assert.ok(pair.cleartext.writable);
|
2010-11-19 22:28:04 +01:00
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.encrypted.pipe(socket);
|
|
|
|
socket.pipe(pair.encrypted);
|
2010-11-19 22:28:04 +01:00
|
|
|
|
|
|
|
log('i set it secure');
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
pair.on('secure', function() {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('connected+secure!');
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.cleartext.write('hello\r\n');
|
2010-12-09 11:31:22 +01:00
|
|
|
log(pair.cleartext.getPeerCertificate());
|
|
|
|
log(pair.cleartext.getCipher());
|
2010-11-19 22:28:04 +01:00
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
pair.cleartext.on('data', function(data) {
|
2010-11-24 03:30:52 +01:00
|
|
|
log('read bytes ' + data.length);
|
|
|
|
pair.cleartext.write(data);
|
2010-11-19 22:28:04 +01:00
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
socket.on('end', function() {
|
2010-11-24 03:30:52 +01:00
|
|
|
log('socket end');
|
2010-11-19 22:28:04 +01:00
|
|
|
});
|
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.cleartext.on('error', function(err) {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('got error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
pair.encrypted.on('error', function(err) {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('encrypted error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', function(err) {
|
|
|
|
log('socket error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
|
2010-11-24 03:30:52 +01:00
|
|
|
socket.on('close', function(err) {
|
|
|
|
log('socket closed');
|
|
|
|
});
|
|
|
|
|
|
|
|
pair.on('error', function(err) {
|
2010-11-19 22:28:04 +01:00
|
|
|
log('secure error: ');
|
|
|
|
log(err);
|
|
|
|
log(err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var gotHello = false;
|
|
|
|
var sentWorld = false;
|
|
|
|
var gotWorld = false;
|
|
|
|
var opensslExitCode = -1;
|
|
|
|
|
2011-01-19 07:27:12 +01:00
|
|
|
server.listen(common.PORT, function() {
|
2010-11-19 22:28:04 +01:00
|
|
|
// To test use: openssl s_client -connect localhost:8000
|
2011-01-19 07:27:12 +01:00
|
|
|
var client = spawn('openssl', ['s_client', '-connect', '127.0.0.1:' + common.PORT]);
|
2010-11-19 22:28:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
var out = '';
|
|
|
|
|
|
|
|
client.stdout.setEncoding('utf8');
|
2010-12-04 23:45:52 +01:00
|
|
|
client.stdout.on('data', function(d) {
|
2010-11-19 22:28:04 +01:00
|
|
|
out += d;
|
|
|
|
|
|
|
|
if (!gotHello && /hello/.test(out)) {
|
|
|
|
gotHello = true;
|
|
|
|
client.stdin.write('world\r\n');
|
|
|
|
sentWorld = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gotWorld && /world/.test(out)) {
|
|
|
|
gotWorld = true;
|
|
|
|
client.stdin.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-02-20 01:56:56 +01:00
|
|
|
client.stdout.pipe(process.stdout, { end: false });
|
2010-11-19 22:28:04 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
client.on('exit', function(code) {
|
2010-11-19 22:28:04 +01:00
|
|
|
opensslExitCode = code;
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
process.on('exit', function() {
|
2010-11-19 22:28:04 +01:00
|
|
|
assert.equal(1, connections);
|
|
|
|
assert.ok(gotHello);
|
|
|
|
assert.ok(sentWorld);
|
|
|
|
assert.ok(gotWorld);
|
|
|
|
assert.equal(0, opensslExitCode);
|
|
|
|
});
|