Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getOwnDMKey function #37

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ sbot.db.create(
Adding this module as a secret-stack plugin means that you can use these methods
on the `sbot.box2` namespace:

- `setOwnDMKey(key)`: Adds a `key` (a buffer) to the list of keys that can be
- `setOwnDMKey(key)`: Sets a `key` (a buffer) as the key that will be
used to encrypt messages to yourself. By specifying the direct message (DM)
for yourself, you are free to supply that from any source. The key you provide
_will_ be persisted locally. For direct messaging other feeds, a key is
automatically derived.
- `getOwnDMKey(cb)`: Gets the key that would be used for DM'ing yourself. On the format `{ key, scheme }`.
- `addGroupInfo(groupId, addInfo, cb)`: `groupId` must be a cloaked message Id or a uri encoded group and `addInfo` must be an object. Can be called multiple times to add multiple read keys. The first key that is added will automatically also be set as the write key. To change the write key, use `pickGroupWriteKey`. If you add a key to an excluded group, the group will be un-excluded. Returns a promise if cb isn't provided. `addInfo` can have these keys:
- `key` must be a buffer. The key can then be used for decrypting messages from the group, and if picked with `pickGroupWriteKey`, as a "recp" to encrypt messages to the group. Note that the keys are not persisted in this module.
- `scheme` _String_ - scheme of that encryption key (optional, there is only one option at the moment which we default to)
Expand Down
7 changes: 7 additions & 0 deletions format.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ function makeEncryptionFormat() {
})
}

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
Expand Down Expand Up @@ -405,6 +411,7 @@ function makeEncryptionFormat() {
decrypt,
// ssb-box2 specific APIs:
setOwnDMKey,
getOwnDMKey,
addGroupInfo,
pickGroupWriteKey,
excludeGroupInfo,
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.init = function (ssb, config) {

return {
setOwnDMKey: encryptionFormat.setOwnDMKey,
getOwnDMKey: encryptionFormat.getOwnDMKey,
canDM: encryptionFormat.canDM,
addGroupInfo: encryptionFormat.addGroupInfo,
pickGroupWriteKey: encryptionFormat.pickGroupWriteKey,
Expand Down
74 changes: 49 additions & 25 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: Unlicense

const { promisify: p } = require('util')
const test = require('tape')
const { check } = require('ssb-encryption-format')
const ssbKeys = require('ssb-keys')
Expand Down Expand Up @@ -213,6 +214,25 @@ test('cannot decrypt own DM after we changed our own DM keys', (t) => {
})
})

test('can get own self dm key', async (t) => {
const box2 = Box2()
const keys = ssbKeys.generate(null, 'alice', 'buttwoo-v1')

await p(box2.setup)({ keys })

const ownKey = Buffer.from(
'30720d8f9cbf37f6d7062826f6decac93e308060a8aaaa77e6a4747f40ee1a76',
'hex'
)

box2.setOwnDMKey(ownKey)

const gottenKey = await p(box2.getOwnDMKey)()

t.equal(gottenKey.key, ownKey, 'got correct key')
t.equal(gottenKey.scheme, keySchemes.feed_id_self, 'got correct scheme')
})

test('cannot encrypt to zero valid recipients', (t) => {
const box2 = Box2()
const keys = ssbKeys.generate(null, 'alice', 'buttwoo-v1')
Expand Down Expand Up @@ -367,30 +387,34 @@ test('decrypt as pobox recipient', (t) => {
const testkey = poBoxDH.toBuffer().secret

box2.setup({ keys }, () => {
box2.addPoBox(poBoxId, {
key: testkey,
}, (err) => {
t.error(err, "added pobox key")

const opts = {
keys,
content: { type: 'post', text: 'super secret' },
previous: null,
timestamp: 12345678900,
tag: buttwoo.tags.SSB_FEED,
hmacKey: null,
recps: [poBoxId, ssbKeys.generate(null, '2').id],
box2.addPoBox(
poBoxId,
{
key: testkey,
},
(err) => {
t.error(err, 'added pobox key')

const opts = {
keys,
content: { type: 'post', text: 'super secret' },
previous: null,
timestamp: 12345678900,
tag: buttwoo.tags.SSB_FEED,
hmacKey: null,
recps: [poBoxId, ssbKeys.generate(null, '2').id],
}

const plaintext = buttwoo.toPlaintextBuffer(opts)
t.true(Buffer.isBuffer(plaintext), 'plaintext is a buffer')

const ciphertext = box2.encrypt(plaintext, opts)

const decrypted = box2.decrypt(ciphertext, { ...opts, author: keys.id })
t.deepEqual(decrypted, plaintext, 'decrypted plaintext is the same')

t.end()
}

const plaintext = buttwoo.toPlaintextBuffer(opts)
t.true(Buffer.isBuffer(plaintext), 'plaintext is a buffer')

const ciphertext = box2.encrypt(plaintext, opts)

const decrypted = box2.decrypt(ciphertext, { ...opts, author: keys.id })
t.deepEqual(decrypted, plaintext, 'decrypted plaintext is the same')

t.end()
})
)
})
})
})
Loading