mock-tls-server 1.0.1 → 1.1.2
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/lib/index.js +26 -11
- package/lib/net.js +2 -4
- package/lib/utils.js +9 -32
- package/package.json +21 -17
- package/types/index.d.ts +9 -8
- package/types/net.d.ts +5 -5
- package/types/utils.d.ts +5 -9
- package/lib/ca.js +0 -140
- package/types/ca.d.ts +0 -75
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as net from './net.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import {CertificateAuthority, DEFAULT_CERT_OPTIONS, getCAoptions, getIssueOptions} from '@cto.af/ca';
|
|
3
|
+
import {classNames, log, normalizeArgs} from './utils.js';
|
|
4
|
+
import {select} from '@cto.af/utils';
|
|
4
5
|
import tls from 'node:tls';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -19,7 +20,7 @@ import tls from 'node:tls';
|
|
|
19
20
|
/**
|
|
20
21
|
* @typedef {LocalOptions &
|
|
21
22
|
* tls.TLSSocketOptions &
|
|
22
|
-
* import('
|
|
23
|
+
* import('@cto.af/ca').CertOptions &
|
|
23
24
|
* import('./net.js').ServerOpts
|
|
24
25
|
* } ServerOpts
|
|
25
26
|
*/
|
|
@@ -35,9 +36,15 @@ import tls from 'node:tls';
|
|
|
35
36
|
* A TLS server that mocks the network, but all of the crypto code is real.
|
|
36
37
|
*/
|
|
37
38
|
export class MockTLSServer extends net.Server {
|
|
38
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* @type {string | Buffer | Array<string | Buffer |
|
|
41
|
+
* import('node:tls').KeyObject> | undefined}
|
|
42
|
+
*/
|
|
39
43
|
#key;
|
|
40
44
|
|
|
45
|
+
/** @type {string | Buffer | Array<string | Buffer> | undefined} */
|
|
46
|
+
ca;
|
|
47
|
+
|
|
41
48
|
/**
|
|
42
49
|
* Create a MockTLSServer instance.
|
|
43
50
|
*
|
|
@@ -53,7 +60,7 @@ export class MockTLSServer extends net.Server {
|
|
|
53
60
|
const [local_opts, cert_opts, socket_opts, tls_opts] = select(
|
|
54
61
|
opts,
|
|
55
62
|
['authority', 'secureConnectionListener'],
|
|
56
|
-
|
|
63
|
+
DEFAULT_CERT_OPTIONS,
|
|
57
64
|
['allowHalfOpen']
|
|
58
65
|
);
|
|
59
66
|
|
|
@@ -68,9 +75,7 @@ export class MockTLSServer extends net.Server {
|
|
|
68
75
|
});
|
|
69
76
|
const temit = tsock.emit;
|
|
70
77
|
tsock.emit = (eventName, ...args) => {
|
|
71
|
-
log('tls', eventName, ...args
|
|
72
|
-
o => ((typeof o === 'object') ? o.constructor.name || o.toString() : o)
|
|
73
|
-
));
|
|
78
|
+
log('tls', eventName, ...classNames(args));
|
|
74
79
|
// @ts-ignore
|
|
75
80
|
return temit.call(tsock, eventName, ...args);
|
|
76
81
|
};
|
|
@@ -83,12 +88,22 @@ export class MockTLSServer extends net.Server {
|
|
|
83
88
|
// User-supplied cert and key. Don't spin up a CA instance.
|
|
84
89
|
this.cert = tls_opts.cert;
|
|
85
90
|
this.ca = tls_opts.ca;
|
|
91
|
+
if (!tls_opts.key) {
|
|
92
|
+
throw new Error('No key found');
|
|
93
|
+
}
|
|
86
94
|
this.#key = tls_opts.key;
|
|
87
95
|
} else {
|
|
88
|
-
const
|
|
89
|
-
|
|
96
|
+
const caOpts = getCAoptions(cert_opts);
|
|
97
|
+
caOpts.temp = true;
|
|
98
|
+
caOpts.mute = true;
|
|
99
|
+
const authority =
|
|
100
|
+
local_opts.authority || new CertificateAuthority(caOpts);
|
|
101
|
+
const issueOpts = getIssueOptions(cert_opts);
|
|
102
|
+
issueOpts.temp = true;
|
|
103
|
+
issueOpts.mute = true;
|
|
104
|
+
const serverProps = authority.issueNew(issueOpts);
|
|
90
105
|
this.cert = serverProps.cert;
|
|
91
|
-
this.ca = serverProps.ca;
|
|
106
|
+
this.ca = serverProps.ca?.cert;
|
|
92
107
|
this.#key = serverProps.key;
|
|
93
108
|
}
|
|
94
109
|
|
package/lib/net.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {extendedError, log, normalizeArgs} from './utils.js';
|
|
1
|
+
import {classNames, extendedError, log, normalizeArgs} from './utils.js';
|
|
2
2
|
import {EventEmitter} from 'node:events';
|
|
3
3
|
import {MockSocket} from './socket.js';
|
|
4
4
|
import {globalKernel} from './kernel.js';
|
|
@@ -131,9 +131,7 @@ export class Server extends EventEmitter {
|
|
|
131
131
|
* otherwise.
|
|
132
132
|
*/
|
|
133
133
|
emit(eventName, ...args) {
|
|
134
|
-
log(this.constructor.name, eventName, ...args
|
|
135
|
-
o => ((typeof o === 'object') ? o.constructor.name || o.toString() : o)
|
|
136
|
-
));
|
|
134
|
+
log(this.constructor.name, eventName, ...classNames(args));
|
|
137
135
|
return super.emit(eventName, ...args);
|
|
138
136
|
}
|
|
139
137
|
|
package/lib/utils.js
CHANGED
|
@@ -65,40 +65,17 @@ export function extendedError(message, opts) {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
*
|
|
68
|
+
* Transform an array of event args to something readable.
|
|
69
69
|
*
|
|
70
|
-
* @
|
|
71
|
-
* @
|
|
72
|
-
* @param {(keyof T)[][]} args Arrays of strings to select into the result
|
|
73
|
-
* objects.
|
|
74
|
-
* @returns {Partial<Record<keyof T, any>>[]} One object for each of args,
|
|
75
|
-
* plus an extra one for everything that was left over.
|
|
70
|
+
* @param {any[]} args Array of event args to transform.
|
|
71
|
+
* @returns {string[]}
|
|
76
72
|
*/
|
|
77
|
-
export function
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (!obj) {
|
|
84
|
-
return res;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const sets = args.map(a => new Set(a));
|
|
88
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
89
|
-
let found = false;
|
|
90
|
-
sets.forEach((s, i) => {
|
|
91
|
-
if (!found && s.has(k)) {
|
|
92
|
-
found = true;
|
|
93
|
-
// @ts-ignore
|
|
94
|
-
res[i][k] = v;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
if (!found) {
|
|
98
|
-
leftovers[k] = v;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return res;
|
|
73
|
+
export function classNames(args) {
|
|
74
|
+
return args.map(
|
|
75
|
+
o => ((typeof o === 'object') ?
|
|
76
|
+
o?.constructor?.name || JSON.stringify(o) :
|
|
77
|
+
String(o))
|
|
78
|
+
);
|
|
102
79
|
}
|
|
103
80
|
|
|
104
81
|
/** @type {util.DebugLoggerFunction|util.DebugLogger} */
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mock-tls-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "types/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"lib",
|
|
10
|
+
"types"
|
|
11
|
+
],
|
|
8
12
|
"scripts": {
|
|
9
13
|
"test": "c8 ava",
|
|
10
14
|
"lint": "eslint .",
|
|
@@ -29,25 +33,25 @@
|
|
|
29
33
|
"license": "MIT",
|
|
30
34
|
"repository": "hildjj/mock-tls-server",
|
|
31
35
|
"dependencies": {
|
|
32
|
-
"
|
|
36
|
+
"@cto.af/ca": "2.4.0",
|
|
37
|
+
"@cto.af/utils": "1.3.2"
|
|
33
38
|
},
|
|
34
39
|
"devDependencies": {
|
|
35
|
-
"@cto.af/eslint-config": "
|
|
36
|
-
"@
|
|
37
|
-
"@types/
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"eslint
|
|
42
|
-
"eslint-plugin-
|
|
43
|
-
"eslint-plugin-
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"typescript": "^5.5.4"
|
|
40
|
+
"@cto.af/eslint-config": "6.0.14",
|
|
41
|
+
"@eslint/markdown": "7.2.0",
|
|
42
|
+
"@types/jsrsasign": "10.5.15",
|
|
43
|
+
"@types/node": "24.5.2",
|
|
44
|
+
"ava": "6.4.1",
|
|
45
|
+
"c8": "10.1.3",
|
|
46
|
+
"eslint": "9.36.0",
|
|
47
|
+
"eslint-plugin-ava": "^15.1.0",
|
|
48
|
+
"eslint-plugin-jsdoc": "60.1.1",
|
|
49
|
+
"p-event": "7.0.0",
|
|
50
|
+
"typedoc": "0.28.13",
|
|
51
|
+
"typescript": "^5.9.2"
|
|
48
52
|
},
|
|
49
|
-
"packageManager": "pnpm@
|
|
53
|
+
"packageManager": "pnpm@10.17.0",
|
|
50
54
|
"engines": {
|
|
51
|
-
"node": ">=
|
|
55
|
+
"node": ">=20"
|
|
52
56
|
}
|
|
53
57
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* registered for the 'secure' event.
|
|
7
7
|
* @returns {tls.TLSSocket} Socket.
|
|
8
8
|
*/
|
|
9
|
-
export function connect(options: tls.ConnectionOptions | number, secureListener?: (
|
|
9
|
+
export function connect(options: tls.ConnectionOptions | number, secureListener?: () => void): tls.TLSSocket;
|
|
10
10
|
/**
|
|
11
11
|
* @typedef {(sock: tls.TLSSocket) => void} SecureConnectionCallback
|
|
12
12
|
*/
|
|
@@ -21,7 +21,7 @@ export function connect(options: tls.ConnectionOptions | number, secureListener?
|
|
|
21
21
|
/**
|
|
22
22
|
* @typedef {LocalOptions &
|
|
23
23
|
* tls.TLSSocketOptions &
|
|
24
|
-
* import('
|
|
24
|
+
* import('@cto.af/ca').CertOptions &
|
|
25
25
|
* import('./net.js').ServerOpts
|
|
26
26
|
* } ServerOpts
|
|
27
27
|
*/
|
|
@@ -42,9 +42,10 @@ export class MockTLSServer extends net.Server {
|
|
|
42
42
|
* @param {SecureConnectionCallback} [secureConnectionListener] Installed as
|
|
43
43
|
* a listener for the 'secureConnection' event.
|
|
44
44
|
*/
|
|
45
|
-
constructor(options?:
|
|
46
|
-
|
|
47
|
-
ca:
|
|
45
|
+
constructor(options?: ServerOpts | SecureConnectionCallback, secureConnectionListener?: SecureConnectionCallback);
|
|
46
|
+
/** @type {string | Buffer | Array<string | Buffer> | undefined} */
|
|
47
|
+
ca: string | Buffer | Array<string | Buffer> | undefined;
|
|
48
|
+
cert: string | Buffer<ArrayBufferLike> | (string | Buffer<ArrayBufferLike>)[] | undefined;
|
|
48
49
|
/**
|
|
49
50
|
* Listen for incoming connections. Whenever `connect` is called on this
|
|
50
51
|
* server's port, create a mock socket wrapped in a TLSSocket.
|
|
@@ -54,7 +55,7 @@ export class MockTLSServer extends net.Server {
|
|
|
54
55
|
* a listener for the 'secureConnection' event.
|
|
55
56
|
* @returns {MockTLSServer} This.
|
|
56
57
|
*/
|
|
57
|
-
listen(options?: number | ListenOptions
|
|
58
|
+
listen(options?: number | ListenOptions, secureConnectionListener?: Function): MockTLSServer;
|
|
58
59
|
#private;
|
|
59
60
|
}
|
|
60
61
|
export const plainConnect: typeof net.connect;
|
|
@@ -78,7 +79,7 @@ export type LocalOptions = {
|
|
|
78
79
|
*/
|
|
79
80
|
secureConnectionListener?: SecureConnectionCallback | undefined;
|
|
80
81
|
};
|
|
81
|
-
export type ServerOpts = LocalOptions & tls.TLSSocketOptions & import("
|
|
82
|
+
export type ServerOpts = LocalOptions & tls.TLSSocketOptions & import("@cto.af/ca").CertOptions & import("./net.js").ServerOpts;
|
|
82
83
|
export type ListenOptions = {
|
|
83
84
|
/**
|
|
84
85
|
* The port to listen on.
|
|
@@ -92,4 +93,4 @@ export type ListenOptions = {
|
|
|
92
93
|
};
|
|
93
94
|
import tls from 'node:tls';
|
|
94
95
|
import * as net from './net.js';
|
|
95
|
-
import { CertificateAuthority } from '
|
|
96
|
+
import { CertificateAuthority } from '@cto.af/ca';
|
package/types/net.d.ts
CHANGED
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
* @returns {DuplexStream} The client side HalfSocket.
|
|
43
43
|
* @throws {TypeError} Invalid type for port.
|
|
44
44
|
*/
|
|
45
|
-
export function connect(options: number | (SocketConnectOpts & import("node:stream").DuplexOptions), connectListener?: SocketCallback
|
|
45
|
+
export function connect(options: number | (SocketConnectOpts & import("node:stream").DuplexOptions), connectListener?: SocketCallback): DuplexStream;
|
|
46
46
|
/**
|
|
47
47
|
* Get the Server associated with a port.
|
|
48
48
|
*
|
|
@@ -58,7 +58,7 @@ export function getServer(options: SocketConnectOpts | number): Server;
|
|
|
58
58
|
* listener for the 'listening' event, if provided.
|
|
59
59
|
* @returns {Server} The created Server.
|
|
60
60
|
*/
|
|
61
|
-
export function createServer(opts?:
|
|
61
|
+
export function createServer(opts?: ServerOpts | SocketCallback, connectionListener?: SocketCallback): Server;
|
|
62
62
|
/**
|
|
63
63
|
* @typedef {(socket: DuplexStream) => void} SocketCallback
|
|
64
64
|
*/
|
|
@@ -73,7 +73,7 @@ export class Server extends EventEmitter<[never]> {
|
|
|
73
73
|
* @param {SocketCallback} [connectionListener] Register this as the event
|
|
74
74
|
* listener for the 'listening' event, if provided.
|
|
75
75
|
*/
|
|
76
|
-
constructor(options?:
|
|
76
|
+
constructor(options?: ServerOpts | SocketCallback, connectionListener?: SocketCallback);
|
|
77
77
|
clients: Set<any>;
|
|
78
78
|
port: number | null;
|
|
79
79
|
listening: boolean;
|
|
@@ -98,7 +98,7 @@ export class Server extends EventEmitter<[never]> {
|
|
|
98
98
|
* a listener for the 'listening' event.
|
|
99
99
|
* @returns {Server} This.
|
|
100
100
|
*/
|
|
101
|
-
listen(options?: number | ListenOptions
|
|
101
|
+
listen(options?: number | ListenOptions, listeningListener?: () => void): Server;
|
|
102
102
|
/**
|
|
103
103
|
* Add a client to this server.
|
|
104
104
|
*
|
|
@@ -118,7 +118,7 @@ export class Server extends EventEmitter<[never]> {
|
|
|
118
118
|
* @param {PlainCallback} [cb] Registered as listener for the 'close' event.
|
|
119
119
|
* @returns {Server} This.
|
|
120
120
|
*/
|
|
121
|
-
close(cb?: PlainCallback
|
|
121
|
+
close(cb?: PlainCallback): Server;
|
|
122
122
|
/**
|
|
123
123
|
* Get the number of current clients.
|
|
124
124
|
*
|
package/types/utils.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* @returns {U & {port?: number} & T} The normalized options.
|
|
14
14
|
* @throws {TypeError} No callback allowed for this function.
|
|
15
15
|
*/
|
|
16
|
-
export function normalizeArgs<T extends Record<string, Function | undefined>, U>(options?: number | U
|
|
16
|
+
export function normalizeArgs<T extends Record<string, Function | undefined>, U>(options?: number | U, listener?: T): U & {
|
|
17
17
|
port?: number;
|
|
18
18
|
} & T;
|
|
19
19
|
/**
|
|
@@ -25,16 +25,12 @@ export function normalizeArgs<T extends Record<string, Function | undefined>, U>
|
|
|
25
25
|
*/
|
|
26
26
|
export function extendedError(message: string, opts: object): Error;
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Transform an array of event args to something readable.
|
|
29
29
|
*
|
|
30
|
-
* @
|
|
31
|
-
* @
|
|
32
|
-
* @param {(keyof T)[][]} args Arrays of strings to select into the result
|
|
33
|
-
* objects.
|
|
34
|
-
* @returns {Partial<Record<keyof T, any>>[]} One object for each of args,
|
|
35
|
-
* plus an extra one for everything that was left over.
|
|
30
|
+
* @param {any[]} args Array of event args to transform.
|
|
31
|
+
* @returns {string[]}
|
|
36
32
|
*/
|
|
37
|
-
export function
|
|
33
|
+
export function classNames(args: any[]): string[];
|
|
38
34
|
/** @type {util.DebugLoggerFunction|util.DebugLogger} */
|
|
39
35
|
export let log: util.DebugLoggerFunction | util.DebugLogger;
|
|
40
36
|
export type Many<T> = T | Readonly<T[]>;
|
package/lib/ca.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import rs from 'jsrsasign';
|
|
2
|
-
|
|
3
|
-
const MINS_5 = 5 * 60 * 1000;
|
|
4
|
-
const SECS_1 = 1000;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @typedef {object} CertOpts
|
|
8
|
-
* @property {Date} [notafter='Now + 5m'] Certificate not valid after this
|
|
9
|
-
* date.
|
|
10
|
-
* @property {Date} [notbefore='Now - 1s'] Certificate not valid before this
|
|
11
|
-
* date.
|
|
12
|
-
* @property {string} [subject] Subject Distinguished Name (DN).
|
|
13
|
-
* @property {string[]} [names=['localhost']] DNS Subject Alternative Names
|
|
14
|
-
* for the cert. Ignored for the CA cert.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @typedef {object} ServerProperties
|
|
19
|
-
* @property {string} cert PEM-encoded server certificate.
|
|
20
|
-
* @property {string} key PEM-encoded server private key.
|
|
21
|
-
* @property {string} ca PEM-encoded CA certificate.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Very rudimentary Certificate Authority (CA).
|
|
26
|
-
* Warning: DO NOT use this on the Internet. It is only for testing purposes.
|
|
27
|
-
*/
|
|
28
|
-
export class CertificateAuthority {
|
|
29
|
-
/** Private key */
|
|
30
|
-
#key;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Creates an instance of CertificateAuthority.
|
|
34
|
-
*
|
|
35
|
-
* @param {CertOpts} [options={}] CA cert options.
|
|
36
|
-
*/
|
|
37
|
-
constructor(options = {}) {
|
|
38
|
-
const now = new Date().getTime();
|
|
39
|
-
|
|
40
|
-
/** @type {Required<CertOpts>} */
|
|
41
|
-
const opts = {
|
|
42
|
-
notafter: new Date(now + MINS_5),
|
|
43
|
-
notbefore: new Date(now - SECS_1),
|
|
44
|
-
subject: 'C=US/ST=Colorado/L=Denver/CN=Example-Root-CA',
|
|
45
|
-
names: ['localhost'],
|
|
46
|
-
...options,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
this.subject = opts.subject;
|
|
50
|
-
|
|
51
|
-
// Create a self-signed CA cert
|
|
52
|
-
const ca_kp = rs.KEYUTIL.generateKeypair('EC', 'secp256r1');
|
|
53
|
-
this.#key = ca_kp.prvKeyObj;
|
|
54
|
-
const pub = ca_kp.pubKeyObj;
|
|
55
|
-
|
|
56
|
-
const ca_cert = new rs.KJUR.asn1.x509.Certificate({
|
|
57
|
-
version: 3,
|
|
58
|
-
serial: {int: now},
|
|
59
|
-
issuer: {str: opts.subject},
|
|
60
|
-
notbefore: rs.datetozulu(opts.notbefore, false, false),
|
|
61
|
-
notafter: rs.datetozulu(opts.notafter, false, false),
|
|
62
|
-
subject: {str: opts.subject},
|
|
63
|
-
sbjpubkey: pub,
|
|
64
|
-
ext: [
|
|
65
|
-
{extname: 'basicConstraints', cA: true},
|
|
66
|
-
],
|
|
67
|
-
sigalg: 'SHA256withECDSA',
|
|
68
|
-
cakey: this.#key,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
/** @type {string} */
|
|
72
|
-
this.cert_pem = ca_cert.getPEM();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Creates and signs a server certificate.
|
|
77
|
-
*
|
|
78
|
-
* @param {CertOpts} [options={}] Server cert options.
|
|
79
|
-
* @returns {ServerProperties} Information about the created certificate.
|
|
80
|
-
* @throws {Error} No subject or names.
|
|
81
|
-
*/
|
|
82
|
-
issue(options) {
|
|
83
|
-
const now = new Date().getTime();
|
|
84
|
-
|
|
85
|
-
/** @type {Required<CertOpts>} */
|
|
86
|
-
const opts = {
|
|
87
|
-
notafter: new Date(now + MINS_5),
|
|
88
|
-
notbefore: new Date(now - SECS_1),
|
|
89
|
-
subject: '',
|
|
90
|
-
names: ['localhost'],
|
|
91
|
-
...options,
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
if (!opts.subject) {
|
|
95
|
-
if (!opts.names?.length) {
|
|
96
|
-
throw new Error('Must include subject or at least one name in opts');
|
|
97
|
-
}
|
|
98
|
-
opts.subject = `C=US/ST=Colorado/L=Denver/CN=${opts.names[0]}`;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const srv_kp = rs.KEYUTIL.generateKeypair('EC', 'secp256r1');
|
|
102
|
-
const srv_prv = srv_kp.prvKeyObj;
|
|
103
|
-
const srv_pub = srv_kp.pubKeyObj;
|
|
104
|
-
|
|
105
|
-
const srv = new rs.KJUR.asn1.x509.Certificate({
|
|
106
|
-
version: 3,
|
|
107
|
-
serial: {int: 2},
|
|
108
|
-
issuer: {str: this.subject},
|
|
109
|
-
notbefore: rs.datetozulu(opts.notbefore, false, false),
|
|
110
|
-
notafter: rs.datetozulu(opts.notafter, false, false),
|
|
111
|
-
subject: {str: opts.subject},
|
|
112
|
-
sbjpubkey: srv_pub,
|
|
113
|
-
ext: [
|
|
114
|
-
{extname: 'authorityKeyIdentifier', kid: this.cert_pem},
|
|
115
|
-
{extname: 'basicConstraints', cA: false},
|
|
116
|
-
{
|
|
117
|
-
extname: 'keyUsage',
|
|
118
|
-
names: [
|
|
119
|
-
'digitalSignature',
|
|
120
|
-
'nonRepudiation',
|
|
121
|
-
'keyEncipherment',
|
|
122
|
-
'dataEncipherment',
|
|
123
|
-
],
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
extname: 'subjectAltName',
|
|
127
|
-
array: opts.names.map(n => ({dns: n})),
|
|
128
|
-
},
|
|
129
|
-
],
|
|
130
|
-
sigalg: 'SHA256withECDSA',
|
|
131
|
-
cakey: this.#key,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
cert: srv.getPEM(),
|
|
136
|
-
key: rs.KEYUTIL.getPEM(srv_prv, 'PKCS1PRV'),
|
|
137
|
-
ca: this.cert_pem,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
package/types/ca.d.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {object} CertOpts
|
|
3
|
-
* @property {Date} [notafter='Now + 5m'] Certificate not valid after this
|
|
4
|
-
* date.
|
|
5
|
-
* @property {Date} [notbefore='Now - 1s'] Certificate not valid before this
|
|
6
|
-
* date.
|
|
7
|
-
* @property {string} [subject] Subject Distinguished Name (DN).
|
|
8
|
-
* @property {string[]} [names=['localhost']] DNS Subject Alternative Names
|
|
9
|
-
* for the cert. Ignored for the CA cert.
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
|
-
* @typedef {object} ServerProperties
|
|
13
|
-
* @property {string} cert PEM-encoded server certificate.
|
|
14
|
-
* @property {string} key PEM-encoded server private key.
|
|
15
|
-
* @property {string} ca PEM-encoded CA certificate.
|
|
16
|
-
*/
|
|
17
|
-
/**
|
|
18
|
-
* Very rudimentary Certificate Authority (CA).
|
|
19
|
-
* Warning: DO NOT use this on the Internet. It is only for testing purposes.
|
|
20
|
-
*/
|
|
21
|
-
export class CertificateAuthority {
|
|
22
|
-
/**
|
|
23
|
-
* Creates an instance of CertificateAuthority.
|
|
24
|
-
*
|
|
25
|
-
* @param {CertOpts} [options={}] CA cert options.
|
|
26
|
-
*/
|
|
27
|
-
constructor(options?: CertOpts | undefined);
|
|
28
|
-
subject: string;
|
|
29
|
-
/** @type {string} */
|
|
30
|
-
cert_pem: string;
|
|
31
|
-
/**
|
|
32
|
-
* Creates and signs a server certificate.
|
|
33
|
-
*
|
|
34
|
-
* @param {CertOpts} [options={}] Server cert options.
|
|
35
|
-
* @returns {ServerProperties} Information about the created certificate.
|
|
36
|
-
* @throws {Error} No subject or names.
|
|
37
|
-
*/
|
|
38
|
-
issue(options?: CertOpts | undefined): ServerProperties;
|
|
39
|
-
#private;
|
|
40
|
-
}
|
|
41
|
-
export type CertOpts = {
|
|
42
|
-
/**
|
|
43
|
-
* Certificate not valid after this
|
|
44
|
-
* date.
|
|
45
|
-
*/
|
|
46
|
-
notafter?: Date | undefined;
|
|
47
|
-
/**
|
|
48
|
-
* Certificate not valid before this
|
|
49
|
-
* date.
|
|
50
|
-
*/
|
|
51
|
-
notbefore?: Date | undefined;
|
|
52
|
-
/**
|
|
53
|
-
* Subject Distinguished Name (DN).
|
|
54
|
-
*/
|
|
55
|
-
subject?: string | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* DNS Subject Alternative Names
|
|
58
|
-
* for the cert. Ignored for the CA cert.
|
|
59
|
-
*/
|
|
60
|
-
names?: string[] | undefined;
|
|
61
|
-
};
|
|
62
|
-
export type ServerProperties = {
|
|
63
|
-
/**
|
|
64
|
-
* PEM-encoded server certificate.
|
|
65
|
-
*/
|
|
66
|
-
cert: string;
|
|
67
|
-
/**
|
|
68
|
-
* PEM-encoded server private key.
|
|
69
|
-
*/
|
|
70
|
-
key: string;
|
|
71
|
-
/**
|
|
72
|
-
* PEM-encoded CA certificate.
|
|
73
|
-
*/
|
|
74
|
-
ca: string;
|
|
75
|
-
};
|