Skip to content

Commit ea72331

Browse files
committed
test: check tls server verification with addCACert
SecureContext.addCACert() adds to the existing root store, preserving root cert entries. option.ca is applied without calling SecureContext.addRootCerts() so should add to the default, empty, root store. This test confirms that the built-in root CAs are not included when options.ca is used. Based on: shigeki@acd5837 PR-URL: #10389 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 99b0c2e commit ea72331

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

test/internet/test-tls-add-ca-cert.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (!common.hasCrypto) {
5+
common.skip('missing crypto');
6+
return;
7+
}
8+
9+
// Test interaction of compiled-in CAs with user-provided CAs.
10+
11+
const assert = require('assert');
12+
const fs = require('fs');
13+
const tls = require('tls');
14+
15+
function filenamePEM(n) {
16+
return require('path').join(common.fixturesDir, 'keys', n + '.pem');
17+
}
18+
19+
function loadPEM(n) {
20+
return fs.readFileSync(filenamePEM(n));
21+
}
22+
23+
const caCert = loadPEM('ca1-cert');
24+
25+
var opts = {
26+
host: 'www.nodejs.org',
27+
port: 443,
28+
rejectUnauthorized: true
29+
};
30+
31+
// Success relies on the compiled in well-known root CAs
32+
tls.connect(opts, common.mustCall(end));
33+
34+
// The .ca option replaces the well-known roots, so connection fails.
35+
opts.ca = caCert;
36+
tls.connect(opts, fail).on('error', common.mustCall((err) => {
37+
assert.strictEqual(err.message, 'unable to get local issuer certificate');
38+
}));
39+
40+
function fail() {
41+
assert(false, 'should fail to connect');
42+
}
43+
44+
// New secure contexts have the well-known root CAs.
45+
opts.secureContext = tls.createSecureContext();
46+
tls.connect(opts, common.mustCall(end));
47+
48+
// Explicit calls to addCACert() add to the default well-known roots, instead
49+
// of replacing, so connection still succeeds.
50+
opts.secureContext.context.addCACert(caCert);
51+
tls.connect(opts, common.mustCall(end));
52+
53+
function end() {
54+
this.end();
55+
}

0 commit comments

Comments
 (0)