Files: e106db19b5757a1b96390c4ef9bc5603e3075aa1 / index.js
2708 bytesRaw
1 | var ssbKeys = require('ssb-keys') |
2 | |
3 | var u = require('./util') |
4 | |
5 | var invite_key = require('./cap') |
6 | |
7 | exports.createInvite = function (seed, host, reveal, private) { |
8 | var keys = ssbKeys.generate(null, seed) //K |
9 | if(keys.id === host) |
10 | throw new Error('do not create invite with own public key') |
11 | return ssbKeys.signObj(keys, invite_key, { |
12 | type: 'invite', |
13 | invite: keys.id, |
14 | host: host, //sign our own key, to prove we created K |
15 | reveal: u.box(reveal, u.hash(u.hash(seed))), |
16 | private: u.box(private, u.hash(seed)) |
17 | }) |
18 | } |
19 | |
20 | exports.verifyInvitePublic = function (msg) { |
21 | if(!ssbKeys.verifyObj(msg.content.invite, invite_key, msg.content)) |
22 | throw new Error('invalid guest signature') |
23 | if(msg.content.host != msg.author) |
24 | throw new Error('host did not match author') |
25 | |
26 | //an ordinary message so doesn't use special hmac_key |
27 | if(!ssbKeys.verifyObj(msg.author, msg)) |
28 | throw new Error('invalid host signature') |
29 | return true |
30 | } |
31 | |
32 | exports.verifyInvitePrivate = function (msg, seed) { |
33 | exports.verifyInvitePublic(msg) |
34 | if(msg.content.reveal) { |
35 | var reveal = u.unbox(msg.content.reveal, u.hash(u.hash(seed))) |
36 | if(!reveal) throw new Error('could not decrypt message to be revealed') |
37 | } |
38 | if(msg.content.private) { |
39 | var private = u.unbox(msg.content.private, u.hash(seed)) |
40 | if(!reveal) throw new Error('could not decrypt private message') |
41 | } |
42 | |
43 | return {reveal: reveal, private: private} |
44 | } |
45 | |
46 | exports.createAccept = function (msg, seed, id) { |
47 | var keys = ssbKeys.generate(null, seed) //K |
48 | if(keys.id != msg.content.invite) throw new Error('seed does not match invite') |
49 | var inviteId = '%'+ssbKeys.hash(JSON.stringify(msg, null, 2)) |
50 | return ssbKeys.signObj(keys, invite_key, { |
51 | type: 'invite/accept', |
52 | reciept: inviteId, |
53 | id: id, |
54 | key: msg.content.reveal ? u.hash(u.hash(seed)).toString('base64') : undefined |
55 | }) |
56 | } |
57 | |
58 | exports.verifyAccept = function (accept, invite) { |
59 | var reveal |
60 | if('%'+ssbKeys.hash(JSON.stringify(invite, null, 2)) !== accept.content.reciept) |
61 | throw new Error('acceptance not matched to given invite') |
62 | if(accept.author === invite.content.id) |
63 | throw new Error('invitee must use a new key, not the same seed') |
64 | if(invite.content.reveal) { |
65 | reveal = u.unbox(invite.content.reveal, new Buffer(accept.content.key, 'base64')) |
66 | if(!reveal) throw new Error('accept did not correctly reveal invite') |
67 | } |
68 | |
69 | if(!ssbKeys.verifyObj(invite.content.invite, invite_key, accept.content)) |
70 | throw new Error('did not verify invite-acceptance contents') |
71 | //an ordinary message, so does not use hmac_key |
72 | if(!ssbKeys.verifyObj(accept.content.id, accept)) |
73 | throw new Error('acceptance must be signed by claimed key') |
74 | return reveal || true |
75 | } |
76 | |
77 | |
78 |
Built with git-ssb-web