mock-tls-server 0.0.1 → 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 +23 -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/README.md
CHANGED
|
@@ -25,25 +25,25 @@ The intent is that the API is similar to that found in Node's
|
|
|
25
25
|
## Example:
|
|
26
26
|
|
|
27
27
|
```js
|
|
28
|
-
import {MockTLSServer, connect} from 'mock-tls-server'
|
|
28
|
+
import {MockTLSServer, connect} from 'mock-tls-server';
|
|
29
29
|
|
|
30
30
|
// Expected output: "Welcome!\nexample\n"
|
|
31
31
|
|
|
32
|
-
const server = new MockTLSServer()
|
|
32
|
+
const server = new MockTLSServer();
|
|
33
33
|
server.listen(4000, sock => {
|
|
34
|
-
sock.write('Welcome!\n')
|
|
35
|
-
sock.pipe(sock)
|
|
36
|
-
})
|
|
34
|
+
sock.write('Welcome!\n');
|
|
35
|
+
sock.pipe(sock);
|
|
36
|
+
});
|
|
37
37
|
|
|
38
38
|
const cli = connect(4000, () => {
|
|
39
39
|
// Send a string, and end the write side of the socket
|
|
40
40
|
// allowHalfOpen is false on both sides by default, so the
|
|
41
41
|
// server will close it's write side in resonse, leading the
|
|
42
42
|
// client to go to the 'closed' state.
|
|
43
|
-
cli.end('example\n')
|
|
43
|
+
cli.end('example\n');
|
|
44
44
|
})
|
|
45
45
|
.on('data', chunk => process.stdout.write(chunk.toString()))
|
|
46
|
-
.on('close', () => server.close())
|
|
46
|
+
.on('close', () => server.close());
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
## Always close the server
|
|
@@ -60,9 +60,9 @@ I use [p-event](https://github.com/sindresorhus/p-event#readme) to wait for
|
|
|
60
60
|
individual events in async functions:
|
|
61
61
|
|
|
62
62
|
```js
|
|
63
|
-
import pEvent from 'p-event'
|
|
63
|
+
import pEvent from 'p-event';
|
|
64
64
|
|
|
65
|
-
await pEvent(client, 'secureConnection')
|
|
65
|
+
await pEvent(client, 'secureConnection');
|
|
66
66
|
```
|
|
67
67
|
|
|
68
68
|
You can call `plainConnect` to get a client socket that hasn't been connected
|
|
@@ -71,20 +71,20 @@ parameters for
|
|
|
71
71
|
[`tls.connect`](https://nodejs.org/api/tls.html#tls_tls_connect_options_callback).
|
|
72
72
|
|
|
73
73
|
```js
|
|
74
|
-
import {
|
|
75
|
-
import tls from 'tls'
|
|
74
|
+
import {MockTLSServer, plainConnect} from 'mock-tls-server';
|
|
75
|
+
import tls from 'node:tls';
|
|
76
76
|
|
|
77
77
|
const server = new MockTLSServer({
|
|
78
|
-
notBefore: new Date(new Date().getTime() + 10000) // Invalid because of time
|
|
79
|
-
})
|
|
80
|
-
server.listen()
|
|
81
|
-
await pEvent(server, 'listening')
|
|
82
|
-
const socket = plainConnect(server.port)
|
|
78
|
+
notBefore: new Date(new Date().getTime() + 10000), // Invalid because of time
|
|
79
|
+
});
|
|
80
|
+
server.listen();
|
|
81
|
+
await pEvent(server, 'listening');
|
|
82
|
+
const socket = plainConnect(server.port);
|
|
83
83
|
tls.connect({
|
|
84
84
|
socket,
|
|
85
85
|
host: 'localhost', // Alter this to test name mismatches
|
|
86
|
-
ca: server.ca
|
|
87
|
-
})
|
|
86
|
+
ca: server.ca, // Alter this to test signing failures
|
|
87
|
+
});
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
See the tests in this package for more ideas.
|
package/lib/ca.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import rs from 'jsrsasign';
|
|
2
2
|
|
|
3
|
-
const MINS_5 = 5 * 60 * 1000
|
|
4
|
-
const SECS_1 = 1000
|
|
5
|
-
|
|
6
|
-
// Private map of CertificateAuthority => private key
|
|
7
|
-
const PRIVATE = new WeakMap()
|
|
3
|
+
const MINS_5 = 5 * 60 * 1000;
|
|
4
|
+
const SECS_1 = 1000;
|
|
8
5
|
|
|
9
6
|
/**
|
|
10
7
|
* @typedef {object} CertOpts
|
|
@@ -29,80 +26,88 @@ const PRIVATE = new WeakMap()
|
|
|
29
26
|
* Warning: DO NOT use this on the Internet. It is only for testing purposes.
|
|
30
27
|
*/
|
|
31
28
|
export class CertificateAuthority {
|
|
29
|
+
/** Private key */
|
|
30
|
+
#key;
|
|
31
|
+
|
|
32
32
|
/**
|
|
33
33
|
* Creates an instance of CertificateAuthority.
|
|
34
34
|
*
|
|
35
|
-
* @param {CertOpts} [
|
|
35
|
+
* @param {CertOpts} [options={}] CA cert options.
|
|
36
36
|
*/
|
|
37
|
-
constructor(
|
|
38
|
-
const now = new Date().getTime()
|
|
39
|
-
|
|
37
|
+
constructor(options = {}) {
|
|
38
|
+
const now = new Date().getTime();
|
|
39
|
+
|
|
40
|
+
/** @type {Required<CertOpts>} */
|
|
41
|
+
const opts = {
|
|
40
42
|
notafter: new Date(now + MINS_5),
|
|
41
43
|
notbefore: new Date(now - SECS_1),
|
|
42
44
|
subject: 'C=US/ST=Colorado/L=Denver/CN=Example-Root-CA',
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
names: ['localhost'],
|
|
46
|
+
...options,
|
|
47
|
+
};
|
|
45
48
|
|
|
46
|
-
this.subject = opts.subject
|
|
49
|
+
this.subject = opts.subject;
|
|
47
50
|
|
|
48
51
|
// Create a self-signed CA cert
|
|
49
|
-
const ca_kp = rs.KEYUTIL.generateKeypair('
|
|
50
|
-
|
|
51
|
-
const pub = ca_kp.pubKeyObj
|
|
52
|
+
const ca_kp = rs.KEYUTIL.generateKeypair('EC', 'secp256r1');
|
|
53
|
+
this.#key = ca_kp.prvKeyObj;
|
|
54
|
+
const pub = ca_kp.pubKeyObj;
|
|
52
55
|
|
|
53
56
|
const ca_cert = new rs.KJUR.asn1.x509.Certificate({
|
|
54
57
|
version: 3,
|
|
55
58
|
serial: {int: now},
|
|
56
59
|
issuer: {str: opts.subject},
|
|
57
|
-
notbefore:
|
|
58
|
-
notafter:
|
|
60
|
+
notbefore: rs.datetozulu(opts.notbefore, false, false),
|
|
61
|
+
notafter: rs.datetozulu(opts.notafter, false, false),
|
|
59
62
|
subject: {str: opts.subject},
|
|
60
63
|
sbjpubkey: pub,
|
|
61
64
|
ext: [
|
|
62
65
|
{extname: 'basicConstraints', cA: true},
|
|
63
66
|
],
|
|
64
|
-
sigalg: '
|
|
65
|
-
cakey:
|
|
66
|
-
})
|
|
67
|
+
sigalg: 'SHA256withECDSA',
|
|
68
|
+
cakey: this.#key,
|
|
69
|
+
});
|
|
67
70
|
|
|
68
71
|
/** @type {string} */
|
|
69
|
-
this.cert_pem = ca_cert.getPEM()
|
|
72
|
+
this.cert_pem = ca_cert.getPEM();
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
/**
|
|
73
76
|
* Creates and signs a server certificate.
|
|
74
77
|
*
|
|
75
|
-
* @param {CertOpts} [
|
|
78
|
+
* @param {CertOpts} [options={}] Server cert options.
|
|
76
79
|
* @returns {ServerProperties} Information about the created certificate.
|
|
77
80
|
* @throws {Error} No subject or names.
|
|
78
81
|
*/
|
|
79
|
-
issue(
|
|
80
|
-
const now = new Date().getTime()
|
|
81
|
-
|
|
82
|
+
issue(options) {
|
|
83
|
+
const now = new Date().getTime();
|
|
84
|
+
|
|
85
|
+
/** @type {Required<CertOpts>} */
|
|
86
|
+
const opts = {
|
|
82
87
|
notafter: new Date(now + MINS_5),
|
|
83
88
|
notbefore: new Date(now - SECS_1),
|
|
84
|
-
subject:
|
|
89
|
+
subject: '',
|
|
85
90
|
names: ['localhost'],
|
|
86
|
-
...
|
|
87
|
-
}
|
|
91
|
+
...options,
|
|
92
|
+
};
|
|
88
93
|
|
|
89
94
|
if (!opts.subject) {
|
|
90
|
-
if (opts.names
|
|
91
|
-
throw new Error('Must include subject or at least one name in opts')
|
|
95
|
+
if (!opts.names?.length) {
|
|
96
|
+
throw new Error('Must include subject or at least one name in opts');
|
|
92
97
|
}
|
|
93
|
-
opts.subject = `C=US/ST=Colorado/L=Denver/CN=${opts.names[0]}
|
|
98
|
+
opts.subject = `C=US/ST=Colorado/L=Denver/CN=${opts.names[0]}`;
|
|
94
99
|
}
|
|
95
100
|
|
|
96
|
-
const srv_kp = rs.KEYUTIL.generateKeypair('
|
|
97
|
-
const srv_prv = srv_kp.prvKeyObj
|
|
98
|
-
const srv_pub = srv_kp.pubKeyObj
|
|
101
|
+
const srv_kp = rs.KEYUTIL.generateKeypair('EC', 'secp256r1');
|
|
102
|
+
const srv_prv = srv_kp.prvKeyObj;
|
|
103
|
+
const srv_pub = srv_kp.pubKeyObj;
|
|
99
104
|
|
|
100
105
|
const srv = new rs.KJUR.asn1.x509.Certificate({
|
|
101
106
|
version: 3,
|
|
102
107
|
serial: {int: 2},
|
|
103
108
|
issuer: {str: this.subject},
|
|
104
|
-
notbefore:
|
|
105
|
-
notafter:
|
|
109
|
+
notbefore: rs.datetozulu(opts.notbefore, false, false),
|
|
110
|
+
notafter: rs.datetozulu(opts.notafter, false, false),
|
|
106
111
|
subject: {str: opts.subject},
|
|
107
112
|
sbjpubkey: srv_pub,
|
|
108
113
|
ext: [
|
|
@@ -122,14 +127,14 @@ export class CertificateAuthority {
|
|
|
122
127
|
array: opts.names.map(n => ({dns: n})),
|
|
123
128
|
},
|
|
124
129
|
],
|
|
125
|
-
sigalg: '
|
|
126
|
-
cakey:
|
|
127
|
-
})
|
|
130
|
+
sigalg: 'SHA256withECDSA',
|
|
131
|
+
cakey: this.#key,
|
|
132
|
+
});
|
|
128
133
|
|
|
129
134
|
return {
|
|
130
135
|
cert: srv.getPEM(),
|
|
131
136
|
key: rs.KEYUTIL.getPEM(srv_prv, 'PKCS1PRV'),
|
|
132
137
|
ca: this.cert_pem,
|
|
133
|
-
}
|
|
138
|
+
};
|
|
134
139
|
}
|
|
135
140
|
}
|