|
| 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