git ssb

0+

Dominic / ssb-peer-invites



Commit 39b9c70c0e6cbb27bccacdc189c8a2f197e9959d

additional checks, and let reveal and private fields to be optional

Dominic Tarr committed on 3/26/2018, 1:20:23 AM
Parent: c7bf4c2176c6338372080b861d88304cfe39b10a

Files changed

index.jschanged
index.jsView
@@ -2,8 +2,9 @@
22
33 var chloride = require('chloride')
44
55 function box (data, key) {
6 + if(!data) return
67 var b = new Buffer(JSON.stringify(data))
78 return chloride.crypto_secretbox_easy(b, key.slice(0, 24), key).toString('base64')
89 }
910
@@ -20,25 +21,37 @@
2021 }
2122
2223
2324 function hash(s) {
24- return chloride.crypto_hash_sha256(new Buffer(s, 'utf8'))
25 + return chloride.crypto_hash_sha256(
26 + 'string' == typeof s ? new Buffer(s, 'utf8') : s
27 + )
2528 }
2629
27-exports.createInvite = function (seed, id, reveal, private) {
30 +var invite_key = hash("user-invites:development")
31 +
32 +exports.createInvite = function (seed, host, reveal, private) {
2833 var keys = ssbKeys.generate(null, seed) //K
29- return ssbKeys.signObj(keys, null, {
34 + if(keys.id === host)
35 + throw new Error('do not create invite with own public key')
36 + return ssbKeys.signObj(keys, invite_key, {
3037 type: 'invite',
3138 invite: keys.id,
32- host: id, //sign our own key, to prove we created K
39 + host: host, //sign our own key, to prove we created K
3340 reveal: box(reveal, hash(hash(seed))),
3441 private: box(private, hash(seed))
3542 })
3643 }
3744
3845 exports.verifyInvitePublic = function (msg) {
39- if(!ssbKeys.verifyObj(msg.content.invite, msg.content)) throw new Error('invalid guest signature')
40- if(!ssbKeys.verifyObj(msg.author, msg)) throw new Error('invalid host signature')
46 + if(!ssbKeys.verifyObj(msg.content.invite, invite_key, msg.content))
47 + throw new Error('invalid guest signature')
48 + if(msg.content.host != msg.author)
49 + throw new Error('host did not match author')
50 +
51 + //an ordinary message so doesn't use special hmac_key
52 + if(!ssbKeys.verifyObj(msg.author, msg))
53 + throw new Error('invalid host signature')
4154 return true
4255 }
4356
4457 exports.verifyInvitePrivate = function (msg, seed) {
@@ -50,36 +63,38 @@
5063 if(msg.content.private) {
5164 var private = unbox(msg.content.private, hash(seed))
5265 if(!reveal) throw new Error('could not decrypt private message')
5366 }
67 +
5468 return {reveal: reveal, private: private}
5569 }
5670
5771 exports.createAccept = function (msg, seed, id) {
5872 var keys = ssbKeys.generate(null, seed) //K
5973 if(keys.id != msg.content.invite) throw new Error('seed does not match invite')
60-
61- var inviteId = ssbKeys.hash(JSON.stringify(msg, null, 2))
62- return ssbKeys.signObj(keys, null, {
74 + var inviteId = '%'+ssbKeys.hash(JSON.stringify(msg, null, 2))
75 + return ssbKeys.signObj(keys, invite_key, {
6376 type: 'invite/accept',
6477 reciept: inviteId,
6578 id: id,
6679 key: msg.content.reveal ? hash(hash(seed)).toString('base64') : undefined
6780 })
6881 }
6982
7083 exports.verifyAccept = function (accept, invite) {
71- console.log(accept, invite)
7284 var reveal
73- if(ssbKeys.hash(JSON.stringify(invite, null, 2)) !== accept.content.reciept)
85 + if('%'+ssbKeys.hash(JSON.stringify(invite, null, 2)) !== accept.content.reciept)
7486 throw new Error('acceptance not matched to given invite')
87 + if(accept.author === invite.content.id)
88 + throw new Error('invitee must use a new key, not the same seed')
7589 if(invite.content.reveal) {
7690 reveal = unbox(invite.content.reveal, new Buffer(accept.content.key, 'base64'))
7791 if(!reveal) throw new Error('accept did not correctly reveal invite')
7892 }
7993
80- if(!ssbKeys.verifyObj(invite.content.invite, accept.content))
94 + if(!ssbKeys.verifyObj(invite.content.invite, invite_key, accept.content))
8195 throw new Error('did not verify invite-acceptance contents')
96 + //an ordinary message, so does not use hmac_key
8297 if(!ssbKeys.verifyObj(accept.content.id, accept))
8398 throw new Error('acceptance must be signed by claimed key')
8499 return reveal || true
85100 }

Built with git-ssb-web