Commit 39b9c70c0e6cbb27bccacdc189c8a2f197e9959d
additional checks, and let reveal and private fields to be optional
Dominic Tarr committed on 3/26/2018, 1:20:23 AMParent: c7bf4c2176c6338372080b861d88304cfe39b10a
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -2,8 +2,9 @@ | ||
2 | 2 … | |
3 | 3 … | var chloride = require('chloride') |
4 | 4 … | |
5 | 5 … | function box (data, key) { |
6 … | + if(!data) return | |
6 | 7 … | var b = new Buffer(JSON.stringify(data)) |
7 | 8 … | return chloride.crypto_secretbox_easy(b, key.slice(0, 24), key).toString('base64') |
8 | 9 … | } |
9 | 10 … | |
@@ -20,25 +21,37 @@ | ||
20 | 21 … | } |
21 | 22 … | |
22 | 23 … | |
23 | 24 … | 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 … | + ) | |
25 | 28 … | } |
26 | 29 … | |
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) { | |
28 | 33 … | 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, { | |
30 | 37 … | type: 'invite', |
31 | 38 … | 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 | |
33 | 40 … | reveal: box(reveal, hash(hash(seed))), |
34 | 41 … | private: box(private, hash(seed)) |
35 | 42 … | }) |
36 | 43 … | } |
37 | 44 … | |
38 | 45 … | 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') | |
41 | 54 … | return true |
42 | 55 … | } |
43 | 56 … | |
44 | 57 … | exports.verifyInvitePrivate = function (msg, seed) { |
@@ -50,36 +63,38 @@ | ||
50 | 63 … | if(msg.content.private) { |
51 | 64 … | var private = unbox(msg.content.private, hash(seed)) |
52 | 65 … | if(!reveal) throw new Error('could not decrypt private message') |
53 | 66 … | } |
67 … | + | |
54 | 68 … | return {reveal: reveal, private: private} |
55 | 69 … | } |
56 | 70 … | |
57 | 71 … | exports.createAccept = function (msg, seed, id) { |
58 | 72 … | var keys = ssbKeys.generate(null, seed) //K |
59 | 73 … | 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, { | |
63 | 76 … | type: 'invite/accept', |
64 | 77 … | reciept: inviteId, |
65 | 78 … | id: id, |
66 | 79 … | key: msg.content.reveal ? hash(hash(seed)).toString('base64') : undefined |
67 | 80 … | }) |
68 | 81 … | } |
69 | 82 … | |
70 | 83 … | exports.verifyAccept = function (accept, invite) { |
71 | - console.log(accept, invite) | |
72 | 84 … | 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) | |
74 | 86 … | 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') | |
75 | 89 … | if(invite.content.reveal) { |
76 | 90 … | reveal = unbox(invite.content.reveal, new Buffer(accept.content.key, 'base64')) |
77 | 91 … | if(!reveal) throw new Error('accept did not correctly reveal invite') |
78 | 92 … | } |
79 | 93 … | |
80 | - if(!ssbKeys.verifyObj(invite.content.invite, accept.content)) | |
94 … | + if(!ssbKeys.verifyObj(invite.content.invite, invite_key, accept.content)) | |
81 | 95 … | throw new Error('did not verify invite-acceptance contents') |
96 … | + //an ordinary message, so does not use hmac_key | |
82 | 97 … | if(!ssbKeys.verifyObj(accept.content.id, accept)) |
83 | 98 … | throw new Error('acceptance must be signed by claimed key') |
84 | 99 … | return reveal || true |
85 | 100 … | } |
Built with git-ssb-web