@libp2p/keychain 0.6.1

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/src/index.ts ADDED
@@ -0,0 +1,607 @@
1
+ /* eslint max-nested-callbacks: ["error", 5] */
2
+
3
+ import { logger } from '@libp2p/logger'
4
+ import sanitize from 'sanitize-filename'
5
+ import mergeOptions from 'merge-options'
6
+ import { Key } from 'interface-datastore/key'
7
+ import { CMS } from './cms.js'
8
+ import errCode from 'err-code'
9
+ import { codes } from './errors.js'
10
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
11
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
12
+ import { generateKeyPair, importKey, unmarshalPrivateKey } from '@libp2p/crypto/keys'
13
+ import type { PeerId } from '@libp2p/interface-peer-id'
14
+ import { pbkdf2, randomBytes } from '@libp2p/crypto'
15
+ import type { Datastore } from 'interface-datastore'
16
+ import { peerIdFromKeys } from '@libp2p/peer-id'
17
+ import type { KeyTypes } from '@libp2p/crypto/keys'
18
+
19
+ const log = logger('libp2p:keychain')
20
+
21
+ export interface DEKConfig {
22
+ hash: string
23
+ salt: string
24
+ iterationCount: number
25
+ keyLength: number
26
+ }
27
+
28
+ export interface KeyChainInit {
29
+ pass?: string
30
+ dek?: DEKConfig
31
+ }
32
+
33
+ /**
34
+ * Information about a key.
35
+ */
36
+ export interface KeyInfo {
37
+ /**
38
+ * The universally unique key id
39
+ */
40
+ id: string
41
+
42
+ /**
43
+ * The local key name.
44
+ */
45
+ name: string
46
+ }
47
+
48
+ const keyPrefix = '/pkcs8/'
49
+ const infoPrefix = '/info/'
50
+ const privates = new WeakMap<object, { dek: string }>()
51
+
52
+ // NIST SP 800-132
53
+ const NIST = {
54
+ minKeyLength: 112 / 8,
55
+ minSaltLength: 128 / 8,
56
+ minIterationCount: 1000
57
+ }
58
+
59
+ const defaultOptions = {
60
+ // See https://cryptosense.com/parametesr-choice-for-pbkdf2/
61
+ dek: {
62
+ keyLength: 512 / 8,
63
+ iterationCount: 10000,
64
+ salt: 'you should override this value with a crypto secure random number',
65
+ hash: 'sha2-512'
66
+ }
67
+ }
68
+
69
+ function validateKeyName (name: string) {
70
+ if (name == null) {
71
+ return false
72
+ }
73
+ if (typeof name !== 'string') {
74
+ return false
75
+ }
76
+ return name === sanitize(name.trim()) && name.length > 0
77
+ }
78
+
79
+ /**
80
+ * Throws an error after a delay
81
+ *
82
+ * This assumes than an error indicates that the keychain is under attack. Delay returning an
83
+ * error to make brute force attacks harder.
84
+ */
85
+ async function randomDelay () {
86
+ const min = 200
87
+ const max = 1000
88
+ const delay = Math.random() * (max - min) + min
89
+
90
+ await new Promise(resolve => setTimeout(resolve, delay))
91
+ }
92
+
93
+ /**
94
+ * Converts a key name into a datastore name
95
+ */
96
+ function DsName (name: string) {
97
+ return new Key(keyPrefix + name)
98
+ }
99
+
100
+ /**
101
+ * Converts a key name into a datastore info name
102
+ */
103
+ function DsInfoName (name: string) {
104
+ return new Key(infoPrefix + name)
105
+ }
106
+
107
+ export interface KeyChainComponents {
108
+ datastore: Datastore
109
+ }
110
+
111
+ /**
112
+ * Manages the lifecycle of a key. Keys are encrypted at rest using PKCS #8.
113
+ *
114
+ * A key in the store has two entries
115
+ * - '/info/*key-name*', contains the KeyInfo for the key
116
+ * - '/pkcs8/*key-name*', contains the PKCS #8 for the key
117
+ *
118
+ */
119
+ export class KeyChain {
120
+ private readonly components: KeyChainComponents
121
+ private readonly init: KeyChainInit
122
+
123
+ /**
124
+ * Creates a new instance of a key chain
125
+ */
126
+ constructor (components: KeyChainComponents, init: KeyChainInit) {
127
+ this.components = components
128
+ this.init = mergeOptions(defaultOptions, init)
129
+
130
+ // Enforce NIST SP 800-132
131
+ if (this.init.pass != null && this.init.pass?.length < 20) {
132
+ throw new Error('pass must be least 20 characters')
133
+ }
134
+ if (this.init.dek?.keyLength != null && this.init.dek.keyLength < NIST.minKeyLength) {
135
+ throw new Error(`dek.keyLength must be least ${NIST.minKeyLength} bytes`)
136
+ }
137
+ if (this.init.dek?.salt?.length != null && this.init.dek.salt.length < NIST.minSaltLength) {
138
+ throw new Error(`dek.saltLength must be least ${NIST.minSaltLength} bytes`)
139
+ }
140
+ if (this.init.dek?.iterationCount != null && this.init.dek.iterationCount < NIST.minIterationCount) {
141
+ throw new Error(`dek.iterationCount must be least ${NIST.minIterationCount}`)
142
+ }
143
+
144
+ const dek = this.init.pass != null && this.init.dek?.salt != null
145
+ ? pbkdf2(
146
+ this.init.pass,
147
+ this.init.dek?.salt,
148
+ this.init.dek?.iterationCount,
149
+ this.init.dek?.keyLength,
150
+ this.init.dek?.hash)
151
+ : ''
152
+
153
+ privates.set(this, { dek })
154
+ }
155
+
156
+ /**
157
+ * Gets an object that can encrypt/decrypt protected data
158
+ * using the Cryptographic Message Syntax (CMS).
159
+ *
160
+ * CMS describes an encapsulation syntax for data protection. It
161
+ * is used to digitally sign, digest, authenticate, or encrypt
162
+ * arbitrary message content
163
+ */
164
+ get cms () {
165
+ const cached = privates.get(this)
166
+
167
+ if (cached == null) {
168
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
169
+ }
170
+
171
+ const dek = cached.dek
172
+
173
+ return new CMS(this, dek)
174
+ }
175
+
176
+ /**
177
+ * Generates the options for a keychain. A random salt is produced.
178
+ *
179
+ * @returns {object}
180
+ */
181
+ static generateOptions (): KeyChainInit {
182
+ const options = Object.assign({}, defaultOptions)
183
+ const saltLength = Math.ceil(NIST.minSaltLength / 3) * 3 // no base64 padding
184
+ options.dek.salt = uint8ArrayToString(randomBytes(saltLength), 'base64')
185
+ return options
186
+ }
187
+
188
+ /**
189
+ * Gets an object that can encrypt/decrypt protected data.
190
+ * The default options for a keychain.
191
+ *
192
+ * @returns {object}
193
+ */
194
+ static get options () {
195
+ return defaultOptions
196
+ }
197
+
198
+ /**
199
+ * Create a new key.
200
+ *
201
+ * @param {string} name - The local key name; cannot already exist.
202
+ * @param {string} type - One of the key types; 'rsa'.
203
+ * @param {number} [size = 2048] - The key size in bits. Used for rsa keys only
204
+ */
205
+ async createKey (name: string, type: KeyTypes, size = 2048): Promise<KeyInfo> {
206
+ if (!validateKeyName(name) || name === 'self') {
207
+ await randomDelay()
208
+ throw errCode(new Error('Invalid key name'), codes.ERR_INVALID_KEY_NAME)
209
+ }
210
+
211
+ if (typeof type !== 'string') {
212
+ await randomDelay()
213
+ throw errCode(new Error('Invalid key type'), codes.ERR_INVALID_KEY_TYPE)
214
+ }
215
+
216
+ const dsname = DsName(name)
217
+ const exists = await this.components.datastore.has(dsname)
218
+ if (exists) {
219
+ await randomDelay()
220
+ throw errCode(new Error('Key name already exists'), codes.ERR_KEY_ALREADY_EXISTS)
221
+ }
222
+
223
+ switch (type.toLowerCase()) {
224
+ case 'rsa':
225
+ if (!Number.isSafeInteger(size) || size < 2048) {
226
+ await randomDelay()
227
+ throw errCode(new Error('Invalid RSA key size'), codes.ERR_INVALID_KEY_SIZE)
228
+ }
229
+ break
230
+ default:
231
+ break
232
+ }
233
+
234
+ let keyInfo
235
+ try {
236
+ const keypair = await generateKeyPair(type, size)
237
+ const kid = await keypair.id()
238
+ const cached = privates.get(this)
239
+
240
+ if (cached == null) {
241
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
242
+ }
243
+
244
+ const dek = cached.dek
245
+ const pem = await keypair.export(dek)
246
+ keyInfo = {
247
+ name: name,
248
+ id: kid
249
+ }
250
+ const batch = this.components.datastore.batch()
251
+ batch.put(dsname, uint8ArrayFromString(pem))
252
+ batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
253
+
254
+ await batch.commit()
255
+ } catch (err: any) {
256
+ await randomDelay()
257
+ throw err
258
+ }
259
+
260
+ return keyInfo
261
+ }
262
+
263
+ /**
264
+ * List all the keys.
265
+ *
266
+ * @returns {Promise<KeyInfo[]>}
267
+ */
268
+ async listKeys () {
269
+ const query = {
270
+ prefix: infoPrefix
271
+ }
272
+
273
+ const info = []
274
+ for await (const value of this.components.datastore.query(query)) {
275
+ info.push(JSON.parse(uint8ArrayToString(value.value)))
276
+ }
277
+
278
+ return info
279
+ }
280
+
281
+ /**
282
+ * Find a key by it's id
283
+ */
284
+ async findKeyById (id: string): Promise<KeyInfo> {
285
+ try {
286
+ const keys = await this.listKeys()
287
+ return keys.find((k) => k.id === id)
288
+ } catch (err: any) {
289
+ await randomDelay()
290
+ throw err
291
+ }
292
+ }
293
+
294
+ /**
295
+ * Find a key by it's name.
296
+ *
297
+ * @param {string} name - The local key name.
298
+ * @returns {Promise<KeyInfo>}
299
+ */
300
+ async findKeyByName (name: string): Promise<KeyInfo> {
301
+ if (!validateKeyName(name)) {
302
+ await randomDelay()
303
+ throw errCode(new Error(`Invalid key name '${name}'`), codes.ERR_INVALID_KEY_NAME)
304
+ }
305
+
306
+ const dsname = DsInfoName(name)
307
+ try {
308
+ const res = await this.components.datastore.get(dsname)
309
+ return JSON.parse(uint8ArrayToString(res))
310
+ } catch (err: any) {
311
+ await randomDelay()
312
+ log.error(err)
313
+ throw errCode(new Error(`Key '${name}' does not exist.`), codes.ERR_KEY_NOT_FOUND)
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Remove an existing key.
319
+ *
320
+ * @param {string} name - The local key name; must already exist.
321
+ * @returns {Promise<KeyInfo>}
322
+ */
323
+ async removeKey (name: string) {
324
+ if (!validateKeyName(name) || name === 'self') {
325
+ await randomDelay()
326
+ throw errCode(new Error(`Invalid key name '${name}'`), codes.ERR_INVALID_KEY_NAME)
327
+ }
328
+ const dsname = DsName(name)
329
+ const keyInfo = await this.findKeyByName(name)
330
+ const batch = this.components.datastore.batch()
331
+ batch.delete(dsname)
332
+ batch.delete(DsInfoName(name))
333
+ await batch.commit()
334
+ return keyInfo
335
+ }
336
+
337
+ /**
338
+ * Rename a key
339
+ *
340
+ * @param {string} oldName - The old local key name; must already exist.
341
+ * @param {string} newName - The new local key name; must not already exist.
342
+ * @returns {Promise<KeyInfo>}
343
+ */
344
+ async renameKey (oldName: string, newName: string): Promise<KeyInfo> {
345
+ if (!validateKeyName(oldName) || oldName === 'self') {
346
+ await randomDelay()
347
+ throw errCode(new Error(`Invalid old key name '${oldName}'`), codes.ERR_OLD_KEY_NAME_INVALID)
348
+ }
349
+ if (!validateKeyName(newName) || newName === 'self') {
350
+ await randomDelay()
351
+ throw errCode(new Error(`Invalid new key name '${newName}'`), codes.ERR_NEW_KEY_NAME_INVALID)
352
+ }
353
+ const oldDsname = DsName(oldName)
354
+ const newDsname = DsName(newName)
355
+ const oldInfoName = DsInfoName(oldName)
356
+ const newInfoName = DsInfoName(newName)
357
+
358
+ const exists = await this.components.datastore.has(newDsname)
359
+ if (exists) {
360
+ await randomDelay()
361
+ throw errCode(new Error(`Key '${newName}' already exists`), codes.ERR_KEY_ALREADY_EXISTS)
362
+ }
363
+
364
+ try {
365
+ const pem = await this.components.datastore.get(oldDsname)
366
+ const res = await this.components.datastore.get(oldInfoName)
367
+
368
+ const keyInfo = JSON.parse(uint8ArrayToString(res))
369
+ keyInfo.name = newName
370
+ const batch = this.components.datastore.batch()
371
+ batch.put(newDsname, pem)
372
+ batch.put(newInfoName, uint8ArrayFromString(JSON.stringify(keyInfo)))
373
+ batch.delete(oldDsname)
374
+ batch.delete(oldInfoName)
375
+ await batch.commit()
376
+ return keyInfo
377
+ } catch (err: any) {
378
+ await randomDelay()
379
+ throw err
380
+ }
381
+ }
382
+
383
+ /**
384
+ * Export an existing key as a PEM encrypted PKCS #8 string
385
+ */
386
+ async exportKey (name: string, password: string) {
387
+ if (!validateKeyName(name)) {
388
+ await randomDelay()
389
+ throw errCode(new Error(`Invalid key name '${name}'`), codes.ERR_INVALID_KEY_NAME)
390
+ }
391
+ if (password == null) {
392
+ await randomDelay()
393
+ throw errCode(new Error('Password is required'), codes.ERR_PASSWORD_REQUIRED)
394
+ }
395
+
396
+ const dsname = DsName(name)
397
+ try {
398
+ const res = await this.components.datastore.get(dsname)
399
+ const pem = uint8ArrayToString(res)
400
+ const cached = privates.get(this)
401
+
402
+ if (cached == null) {
403
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
404
+ }
405
+
406
+ const dek = cached.dek
407
+ const privateKey = await importKey(pem, dek)
408
+ return await privateKey.export(password)
409
+ } catch (err: any) {
410
+ await randomDelay()
411
+ throw err
412
+ }
413
+ }
414
+
415
+ /**
416
+ * Export an existing key as a PeerId
417
+ */
418
+ async exportPeerId (name: string) {
419
+ const password = 'temporary-password'
420
+ const pem = await this.exportKey(name, password)
421
+ const privateKey = await importKey(pem, password)
422
+
423
+ return await peerIdFromKeys(privateKey.public.bytes, privateKey.bytes)
424
+ }
425
+
426
+ /**
427
+ * Import a new key from a PEM encoded PKCS #8 string
428
+ *
429
+ * @param {string} name - The local key name; must not already exist.
430
+ * @param {string} pem - The PEM encoded PKCS #8 string
431
+ * @param {string} password - The password.
432
+ * @returns {Promise<KeyInfo>}
433
+ */
434
+ async importKey (name: string, pem: string, password: string): Promise<KeyInfo> {
435
+ if (!validateKeyName(name) || name === 'self') {
436
+ await randomDelay()
437
+ throw errCode(new Error(`Invalid key name '${name}'`), codes.ERR_INVALID_KEY_NAME)
438
+ }
439
+ if (pem == null) {
440
+ await randomDelay()
441
+ throw errCode(new Error('PEM encoded key is required'), codes.ERR_PEM_REQUIRED)
442
+ }
443
+ const dsname = DsName(name)
444
+ const exists = await this.components.datastore.has(dsname)
445
+ if (exists) {
446
+ await randomDelay()
447
+ throw errCode(new Error(`Key '${name}' already exists`), codes.ERR_KEY_ALREADY_EXISTS)
448
+ }
449
+
450
+ let privateKey
451
+ try {
452
+ privateKey = await importKey(pem, password)
453
+ } catch (err: any) {
454
+ await randomDelay()
455
+ throw errCode(new Error('Cannot read the key, most likely the password is wrong'), codes.ERR_CANNOT_READ_KEY)
456
+ }
457
+
458
+ let kid
459
+ try {
460
+ kid = await privateKey.id()
461
+ const cached = privates.get(this)
462
+
463
+ if (cached == null) {
464
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
465
+ }
466
+
467
+ const dek = cached.dek
468
+ pem = await privateKey.export(dek)
469
+ } catch (err: any) {
470
+ await randomDelay()
471
+ throw err
472
+ }
473
+
474
+ const keyInfo = {
475
+ name: name,
476
+ id: kid
477
+ }
478
+ const batch = this.components.datastore.batch()
479
+ batch.put(dsname, uint8ArrayFromString(pem))
480
+ batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
481
+ await batch.commit()
482
+
483
+ return keyInfo
484
+ }
485
+
486
+ /**
487
+ * Import a peer key
488
+ */
489
+ async importPeer (name: string, peer: PeerId): Promise<KeyInfo> {
490
+ try {
491
+ if (!validateKeyName(name)) {
492
+ throw errCode(new Error(`Invalid key name '${name}'`), codes.ERR_INVALID_KEY_NAME)
493
+ }
494
+ if (peer == null) {
495
+ throw errCode(new Error('PeerId is required'), codes.ERR_MISSING_PRIVATE_KEY)
496
+ }
497
+ if (peer.privateKey == null) {
498
+ throw errCode(new Error('PeerId.privKey is required'), codes.ERR_MISSING_PRIVATE_KEY)
499
+ }
500
+
501
+ const privateKey = await unmarshalPrivateKey(peer.privateKey)
502
+
503
+ const dsname = DsName(name)
504
+ const exists = await this.components.datastore.has(dsname)
505
+ if (exists) {
506
+ await randomDelay()
507
+ throw errCode(new Error(`Key '${name}' already exists`), codes.ERR_KEY_ALREADY_EXISTS)
508
+ }
509
+
510
+ const cached = privates.get(this)
511
+
512
+ if (cached == null) {
513
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
514
+ }
515
+
516
+ const dek = cached.dek
517
+ const pem = await privateKey.export(dek)
518
+ const keyInfo: KeyInfo = {
519
+ name: name,
520
+ id: peer.toString()
521
+ }
522
+ const batch = this.components.datastore.batch()
523
+ batch.put(dsname, uint8ArrayFromString(pem))
524
+ batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
525
+ await batch.commit()
526
+ return keyInfo
527
+ } catch (err: any) {
528
+ await randomDelay()
529
+ throw err
530
+ }
531
+ }
532
+
533
+ /**
534
+ * Gets the private key as PEM encoded PKCS #8 string
535
+ */
536
+ async getPrivateKey (name: string): Promise<string> {
537
+ if (!validateKeyName(name)) {
538
+ await randomDelay()
539
+ throw errCode(new Error(`Invalid key name '${name}'`), codes.ERR_INVALID_KEY_NAME)
540
+ }
541
+
542
+ try {
543
+ const dsname = DsName(name)
544
+ const res = await this.components.datastore.get(dsname)
545
+ return uint8ArrayToString(res)
546
+ } catch (err: any) {
547
+ await randomDelay()
548
+ log.error(err)
549
+ throw errCode(new Error(`Key '${name}' does not exist.`), codes.ERR_KEY_NOT_FOUND)
550
+ }
551
+ }
552
+
553
+ /**
554
+ * Rotate keychain password and re-encrypt all associated keys
555
+ */
556
+ async rotateKeychainPass (oldPass: string, newPass: string) {
557
+ if (typeof oldPass !== 'string') {
558
+ await randomDelay()
559
+ throw errCode(new Error(`Invalid old pass type '${typeof oldPass}'`), codes.ERR_INVALID_OLD_PASS_TYPE)
560
+ }
561
+ if (typeof newPass !== 'string') {
562
+ await randomDelay()
563
+ throw errCode(new Error(`Invalid new pass type '${typeof newPass}'`), codes.ERR_INVALID_NEW_PASS_TYPE)
564
+ }
565
+ if (newPass.length < 20) {
566
+ await randomDelay()
567
+ throw errCode(new Error(`Invalid pass length ${newPass.length}`), codes.ERR_INVALID_PASS_LENGTH)
568
+ }
569
+ log('recreating keychain')
570
+ const cached = privates.get(this)
571
+
572
+ if (cached == null) {
573
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
574
+ }
575
+
576
+ const oldDek = cached.dek
577
+ this.init.pass = newPass
578
+ const newDek = newPass != null && this.init.dek?.salt != null
579
+ ? pbkdf2(
580
+ newPass,
581
+ this.init.dek.salt,
582
+ this.init.dek?.iterationCount,
583
+ this.init.dek?.keyLength,
584
+ this.init.dek?.hash)
585
+ : ''
586
+ privates.set(this, { dek: newDek })
587
+ const keys = await this.listKeys()
588
+ for (const key of keys) {
589
+ const res = await this.components.datastore.get(DsName(key.name))
590
+ const pem = uint8ArrayToString(res)
591
+ const privateKey = await importKey(pem, oldDek)
592
+ const password = newDek.toString()
593
+ const keyAsPEM = await privateKey.export(password)
594
+
595
+ // Update stored key
596
+ const batch = this.components.datastore.batch()
597
+ const keyInfo = {
598
+ name: key.name,
599
+ id: key.id
600
+ }
601
+ batch.put(DsName(key.name), uint8ArrayFromString(keyAsPEM))
602
+ batch.put(DsInfoName(key.name), uint8ArrayFromString(JSON.stringify(keyInfo)))
603
+ await batch.commit()
604
+ }
605
+ log('keychain reconstructed')
606
+ }
607
+ }
package/src/util.ts ADDED
@@ -0,0 +1,82 @@
1
+ import 'node-forge/lib/x509.js'
2
+ // @ts-expect-error types are missing
3
+ import forge from 'node-forge/lib/forge.js'
4
+
5
+ const pki = forge.pki
6
+
7
+ /**
8
+ * Gets a self-signed X.509 certificate for the key.
9
+ *
10
+ * The output Uint8Array contains the PKCS #7 message in DER.
11
+ *
12
+ * TODO: move to libp2p-crypto package
13
+ */
14
+ export const certificateForKey = (key: any, privateKey: forge.pki.rsa.PrivateKey) => {
15
+ const publicKey = pki.rsa.setPublicKey(privateKey.n, privateKey.e)
16
+ const cert = pki.createCertificate()
17
+ cert.publicKey = publicKey
18
+ cert.serialNumber = '01'
19
+ cert.validity.notBefore = new Date()
20
+ cert.validity.notAfter = new Date()
21
+ cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 10) // eslint-disable-line @typescript-eslint/restrict-plus-operands
22
+ const attrs = [{
23
+ name: 'organizationName',
24
+ value: 'ipfs'
25
+ }, {
26
+ shortName: 'OU',
27
+ value: 'keystore'
28
+ }, {
29
+ name: 'commonName',
30
+ value: key.id
31
+ }]
32
+ cert.setSubject(attrs)
33
+ cert.setIssuer(attrs)
34
+ cert.setExtensions([{
35
+ name: 'basicConstraints',
36
+ cA: true
37
+ }, {
38
+ name: 'keyUsage',
39
+ keyCertSign: true,
40
+ digitalSignature: true,
41
+ nonRepudiation: true,
42
+ keyEncipherment: true,
43
+ dataEncipherment: true
44
+ }, {
45
+ name: 'extKeyUsage',
46
+ serverAuth: true,
47
+ clientAuth: true,
48
+ codeSigning: true,
49
+ emailProtection: true,
50
+ timeStamping: true
51
+ }, {
52
+ name: 'nsCertType',
53
+ client: true,
54
+ server: true,
55
+ email: true,
56
+ objsign: true,
57
+ sslCA: true,
58
+ emailCA: true,
59
+ objCA: true
60
+ }])
61
+ // self-sign certificate
62
+ cert.sign(privateKey)
63
+
64
+ return cert
65
+ }
66
+
67
+ /**
68
+ * Finds the first item in a collection that is matched in the
69
+ * `asyncCompare` function.
70
+ *
71
+ * `asyncCompare` is an async function that must
72
+ * resolve to either `true` or `false`.
73
+ *
74
+ * @param {Array} array
75
+ * @param {function(*)} asyncCompare - An async function that returns a boolean
76
+ */
77
+ export async function findAsync <T> (array: T[], asyncCompare: (val: T) => Promise<any>) {
78
+ const promises = array.map(asyncCompare)
79
+ const results = await Promise.all(promises)
80
+ const index = results.findIndex(result => result)
81
+ return array[index]
82
+ }