2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-05-30 01:45:20 +02:00
|
|
|
const common = require('../common');
|
2011-07-28 17:52:04 +02:00
|
|
|
if (!process.features.tls_sni) {
|
2016-05-11 21:34:52 +02:00
|
|
|
common.skip('node compiled without OpenSSL or ' +
|
2015-07-06 05:24:12 +02:00
|
|
|
'with old OpenSSL version.');
|
2015-08-02 16:06:43 +02:00
|
|
|
return;
|
2011-07-28 17:52:04 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:42:45 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2015-03-04 02:11:21 +01:00
|
|
|
|
|
|
|
if (!common.hasCrypto) {
|
2016-05-11 21:34:52 +02:00
|
|
|
common.skip('missing crypto');
|
2015-07-07 17:25:55 +02:00
|
|
|
return;
|
2015-03-04 02:11:21 +01:00
|
|
|
}
|
|
|
|
var tls = require('tls');
|
2011-07-28 17:52:04 +02:00
|
|
|
|
|
|
|
function filenamePEM(n) {
|
2011-10-05 00:08:18 +02:00
|
|
|
return require('path').join(common.fixturesDir, 'keys', n + '.pem');
|
2011-07-28 17:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadPEM(n) {
|
|
|
|
return fs.readFileSync(filenamePEM(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
var serverOptions = {
|
|
|
|
key: loadPEM('agent2-key'),
|
|
|
|
cert: loadPEM('agent2-cert')
|
|
|
|
};
|
|
|
|
|
|
|
|
var SNIContexts = {
|
|
|
|
'a.example.com': {
|
|
|
|
key: loadPEM('agent1-key'),
|
|
|
|
cert: loadPEM('agent1-cert')
|
|
|
|
},
|
|
|
|
'asterisk.test.com': {
|
|
|
|
key: loadPEM('agent3-key'),
|
|
|
|
cert: loadPEM('agent3-cert')
|
2015-04-18 10:19:23 +02:00
|
|
|
},
|
|
|
|
'chain.example.com': {
|
|
|
|
key: loadPEM('agent6-key'),
|
|
|
|
// NOTE: Contains ca3 chain cert
|
|
|
|
cert: loadPEM('agent6-cert')
|
2011-07-28 17:52:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var clientsOptions = [{
|
2016-05-29 09:06:56 +02:00
|
|
|
port: undefined,
|
2011-07-28 17:52:04 +02:00
|
|
|
ca: [loadPEM('ca1-cert')],
|
2012-08-30 16:43:20 +02:00
|
|
|
servername: 'a.example.com',
|
|
|
|
rejectUnauthorized: false
|
2013-12-05 16:16:01 +01:00
|
|
|
}, {
|
2016-05-29 09:06:56 +02:00
|
|
|
port: undefined,
|
2011-07-28 17:52:04 +02:00
|
|
|
ca: [loadPEM('ca2-cert')],
|
2012-08-30 16:43:20 +02:00
|
|
|
servername: 'b.test.com',
|
|
|
|
rejectUnauthorized: false
|
2013-12-05 16:16:01 +01:00
|
|
|
}, {
|
2016-05-29 09:06:56 +02:00
|
|
|
port: undefined,
|
2013-12-05 16:16:01 +01:00
|
|
|
ca: [loadPEM('ca2-cert')],
|
|
|
|
servername: 'a.b.test.com',
|
|
|
|
rejectUnauthorized: false
|
|
|
|
}, {
|
2016-05-29 09:06:56 +02:00
|
|
|
port: undefined,
|
2011-07-28 17:52:04 +02:00
|
|
|
ca: [loadPEM('ca1-cert')],
|
2012-08-30 16:43:20 +02:00
|
|
|
servername: 'c.wrong.com',
|
|
|
|
rejectUnauthorized: false
|
2015-04-18 10:19:23 +02:00
|
|
|
}, {
|
2016-05-29 09:06:56 +02:00
|
|
|
port: undefined,
|
2015-04-18 10:19:23 +02:00
|
|
|
ca: [loadPEM('ca1-cert')],
|
|
|
|
servername: 'chain.example.com',
|
|
|
|
rejectUnauthorized: false
|
2011-07-28 17:52:04 +02:00
|
|
|
}];
|
|
|
|
|
2016-01-13 21:42:45 +01:00
|
|
|
const serverResults = [];
|
|
|
|
const clientResults = [];
|
2011-07-28 17:52:04 +02:00
|
|
|
|
|
|
|
var server = tls.createServer(serverOptions, function(c) {
|
|
|
|
serverResults.push(c.servername);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.addContext('a.example.com', SNIContexts['a.example.com']);
|
|
|
|
server.addContext('*.test.com', SNIContexts['asterisk.test.com']);
|
2015-04-18 10:19:23 +02:00
|
|
|
server.addContext('chain.example.com', SNIContexts['chain.example.com']);
|
2011-07-28 17:52:04 +02:00
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
server.listen(0, startTest);
|
2011-07-28 17:52:04 +02:00
|
|
|
|
|
|
|
function startTest() {
|
2013-12-05 16:16:01 +01:00
|
|
|
var i = 0;
|
|
|
|
function start() {
|
|
|
|
// No options left
|
|
|
|
if (i === clientsOptions.length)
|
|
|
|
return server.close();
|
|
|
|
|
|
|
|
var options = clientsOptions[i++];
|
2016-05-29 09:06:56 +02:00
|
|
|
options.port = server.address().port;
|
2011-11-01 16:28:04 +01:00
|
|
|
var client = tls.connect(options, function() {
|
2012-07-20 19:10:23 +02:00
|
|
|
clientResults.push(
|
|
|
|
client.authorizationError &&
|
|
|
|
/Hostname\/IP doesn't/.test(client.authorizationError));
|
2011-07-28 17:52:04 +02:00
|
|
|
client.destroy();
|
|
|
|
|
2013-12-05 16:16:01 +01:00
|
|
|
// Continue
|
|
|
|
start();
|
2011-07-28 17:52:04 +02:00
|
|
|
});
|
2016-01-15 09:53:11 +01:00
|
|
|
}
|
2011-07-28 17:52:04 +02:00
|
|
|
|
2013-12-05 16:16:01 +01:00
|
|
|
start();
|
2011-10-05 00:08:18 +02:00
|
|
|
}
|
2011-07-28 17:52:04 +02:00
|
|
|
|
|
|
|
process.on('exit', function() {
|
2016-04-20 00:37:45 +02:00
|
|
|
assert.deepStrictEqual(serverResults, [
|
2015-04-18 10:19:23 +02:00
|
|
|
'a.example.com', 'b.test.com', 'a.b.test.com', 'c.wrong.com',
|
|
|
|
'chain.example.com'
|
|
|
|
]);
|
2016-04-20 00:37:45 +02:00
|
|
|
assert.deepStrictEqual(clientResults, [true, true, false, false, true]);
|
2011-07-28 17:52:04 +02:00
|
|
|
});
|