2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-12-01 17:16:42 +01:00
|
|
|
const common = require('../common');
|
2014-04-14 19:15:57 +02:00
|
|
|
|
|
|
|
if (!process.features.tls_ocsp) {
|
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;
|
2014-04-14 19:15:57 +02:00
|
|
|
}
|
|
|
|
if (!common.opensslCli) {
|
2016-05-11 21:34:52 +02:00
|
|
|
common.skip('node compiled without OpenSSL CLI.');
|
2015-07-07 17:25:55 +02:00
|
|
|
return;
|
2014-04-14 19:15:57 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-12-01 17:16:42 +01:00
|
|
|
const tls = require('tls');
|
2015-03-04 02:11:21 +01:00
|
|
|
|
2016-12-01 17:16:42 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const join = require('path').join;
|
2014-11-04 17:14:55 +01:00
|
|
|
|
2016-05-02 19:27:12 +02:00
|
|
|
const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
|
|
|
|
|
2016-12-01 17:16:42 +01:00
|
|
|
const pfx = fs.readFileSync(join(common.fixturesDir, 'keys', 'agent1-pfx.pem'));
|
2014-04-14 19:15:57 +02:00
|
|
|
|
|
|
|
function test(testOptions, cb) {
|
|
|
|
|
2016-12-01 17:16:42 +01:00
|
|
|
const keyFile = join(common.fixturesDir, 'keys', 'agent1-key.pem');
|
|
|
|
const certFile = join(common.fixturesDir, 'keys', 'agent1-cert.pem');
|
|
|
|
const caFile = join(common.fixturesDir, 'keys', 'ca1-cert.pem');
|
|
|
|
const key = fs.readFileSync(keyFile);
|
|
|
|
const cert = fs.readFileSync(certFile);
|
|
|
|
const ca = fs.readFileSync(caFile);
|
|
|
|
const options = {
|
2014-04-14 19:15:57 +02:00
|
|
|
key: key,
|
|
|
|
cert: cert,
|
|
|
|
ca: [ca]
|
|
|
|
};
|
2016-12-01 17:16:42 +01:00
|
|
|
let requestCount = 0;
|
|
|
|
let clientSecure = 0;
|
|
|
|
let ocspCount = 0;
|
|
|
|
let ocspResponse;
|
2014-04-14 19:15:57 +02:00
|
|
|
|
2015-12-05 22:53:30 +01:00
|
|
|
if (testOptions.pfx) {
|
|
|
|
delete options.key;
|
|
|
|
delete options.cert;
|
|
|
|
options.pfx = testOptions.pfx;
|
|
|
|
options.passphrase = testOptions.passphrase;
|
|
|
|
}
|
|
|
|
|
2016-12-01 17:16:42 +01:00
|
|
|
const server = tls.createServer(options, function(cleartext) {
|
2014-04-14 19:15:57 +02:00
|
|
|
cleartext.on('error', function(er) {
|
|
|
|
// We're ok with getting ECONNRESET in this test, but it's
|
|
|
|
// timing-dependent, and thus unreliable. Any other errors
|
|
|
|
// are just failures, though.
|
|
|
|
if (er.code !== 'ECONNRESET')
|
|
|
|
throw er;
|
|
|
|
});
|
|
|
|
++requestCount;
|
|
|
|
cleartext.end();
|
|
|
|
});
|
|
|
|
server.on('OCSPRequest', function(cert, issuer, callback) {
|
|
|
|
++ocspCount;
|
|
|
|
assert.ok(Buffer.isBuffer(cert));
|
|
|
|
assert.ok(Buffer.isBuffer(issuer));
|
|
|
|
|
|
|
|
// Just to check that async really works there
|
|
|
|
setTimeout(function() {
|
|
|
|
callback(null,
|
2016-01-26 00:00:06 +01:00
|
|
|
testOptions.response ? Buffer.from(testOptions.response) : null);
|
2014-04-14 19:15:57 +02:00
|
|
|
}, 100);
|
|
|
|
});
|
2016-05-29 09:06:56 +02:00
|
|
|
server.listen(0, function() {
|
2016-12-01 17:16:42 +01:00
|
|
|
const client = tls.connect({
|
2016-05-29 09:06:56 +02:00
|
|
|
port: this.address().port,
|
2014-11-04 17:14:55 +01:00
|
|
|
requestOCSP: testOptions.ocsp !== false,
|
|
|
|
secureOptions: testOptions.ocsp === false ?
|
2016-05-02 19:27:12 +02:00
|
|
|
SSL_OP_NO_TICKET : 0,
|
2014-04-14 19:15:57 +02:00
|
|
|
rejectUnauthorized: false
|
|
|
|
}, function() {
|
2014-11-04 17:14:55 +01:00
|
|
|
clientSecure++;
|
2014-04-14 19:15:57 +02:00
|
|
|
});
|
|
|
|
client.on('OCSPResponse', function(resp) {
|
|
|
|
ocspResponse = resp;
|
|
|
|
if (resp)
|
|
|
|
client.destroy();
|
|
|
|
});
|
|
|
|
client.on('close', function() {
|
|
|
|
server.close(cb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
2014-11-04 17:14:55 +01:00
|
|
|
if (testOptions.ocsp === false) {
|
2016-12-01 17:16:42 +01:00
|
|
|
assert.strictEqual(requestCount, clientSecure);
|
|
|
|
assert.strictEqual(requestCount, 1);
|
2014-11-04 17:14:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-14 19:15:57 +02:00
|
|
|
if (testOptions.response) {
|
2016-12-01 17:16:42 +01:00
|
|
|
assert.strictEqual(ocspResponse.toString(), testOptions.response);
|
2014-04-14 19:15:57 +02:00
|
|
|
} else {
|
2016-12-01 17:16:42 +01:00
|
|
|
assert.strictEqual(ocspResponse, null);
|
2014-04-14 19:15:57 +02:00
|
|
|
}
|
2016-12-01 17:16:42 +01:00
|
|
|
assert.strictEqual(requestCount, testOptions.response ? 0 : 1);
|
|
|
|
assert.strictEqual(clientSecure, requestCount);
|
|
|
|
assert.strictEqual(ocspCount, 1);
|
2014-04-14 19:15:57 +02:00
|
|
|
});
|
|
|
|
}
|
2015-12-05 22:53:30 +01:00
|
|
|
|
2016-12-01 17:16:42 +01:00
|
|
|
const tests = [
|
2015-12-05 22:53:30 +01:00
|
|
|
{ response: false },
|
|
|
|
{ response: 'hello world' },
|
|
|
|
{ ocsp: false }
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!common.hasFipsCrypto) {
|
|
|
|
tests.push({ pfx: pfx, passphrase: 'sample', response: 'hello pfx' });
|
|
|
|
}
|
|
|
|
|
|
|
|
function runTests(i) {
|
|
|
|
if (i === tests.length) return;
|
|
|
|
|
|
|
|
test(tests[i], common.mustCall(function() {
|
|
|
|
runTests(i + 1);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
runTests(0);
|