-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.js
436 lines (360 loc) · 11.6 KB
/
format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// SPDX-FileCopyrightText: 2022 Andre 'Staltz' Medeiros
//
// SPDX-License-Identifier: LGPL-3.0-only
const { promisify } = require('util')
const BFE = require('ssb-bfe')
const Ref = require('ssb-ref')
const Uri = require('ssb-uri2')
const path = require('path')
const os = require('os')
const { box, unbox } = require('envelope-js')
const { SecretKey, DHKeys, poBoxKey } = require('ssb-private-group-keys')
const { keySchemes } = require('private-group-spec')
const Keyring = require('ssb-keyring')
const { ReadyGate } = require('./utils')
const pull = require('pull-stream')
const pullDefer = require('pull-defer')
function reportError(err) {
if (err) console.error(err)
}
const ATTEMPT1 = { maxAttempts: 1 }
const ATTEMPT16 = { maxAttempts: 16 }
function makeEncryptionFormat() {
let keyring = null
const keyringReady = new ReadyGate()
let legacyMode = true
let mainKeys = null
function setup(config, cb) {
const keyringPath = path.join(
config.path || path.join(os.tmpdir(), '.ssb-keyring-' + Date.now()),
(config.box2 && config.box2.path) || 'keyring'
)
Keyring(keyringPath, (err, api) => {
if (err) return cb(err)
keyring = api
mainKeys = config.keys
keyringReady.setReady()
cb()
})
}
function teardown(cb) {
keyringReady.onReady(() => {
keyring.close(cb)
})
}
function disableLegacyMode() {
legacyMode = false
}
function isRawGroupKey(recp) {
return (
recp &&
recp.scheme === keySchemes.private_group &&
Buffer.isBuffer(recp.key)
)
}
function isGroupId(id) {
return Ref.isCloakedMsgId(id) || Uri.isIdentityGroupSSBURI(id)
}
function isPoBoxId(id) {
return Uri.isIdentityPOBoxSSBURI(id)
}
function isFeed(recp) {
return (
Ref.isFeed(recp) ||
Uri.isClassicFeedSSBURI(recp) ||
Uri.isBendyButtV1FeedSSBURI(recp) ||
Uri.isButtwooV1FeedSSBURI(recp)
)
}
function setOwnDMKey(key) {
keyringReady.onReady(() => {
keyring.self.set({ key }, reportError)
})
}
function getOwnDMKey(cb) {
keyringReady.onReady(() => {
cb(null, keyring.self.get())
})
}
function addDMPairSync(myKeys, theirId) {
if (!keyringReady.ready) throw new Error('keyring not ready')
const myId = myKeys.id
const myDhKeys = new DHKeys(myKeys, { fromEd25519: true })
const theirKeys = { public: BFE.encode(theirId).slice(2) }
const theirDhKeys = new DHKeys(theirKeys, { fromEd25519: true })
return keyring.dm.add(myId, theirId, myDhKeys, theirDhKeys, reportError)
}
function addDMTriangle(xRootId, xLeafId, yLeafId) {
keyringReady.onReady(() => {
keyring.dm.addTriangle(xRootId, xLeafId, yLeafId, reportError)
})
}
function canDM(myLeafId, theirRootId, cb) {
keyringReady.onReady(() => {
const theirLeafId = keyring.dm.triangulate(theirRootId, myLeafId)
cb(null, !!theirLeafId)
})
}
function addSigningKeys(keys, name) {
keyringReady.onReady(() => {
addSigningKeysSync(keys, name)
})
}
function addSigningKeysSync(keys, name) {
if (!keyringReady.ready) throw new Error('keyring not ready')
if (name) return keyring.signing.addNamed(name, keys)
else return keyring.signing.add(keys)
}
function getRootSigningKey(cb) {
keyringReady.onReady(() => {
cb(null, keyring.signing.get('root'))
})
}
function addGroupInfo(id, info, cb) {
if (cb === undefined) return promisify(addGroupInfo)(id, info)
keyringReady.onReady(() => {
keyring.group.add(id, info, cb)
})
}
function pickGroupWriteKey(id, pickedKey, cb) {
if (cb === undefined) return promisify(pickGroupWriteKey)(id, pickedKey)
keyringReady.onReady(() => {
keyring.group.pickWriteKey(id, pickedKey, cb)
})
}
function excludeGroupInfo(id, cb) {
if (cb === undefined) return promisify(excludeGroupInfo)(id)
if (!id) cb(new Error('Group id required'))
keyringReady.onReady(() => {
keyring.group.exclude(id, cb)
})
}
function listGroupIds(opts = {}) {
const deferredSource = pullDefer.source()
keyringReady.onReady(() => {
const source = keyring.group.list({
live: !!opts.live,
excluded: !!opts.excluded,
})
deferredSource.resolve(source)
})
return deferredSource
}
function getGroupInfo(groupId, cb) {
if (cb === undefined) return promisify(getGroupInfo)(groupId)
if (!groupId) cb(new Error('Group id required'))
keyringReady.onReady(() => {
cb(null, keyring.group.get(groupId))
})
}
function getGroupInfoUpdates(groupId) {
if (!groupId) return pull.error(new Error('Group id required'))
const deferredSource = pullDefer.source()
keyringReady.onReady(() => {
const source = keyring.group.getUpdates(groupId)
deferredSource.resolve(source)
})
return deferredSource
}
function addPoBox(poBoxId, info, cb) {
if (cb === undefined) return promisify(addPoBox)(poBoxId, info)
if (!poBoxId) cb(new Error('pobox id required'))
if (!info) cb(new Error('pobox info required'))
keyringReady.onReady(() => {
keyring.poBox.add(poBoxId, info, cb)
})
}
function hasPoBox(poBoxId, cb) {
if (cb === undefined) return promisify(hasPoBox)(poBoxId)
if (!poBoxId) cb(new Error('pobox id required'))
keyringReady.onReady(() => {
cb(null, keyring.poBox.has(poBoxId))
})
}
function getPoBox(poBoxId, cb) {
if (cb === undefined) return promisify(getPoBox)(poBoxId)
if (!poBoxId) cb(new Error('pobox id required'))
keyringReady.onReady(() => {
cb(null, keyring.poBox.get(poBoxId))
})
}
function listPoBoxIds() {
const deferredSource = pullDefer.source()
keyringReady.onReady(() => {
const source = pull.values(keyring.poBox.list())
deferredSource.resolve(source)
})
return deferredSource
}
function dmEncryptionKey(authorKeys, recp) {
if (legacyMode) {
if (!keyring.dm.has(authorKeys.id, recp)) addDMPairSync(authorKeys, recp)
const dmKey = keyring.dm.get(authorKeys.id, recp)
if (!dmKey) {
throw new Error('DM keys not supported for recipient ' + recp)
}
dmKey.scheme = keySchemes.feed_id_dm
return dmKey
} else {
const theirRootId = recp
const myLeafId = authorKeys.id
const theirLeafId = keyring.dm.triangulate(theirRootId, myLeafId)
if (!theirLeafId || typeof theirLeafId !== 'string') {
throw new Error('DM encryption failed to triangulate ' + theirRootId)
}
const dmKeys = keyring.dm.get(myLeafId, theirLeafId)
if (!dmKeys) {
throw new Error(
'DM encryption failed to find DH keys for mirrored leaf feed ' +
theirLeafId
)
}
return dmKeys
}
}
function encrypt(plaintextBuf, opts) {
const recps = opts.recps
const authorId = opts.keys.id
const previousId = opts.previous
const easyPoBoxKey = poBoxKey.easy(opts.keys)
const encryptionKeys = recps.map((recp) => {
if (isRawGroupKey(recp)) {
return recp
} else if (recp === authorId || keyring.signing.has(recp)) {
return keyring.self.get()
} else if (isFeed(recp)) {
return dmEncryptionKey(opts.keys, recp)
} else if (isGroupId(recp) && keyring.group.has(recp)) {
return keyring.group.get(recp).writeKey
} else if (isPoBoxId(recp)) {
return easyPoBoxKey(recp)
} else throw new Error('Unsupported recipient: ' + recp)
})
const validCount = encryptionKeys.length
if (validCount === 0) {
throw new Error(`no box2 keys found for recipients: ${recps}`)
}
if (validCount > 16) {
// prettier-ignore
throw new Error(`private-group spec allows maximum 16 slots, but you've tried to use ${validCount}`)
}
const validGroupCount = encryptionKeys.filter(
(encryptKeys) => encryptKeys.scheme === keySchemes.private_group
).length
if (validGroupCount > 1) {
// prettier-ignore
throw new Error(`private-group spec only supports one group recipient, but you've tried to use ${validGroupCount}`)
}
const msgSymmKey = new SecretKey().toBuffer()
const authorIdBFE = BFE.encode(authorId)
const previousMsgIdBFE = BFE.encode(previousId)
const ciphertextBuf = box(
plaintextBuf,
authorIdBFE,
previousMsgIdBFE,
msgSymmKey,
encryptionKeys
)
return ciphertextBuf
}
function selfDecryptionKeys(authorId) {
const selfKeys = keyring.self.get()
if (keyring.signing.has(authorId)) return [selfKeys]
else if (legacyMode && authorId === mainKeys.id) return [selfKeys]
else return []
}
function dmDecryptionKeys(authorId) {
if (legacyMode) {
const dmKeys = keyring.dm.get(mainKeys.id, authorId)
if (!dmKeys) addDMPairSync(mainKeys, authorId)
if (!keyring.dm.has(mainKeys.id, authorId)) return []
const dmKey = keyring.dm.get(mainKeys.id, authorId)
dmKey.scheme = keySchemes.feed_id_dm
return [dmKey]
} else {
const myRootKeys = keyring.signing.get('root')
if (!myRootKeys) return []
const myLeafId = keyring.dm.triangulate(myRootKeys.id, authorId)
if (!myLeafId) return []
if (!keyring.dm.has(myLeafId, authorId)) return []
return [keyring.dm.get(myLeafId, authorId)]
}
}
function poBoxDecryptionKey(authorId, authorIdBFE, poBoxId) {
// TODO - consider how to reduce redundent computation + memory use here
const data = keyring.poBox.get(poBoxId)
const poBox_dh_secret = Buffer.concat([
BFE.toTF('encryption-key', 'box2-pobox-dh'),
data.key,
])
const poBox_id = BFE.encode(poBoxId)
const poBox_dh_public = Buffer.concat([
BFE.toTF('encryption-key', 'box2-pobox-dh'),
poBox_id.slice(2),
])
const author_dh_public = new DHKeys(
{ public: authorId },
{ fromEd25519: true }
).toBFE().public
return poBoxKey(
poBox_dh_secret,
poBox_dh_public,
poBox_id,
author_dh_public,
authorIdBFE
)
}
function decrypt(ciphertextBuf, opts) {
const authorId = opts.author
const authorBFE = BFE.encode(authorId)
const previousBFE = BFE.encode(opts.previous)
const unboxWith = unbox.bind(null, ciphertextBuf, authorBFE, previousBFE)
let plaintextBuf = null
const groups = keyring.group.listSync()
const excludedGroups = keyring.group.listSync({ excluded: true })
const groupKeys = [...groups, ...excludedGroups]
.map(keyring.group.get)
.map((groupInfo) => groupInfo.readKeys)
.flat()
if ((plaintextBuf = unboxWith(groupKeys, ATTEMPT1))) return plaintextBuf
const selfKey = selfDecryptionKeys(authorId)
if ((plaintextBuf = unboxWith(selfKey, ATTEMPT16))) return plaintextBuf
const dmKey = dmDecryptionKeys(authorId)
if ((plaintextBuf = unboxWith(dmKey, ATTEMPT16))) return plaintextBuf
const poBoxKeys = keyring.poBox
.list()
.map((poBoxId) => poBoxDecryptionKey(authorId, authorBFE, poBoxId))
if ((plaintextBuf = unboxWith(poBoxKeys, ATTEMPT16))) return plaintextBuf
return null
}
return {
// ssb-encryption-format API:
name: 'box2',
setup,
teardown,
encrypt,
decrypt,
// ssb-box2 specific APIs:
setOwnDMKey,
getOwnDMKey,
addGroupInfo,
pickGroupWriteKey,
excludeGroupInfo,
listGroupIds,
getGroupInfo,
getGroupInfoUpdates,
canDM,
addPoBox,
hasPoBox,
getPoBox,
listPoBoxIds,
// Internal APIs:
addSigningKeys,
addSigningKeysSync,
addDMPairSync,
addDMTriangle,
getRootSigningKey,
disableLegacyMode,
}
}
module.exports = makeEncryptionFormat