Skip to content

Commit 4207bce

Browse files
sam-githubMylesBorins
authored andcommitted
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 Backport-PR-URL: #12468 PR-URL: #10389 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent cbfc3fc commit 4207bce

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
constcommon=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+
constassert=require('assert');
12+
constfs=require('fs');
13+
consttls=require('tls');
14+
15+
functionfilenamePEM(n){
16+
returnrequire('path').join(common.fixturesDir,'keys',n+'.pem');
17+
}
18+
19+
functionloadPEM(n){
20+
returnfs.readFileSync(filenamePEM(n));
21+
}
22+
23+
constcaCert=loadPEM('ca1-cert');
24+
25+
constopts={
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+
functionfail(){
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+
functionend(){
54+
this.end();
55+
}

0 commit comments

Comments
 (0)