forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_bundle_v3_test.go
331 lines (287 loc) · 8.85 KB
/
key_bundle_v3_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfsmd
import (
"testing"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-codec/codec"
"github.com/keybase/kbfs/kbfscodec"
"github.com/keybase/kbfs/kbfscrypto"
"github.com/stretchr/testify/require"
)
// Make sure creating an WKB ID for a WKB with no keys fails.
func TestWKBID(t *testing.T) {
codec := kbfscodec.NewMsgpack()
var wkb TLFWriterKeyBundleV3
_, err := MakeTLFWriterKeyBundleID(codec, wkb)
require.Error(t, err)
wkb.Keys = UserDeviceKeyInfoMapV3{
keybase1.UID(0): nil,
}
_, err = MakeTLFWriterKeyBundleID(codec, wkb)
require.NoError(t, err)
}
// Make sure that RKBs can be created with nil vs. empty keys get the
// same ID.
func TestRKBID(t *testing.T) {
codec := kbfscodec.NewMsgpack()
var wkb1, wkb2 TLFReaderKeyBundleV3
wkb2.Keys = make(UserDeviceKeyInfoMapV3)
id1, err := MakeTLFReaderKeyBundleID(codec, wkb1)
require.NoError(t, err)
id2, err := MakeTLFReaderKeyBundleID(codec, wkb2)
require.NoError(t, err)
require.Equal(t, id1, id2)
}
// TestRemoveDevicesNotInV3 checks basic functionality of
// removeDevicesNotIn().
func TestRemoveDevicesNotInV3(t *testing.T) {
uid1 := keybase1.MakeTestUID(0x1)
uid2 := keybase1.MakeTestUID(0x2)
uid3 := keybase1.MakeTestUID(0x3)
key1a := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key1")
key1b := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key2")
key2a := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key3")
key2b := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key4")
key2c := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key5")
key3a := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key6")
half1a := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x1})
half1b := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x2})
half2a := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x3})
half2b := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x4})
half2c := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x5})
half3a := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x6})
id1a, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid1, key1a, half1a)
require.NoError(t, err)
id1b, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid1, key1b, half1b)
require.NoError(t, err)
id2a, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid2, key2a, half2a)
require.NoError(t, err)
id2b, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid2, key2b, half2b)
require.NoError(t, err)
id2c, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid2, key2c, half2c)
require.NoError(t, err)
id3a, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid2, key3a, half3a)
require.NoError(t, err)
udkimV3 := UserDeviceKeyInfoMapV3{
uid1: DeviceKeyInfoMapV3{
key1a: TLFCryptKeyInfo{
ServerHalfID: id1a,
EPubKeyIndex: 1,
},
key1b: TLFCryptKeyInfo{
ServerHalfID: id1b,
EPubKeyIndex: 2,
},
},
uid2: DeviceKeyInfoMapV3{
key2a: TLFCryptKeyInfo{
ServerHalfID: id2a,
EPubKeyIndex: 2,
},
key2b: TLFCryptKeyInfo{
ServerHalfID: id2b,
EPubKeyIndex: 0,
},
key2c: TLFCryptKeyInfo{
ServerHalfID: id2c,
EPubKeyIndex: 0,
},
},
uid3: DeviceKeyInfoMapV3{
key3a: TLFCryptKeyInfo{
ServerHalfID: id3a,
EPubKeyIndex: 2,
},
},
}
removalInfo := udkimV3.RemoveDevicesNotIn(UserDevicePublicKeys{
uid2: {key2a: true, key2c: true},
uid3: {key3a: true},
})
require.Equal(t, UserDeviceKeyInfoMapV3{
uid2: DeviceKeyInfoMapV3{
key2a: TLFCryptKeyInfo{
ServerHalfID: id2a,
EPubKeyIndex: 2,
},
key2c: TLFCryptKeyInfo{
ServerHalfID: id2c,
EPubKeyIndex: 0,
},
},
uid3: DeviceKeyInfoMapV3{
key3a: TLFCryptKeyInfo{
ServerHalfID: id3a,
EPubKeyIndex: 2,
},
},
}, udkimV3)
require.Equal(t, ServerHalfRemovalInfo{
uid1: UserServerHalfRemovalInfo{
UserRemoved: true,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{
key1a: []kbfscrypto.TLFCryptKeyServerHalfID{id1a},
key1b: []kbfscrypto.TLFCryptKeyServerHalfID{id1b},
},
},
uid2: UserServerHalfRemovalInfo{
UserRemoved: false,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{
key2b: []kbfscrypto.TLFCryptKeyServerHalfID{id2b},
},
},
}, removalInfo)
}
// TestRemoveLastDeviceV3 checks behavior of removeDevicesNotIn() with
// respect to removing the last device of a user vs. removing the user
// completely.
//
// This is a regression test for KBFS-1898.
func TestRemoveLastDeviceV3(t *testing.T) {
uid1 := keybase1.MakeTestUID(0x1)
uid2 := keybase1.MakeTestUID(0x2)
uid3 := keybase1.MakeTestUID(0x3)
uid4 := keybase1.MakeTestUID(0x4)
key1 := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key1")
key2 := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key2")
half1 := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x1})
half2 := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x2})
id1, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid1, key1, half1)
require.NoError(t, err)
id2, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid2, key2, half2)
require.NoError(t, err)
udkimV3 := UserDeviceKeyInfoMapV3{
uid1: DeviceKeyInfoMapV3{
key1: TLFCryptKeyInfo{
ServerHalfID: id1,
EPubKeyIndex: 1,
},
},
uid2: DeviceKeyInfoMapV3{
key2: TLFCryptKeyInfo{
ServerHalfID: id2,
EPubKeyIndex: 2,
},
},
uid3: DeviceKeyInfoMapV3{},
uid4: DeviceKeyInfoMapV3{},
}
removalInfo := udkimV3.RemoveDevicesNotIn(UserDevicePublicKeys{
uid1: {},
uid3: {},
})
require.Equal(t, UserDeviceKeyInfoMapV3{
uid1: DeviceKeyInfoMapV3{},
uid3: DeviceKeyInfoMapV3{},
}, udkimV3)
require.Equal(t, ServerHalfRemovalInfo{
uid1: UserServerHalfRemovalInfo{
UserRemoved: false,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{
key1: []kbfscrypto.TLFCryptKeyServerHalfID{id1},
},
},
uid2: UserServerHalfRemovalInfo{
UserRemoved: true,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{
key2: []kbfscrypto.TLFCryptKeyServerHalfID{id2},
},
},
uid4: UserServerHalfRemovalInfo{
UserRemoved: true,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{},
},
}, removalInfo)
}
type deviceKeyInfoMapV3Future map[kbfscrypto.CryptPublicKey]tlfCryptKeyInfoFuture
func (dkimf deviceKeyInfoMapV3Future) toCurrent() DeviceKeyInfoMapV3 {
dkim := make(DeviceKeyInfoMapV3, len(dkimf))
for k, kif := range dkimf {
ki := kif.toCurrent()
dkim[k] = TLFCryptKeyInfo(ki)
}
return dkim
}
type userDeviceKeyInfoMapV3Future map[keybase1.UID]deviceKeyInfoMapV3Future
func (udkimf userDeviceKeyInfoMapV3Future) toCurrent() UserDeviceKeyInfoMapV3 {
udkim := make(UserDeviceKeyInfoMapV3)
for u, dkimf := range udkimf {
dkim := dkimf.toCurrent()
udkim[u] = dkim
}
return udkim
}
type tlfWriterKeyBundleV3Future struct {
TLFWriterKeyBundleV3
// Override TLFWriterKeyBundleV3.Keys.
Keys userDeviceKeyInfoMapV3Future `codec:"wKeys"`
kbfscodec.Extra
}
func (wkbf tlfWriterKeyBundleV3Future) toCurrent() TLFWriterKeyBundleV3 {
wkb := wkbf.TLFWriterKeyBundleV3
wkb.Keys = wkbf.Keys.toCurrent()
return wkb
}
func (wkbf tlfWriterKeyBundleV3Future) ToCurrentStruct() kbfscodec.CurrentStruct {
return wkbf.toCurrent()
}
func makeFakeDeviceKeyInfoMapV3Future(t *testing.T) userDeviceKeyInfoMapV3Future {
return userDeviceKeyInfoMapV3Future{
"fake uid": deviceKeyInfoMapV3Future{
kbfscrypto.MakeFakeCryptPublicKeyOrBust("fake key"): makeFakeTLFCryptKeyInfoFuture(t),
},
}
}
func makeFakeTLFWriterKeyBundleV3Future(t *testing.T) tlfWriterKeyBundleV3Future {
wkb := TLFWriterKeyBundleV3{
nil,
kbfscrypto.MakeTLFPublicKey([32]byte{0xa}),
kbfscrypto.TLFEphemeralPublicKeys{
kbfscrypto.MakeTLFEphemeralPublicKey([32]byte{0xb}),
},
kbfscrypto.EncryptedTLFCryptKeys{},
codec.UnknownFieldSetHandler{},
}
return tlfWriterKeyBundleV3Future{
wkb,
makeFakeDeviceKeyInfoMapV3Future(t),
kbfscodec.MakeExtraOrBust("TLFWriterKeyBundleV3", t),
}
}
func TestTLFWriterKeyBundleV3UnknownFields(t *testing.T) {
testStructUnknownFields(t, makeFakeTLFWriterKeyBundleV3Future(t))
}
type tlfReaderKeyBundleV3Future struct {
TLFReaderKeyBundleV3
// Override TLFReaderKeyBundleV3.Keys.
Keys userDeviceKeyInfoMapV3Future `codec:"rKeys"`
kbfscodec.Extra
}
func (rkbf tlfReaderKeyBundleV3Future) toCurrent() TLFReaderKeyBundleV3 {
rkb := rkbf.TLFReaderKeyBundleV3
rkb.Keys = rkbf.Keys.toCurrent()
return rkb
}
func (rkbf tlfReaderKeyBundleV3Future) ToCurrentStruct() kbfscodec.CurrentStruct {
return rkbf.toCurrent()
}
func makeFakeTLFReaderKeyBundleV3Future(
t *testing.T) tlfReaderKeyBundleV3Future {
rkb := TLFReaderKeyBundleV3{
nil,
kbfscrypto.TLFEphemeralPublicKeys{
kbfscrypto.MakeTLFEphemeralPublicKey([32]byte{0xc}),
},
codec.UnknownFieldSetHandler{},
}
return tlfReaderKeyBundleV3Future{
rkb,
makeFakeDeviceKeyInfoMapV3Future(t),
kbfscodec.MakeExtraOrBust("TLFReaderKeyBundleV3", t),
}
}
func TestTLFReaderKeyBundleV3UnknownFields(t *testing.T) {
testStructUnknownFields(t, makeFakeTLFReaderKeyBundleV3Future(t))
}