mock-tls-server 0.0.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -18
- package/lib/ca.js +46 -41
- package/lib/index.js +70 -345
- package/lib/kernel.js +23 -22
- package/lib/net.js +94 -88
- package/lib/socket.js +36 -26
- package/lib/utils.js +54 -38
- package/package.json +22 -20
- package/types/ca.d.ts +9 -8
- package/types/index.d.ts +46 -763
- package/types/kernel.d.ts +6 -5
- package/types/net.d.ts +63 -46
- package/types/socket.d.ts +12 -3
- package/types/utils.d.ts +20 -13
package/lib/index.js
CHANGED
|
@@ -1,177 +1,43 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import tls from 'tls'
|
|
5
|
-
|
|
6
|
-
const PRIVATE = new WeakMap()
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @callback SecureConnectionCallback
|
|
10
|
-
* @param {tls.TLSSocket} sock The server side of the socket that was connected.
|
|
11
|
-
* @returns {void}
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @callback PSKCallback
|
|
16
|
-
* @param {string} [hint] Optional message sent from the server to help client
|
|
17
|
-
* decide which identity to use during negotiation. Always null if TLS 1.3
|
|
18
|
-
* is used.
|
|
19
|
-
* @returns {tls.PSKCallbackNegotation|null} Or null to stop the
|
|
20
|
-
* negotiation process. PSK must be compatible with the selected cipher's
|
|
21
|
-
* digest.
|
|
22
|
-
*/
|
|
1
|
+
import * as net from './net.js';
|
|
2
|
+
import {log, normalizeArgs, select} from './utils.js';
|
|
3
|
+
import {CertificateAuthority} from './ca.js';
|
|
4
|
+
import tls from 'node:tls';
|
|
23
5
|
|
|
24
6
|
/**
|
|
25
|
-
* @
|
|
26
|
-
* @param {Error} [error] An error ocurred.
|
|
27
|
-
* @param {tls.SecureContext} [context] The context to use, if desired.
|
|
28
|
-
* @returns {void}
|
|
7
|
+
* @typedef {(sock: tls.TLSSocket) => void} SecureConnectionCallback
|
|
29
8
|
*/
|
|
30
9
|
|
|
31
10
|
/**
|
|
32
|
-
* @
|
|
33
|
-
* @param {string} serverName The name passed by the client.
|
|
34
|
-
* @param {SecureContextCallback} callback Choose a security context
|
|
35
|
-
* for this connection.
|
|
36
|
-
* @returns {void}
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* @typedef {object} ServerOpts
|
|
11
|
+
* @typedef {object} LocalOptions
|
|
41
12
|
* @property {CertificateAuthority} [authority] A CA instance to use. If you
|
|
42
13
|
* are creating lots of servers, create one CA to pass into each of them, so
|
|
43
14
|
* that certs don't need to be created from scratch for each.
|
|
44
15
|
* @property {SecureConnectionCallback} [secureConnectionListener] Installed
|
|
45
16
|
* as a listener for the 'secureConnection' event.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @property {
|
|
60
|
-
*
|
|
61
|
-
* array of strings, Buffers or DataViews, or a single Buffer or DataView
|
|
62
|
-
* containing the supported ALPN protocols. Buffers should have the format
|
|
63
|
-
* [len][name][len][name]... E.g. '\x08http/1.1\x08http/1.0', where the len
|
|
64
|
-
* byte is the length of the next protocol name. Passing an array is usually
|
|
65
|
-
* much simpler, e.g. ['http/1.1', 'http/1.0']. Protocols earlier in the
|
|
66
|
-
* list have higher preference than those later.
|
|
67
|
-
* @property {SNICallback} SNICallback See tls.createServer().
|
|
68
|
-
* @property {Buffer} [session] A Buffer instance, containing TLS session.
|
|
69
|
-
* @property {boolean} requestOCSP If true, specifies that the OCSP status
|
|
70
|
-
* request extension will be added to the client hello and an 'OCSPResponse'
|
|
71
|
-
* event will be emitted on the socket before establishing a secure
|
|
72
|
-
* communication.
|
|
73
|
-
* @property {tls.SecureContext} [secureContext] TLS context object created
|
|
74
|
-
* with tls.createSecureContext(). If a secureContext is not provided, one
|
|
75
|
-
* will be created by passing the entire options object to
|
|
76
|
-
* tls.createSecureContext().
|
|
77
|
-
* @property {string[]|string|Buffer[]|Buffer} [ca] Optionally override the
|
|
78
|
-
* trusted CA certificates. Default is to trust the well-known CAs curated
|
|
79
|
-
* by Mozilla. Mozilla's CAs are completely replaced when CAs are explicitly
|
|
80
|
-
* specified using this option.
|
|
81
|
-
* @property {string[]|string|Buffer[]|Buffer} [cert] Cert chains in PEM
|
|
82
|
-
* format. One cert chain should be provided per private key. Each cert
|
|
83
|
-
* chain should consist of the PEM formatted certificate for a provided
|
|
84
|
-
* private key, followed by the PEM formatted intermediate certificates (if
|
|
85
|
-
* any), in order, and not including the root CA (the root CA must be
|
|
86
|
-
* pre-known to the peer, see ca). When providing multiple cert chains, they
|
|
87
|
-
* do not have to be in the same order as their private keys in key. If the
|
|
88
|
-
* intermediate certificates are not provided, the peer will not be able to
|
|
89
|
-
* validate the certificate, and the handshake will fail.
|
|
90
|
-
* @property {string} [sigalgs] Colon-separated list of supported signature
|
|
91
|
-
* algorithms. The list can contain digest algorithms (SHA256, MD5 etc.),
|
|
92
|
-
* public key algorithms (RSA-PSS, ECDSA etc.), combination of both
|
|
93
|
-
* ('RSA+SHA384') or TLS v1.3 scheme names (rsa_pss_pss_sha512). See OpenSSL
|
|
94
|
-
* man pages for more info.
|
|
95
|
-
* @property {string} [ciphers] Cipher suite specification, replacing the
|
|
96
|
-
* default. Cipher names are separated by colons, and must be uppercased in
|
|
97
|
-
* order for OpenSSL to accept them.
|
|
98
|
-
* @property {string} [clientCertEngine] Name of an OpenSSL engine which can
|
|
99
|
-
* provide the client certificate.
|
|
100
|
-
* @property {string|string[]|Buffer|Buffer[]} [crl] PEM formatted CRLs
|
|
101
|
-
* (Certificate Revocation Lists).
|
|
102
|
-
* @property {string|Buffer} [dhparam] Diffie-Hellman parameters, required for
|
|
103
|
-
* perfect forward secrecy. Use openssl dhparam to create the parameters.
|
|
104
|
-
* The key length must be greater than or equal to 1024 bits or else an
|
|
105
|
-
* error will be thrown. Although 1024 bits is permissible, use 2048 bits or
|
|
106
|
-
* larger for stronger security. If omitted or invalid, the parameters are
|
|
107
|
-
* silently discarded and DHE ciphers will not be available.
|
|
108
|
-
* @property {string} [ecdhCurve] A string describing a named curve or a colon
|
|
109
|
-
* separated list of curve NIDs or names, for example P-521:P-384:P-256, to
|
|
110
|
-
* use for ECDH key agreement. Set to auto to select the curve
|
|
111
|
-
* automatically. Use crypto.getCurves() to obtain a list of available curve
|
|
112
|
-
* names. On recent releases, openssl ecparam -list_curves will also display
|
|
113
|
-
* the name and description of each available elliptic curve. Default:
|
|
114
|
-
* tls.DEFAULT_ECDH_CURVE.
|
|
115
|
-
* @property {boolean} [honorCipherOrder] Attempt to use the server's cipher
|
|
116
|
-
* suite preferences instead of the client's. When true, causes
|
|
117
|
-
* SSL_OP_CIPHER_SERVER_PREFERENCE to be set in secureOptions, see OpenSSL
|
|
118
|
-
* Options for more information.
|
|
119
|
-
* @property {string|string[]|Buffer|Buffer[]|object[]} [key] Private keys in
|
|
120
|
-
* PEM format. PEM allows the option of private keys being encrypted.
|
|
121
|
-
* Encrypted keys will be decrypted with options.passphrase. Multiple keys
|
|
122
|
-
* using different algorithms can be provided either as an array of
|
|
123
|
-
* unencrypted key strings or buffers, or an array of objects in the form
|
|
124
|
-
* {pem:string|buffer[, passphrase: string]}. The object form can only occur
|
|
125
|
-
* in an array. Passphrase is optional. Encrypted keys will be decrypted
|
|
126
|
-
* with object.passphrase if provided, or options.passphrase if it is not.
|
|
127
|
-
* @property {string} [privateKeyEngine] Name of an OpenSSL engine to get
|
|
128
|
-
* private key from. Should be used together with privateKeyIdentifier.
|
|
129
|
-
* @property {string} [privateKeyIdentifier] Identifier of a private key
|
|
130
|
-
* managed by an OpenSSL engine. Should be used together with
|
|
131
|
-
* privateKeyEngine. Should not be set together with key, because both
|
|
132
|
-
* options define a private key in different ways.
|
|
133
|
-
* @property {string} [maxVersion=tls.DEFAULT_MAX_VERSION] Optionally set the
|
|
134
|
-
* maximum TLS version to allow. One of 'TLSv1.3', 'TLSv1.2', 'TLSv1.1', or
|
|
135
|
-
* 'TLSv1'. Cannot be specified along with the secureProtocol option; use
|
|
136
|
-
* one or the other.
|
|
137
|
-
* @property {string} [minVersion=tls.DEFAULT_MIN_VERSION] Optionally set the
|
|
138
|
-
* minimum TLS version to allow. One of 'TLSv1.3', 'TLSv1.2', 'TLSv1.1', or
|
|
139
|
-
* 'TLSv1'. Cannot be specified along with the secureProtocol option; use
|
|
140
|
-
* one or the other. Avoid setting to less than TLSv1.2, but it may be
|
|
141
|
-
* required for interoperability.
|
|
142
|
-
* @property {string} [passphrase] Shared passphrase used for a single private
|
|
143
|
-
* key and/or a PFX.
|
|
144
|
-
* @property {string|string[]|Buffer|Buffer[]|object[]} [pfx] PFX or PKCS12
|
|
145
|
-
* encoded private key and certificate chain. PFX is an alternative to
|
|
146
|
-
* providing key and cert individually. PFX is usually encrypted, if it is,
|
|
147
|
-
* passphrase will be used to decrypt it.
|
|
148
|
-
* @property {number} [secureOptions] Optionally affect the OpenSSL protocol
|
|
149
|
-
* behavior, which is not usually necessary. This should be used carefully
|
|
150
|
-
* if at all! Value is a numeric bitmask of the SSL_OP_* options from
|
|
151
|
-
* OpenSSL Options.
|
|
152
|
-
* @property {string} [secureProtocol] Legacy mechanism to select the TLS
|
|
153
|
-
* protocol version to use, it does not support independent control of the
|
|
154
|
-
* minimum and maximum version, and does not support limiting the protocol
|
|
155
|
-
* to TLSv1.3. Use minVersion and maxVersion instead. The possible values
|
|
156
|
-
* are listed as SSL_METHODS, use the function names as strings. For
|
|
157
|
-
* example, use 'TLSv1_1_method' to force TLS version 1.1, or 'TLS_method'
|
|
158
|
-
* to allow any TLS protocol version up to TLSv1.3. It is not recommended to
|
|
159
|
-
* use TLS versions less than 1.2, but it may be required for
|
|
160
|
-
* interoperability. Default: none, see minVersion.
|
|
161
|
-
* @property {string} [sessionIdContext] Opaque identifier used by servers to
|
|
162
|
-
* ensure session state is not shared between applications. Unused by
|
|
163
|
-
* clients.
|
|
164
|
-
* @property {Buffer} [ticketKeys] 48-bytes of cryptographically strong
|
|
165
|
-
* pseudorandom data. See Session Resumption for more information.
|
|
166
|
-
* @property {number} [sessionTimeout=300] The number of seconds after which a
|
|
167
|
-
* TLS session created by the server will no longer be resumable. See
|
|
168
|
-
* Session Resumption for more information.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {LocalOptions &
|
|
21
|
+
* tls.TLSSocketOptions &
|
|
22
|
+
* import('./ca.js').CertOpts &
|
|
23
|
+
* import('./net.js').ServerOpts
|
|
24
|
+
* } ServerOpts
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {object} ListenOptions
|
|
29
|
+
* @property {number} port The port to listen on.
|
|
30
|
+
* @property {SecureConnectionCallback} secureConnectionListener If it exists,
|
|
31
|
+
* installed as a listener for the 'secureConnection' event.
|
|
169
32
|
*/
|
|
170
33
|
|
|
171
34
|
/**
|
|
172
35
|
* A TLS server that mocks the network, but all of the crypto code is real.
|
|
173
36
|
*/
|
|
174
37
|
export class MockTLSServer extends net.Server {
|
|
38
|
+
/** @type {string} */
|
|
39
|
+
#key;
|
|
40
|
+
|
|
175
41
|
/**
|
|
176
42
|
* Create a MockTLSServer instance.
|
|
177
43
|
*
|
|
@@ -180,13 +46,16 @@ export class MockTLSServer extends net.Server {
|
|
|
180
46
|
* a listener for the 'secureConnection' event.
|
|
181
47
|
*/
|
|
182
48
|
constructor(options, secureConnectionListener) {
|
|
183
|
-
|
|
49
|
+
if (typeof options === 'function') {
|
|
50
|
+
options = {secureConnectionListener: options};
|
|
51
|
+
}
|
|
52
|
+
const opts = normalizeArgs(options, {secureConnectionListener});
|
|
184
53
|
const [local_opts, cert_opts, socket_opts, tls_opts] = select(
|
|
185
54
|
opts,
|
|
186
55
|
['authority', 'secureConnectionListener'],
|
|
187
56
|
['notafter', 'notbefore', 'subject', 'names'],
|
|
188
57
|
['allowHalfOpen']
|
|
189
|
-
)
|
|
58
|
+
);
|
|
190
59
|
|
|
191
60
|
super(socket_opts, sock => {
|
|
192
61
|
// Turn connection into TLS sock and call secureConnectionListener
|
|
@@ -194,47 +63,41 @@ export class MockTLSServer extends net.Server {
|
|
|
194
63
|
const tsock = new tls.TLSSocket(sock, {
|
|
195
64
|
isServer: true,
|
|
196
65
|
cert: this.cert,
|
|
197
|
-
key:
|
|
66
|
+
key: this.#key,
|
|
198
67
|
...tls_opts,
|
|
199
|
-
})
|
|
200
|
-
const temit = tsock.emit
|
|
68
|
+
});
|
|
69
|
+
const temit = tsock.emit;
|
|
201
70
|
tsock.emit = (eventName, ...args) => {
|
|
202
71
|
log('tls', eventName, ...args.map(
|
|
203
72
|
o => ((typeof o === 'object') ? o.constructor.name || o.toString() : o)
|
|
204
|
-
))
|
|
205
|
-
|
|
206
|
-
|
|
73
|
+
));
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
return temit.call(tsock, eventName, ...args);
|
|
76
|
+
};
|
|
207
77
|
tsock.on('secure', () => {
|
|
208
|
-
process.nextTick(() => this.emit('secureConnection', tsock))
|
|
209
|
-
})
|
|
210
|
-
})
|
|
78
|
+
process.nextTick(() => this.emit('secureConnection', tsock));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
211
81
|
|
|
212
82
|
if (opts.cert) {
|
|
213
83
|
// User-supplied cert and key. Don't spin up a CA instance.
|
|
214
|
-
this.cert = tls_opts.cert
|
|
215
|
-
this.ca = tls_opts.ca
|
|
216
|
-
|
|
84
|
+
this.cert = tls_opts.cert;
|
|
85
|
+
this.ca = tls_opts.ca;
|
|
86
|
+
this.#key = tls_opts.key;
|
|
217
87
|
} else {
|
|
218
|
-
const authority = local_opts.authority || new CertificateAuthority()
|
|
219
|
-
const serverProps = authority.issue(cert_opts)
|
|
220
|
-
this.cert = serverProps.cert
|
|
221
|
-
this.ca = serverProps.ca
|
|
222
|
-
|
|
88
|
+
const authority = local_opts.authority || new CertificateAuthority();
|
|
89
|
+
const serverProps = authority.issue(cert_opts);
|
|
90
|
+
this.cert = serverProps.cert;
|
|
91
|
+
this.ca = serverProps.ca;
|
|
92
|
+
this.#key = serverProps.key;
|
|
223
93
|
}
|
|
224
94
|
|
|
225
95
|
if (local_opts.secureConnectionListener) {
|
|
226
|
-
this.on('secureConnection', local_opts.secureConnectionListener)
|
|
96
|
+
this.on('secureConnection', local_opts.secureConnectionListener);
|
|
227
97
|
}
|
|
228
98
|
// Create a real TLSSocket for each connection.
|
|
229
99
|
}
|
|
230
100
|
|
|
231
|
-
/**
|
|
232
|
-
* @typedef {object} ListenOptions
|
|
233
|
-
* @property {number} port The port to listen on.
|
|
234
|
-
* @property {Function} secureConnectionListener If it exists, installed as
|
|
235
|
-
* a listener for the 'secureConnection' event.
|
|
236
|
-
*/
|
|
237
|
-
|
|
238
101
|
/**
|
|
239
102
|
* Listen for incoming connections. Whenever `connect` is called on this
|
|
240
103
|
* server's port, create a mock socket wrapped in a TLSSocket.
|
|
@@ -246,192 +109,54 @@ export class MockTLSServer extends net.Server {
|
|
|
246
109
|
*/
|
|
247
110
|
// @ts-ignore TS2416: This is compatible-enough with the base class.
|
|
248
111
|
listen(options, secureConnectionListener) {
|
|
249
|
-
const
|
|
112
|
+
const {port, secureConnectionListener: onConnect} =
|
|
113
|
+
normalizeArgs(options, {secureConnectionListener});
|
|
250
114
|
|
|
251
|
-
if (
|
|
252
|
-
this.on('secureConnection',
|
|
115
|
+
if (onConnect) {
|
|
116
|
+
this.on('secureConnection', onConnect);
|
|
253
117
|
}
|
|
254
|
-
super.listen(
|
|
255
|
-
return this
|
|
118
|
+
super.listen(port);
|
|
119
|
+
return this;
|
|
256
120
|
}
|
|
257
121
|
}
|
|
258
122
|
|
|
259
|
-
/**
|
|
260
|
-
* All of the options that can be passed in to connect(), with text copied
|
|
261
|
-
* from the Node docs.
|
|
262
|
-
*
|
|
263
|
-
* @typedef {object} SocketConnectOpts
|
|
264
|
-
* @property {number} port Listening server port to connect to.
|
|
265
|
-
* @property {boolean} [allowHalfOpen=false] If set to false, then the socket
|
|
266
|
-
* will automatically end the writable side when the readable side ends.
|
|
267
|
-
* @property {boolean} [rejectUnauthorized=true] If not false, the server
|
|
268
|
-
* certificate is verified against the list of supplied CAs. An 'error'
|
|
269
|
-
* event is emitted if verification fails; err.code contains the OpenSSL
|
|
270
|
-
* error code.
|
|
271
|
-
* @property {PSKCallback} [pskCallback] When negotiating TLS-PSK (pre-shared
|
|
272
|
-
* keys), this function is called with optional identity hint provided by
|
|
273
|
-
* the server or null in case of TLS 1.3 where hint was removed. It will be
|
|
274
|
-
* necessary to provide a custom tls.checkServerIdentity() for the
|
|
275
|
-
* connection as the default one will try to check host name/IP of the
|
|
276
|
-
* server against the certificate but that's not applicable for PSK because
|
|
277
|
-
* there won't be a certificate present. More information can be found in
|
|
278
|
-
* the RFC 4279.
|
|
279
|
-
* @property {string[]|Buffer[]|DataView[]|Buffer|DataView} [ALPNProtocols] An
|
|
280
|
-
* array of strings, Buffers or DataViews, or a single Buffer or DataView
|
|
281
|
-
* containing the supported ALPN protocols. Buffers should have the format
|
|
282
|
-
* [len][name][len][name]... E.g. '\x08http/1.1\x08http/1.0', where the len
|
|
283
|
-
* byte is the length of the next protocol name. Passing an array is usually
|
|
284
|
-
* much simpler, e.g. ['http/1.1', 'http/1.0']. Protocols earlier in the
|
|
285
|
-
* list have higher preference than those later.
|
|
286
|
-
* @property {string} [servername] Server name for the SNI (Server Name
|
|
287
|
-
* Indication) TLS extension. It is the name of the host being connected to,
|
|
288
|
-
* and must be a host name, and not an IP address. It can be used by a
|
|
289
|
-
* multi-homed server to choose the correct certificate to present to the
|
|
290
|
-
* client, see the SNICallback option to tls.createServer().
|
|
291
|
-
* @property {tls.checkServerIdentity} [checkServerIdentity] A callback
|
|
292
|
-
* function to be used (instead of the builtin tls.checkServerIdentity()
|
|
293
|
-
* function) when checking the server's host name (or the provided
|
|
294
|
-
* servername when explicitly set) against the certificate. This should
|
|
295
|
-
* return an <Error> if verification fails. The method should return
|
|
296
|
-
* undefined if the servername and cert are verified.
|
|
297
|
-
* @property {Buffer} [session] A Buffer instance, containing TLS session.
|
|
298
|
-
* @property {number} [minDHSize=1024] Minimum size of the DH parameter in
|
|
299
|
-
* bits to accept a TLS connection. When a server offers a DH parameter with
|
|
300
|
-
* a size less than minDHSize, the TLS connection is destroyed and an error
|
|
301
|
-
* is thrown.
|
|
302
|
-
* @property {number} [highWaterMark=16 * 1024] Consistent with the readable
|
|
303
|
-
* stream highWaterMark parameter.
|
|
304
|
-
* @property {tls.SecureContext} [secureContext] TLS context object created
|
|
305
|
-
* with tls.createSecureContext(). If a secureContext is not provided, one
|
|
306
|
-
* will be created by passing the entire options object to
|
|
307
|
-
* tls.createSecureContext().
|
|
308
|
-
* @property {string[]|string|Buffer[]|Buffer} [ca] Optionally override the
|
|
309
|
-
* trusted CA certificates. Default is to trust the well-known CAs curated
|
|
310
|
-
* by Mozilla. Mozilla's CAs are completely replaced when CAs are explicitly
|
|
311
|
-
* specified using this option.
|
|
312
|
-
* @property {string[]|string|Buffer[]|Buffer} [cert] Cert chains in PEM
|
|
313
|
-
* format. One cert chain should be provided per private key. Each cert
|
|
314
|
-
* chain should consist of the PEM formatted certificate for a provided
|
|
315
|
-
* private key, followed by the PEM formatted intermediate certificates (if
|
|
316
|
-
* any), in order, and not including the root CA (the root CA must be
|
|
317
|
-
* pre-known to the peer, see ca). When providing multiple cert chains, they
|
|
318
|
-
* do not have to be in the same order as their private keys in key. If the
|
|
319
|
-
* intermediate certificates are not provided, the peer will not be able to
|
|
320
|
-
* validate the certificate, and the handshake will fail.
|
|
321
|
-
* @property {string} [sigalgs] Colon-separated list of supported signature
|
|
322
|
-
* algorithms. The list can contain digest algorithms (SHA256, MD5 etc.),
|
|
323
|
-
* public key algorithms (RSA-PSS, ECDSA etc.), combination of both
|
|
324
|
-
* ('RSA+SHA384') or TLS v1.3 scheme names (rsa_pss_pss_sha512). See OpenSSL
|
|
325
|
-
* man pages for more info.
|
|
326
|
-
* @property {string} [ciphers] Cipher suite specification, replacing the
|
|
327
|
-
* default. Cipher names are separated by colons, and must be uppercased in
|
|
328
|
-
* order for OpenSSL to accept them.
|
|
329
|
-
* @property {string} [clientCertEngine] Name of an OpenSSL engine which can
|
|
330
|
-
* provide the client certificate.
|
|
331
|
-
* @property {string|string[]|Buffer|Buffer[]} [crl] PEM formatted CRLs
|
|
332
|
-
* (Certificate Revocation Lists).
|
|
333
|
-
* @property {string|Buffer} [dhparam] Diffie-Hellman parameters, required for
|
|
334
|
-
* perfect forward secrecy. Use openssl dhparam to create the parameters.
|
|
335
|
-
* The key length must be greater than or equal to 1024 bits or else an
|
|
336
|
-
* error will be thrown. Although 1024 bits is permissible, use 2048 bits or
|
|
337
|
-
* larger for stronger security. If omitted or invalid, the parameters are
|
|
338
|
-
* silently discarded and DHE ciphers will not be available.
|
|
339
|
-
* @property {string} [ecdhCurve] A string describing a named curve or a colon
|
|
340
|
-
* separated list of curve NIDs or names, for example P-521:P-384:P-256, to
|
|
341
|
-
* use for ECDH key agreement. Set to auto to select the curve
|
|
342
|
-
* automatically. Use crypto.getCurves() to obtain a list of available curve
|
|
343
|
-
* names. On recent releases, openssl ecparam -list_curves will also display
|
|
344
|
-
* the name and description of each available elliptic curve. Default:
|
|
345
|
-
* tls.DEFAULT_ECDH_CURVE.
|
|
346
|
-
* @property {boolean} [honorCipherOrder] Attempt to use the server's cipher
|
|
347
|
-
* suite preferences instead of the client's. When true, causes
|
|
348
|
-
* SSL_OP_CIPHER_SERVER_PREFERENCE to be set in secureOptions, see OpenSSL
|
|
349
|
-
* Options for more information.
|
|
350
|
-
* @property {string|string[]|Buffer|Buffer[]|object[]} [key] Private keys in
|
|
351
|
-
* PEM format. PEM allows the option of private keys being encrypted.
|
|
352
|
-
* Encrypted keys will be decrypted with options.passphrase. Multiple keys
|
|
353
|
-
* using different algorithms can be provided either as an array of
|
|
354
|
-
* unencrypted key strings or buffers, or an array of objects in the form
|
|
355
|
-
* {pem:string|buffer[, passphrase: string]}. The object form can only occur
|
|
356
|
-
* in an array. Passphrase is optional. Encrypted keys will be decrypted
|
|
357
|
-
* with object.passphrase if provided, or options.passphrase if it is not.
|
|
358
|
-
* @property {string} [privateKeyEngine] Name of an OpenSSL engine to get
|
|
359
|
-
* private key from. Should be used together with privateKeyIdentifier.
|
|
360
|
-
* @property {string} [privateKeyIdentifier] Identifier of a private key
|
|
361
|
-
* managed by an OpenSSL engine. Should be used together with
|
|
362
|
-
* privateKeyEngine. Should not be set together with key, because both
|
|
363
|
-
* options define a private key in different ways.
|
|
364
|
-
* @property {string} [maxVersion=tls.DEFAULT_MAX_VERSION] Optionally set the
|
|
365
|
-
* maximum TLS version to allow. One of 'TLSv1.3', 'TLSv1.2', 'TLSv1.1', or
|
|
366
|
-
* 'TLSv1'. Cannot be specified along with the secureProtocol option; use
|
|
367
|
-
* one or the other.
|
|
368
|
-
* @property {string} [minVersion=tls.DEFAULT_MIN_VERSION] Optionally set the
|
|
369
|
-
* minimum TLS version to allow. One of 'TLSv1.3', 'TLSv1.2', 'TLSv1.1', or
|
|
370
|
-
* 'TLSv1'. Cannot be specified along with the secureProtocol option; use
|
|
371
|
-
* one or the other. Avoid setting to less than TLSv1.2, but it may be
|
|
372
|
-
* required for interoperability.
|
|
373
|
-
* @property {string} [passphrase] Shared passphrase used for a single private
|
|
374
|
-
* key and/or a PFX.
|
|
375
|
-
* @property {string|string[]|Buffer|Buffer[]|object[]} [pfx] PFX or PKCS12
|
|
376
|
-
* encoded private key and certificate chain. PFX is an alternative to
|
|
377
|
-
* providing key and cert individually. PFX is usually encrypted, if it is,
|
|
378
|
-
* passphrase will be used to decrypt it.
|
|
379
|
-
* @property {number} [secureOptions] Optionally affect the OpenSSL protocol
|
|
380
|
-
* behavior, which is not usually necessary. This should be used carefully
|
|
381
|
-
* if at all! Value is a numeric bitmask of the SSL_OP_* options from
|
|
382
|
-
* OpenSSL Options.
|
|
383
|
-
* @property {string} [secureProtocol] Legacy mechanism to select the TLS
|
|
384
|
-
* protocol version to use, it does not support independent control of the
|
|
385
|
-
* minimum and maximum version, and does not support limiting the protocol
|
|
386
|
-
* to TLSv1.3. Use minVersion and maxVersion instead. The possible values
|
|
387
|
-
* are listed as SSL_METHODS, use the function names as strings. For
|
|
388
|
-
* example, use 'TLSv1_1_method' to force TLS version 1.1, or 'TLS_method'
|
|
389
|
-
* to allow any TLS protocol version up to TLSv1.3. It is not recommended to
|
|
390
|
-
* use TLS versions less than 1.2, but it may be required for
|
|
391
|
-
* interoperability. Default: none, see minVersion.
|
|
392
|
-
* @property {string} [sessionIdContext] Opaque identifier used by servers to
|
|
393
|
-
* ensure session state is not shared between applications. Unused by
|
|
394
|
-
* clients.
|
|
395
|
-
* @property {Buffer} [ticketKeys] 48-bytes of cryptographically strong
|
|
396
|
-
* pseudorandom data. See Session Resumption for more information.
|
|
397
|
-
* @property {number} [sessionTimeout=300] The number of seconds after which a
|
|
398
|
-
* TLS session created by the server will no longer be resumable. See
|
|
399
|
-
* Session Resumption for more information.
|
|
400
|
-
*/
|
|
401
|
-
|
|
402
123
|
/**
|
|
403
124
|
* Create a client connection to a listening server.
|
|
404
125
|
*
|
|
405
|
-
* @param {
|
|
406
|
-
* @param {
|
|
126
|
+
* @param {tls.ConnectionOptions|number} options Port to connect to.
|
|
127
|
+
* @param {() => void} [secureListener] If specified, function is
|
|
407
128
|
* registered for the 'secure' event.
|
|
408
129
|
* @returns {tls.TLSSocket} Socket.
|
|
409
130
|
*/
|
|
410
131
|
export function connect(options, secureListener) {
|
|
411
132
|
const {port, secureListener: listener, ...opts} =
|
|
412
|
-
normalizeArgs(options, {secureListener})
|
|
133
|
+
normalizeArgs(options, {secureListener});
|
|
413
134
|
|
|
414
|
-
|
|
135
|
+
if (typeof port !== 'number') {
|
|
136
|
+
throw new TypeError('Invalid type for port');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
opts.socket = net.connect(port); // Emits error if connection failed
|
|
415
140
|
if (!opts.ca) {
|
|
416
|
-
const srv = net.getServer(port)
|
|
141
|
+
const srv = net.getServer(port);
|
|
417
142
|
if (srv && (srv instanceof MockTLSServer)) {
|
|
418
|
-
opts.ca = srv.ca
|
|
143
|
+
opts.ca = srv.ca;
|
|
419
144
|
}
|
|
420
145
|
}
|
|
421
146
|
|
|
422
|
-
const secureSocket = tls.connect(opts)
|
|
147
|
+
const secureSocket = tls.connect(opts);
|
|
423
148
|
|
|
424
149
|
// Force-close on error.
|
|
425
|
-
secureSocket.on('error', () => opts.socket
|
|
150
|
+
secureSocket.on('error', () => opts.socket?.emit('close'));
|
|
426
151
|
if (listener) {
|
|
427
|
-
secureSocket.on('secure', listener)
|
|
152
|
+
secureSocket.on('secure', listener);
|
|
428
153
|
}
|
|
429
|
-
return secureSocket
|
|
154
|
+
return secureSocket;
|
|
430
155
|
}
|
|
431
156
|
|
|
432
|
-
export const plainConnect = net.connect
|
|
157
|
+
export const plainConnect = net.connect;
|
|
433
158
|
export default {
|
|
434
159
|
MockTLSServer,
|
|
435
160
|
connect,
|
|
436
161
|
plainConnect,
|
|
437
|
-
}
|
|
162
|
+
};
|
package/lib/kernel.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {extendedError} from './utils.js'
|
|
1
|
+
import {extendedError} from './utils.js';
|
|
3
2
|
|
|
4
|
-
const FIRST_PORT = 1024
|
|
3
|
+
const FIRST_PORT = 1024;
|
|
5
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import('node:events').EventEmitter} EventEmitter
|
|
7
|
+
*/
|
|
6
8
|
/**
|
|
7
9
|
* Simulate the bit of the OS kernel that uses port numbers to make endpoints.
|
|
8
10
|
*/
|
|
@@ -11,13 +13,12 @@ export class Kernel {
|
|
|
11
13
|
* Creates an instance of Kernel.
|
|
12
14
|
*/
|
|
13
15
|
constructor() {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.ports = {}
|
|
16
|
+
/** @type {Record<number, EventEmitter>} */
|
|
17
|
+
this.ports = {};
|
|
17
18
|
|
|
18
19
|
/** @type {any} */
|
|
19
|
-
this.timer = null
|
|
20
|
-
this.count = 0
|
|
20
|
+
this.timer = null;
|
|
21
|
+
this.count = 0;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -33,32 +34,32 @@ export class Kernel {
|
|
|
33
34
|
open(port, server) {
|
|
34
35
|
if (!port) {
|
|
35
36
|
// Find the next open port
|
|
36
|
-
port = FIRST_PORT
|
|
37
|
+
port = FIRST_PORT;
|
|
37
38
|
while (this.ports[port]) {
|
|
38
|
-
port
|
|
39
|
+
port++;
|
|
39
40
|
}
|
|
40
41
|
} else if (this.ports[port]) {
|
|
41
42
|
throw extendedError('Address already in use', {
|
|
42
43
|
code: 'EADDRINUSE',
|
|
43
|
-
})
|
|
44
|
+
});
|
|
44
45
|
}
|
|
45
|
-
this.ports[port] = server
|
|
46
|
-
this.count
|
|
46
|
+
this.ports[port] = server;
|
|
47
|
+
this.count++;
|
|
47
48
|
if (!this.timer) {
|
|
48
49
|
// Keep the process alive while we've got servers listening
|
|
49
50
|
this.timer = setInterval(() => {
|
|
50
51
|
/* c8 ignore next */
|
|
51
|
-
}, 0x7fffffff)
|
|
52
|
+
}, 0x7fffffff);
|
|
52
53
|
}
|
|
53
54
|
server.on('close', () => {
|
|
54
|
-
delete this.ports[port]
|
|
55
|
-
this.count
|
|
55
|
+
delete this.ports[port];
|
|
56
|
+
this.count--;
|
|
56
57
|
if (this.count <= 0) {
|
|
57
|
-
clearInterval(this.timer)
|
|
58
|
-
this.timer = null
|
|
58
|
+
clearInterval(this.timer);
|
|
59
|
+
this.timer = null;
|
|
59
60
|
}
|
|
60
|
-
})
|
|
61
|
-
return port
|
|
61
|
+
});
|
|
62
|
+
return port;
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
/**
|
|
@@ -68,8 +69,8 @@ export class Kernel {
|
|
|
68
69
|
* @returns {EventEmitter} The server, or null if not found.
|
|
69
70
|
*/
|
|
70
71
|
lookup(port) {
|
|
71
|
-
return this.ports[port]
|
|
72
|
+
return this.ports[port];
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
export const globalKernel = new Kernel()
|
|
76
|
+
export const globalKernel = new Kernel();
|