Files: 31ba9f2ced690940575b567c8e7a790e97e21ec3 / test / invalid.js
5802 bytesRaw
1 | var tape = require('tape') |
2 | var ssbKeys = require('ssb-keys') |
3 | var v = require('ssb-validate') |
4 | var i = require('../valid') |
5 | var u = require('../util') |
6 | var crypto = require('crypto') |
7 | var caps = { |
8 | sign: crypto.randomBytes(32),//.toString('base64'), |
9 | peerInvite: crypto.randomBytes(32),//.toString('base64'), |
10 | shs: crypto.randomBytes(32),//.toString('base64'), |
11 | } |
12 | |
13 | var invite_key = require('../cap') |
14 | |
15 | var hash = u.hash |
16 | |
17 | var alice = ssbKeys.generate(null, hash('ALICE')) |
18 | var bob = ssbKeys.generate(null, hash('BOB')) |
19 | |
20 | |
21 | function throws(t, test, code) { |
22 | if(!code) throw new Error('error code must be provided') |
23 | try { |
24 | test() |
25 | t.fail('expected:'+test+' to throw code:'+code) |
26 | } catch(err) { |
27 | console.error(err.stack) |
28 | t.ok(err.code, 'errors must have an error code') |
29 | t.equal(err.code, code) |
30 | } |
31 | } |
32 | |
33 | //any bit in invite{invite,host,reveal} is flipped |
34 | tape('invalid - wrong invitee', function (t) { |
35 | |
36 | //construct a message where host does not match |
37 | var seed = hash('seed2') |
38 | var keys = ssbKeys.generate(null, seed) |
39 | var invalid = ssbKeys.signObj(keys, caps.peerInvite, { |
40 | type: 'peer-invite', |
41 | invite: ssbKeys.generate(null, hash('seed3')), |
42 | host: alice.id |
43 | }) |
44 | |
45 | var msg = v.create(null, alice, caps.sign, invalid, new Date('2018-03-26T06:14:18.377Z')) |
46 | |
47 | throws(t, function () { |
48 | i.verifyInvitePublic(msg, caps) |
49 | }, 'peer-invites:invite-signature-failed') |
50 | |
51 | throws(t, function () { |
52 | i.verifyInvitePrivate(msg, seed, caps) |
53 | }, 'peer-invites:invite-signature-failed') |
54 | |
55 | t.end() |
56 | }) |
57 | |
58 | //any bit in invite{invite,host,reveal} is flipped |
59 | tape('invalid - wrong invitee', function (t) { |
60 | |
61 | //construct a message where host does not match |
62 | var seed = hash('seed2') |
63 | var keys = ssbKeys.generate(null, seed) |
64 | var wrong_seed = hash('wrong_seed') |
65 | var invalid = ssbKeys.signObj(keys, caps.peerInvite, { |
66 | type: 'peer-invite', |
67 | invite: keys.id, //correct key |
68 | reveal: u.box({hidden: true}, u.hash(u.hash(wrong_seed))), |
69 | host: alice.id |
70 | }) |
71 | var invite_msg = v.create(null, alice, caps.sign, invalid, new Date('2018-03-26T06:14:18.377Z')) |
72 | |
73 | t.ok(i.verifyInvitePublic(invite_msg, caps)) |
74 | |
75 | throws(t, function () { |
76 | i.verifyInvitePrivate(invite_msg, seed, caps) |
77 | }, 'peer-invites:decrypt-reveal-failed') |
78 | |
79 | //say if the invitee creates a accept message anyway. |
80 | |
81 | throws(t, function () { |
82 | i.createAccept(invite_msg, wrong_seed, bob.id, caps) |
83 | }, 'peer-invites:seed-must-match-invite') |
84 | |
85 | |
86 | throws(t, function () { |
87 | i.createAccept(invite_msg, seed, bob.id, caps) |
88 | }, 'peer-invites:decrypt-reveal-failed') |
89 | |
90 | var accept = ssbKeys.signObj(ssbKeys.generate(null, seed), caps.peerInvite, { |
91 | type: 'peer-invite/accept', |
92 | receipt: '%'+ssbKeys.hash(JSON.stringify(invite_msg, null, 2)), |
93 | id: bob.id, |
94 | key: u.hash(u.hash(seed)) //what the reveal key should be. |
95 | }) |
96 | |
97 | var accept_msg = |
98 | v.create(null, bob, caps.sign, accept, new Date('2018-03-26T06:14:18.377Z')) |
99 | |
100 | throws(t, function () { |
101 | i.verifyAccept(accept_msg, invite_msg, caps) |
102 | }, 'peer-invites:decrypt-accept-reveal-failed') |
103 | |
104 | var accept2 = ssbKeys.signObj(ssbKeys.generate(null, seed), caps.peerInvite, { |
105 | type: 'peer-invite/accept', |
106 | receipt: '%'+ssbKeys.hash(JSON.stringify(invite_msg, null, 2)), |
107 | id: bob.id, |
108 | key: u.hash('not the key') //what the reveal key should be. |
109 | }) |
110 | |
111 | throws(t, function () { |
112 | i.verifyAccept(accept_msg, invite_msg, caps) |
113 | }, 'peer-invites:decrypt-accept-reveal-failed') |
114 | |
115 | t.end() |
116 | }) |
117 | |
118 | tape('wrong invite', function (t) { |
119 | var seed = hash('seed1') |
120 | |
121 | var invite1 = v.create(null, alice, caps.sign, i.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}, caps), new Date('2018-03-14T06:14:18.377Z')) |
122 | |
123 | t.deepEqual({ |
124 | reveal: {name: 'bob'}, |
125 | private: {text: 'welcome to ssb!'} |
126 | }, i.verifyInvitePrivate(invite1, seed, caps)) |
127 | |
128 | |
129 | var accept_content = i.createAccept(invite1, seed, bob.id, caps) |
130 | var accept = v.create(null, bob, caps.sign, accept_content, new Date('2018-03-14T06:32:18.377Z')) |
131 | |
132 | var seed2 = hash('seed2') |
133 | var invite2 = v.create(null, alice, caps.sign, i.createInvite(seed2, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}, caps), new Date('2018-03-14T06:14:18.377Z')) |
134 | |
135 | //just test we do not verify the incorrect invite |
136 | throws(t, function () { |
137 | i.verifyAccept(accept, invite2, caps) |
138 | }, 'peer-invites:accept-wrong-invite') |
139 | |
140 | t.end() |
141 | |
142 | }) |
143 | |
144 | tape('wrong invite', function (t) { |
145 | var seed = hash('seed1') |
146 | |
147 | var invite = v.create(null, alice, caps.sign, i.createInvite(seed, alice.id, null, null, caps), new Date('2018-03-14T06:14:18.377Z')) |
148 | var seed2 = hash('seed2') |
149 | var accept_content = ssbKeys.signObj(ssbKeys.generate(null, seed2), caps.peerInvite, { |
150 | type: 'peer-invite/accept', |
151 | receipt: '%'+ssbKeys.hash(JSON.stringify(invite, null, 2)), |
152 | id: bob.id, |
153 | }) |
154 | var accept2 = v.create(null, bob, caps.sign, accept_content, new Date('2018-03-14T06:32:18.377Z')) |
155 | |
156 | |
157 | //just test we do not verify the incorrect invite |
158 | throws(t, function () { |
159 | i.verifyAccept(accept2, invite, caps) |
160 | }, 'peer-invites:accept-invite-signature-failed') |
161 | |
162 | t.end() |
163 | }) |
164 | |
165 | |
166 | tape('wrong invite', function (t) { |
167 | var seed = hash('seed1') |
168 | |
169 | var invite = v.create(null, alice, caps.sign, i.createInvite(seed, alice.id, 'REVEAL', null, caps), new Date('2018-03-14T06:14:18.377Z')) |
170 | var accept_content = ssbKeys.signObj(ssbKeys.generate(null, seed), caps.peerInvite, { |
171 | type: 'peer-invite/accept', |
172 | receipt: '%'+ssbKeys.hash(JSON.stringify(invite, null, 2)), |
173 | id: bob.id, |
174 | //key is missing! |
175 | }) |
176 | var accept2 = v.create(null, bob, caps.sign, accept_content, new Date('2018-03-14T06:32:18.377Z')) |
177 | |
178 | |
179 | //just test we do not verify the incorrect invite |
180 | throws(t, function () { |
181 | i.verifyAccept(accept2, invite, caps) |
182 | }, 'peer-invites:accept-must-reveal-key') |
183 | |
184 | t.end() |
185 | }) |
186 | |
187 | |
188 | |
189 |
Built with git-ssb-web