1'use strict';
2
3const common = require('../common');
4const fixtures = require('../common/fixtures');
5
6if (!common.hasCrypto)
7  common.skip('missing crypto');
8
9const assert = require('assert');
10const tls = require('tls');
11
12function toArrayBuffer(buf) {
13  const ab = new ArrayBuffer(buf.length);
14  const view = new Uint8Array(ab);
15  return buf.map((b, i) => view[i] = b);
16}
17
18function toDataView(buf) {
19  const ab = new ArrayBuffer(buf.length);
20  const view = new DataView(ab);
21  return buf.map((b, i) => view[i] = b);
22}
23
24const keyBuff = fixtures.readKey('agent1-key.pem');
25const certBuff = fixtures.readKey('agent1-cert.pem');
26const keyBuff2 = fixtures.readKey('ec-key.pem');
27const certBuff2 = fixtures.readKey('ec-cert.pem');
28const caCert = fixtures.readKey('ca1-cert.pem');
29const caCert2 = fixtures.readKey('ca2-cert.pem');
30const keyStr = keyBuff.toString();
31const certStr = certBuff.toString();
32const keyStr2 = keyBuff2.toString();
33const certStr2 = certBuff2.toString();
34const caCertStr = caCert.toString();
35const caCertStr2 = caCert2.toString();
36const keyArrBuff = toArrayBuffer(keyBuff);
37const certArrBuff = toArrayBuffer(certBuff);
38const caArrBuff = toArrayBuffer(caCert);
39const keyDataView = toDataView(keyBuff);
40const certDataView = toDataView(certBuff);
41const caArrDataView = toDataView(caCert);
42
43// Checks to ensure tls.createServer doesn't throw an error
44// Format ['key', 'cert']
45[
46  [keyBuff, certBuff],
47  [false, certBuff],
48  [keyBuff, false],
49  [keyStr, certStr],
50  [false, certStr],
51  [keyStr, false],
52  [false, false],
53  [keyArrBuff, certArrBuff],
54  [keyArrBuff, false],
55  [false, certArrBuff],
56  [keyDataView, certDataView],
57  [keyDataView, false],
58  [false, certDataView],
59  [[keyBuff, keyBuff2], [certBuff, certBuff2]],
60  [[keyStr, keyStr2], [certStr, certStr2]],
61  [[keyStr, keyStr2], false],
62  [false, [certStr, certStr2]],
63  [[{ pem: keyBuff }], false],
64  [[{ pem: keyBuff }, { pem: keyBuff }], false],
65].forEach(([key, cert]) => {
66  tls.createServer({ key, cert });
67});
68
69// Checks to ensure tls.createServer predictably throws an error
70// Format ['key', 'cert', 'expected message']
71[
72  [true, certBuff],
73  [true, certStr],
74  [true, certArrBuff],
75  [true, certDataView],
76  [true, false],
77  [true, false],
78  [{ pem: keyBuff }, false],
79  [[keyBuff, true], [certBuff, certBuff2], 1],
80  [[true, keyStr2], [certStr, certStr2], 0],
81  [[true, false], [certBuff, certBuff2], 0],
82  [true, [certBuff, certBuff2]],
83].forEach(([key, cert, index]) => {
84  const val = index === undefined ? key : key[index];
85  assert.throws(() => {
86    tls.createServer({ key, cert });
87  }, {
88    code: 'ERR_INVALID_ARG_TYPE',
89    name: 'TypeError',
90    message: 'The "options.key" property must be of type string or an ' +
91             'instance of Buffer, TypedArray, or DataView.' +
92             common.invalidArgTypeHelper(val)
93  });
94});
95
96[
97  [keyBuff, true],
98  [keyStr, true],
99  [keyArrBuff, true],
100  [keyDataView, true],
101  [true, true],
102  [false, true],
103  [false, { pem: keyBuff }],
104  [false, 1],
105  [[keyBuff, keyBuff2], [true, certBuff2], 0],
106  [[keyStr, keyStr2], [certStr, true], 1],
107  [[keyStr, keyStr2], [true, false], 0],
108  [[keyStr, keyStr2], true],
109].forEach(([key, cert, index]) => {
110  const val = index === undefined ? cert : cert[index];
111  assert.throws(() => {
112    tls.createServer({ key, cert });
113  }, {
114    code: 'ERR_INVALID_ARG_TYPE',
115    name: 'TypeError',
116    message: 'The "options.cert" property must be of type string or an ' +
117             'instance of Buffer, TypedArray, or DataView.' +
118             common.invalidArgTypeHelper(val)
119  });
120});
121
122// Checks to ensure tls.createServer works with the CA parameter
123// Format ['key', 'cert', 'ca']
124[
125  [keyBuff, certBuff, caCert],
126  [keyBuff, certBuff, [caCert, caCert2]],
127  [keyBuff, certBuff, caCertStr],
128  [keyBuff, certBuff, [caCertStr, caCertStr2]],
129  [keyBuff, certBuff, caArrBuff],
130  [keyBuff, certBuff, caArrDataView],
131  [keyBuff, certBuff, false],
132].forEach(([key, cert, ca]) => {
133  tls.createServer({ key, cert, ca });
134});
135
136// Checks to ensure tls.createServer throws an error for CA assignment
137// Format ['key', 'cert', 'ca']
138[
139  [keyBuff, certBuff, true],
140  [keyBuff, certBuff, {}],
141  [keyBuff, certBuff, 1],
142  [keyBuff, certBuff, true],
143  [keyBuff, certBuff, [caCert, true], 1],
144].forEach(([key, cert, ca, index]) => {
145  const val = index === undefined ? ca : ca[index];
146  assert.throws(() => {
147    tls.createServer({ key, cert, ca });
148  }, {
149    code: 'ERR_INVALID_ARG_TYPE',
150    name: 'TypeError',
151    message: 'The "options.ca" property must be of type string or an instance' +
152             ' of Buffer, TypedArray, or DataView.' +
153             common.invalidArgTypeHelper(val)
154  });
155});
156
157// Checks to ensure tls.createSecureContext works with false-y input
158// Format ['key', 'cert', 'ca']
159[
160  [null, null, null],
161  [false, false, false],
162  [undefined, undefined, undefined],
163  ['', '', ''],
164  [0, 0, 0],
165].forEach(([key, cert, ca]) => {
166  tls.createSecureContext({ key, cert, ca });
167});
168