Skip to content

Commit 9e890fe

Browse files
author
Shigeki Ohtsu
committed
crypto: fix VerifyCallback in case of verify error
3beb880 has a bug in VerifyCallback when preverify is 1 and the cert chain has an verify error. If the error is UNABLE_TO_GET_ISSUER_CERT_LOCALLY, it leads an assertion error in finding rootCA. The whitelist check should be made only when the cert chain has no verify error with X509_V_OK. Fixes: #2061 PR-URL: #2064 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 8cee8f5 commit 9e890fe

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

src/node_crypto.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,7 @@ inline CheckResult CheckWhitelistedServerCert(X509_STORE_CTX* ctx) {
23102310
inline int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
23112311
// Failure on verification of the cert is handled in
23122312
// Connection::VerifyError.
2313-
if (preverify_ok == 0)
2313+
if (preverify_ok == 0 || X509_STORE_CTX_get_error(ctx) != X509_V_OK)
23142314
return 1;
23152315

23162316
// Server does not need to check the whitelist.

test/parallel/test-tls-cnnic-whitelist.js

+65-24
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,74 @@ if (!common.hasCrypto) {
1010
var tls = require('tls');
1111
var fs = require('fs');
1212
var path = require('path');
13+
var finished = 0;
1314

14-
var error = false;
15-
16-
// agent7-cert.pem is issued by the fake CNNIC root CA so that its
17-
// hash is not listed in the whitelist.
18-
var options = {
19-
key: fs.readFileSync(path.join(common.fixturesDir, 'keys/agent7-key.pem')),
20-
cert: fs.readFileSync(path.join(common.fixturesDir, 'keys/agent7-cert.pem'))
21-
};
22-
23-
var server = tls.createServer(options, function(s) {
24-
s.resume();
25-
}).listen(common.PORT, function() {
26-
var client = tls.connect({
27-
port: common.PORT,
28-
rejectUnauthorized: true,
15+
function filenamePEM(n) {
16+
return path.join(common.fixturesDir, 'keys', n + '.pem');
17+
}
18+
19+
function loadPEM(n) {
20+
return fs.readFileSync(filenamePEM(n));
21+
}
22+
23+
var testCases = [
24+
{ // Test 0: for the check of a cert not existed in the whitelist.
25+
// agent7-cert.pem is issued by the fake CNNIC root CA so that its
26+
// hash is not listed in the whitelist.
2927
// fake-cnnic-root-cert has the same subject name as the original
3028
// rootCA.
31-
ca: [fs.readFileSync(path.join(common.fixturesDir,
32-
'keys/fake-cnnic-root-cert.pem'))]
33-
});
34-
client.on('error', function(e) {
35-
assert.strictEqual(e.code, 'CERT_REVOKED');
36-
error = true;
37-
server.close();
29+
serverOpts: {
30+
key: loadPEM('agent7-key'),
31+
cert: loadPEM('agent7-cert')
32+
},
33+
clientOpts: {
34+
port: common.PORT,
35+
rejectUnauthorized: true,
36+
ca: [loadPEM('fake-cnnic-root-cert')]
37+
},
38+
errorCode: 'CERT_REVOKED'
39+
},
40+
// Test 1: for the fix of iojs#2061
41+
// agent6-cert.pem is signed by intermidate cert of ca3.
42+
// The server has a cert chain of agent6->ca3->ca1(root) but
43+
// tls.connect should be failed with an error of
44+
// UNABLE_TO_GET_ISSUER_CERT_LOCALLY since the root CA of ca1 is not
45+
// installed locally.
46+
{
47+
serverOpts: {
48+
ca: loadPEM('ca3-key'),
49+
key: loadPEM('agent6-key'),
50+
cert: loadPEM('agent6-cert')
51+
},
52+
clientOpts: {
53+
port: common.PORT,
54+
rejectUnauthorized: true
55+
},
56+
errorCode: 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY'
57+
}
58+
];
59+
60+
function runTest(tindex) {
61+
var tcase = testCases[tindex];
62+
63+
if (!tcase) return;
64+
65+
var server = tls.createServer(tcase.serverOpts, function(s) {
66+
s.resume();
67+
}).listen(common.PORT, function() {
68+
var client = tls.connect(tcase.clientOpts);
69+
client.on('error', function(e) {
70+
assert.strictEqual(e.code, tcase.errorCode);
71+
server.close(function() {
72+
finished++;
73+
runTest(tindex + 1);
74+
});
75+
});
3876
});
39-
});
77+
}
78+
79+
runTest(0);
80+
4081
process.on('exit', function() {
41-
assert(error);
82+
assert.equal(finished, testCases.length);
4283
});

0 commit comments

Comments
 (0)