@libp2p/keychain 0.6.1 → 0.6.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/src/cms.ts DELETED
@@ -1,150 +0,0 @@
1
- import 'node-forge/lib/pkcs7.js'
2
- import 'node-forge/lib/pbe.js'
3
- // @ts-expect-error types are missing
4
- import forge from 'node-forge/lib/forge.js'
5
- import { certificateForKey, findAsync } from './util.js'
6
- import errCode from 'err-code'
7
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
9
- import { codes } from './errors.js'
10
- import { logger } from '@libp2p/logger'
11
- import type { KeyChain } from './index.js'
12
-
13
- const log = logger('libp2p:keychain:cms')
14
-
15
- const privates = new WeakMap<object, { dek: string }>()
16
-
17
- /**
18
- * Cryptographic Message Syntax (aka PKCS #7)
19
- *
20
- * CMS describes an encapsulation syntax for data protection. It
21
- * is used to digitally sign, digest, authenticate, or encrypt
22
- * arbitrary message content.
23
- *
24
- * See RFC 5652 for all the details.
25
- */
26
- export class CMS {
27
- private readonly keychain: KeyChain
28
-
29
- /**
30
- * Creates a new instance with a keychain
31
- */
32
- constructor (keychain: KeyChain, dek: string) {
33
- if (keychain == null) {
34
- throw errCode(new Error('keychain is required'), codes.ERR_KEYCHAIN_REQUIRED)
35
- }
36
-
37
- this.keychain = keychain
38
- privates.set(this, { dek })
39
- }
40
-
41
- /**
42
- * Creates some protected data.
43
- *
44
- * The output Uint8Array contains the PKCS #7 message in DER.
45
- */
46
- async encrypt (name: string, plain: Uint8Array): Promise<Uint8Array> {
47
- if (!(plain instanceof Uint8Array)) {
48
- throw errCode(new Error('Plain data must be a Uint8Array'), codes.ERR_INVALID_PARAMETERS)
49
- }
50
-
51
- const key = await this.keychain.findKeyByName(name)
52
- const pem = await this.keychain.getPrivateKey(name)
53
- const cached = privates.get(this)
54
-
55
- if (cached == null) {
56
- throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
57
- }
58
-
59
- const dek = cached.dek
60
- const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek)
61
- const certificate = await certificateForKey(key, privateKey)
62
-
63
- // create a p7 enveloped message
64
- const p7 = forge.pkcs7.createEnvelopedData()
65
- p7.addRecipient(certificate)
66
- p7.content = forge.util.createBuffer(plain)
67
- p7.encrypt()
68
-
69
- // convert message to DER
70
- const der = forge.asn1.toDer(p7.toAsn1()).getBytes()
71
- return uint8ArrayFromString(der, 'ascii')
72
- }
73
-
74
- /**
75
- * Reads some protected data.
76
- *
77
- * The keychain must contain one of the keys used to encrypt the data. If none of the keys
78
- * exists, an Error is returned with the property 'missingKeys'. It is array of key ids.
79
- */
80
- async decrypt (cmsData: Uint8Array): Promise<Uint8Array> {
81
- if (!(cmsData instanceof Uint8Array)) {
82
- throw errCode(new Error('CMS data is required'), codes.ERR_INVALID_PARAMETERS)
83
- }
84
-
85
- let cms: any
86
- try {
87
- const buf = forge.util.createBuffer(uint8ArrayToString(cmsData, 'ascii'))
88
- const obj = forge.asn1.fromDer(buf)
89
-
90
- cms = forge.pkcs7.messageFromAsn1(obj)
91
- } catch (err: any) {
92
- log.error(err)
93
- throw errCode(new Error('Invalid CMS'), codes.ERR_INVALID_CMS)
94
- }
95
-
96
- // Find a recipient whose key we hold. We only deal with recipient certs
97
- // issued by ipfs (O=ipfs).
98
- const recipients: any = cms.recipients
99
- // @ts-expect-error cms types not defined
100
- .filter(r => r.issuer.find(a => a.shortName === 'O' && a.value === 'ipfs'))
101
- // @ts-expect-error cms types not defined
102
- .filter(r => r.issuer.find(a => a.shortName === 'CN'))
103
- // @ts-expect-error cms types not defined
104
- .map(r => {
105
- return {
106
- recipient: r,
107
- // @ts-expect-error cms types not defined
108
- keyId: r.issuer.find(a => a.shortName === 'CN').value
109
- }
110
- })
111
-
112
- const r = await findAsync(recipients, async (recipient: any) => {
113
- try {
114
- const key = await this.keychain.findKeyById(recipient.keyId)
115
- if (key != null) {
116
- return true
117
- }
118
- } catch (err: any) {
119
- return false
120
- }
121
- return false
122
- })
123
-
124
- if (r == null) {
125
- // @ts-expect-error cms types not defined
126
- const missingKeys: string[] = recipients.map(r => r.keyId)
127
- throw errCode(new Error(`Decryption needs one of the key(s): ${missingKeys.join(', ')}`), codes.ERR_MISSING_KEYS, {
128
- missingKeys
129
- })
130
- }
131
-
132
- const key = await this.keychain.findKeyById(r.keyId)
133
-
134
- if (key == null) {
135
- throw errCode(new Error('No key available to decrypto'), codes.ERR_NO_KEY)
136
- }
137
-
138
- const pem = await this.keychain.getPrivateKey(key.name)
139
- const cached = privates.get(this)
140
-
141
- if (cached == null) {
142
- throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS)
143
- }
144
-
145
- const dek = cached.dek
146
- const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek)
147
- cms.decrypt(r.recipient, privateKey)
148
- return uint8ArrayFromString(cms.content.getBytes(), 'ascii')
149
- }
150
- }