• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const fixtures = require('../common/fixtures');
8const assert = require('assert');
9const tls = require('tls');
10
11function loadPEM(n) {
12  return fixtures.readKey(`${n}.pem`);
13}
14
15const serverOptions = {
16  key: loadPEM('agent2-key'),
17  cert: loadPEM('agent2-cert'),
18  ca: [ loadPEM('ca2-cert') ],
19  requestCert: true,
20  rejectUnauthorized: false,
21};
22
23let connections = 0;
24
25const server = tls.createServer(serverOptions, (c) => {
26  if (++connections === 3) {
27    server.close();
28  }
29  if (c.servername === 'unknowncontext') {
30    assert.strictEqual(c.authorized, false);
31    return;
32  }
33  assert.strictEqual(c.authorized, true);
34});
35
36const secureContext = {
37  key: loadPEM('agent1-key'),
38  cert: loadPEM('agent1-cert'),
39  ca: [ loadPEM('ca1-cert') ],
40};
41server.addContext('context1', secureContext);
42server.addContext('context2', tls.createSecureContext(secureContext));
43
44const clientOptionsBase = {
45  key: loadPEM('agent1-key'),
46  cert: loadPEM('agent1-cert'),
47  ca: [ loadPEM('ca1-cert') ],
48  rejectUnauthorized: false,
49};
50
51server.listen(0, common.mustCall(() => {
52  const client1 = tls.connect({
53    ...clientOptionsBase,
54    port: server.address().port,
55    servername: 'context1',
56  }, common.mustCall(() => {
57    client1.end();
58  }));
59
60  const client2 = tls.connect({
61    ...clientOptionsBase,
62    port: server.address().port,
63    servername: 'context2',
64  }, common.mustCall(() => {
65    client2.end();
66  }));
67
68  const client3 = tls.connect({
69    ...clientOptionsBase,
70    port: server.address().port,
71    servername: 'unknowncontext',
72  }, common.mustCall(() => {
73    client3.end();
74  }));
75}));
76