2017-01-03 22:16:48 +01: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 13:00:06 +02:00
|
|
|
'use strict';
|
2014-01-18 13:18:25 +01:00
|
|
|
|
2016-12-31 00:38:06 +01:00
|
|
|
const common = require('../common');
|
2011-03-10 09:54:52 +01:00
|
|
|
|
2017-07-01 01:29:09 +02:00
|
|
|
if (!common.opensslCli)
|
2016-05-11 21:34:52 +02:00
|
|
|
common.skip('node compiled without OpenSSL CLI.');
|
2011-01-02 10:54:19 +01:00
|
|
|
|
2017-07-01 01:29:09 +02:00
|
|
|
if (!common.hasCrypto)
|
2016-05-11 21:34:52 +02:00
|
|
|
common.skip('missing crypto');
|
2015-03-04 02:11:21 +01:00
|
|
|
|
2016-12-31 00:38:06 +01:00
|
|
|
const join = require('path').join;
|
|
|
|
const net = require('net');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const tls = require('tls');
|
|
|
|
const spawn = require('child_process').spawn;
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2013-12-11 18:20:17 +01:00
|
|
|
test1();
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 01:05:10 +02:00
|
|
|
// simple/test-tls-securepair-client
|
|
|
|
function test1() {
|
|
|
|
test('agent.key', 'agent.crt', null, test2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple/test-tls-ext-key-usage
|
|
|
|
function test2() {
|
|
|
|
function check(pair) {
|
|
|
|
// "TLS Web Client Authentication"
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(pair.cleartext.getPeerCertificate().ext_key_usage.length,
|
|
|
|
1);
|
|
|
|
assert.strictEqual(pair.cleartext.getPeerCertificate().ext_key_usage[0],
|
|
|
|
'1.3.6.1.5.5.7.3.2');
|
2012-04-11 01:05:10 +02:00
|
|
|
}
|
|
|
|
test('keys/agent4-key.pem', 'keys/agent4-cert.pem', check);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test(keyfn, certfn, check, next) {
|
|
|
|
keyfn = join(common.fixturesDir, keyfn);
|
2017-01-08 14:19:00 +01:00
|
|
|
const key = fs.readFileSync(keyfn).toString();
|
2012-04-11 00:47:52 +02:00
|
|
|
|
2012-04-11 01:05:10 +02:00
|
|
|
certfn = join(common.fixturesDir, certfn);
|
2017-01-08 14:19:00 +01:00
|
|
|
const cert = fs.readFileSync(certfn).toString();
|
2012-04-11 00:47:52 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const server = spawn(common.opensslCli, ['s_server',
|
|
|
|
'-accept', common.PORT,
|
|
|
|
'-cert', certfn,
|
|
|
|
'-key', keyfn]);
|
2012-04-11 00:47:52 +02:00
|
|
|
server.stdout.pipe(process.stdout);
|
|
|
|
server.stderr.pipe(process.stdout);
|
|
|
|
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let state = 'WAIT-ACCEPT';
|
2012-04-11 00:47:52 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let serverStdoutBuffer = '';
|
2012-04-11 00:47:52 +02:00
|
|
|
server.stdout.setEncoding('utf8');
|
|
|
|
server.stdout.on('data', function(s) {
|
|
|
|
serverStdoutBuffer += s;
|
|
|
|
console.error(state);
|
|
|
|
switch (state) {
|
|
|
|
case 'WAIT-ACCEPT':
|
2017-06-15 02:33:50 +02:00
|
|
|
if (/ACCEPT/.test(serverStdoutBuffer)) {
|
2012-04-11 00:47:52 +02:00
|
|
|
// Give s_server half a second to start up.
|
|
|
|
setTimeout(startClient, 500);
|
|
|
|
state = 'WAIT-HELLO';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'WAIT-HELLO':
|
2017-06-15 02:33:50 +02:00
|
|
|
if (/hello/.test(serverStdoutBuffer)) {
|
2012-04-11 00:47:52 +02:00
|
|
|
|
|
|
|
// End the current SSL connection and exit.
|
|
|
|
// See s_server(1ssl).
|
|
|
|
server.stdin.write('Q');
|
|
|
|
|
|
|
|
state = 'WAIT-SERVER-CLOSE';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2010-12-16 21:22:30 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const timeout = setTimeout(function() {
|
2012-04-11 00:47:52 +02:00
|
|
|
server.kill();
|
|
|
|
process.exit(1);
|
|
|
|
}, 5000);
|
2010-12-20 20:08:51 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let gotWriteCallback = false;
|
|
|
|
let serverExitCode = -1;
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
server.on('exit', function(code) {
|
|
|
|
serverExitCode = code;
|
|
|
|
clearTimeout(timeout);
|
2012-04-11 01:05:10 +02:00
|
|
|
if (next) next();
|
2012-04-11 00:47:52 +02:00
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
function startClient() {
|
2017-01-08 14:19:00 +01:00
|
|
|
const s = new net.Stream();
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2017-07-11 02:55:21 +02:00
|
|
|
const sslcontext = tls.createSecureContext({ key: key, cert: cert });
|
2012-04-11 00:47:52 +02:00
|
|
|
sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const pair = tls.createSecurePair(sslcontext, false);
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
assert.ok(pair.encrypted.writable);
|
|
|
|
assert.ok(pair.cleartext.writable);
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
pair.encrypted.pipe(s);
|
|
|
|
s.pipe(pair.encrypted);
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2016-09-24 13:26:05 +02:00
|
|
|
s.connect(common.PORT);
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
s.on('connect', function() {
|
|
|
|
console.log('client connected');
|
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
pair.on('secure', function() {
|
|
|
|
console.log('client: connected+secure!');
|
|
|
|
console.log('client pair.cleartext.getPeerCertificate(): %j',
|
|
|
|
pair.cleartext.getPeerCertificate());
|
|
|
|
console.log('client pair.cleartext.getCipher(): %j',
|
|
|
|
pair.cleartext.getCipher());
|
2012-04-11 01:05:10 +02:00
|
|
|
if (check) check(pair);
|
2012-04-11 00:47:52 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
pair.cleartext.write('hello\r\n', function() {
|
|
|
|
gotWriteCallback = true;
|
|
|
|
});
|
|
|
|
}, 500);
|
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
pair.cleartext.on('data', function(d) {
|
|
|
|
console.log('cleartext: %s', d.toString());
|
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
s.on('close', function() {
|
|
|
|
console.log('client close');
|
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
pair.encrypted.on('error', function(err) {
|
2017-04-28 03:06:42 +02:00
|
|
|
console.log(`encrypted error: ${err}`);
|
2012-04-11 00:47:52 +02:00
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
s.on('error', function(err) {
|
2017-04-28 03:06:42 +02:00
|
|
|
console.log(`socket error: ${err}`);
|
2012-04-11 00:47:52 +02:00
|
|
|
});
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
pair.on('error', function(err) {
|
2017-04-28 03:06:42 +02:00
|
|
|
console.log(`secure error: ${err}`);
|
2012-04-11 00:47:52 +02:00
|
|
|
});
|
|
|
|
}
|
2010-11-29 03:40:30 +01:00
|
|
|
|
2012-04-11 00:47:52 +02:00
|
|
|
|
|
|
|
process.on('exit', function() {
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(0, serverExitCode);
|
|
|
|
assert.strictEqual('WAIT-SERVER-CLOSE', state);
|
2012-04-11 00:47:52 +02:00
|
|
|
assert.ok(gotWriteCallback);
|
|
|
|
});
|
|
|
|
}
|