forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root_metadata_v2_test.go
1214 lines (1034 loc) · 34.1 KB
/
root_metadata_v2_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 (
"context"
"fmt"
"sort"
"testing"
"github.com/keybase/client/go/externals"
"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/keybase/kbfs/tlf"
"github.com/stretchr/testify/require"
)
func TestRootMetadataVersionV2(t *testing.T) {
tlfID := tlf.FakeID(1, tlf.Private)
// Metadata objects with unresolved assertions should have
// InitialExtraMetadataVer.
uid := keybase1.MakeTestUID(1)
bh, err := tlf.MakeHandle(
[]keybase1.UserOrTeamID{uid.AsUserOrTeam()}, nil, []keybase1.SocialAssertion{{}},
nil, nil)
require.NoError(t, err)
rmd, err := MakeInitialRootMetadataV2(tlfID, bh)
require.NoError(t, err)
require.Equal(t, InitialExtraMetadataVer, rmd.Version())
// All other folders should use PreExtraMetadataVer.
bh2, err := tlf.MakeHandle([]keybase1.UserOrTeamID{uid.AsUserOrTeam()}, nil, nil, nil, nil)
require.NoError(t, err)
rmd2, err := MakeInitialRootMetadata(
InitialExtraMetadataVer, tlfID, bh2)
require.NoError(t, err)
require.Equal(t, PreExtraMetadataVer, rmd2.Version())
// ... including if unresolved assertions get resolved.
rmd.SetUnresolvedWriters(nil)
require.Equal(t, PreExtraMetadataVer, rmd.Version())
}
func TestWriterMetadataV2Empty(t *testing.T) {
codec := kbfscodec.NewMsgpack()
wmd := WriterMetadataV2{
ID: tlf.FakeID(1, tlf.Public),
}
buf, err := codec.Encode(wmd)
require.NoError(t, err)
// Expected length derived by running with a known good
// codec. If the length is greater, something might be broken
// with omitempty with the Extra struct field.
require.Equal(t, 112, len(buf))
}
// Test that old encoded WriterMetadataV2 objects (i.e., without any
// extra fields) can be deserialized and serialized to the same form,
// which is important for RootMetadataV2.IsValidAndSigned().
func testWriterMetadataV2UnchangedEncoding(t *testing.T, ver MetadataVer) {
encodedWm := []byte{
0x89, 0xa3, 0x42, 0x49, 0x44, 0xc4, 0x10, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa9,
0x44, 0x69, 0x73, 0x6b, 0x55, 0x73, 0x61, 0x67,
0x65, 0x64, 0xa2, 0x49, 0x44, 0xc4, 0x10, 0x1,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x16, 0xb3,
0x4c, 0x61, 0x73, 0x74, 0x4d, 0x6f, 0x64, 0x69,
0x66, 0x79, 0x69, 0x6e, 0x67, 0x57, 0x72, 0x69,
0x74, 0x65, 0x72, 0xa4, 0x75, 0x69, 0x64, 0x31,
0xa8, 0x52, 0x65, 0x66, 0x42, 0x79, 0x74, 0x65,
0x73, 0x63, 0xaa, 0x55, 0x6e, 0x72, 0x65, 0x66,
0x42, 0x79, 0x74, 0x65, 0x73, 0x65, 0xa6, 0x57,
0x46, 0x6c, 0x61, 0x67, 0x73, 0xa, 0xa7, 0x57,
0x72, 0x69, 0x74, 0x65, 0x72, 0x73, 0x92, 0xa4,
0x75, 0x69, 0x64, 0x31, 0xa4, 0x75, 0x69, 0x64,
0x32, 0xa4, 0x64, 0x61, 0x74, 0x61, 0xc4, 0x2,
0xa, 0xb,
}
expectedWm := WriterMetadataV2{
SerializedPrivateMetadata: []byte{0xa, 0xb},
LastModifyingWriter: "uid1",
Writers: []keybase1.UserOrTeamID{"uid1", "uid2"},
ID: tlf.FakeID(1, tlf.Private),
BID: NullBranchID,
WFlags: 0xa,
DiskUsage: 100,
RefBytes: 99,
UnrefBytes: 101,
}
c := kbfscodec.NewMsgpack()
var wm WriterMetadataV2
err := c.Decode(encodedWm, &wm)
require.NoError(t, err)
require.Equal(t, expectedWm, wm)
buf, err := c.Encode(wm)
require.NoError(t, err)
require.Equal(t, encodedWm, buf)
}
func TestWriterMetadataV2UnchangedEncoding(t *testing.T) {
runTestOverMetadataVers(t, testWriterMetadataV2UnchangedEncoding)
}
// Test that WriterMetadataV2 has only a fixed (frozen) set of fields.
func TestWriterMetadataV2EncodedFields(t *testing.T) {
sa1, _ := externals.NormalizeSocialAssertion("uid1@twitter")
sa2, _ := externals.NormalizeSocialAssertion("uid2@twitter")
// Usually exactly one of Writers/WKeys is filled in, but we
// fill in both here for testing.
wm := WriterMetadataV2{
ID: tlf.FakeID(0xa, tlf.Private),
Writers: []keybase1.UserOrTeamID{"uid1", "uid2"},
WKeys: TLFWriterKeyGenerationsV2{{}},
Extra: WriterMetadataExtraV2{
UnresolvedWriters: []keybase1.SocialAssertion{sa1, sa2},
},
}
c := kbfscodec.NewMsgpack()
buf, err := c.Encode(wm)
require.NoError(t, err)
var m map[string]interface{}
err = c.Decode(buf, &m)
require.NoError(t, err)
expectedFields := []string{
"BID",
"DiskUsage",
"ID",
"LastModifyingWriter",
"RefBytes",
"UnrefBytes",
"WFlags",
"WKeys",
"Writers",
"data",
"x",
}
var fields []string
for field := range m {
fields = append(fields, field)
}
sort.Strings(fields)
require.Equal(t, expectedFields, fields)
}
type writerMetadataExtraV2Future struct {
WriterMetadataExtraV2
kbfscodec.Extra
}
func (wmef writerMetadataExtraV2Future) toCurrent() WriterMetadataExtraV2 {
return wmef.WriterMetadataExtraV2
}
type tlfWriterKeyGenerationsV2Future []*tlfWriterKeyBundleV2Future
func (wkgf tlfWriterKeyGenerationsV2Future) toCurrent() TLFWriterKeyGenerationsV2 {
wkg := make(TLFWriterKeyGenerationsV2, len(wkgf))
for i, wkbf := range wkgf {
wkb := wkbf.toCurrent()
wkg[i] = wkb
}
return wkg
}
type writerMetadataV2Future struct {
WriterMetadataV2
// Override WriterMetadata.WKeys.
WKeys tlfWriterKeyGenerationsV2Future
// Override WriterMetadata.Extra.
//
// TODO: Remove omitemptycheckstruct once we use a version of
// go-codec that supports omitempty for structs.
Extra writerMetadataExtraV2Future `codec:"x,omitempty,omitemptycheckstruct"`
}
func (wmf writerMetadataV2Future) toCurrent() WriterMetadataV2 {
wm := wmf.WriterMetadataV2
wm.WKeys = wmf.WKeys.toCurrent()
wm.Extra = wmf.Extra.toCurrent()
return wm
}
func (wmf writerMetadataV2Future) ToCurrentStruct() kbfscodec.CurrentStruct {
return wmf.toCurrent()
}
func makeFakeWriterMetadataV2Future(t *testing.T) writerMetadataV2Future {
wmd := WriterMetadataV2{
// This needs to be list format so it fails to compile if new fields
// are added, effectively checking at compile time whether new fields
// have been added
[]byte{0xa, 0xb},
"uid1",
[]keybase1.UserOrTeamID{"uid1", "uid2"},
nil,
tlf.FakeID(1, tlf.Private),
NullBranchID,
0xa,
100,
0,
99,
101,
0,
WriterMetadataExtraV2{},
}
wkb := makeFakeTLFWriterKeyBundleV2Future(t)
sa, _ := externals.NormalizeSocialAssertion("foo@twitter")
return writerMetadataV2Future{
wmd,
tlfWriterKeyGenerationsV2Future{&wkb},
writerMetadataExtraV2Future{
WriterMetadataExtraV2{
// This needs to be list format so it fails to compile if new
// fields are added, effectively checking at compile time
// whether new fields have been added
[]keybase1.SocialAssertion{sa},
codec.UnknownFieldSetHandler{},
},
kbfscodec.MakeExtraOrBust("WriterMetadata", t),
},
}
}
func TestWriterMetadataV2UnknownFields(t *testing.T) {
testStructUnknownFields(t, makeFakeWriterMetadataV2Future(t))
}
type tlfReaderKeyGenerationsV2Future []*tlfReaderKeyBundleV2Future
func (rkgf tlfReaderKeyGenerationsV2Future) toCurrent() TLFReaderKeyGenerationsV2 {
rkg := make(TLFReaderKeyGenerationsV2, len(rkgf))
for i, rkbf := range rkgf {
rkb := rkbf.toCurrent()
rkg[i] = rkb
}
return rkg
}
// rootMetadataWrapper exists only to add extra depth to fields
// in RootMetadata, so that they may be overridden in
// rootMetadataV2Future.
type rootMetadataWrapper struct {
RootMetadataV2
}
type rootMetadataV2Future struct {
// Override RootMetadata.WriterMetadata. Put it first to work
// around a bug in codec's field lookup code.
//
// TODO: Report and fix this bug upstream.
writerMetadataV2Future
rootMetadataWrapper
// Override RootMetadata.RKeys.
RKeys tlfReaderKeyGenerationsV2Future `codec:",omitempty"`
kbfscodec.Extra
}
func (brmf *rootMetadataV2Future) toCurrent() RootMetadata {
rm := brmf.rootMetadataWrapper.RootMetadataV2
rm.WriterMetadataV2 = WriterMetadataV2(brmf.writerMetadataV2Future.toCurrent())
rm.RKeys = brmf.RKeys.toCurrent()
return &rm
}
func (brmf *rootMetadataV2Future) ToCurrentStruct() kbfscodec.CurrentStruct {
return brmf.toCurrent()
}
func makeFakeRootMetadataV2Future(t *testing.T) *rootMetadataV2Future {
wmf := makeFakeWriterMetadataV2Future(t)
rkb := makeFakeTLFReaderKeyBundleV2Future(t)
sa, _ := externals.NormalizeSocialAssertion("bar@github")
rmf := rootMetadataV2Future{
wmf,
rootMetadataWrapper{
RootMetadataV2{
// This needs to be list format so it
// fails to compile if new fields are
// added, effectively checking at
// compile time whether new fields
// have been added
WriterMetadataV2{},
kbfscrypto.SignatureInfo{
Version: 100,
Signature: []byte{0xc},
VerifyingKey: kbfscrypto.MakeFakeVerifyingKeyOrBust("fake kid"),
},
"uid1",
0xb,
5,
FakeID(1),
nil,
[]keybase1.SocialAssertion{sa},
nil,
nil,
codec.UnknownFieldSetHandler{},
},
},
[]*tlfReaderKeyBundleV2Future{&rkb},
kbfscodec.MakeExtraOrBust("RootMetadata", t),
}
return &rmf
}
func TestRootMetadataV2UnknownFields(t *testing.T) {
testStructUnknownFields(t, makeFakeRootMetadataV2Future(t))
}
func TestIsValidRekeyRequestBasicV2(t *testing.T) {
tlfID := tlf.FakeID(1, tlf.Private)
uid := keybase1.MakeTestUID(1)
bh, err := tlf.MakeHandle(
[]keybase1.UserOrTeamID{uid.AsUserOrTeam()}, nil, nil, nil, nil)
require.NoError(t, err)
brmd, err := MakeInitialRootMetadataV2(tlfID, bh)
require.NoError(t, err)
ctx := context.Background()
codec := kbfscodec.NewMsgpack()
signer := kbfscrypto.SigningKeySigner{
Key: kbfscrypto.MakeFakeSigningKeyOrBust("key1"),
}
err = brmd.SignWriterMetadataInternally(ctx, codec, signer)
require.NoError(t, err)
newBrmd, err := MakeInitialRootMetadataV2(tlfID, bh)
require.NoError(t, err)
ok, err := newBrmd.IsValidRekeyRequest(
codec, brmd, newBrmd.LastModifyingWriter(), nil, nil)
require.NoError(t, err)
// Should fail because the copy bit is unset.
require.False(t, ok)
// Set the copy bit; note the writer metadata is the same.
newBrmd.SetWriterMetadataCopiedBit()
signer2 := kbfscrypto.SigningKeySigner{
Key: kbfscrypto.MakeFakeSigningKeyOrBust("key2"),
}
err = newBrmd.SignWriterMetadataInternally(ctx, codec, signer2)
require.NoError(t, err)
ok, err = newBrmd.IsValidRekeyRequest(
codec, brmd, newBrmd.LastModifyingWriter(), nil, nil)
require.NoError(t, err)
// Should fail because of mismatched writer metadata siginfo.
require.False(t, ok)
// Re-sign to get the same signature.
err = newBrmd.SignWriterMetadataInternally(ctx, codec, signer)
require.NoError(t, err)
ok, err = newBrmd.IsValidRekeyRequest(
codec, brmd, newBrmd.LastModifyingWriter(), nil, nil)
require.NoError(t, err)
require.True(t, ok)
}
func TestRevokeRemovedDevicesV2(t *testing.T) {
uid1 := keybase1.MakeTestUID(0x1)
uid2 := keybase1.MakeTestUID(0x2)
uid3 := keybase1.MakeTestUID(0x3)
key1 := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key1")
key2 := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key2")
key3 := kbfscrypto.MakeFakeCryptPublicKeyOrBust("key3")
half1a := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x1})
half1b := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x2})
half2a := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x3})
half2b := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x4})
half3a := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x5})
half3b := kbfscrypto.MakeTLFCryptKeyServerHalf([32]byte{0x6})
id1a, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid1, key1, half1a)
require.NoError(t, err)
id1b, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid1, key1, half1b)
require.NoError(t, err)
id2a, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid2, key2, half2a)
require.NoError(t, err)
id2b, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid2, key2, half2b)
require.NoError(t, err)
id3a, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid3, key3, half3a)
require.NoError(t, err)
id3b, err := kbfscrypto.MakeTLFCryptKeyServerHalfID(uid3, key3, half3b)
require.NoError(t, err)
tlfID := tlf.FakeID(1, tlf.Private)
bh, err := tlf.MakeHandle(
[]keybase1.UserOrTeamID{uid1.AsUserOrTeam(), uid2.AsUserOrTeam()},
[]keybase1.UserOrTeamID{uid3.AsUserOrTeam()}, nil, nil, nil)
require.NoError(t, err)
brmd, err := MakeInitialRootMetadataV2(tlfID, bh)
require.NoError(t, err)
brmd.WKeys = TLFWriterKeyGenerationsV2{
TLFWriterKeyBundleV2{
WKeys: UserDeviceKeyInfoMapV2{
uid1: DeviceKeyInfoMapV2{
key1.KID(): TLFCryptKeyInfo{
ServerHalfID: id1a,
EPubKeyIndex: 0,
},
},
uid2: DeviceKeyInfoMapV2{
key2.KID(): TLFCryptKeyInfo{
ServerHalfID: id2a,
EPubKeyIndex: 1,
},
},
},
},
TLFWriterKeyBundleV2{
WKeys: UserDeviceKeyInfoMapV2{
uid1: DeviceKeyInfoMapV2{
key1.KID(): TLFCryptKeyInfo{
ServerHalfID: id1b,
EPubKeyIndex: 0,
},
},
uid2: DeviceKeyInfoMapV2{
key2.KID(): TLFCryptKeyInfo{
ServerHalfID: id2b,
EPubKeyIndex: 0,
},
},
},
},
}
brmd.RKeys = TLFReaderKeyGenerationsV2{
TLFReaderKeyBundleV2{
RKeys: UserDeviceKeyInfoMapV2{
uid3: DeviceKeyInfoMapV2{
key3.KID(): TLFCryptKeyInfo{
ServerHalfID: id3a,
EPubKeyIndex: 0,
},
},
},
},
TLFReaderKeyBundleV2{
RKeys: UserDeviceKeyInfoMapV2{
uid3: DeviceKeyInfoMapV2{
key3.KID(): TLFCryptKeyInfo{
ServerHalfID: id3b,
EPubKeyIndex: 0,
},
},
},
},
}
updatedWriterKeys := UserDevicePublicKeys{
uid1: {key1: true},
}
updatedReaderKeys := UserDevicePublicKeys{
uid3: {key3: true},
}
removalInfo, err := brmd.RevokeRemovedDevices(
updatedWriterKeys, updatedReaderKeys, nil)
require.NoError(t, err)
require.Equal(t, ServerHalfRemovalInfo{
uid2: UserServerHalfRemovalInfo{
UserRemoved: true,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{
key2: []kbfscrypto.TLFCryptKeyServerHalfID{id2a, id2b},
},
},
}, removalInfo)
expectedWKeys := TLFWriterKeyGenerationsV2{
TLFWriterKeyBundleV2{
WKeys: UserDeviceKeyInfoMapV2{
uid1: DeviceKeyInfoMapV2{
key1.KID(): TLFCryptKeyInfo{
ServerHalfID: id1a,
EPubKeyIndex: 0,
},
},
},
},
TLFWriterKeyBundleV2{
WKeys: UserDeviceKeyInfoMapV2{
uid1: DeviceKeyInfoMapV2{
key1.KID(): TLFCryptKeyInfo{
ServerHalfID: id1b,
EPubKeyIndex: 0,
},
},
},
},
}
require.Equal(t, expectedWKeys, brmd.WKeys)
expectedRKeys := TLFReaderKeyGenerationsV2{
TLFReaderKeyBundleV2{
RKeys: UserDeviceKeyInfoMapV2{
uid3: DeviceKeyInfoMapV2{
key3.KID(): TLFCryptKeyInfo{
ServerHalfID: id3a,
EPubKeyIndex: 0,
},
},
},
},
TLFReaderKeyBundleV2{
RKeys: UserDeviceKeyInfoMapV2{
uid3: DeviceKeyInfoMapV2{
key3.KID(): TLFCryptKeyInfo{
ServerHalfID: id3b,
EPubKeyIndex: 0,
},
},
},
},
}
require.Equal(t, expectedRKeys, brmd.RKeys)
}
// TestRevokeLastDeviceV2 checks behavior of RevokeRemovedDevices with
// respect to removing the last device of a user vs. removing the user
// completely.
func TestRevokeLastDeviceV2(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)
tlfID := tlf.FakeID(1, tlf.Private)
bh, err := tlf.MakeHandle(
[]keybase1.UserOrTeamID{uid1.AsUserOrTeam(), uid2.AsUserOrTeam()},
[]keybase1.UserOrTeamID{uid3.AsUserOrTeam(), uid4.AsUserOrTeam()},
nil, nil, nil)
require.NoError(t, err)
brmd, err := MakeInitialRootMetadataV2(tlfID, bh)
require.NoError(t, err)
brmd.WKeys = TLFWriterKeyGenerationsV2{
TLFWriterKeyBundleV2{
WKeys: UserDeviceKeyInfoMapV2{
uid1: DeviceKeyInfoMapV2{
key1.KID(): TLFCryptKeyInfo{
ServerHalfID: id1,
EPubKeyIndex: 0,
},
},
uid2: DeviceKeyInfoMapV2{
key2.KID(): TLFCryptKeyInfo{
ServerHalfID: id2,
EPubKeyIndex: 1,
},
},
},
},
}
brmd.RKeys = TLFReaderKeyGenerationsV2{
TLFReaderKeyBundleV2{
RKeys: UserDeviceKeyInfoMapV2{
uid3: DeviceKeyInfoMapV2{},
uid4: DeviceKeyInfoMapV2{},
},
},
}
updatedWriterKeys := UserDevicePublicKeys{
uid1: {},
}
updatedReaderKeys := UserDevicePublicKeys{
uid3: {},
}
removalInfo, err := brmd.RevokeRemovedDevices(
updatedWriterKeys, updatedReaderKeys, nil)
require.NoError(t, err)
require.Equal(t, ServerHalfRemovalInfo{
uid1: UserServerHalfRemovalInfo{
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{
key1: []kbfscrypto.TLFCryptKeyServerHalfID{id1},
},
},
uid2: UserServerHalfRemovalInfo{
UserRemoved: true,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{
key2: []kbfscrypto.TLFCryptKeyServerHalfID{id2},
},
},
uid4: UserServerHalfRemovalInfo{
UserRemoved: true,
DeviceServerHalfIDs: DeviceServerHalfRemovalInfo{},
},
}, removalInfo)
expectedWKeys := TLFWriterKeyGenerationsV2{
TLFWriterKeyBundleV2{
WKeys: UserDeviceKeyInfoMapV2{
uid1: DeviceKeyInfoMapV2{},
},
},
}
require.Equal(t, expectedWKeys, brmd.WKeys)
expectedRKeys := TLFReaderKeyGenerationsV2{
TLFReaderKeyBundleV2{
RKeys: UserDeviceKeyInfoMapV2{
uid3: DeviceKeyInfoMapV2{},
},
},
}
require.Equal(t, expectedRKeys, brmd.RKeys)
}
type userDeviceSet UserDevicePublicKeys
// union returns the union of the user's keys in uds and other. For a
// particular user, it's assumed that that user's keys in uds and
// other are disjoint.
func (uds userDeviceSet) union(other userDeviceSet) userDeviceSet {
u := make(userDeviceSet)
for uid, keys := range uds {
u[uid] = make(DevicePublicKeys)
for key := range keys {
u[uid][key] = true
}
}
for uid, keys := range other {
if u[uid] == nil {
u[uid] = make(DevicePublicKeys)
}
for key := range keys {
if u[uid][key] {
panic(fmt.Sprintf(
"uid=%s key=%s exists in both",
uid, key))
}
u[uid][key] = true
}
}
return u
}
// userDevicePrivateKeys is a map from users to that user's set of
// device private keys.
type userDevicePrivateKeys map[keybase1.UID]map[kbfscrypto.CryptPrivateKey]bool
func (udpk userDevicePrivateKeys) hasKeys() bool {
for _, privKeys := range udpk {
if len(privKeys) > 0 {
return true
}
}
return false
}
func (udpk userDevicePrivateKeys) toPublicKeys() UserDevicePublicKeys {
pubKeys := make(UserDevicePublicKeys)
for uid, privKeys := range udpk {
pubKeys[uid] = make(DevicePublicKeys)
for privKey := range privKeys {
pubKey := privKey.GetPublicKey()
pubKeys[uid][pubKey] = true
}
}
return pubKeys
}
// expectedRekeyInfoV2 contains all the information needed to check a
// call to UpdateKeyBundles.
//
// If both writerPrivKeys and readerPrivKeys are empty, then
// ePubKeyIndex and ePubKey are ignored.
type expectedRekeyInfoV2 struct {
writerPrivKeys, readerPrivKeys userDevicePrivateKeys
serverHalves []UserDeviceKeyServerHalves
ePubKeyIndex int
ePubKey kbfscrypto.TLFEphemeralPublicKey
}
func checkCryptKeyInfo(t *testing.T, privKey kbfscrypto.CryptPrivateKey,
serverHalf kbfscrypto.TLFCryptKeyServerHalf, expectedEPubKeyIndex int,
expectedEPubKey kbfscrypto.TLFEphemeralPublicKey,
expectedTLFCryptKey kbfscrypto.TLFCryptKey, info TLFCryptKeyInfo,
ePubKey kbfscrypto.TLFEphemeralPublicKey) {
require.Equal(t, expectedEPubKeyIndex, info.EPubKeyIndex)
require.Equal(t, expectedEPubKey, ePubKey)
clientHalf, err := kbfscrypto.DecryptTLFCryptKeyClientHalf(
privKey, ePubKey, info.ClientHalf)
require.NoError(t, err)
tlfCryptKey := kbfscrypto.UnmaskTLFCryptKey(serverHalf, clientHalf)
require.Equal(t, expectedTLFCryptKey, tlfCryptKey)
}
// checkGetTLFCryptKeyV2 checks that wkb and rkb (for the given
// KeyGen) contain the info necessary to get the TLF crypt key for
// each user in expected, which must all match expectedTLFCryptKey.
func checkGetTLFCryptKeyV2(t *testing.T, keyGen KeyGen,
expected expectedRekeyInfoV2,
expectedTLFCryptKey kbfscrypto.TLFCryptKey,
wkb *TLFWriterKeyBundleV2, rkb *TLFReaderKeyBundleV2) {
expectedServerHalves := expected.serverHalves[keyGen-FirstValidKeyGen]
for uid, privKeys := range expected.writerPrivKeys {
for privKey := range privKeys {
pubKey := privKey.GetPublicKey()
serverHalf, ok := expectedServerHalves[uid][pubKey]
require.True(t, ok, "writer uid=%s, key=%s",
uid, pubKey)
info, ok := wkb.WKeys[uid][pubKey.KID()]
require.True(t, ok)
ePubKey := wkb.TLFEphemeralPublicKeys[info.EPubKeyIndex]
checkCryptKeyInfo(t, privKey, serverHalf,
expected.ePubKeyIndex, expected.ePubKey,
expectedTLFCryptKey, info, ePubKey)
}
}
for uid, privKeys := range expected.readerPrivKeys {
for privKey := range privKeys {
pubKey := privKey.GetPublicKey()
serverHalf, ok := expectedServerHalves[uid][pubKey]
require.True(t, ok, "reader uid=%s, key=%s",
uid, pubKey)
info, ok := rkb.RKeys[uid][pubKey.KID()]
require.True(t, ok)
_, _, ePubKey, err := GetEphemeralPublicKeyInfoV2(
info, *wkb, *rkb)
require.NoError(t, err)
checkCryptKeyInfo(t, privKey, serverHalf,
expected.ePubKeyIndex, expected.ePubKey,
expectedTLFCryptKey, info, ePubKey)
}
}
}
// accumulatePublicKeys returns the union of each user's keys in
// pubKeys1 and pubKeys2. A user's keys in pubKeys1 and pubKeys2 must
// be disjoint.
func accumulatePublicKeys(
pubKeys1, pubKeys2 UserDevicePublicKeys) UserDevicePublicKeys {
pubKeys := make(UserDevicePublicKeys)
for uid, keys := range pubKeys1 {
pubKeys[uid] = make(DevicePublicKeys)
for key := range keys {
pubKeys[uid][key] = true
}
}
for uid, keys := range pubKeys2 {
if pubKeys[uid] == nil {
pubKeys[uid] = make(DevicePublicKeys)
}
for key := range keys {
if pubKeys[uid][key] {
panic(fmt.Sprintf(
"uid=%s key=%s exists in both",
uid, key))
}
pubKeys[uid][key] = true
}
}
return pubKeys
}
// unionPublicKeyUsers returns the union of the usersin pubKeys1 and
// pubKeys2, which must be disjoint. Not a deep copy.
func unionPublicKeyUsers(
pubKeys1, pubKeys2 UserDevicePublicKeys) UserDevicePublicKeys {
pubKeys := make(UserDevicePublicKeys)
for uid, keys := range pubKeys1 {
pubKeys[uid] = keys
}
for uid, keys := range pubKeys2 {
if pubKeys[uid] != nil {
panic(fmt.Sprintf("uid=%s exists in both", uid))
}
pubKeys[uid] = keys
}
return pubKeys
}
func userDeviceKeyInfoMapV2ToPublicKeys(
udkimV2 UserDeviceKeyInfoMapV2) UserDevicePublicKeys {
pubKeys := make(UserDevicePublicKeys)
for uid, dkimV2 := range udkimV2 {
pubKeys[uid] = make(DevicePublicKeys)
for kid := range dkimV2 {
pubKeys[uid][kbfscrypto.MakeCryptPublicKey(kid)] = true
}
}
return pubKeys
}
func userDeviceServerHalvesToPublicKeys(
serverHalves UserDeviceKeyServerHalves) UserDevicePublicKeys {
pubKeys := make(UserDevicePublicKeys)
for uid, keys := range serverHalves {
pubKeys[uid] = make(DevicePublicKeys)
for key := range keys {
pubKeys[uid][key] = true
}
}
return pubKeys
}
// checkKeyBundlesV2 checks that rmd's key bundles contain exactly the
// info expected from expectedRekeyInfos and expectedPubKey.
func checkKeyBundlesV2(t *testing.T, expectedRekeyInfos []expectedRekeyInfoV2,
expectedTLFCryptKeys []kbfscrypto.TLFCryptKey,
expectedPubKeys []kbfscrypto.TLFPublicKey, rmd *RootMetadataV2) {
require.Equal(t, len(expectedTLFCryptKeys), len(expectedPubKeys))
require.Equal(t, len(expectedTLFCryptKeys),
int(rmd.LatestKeyGeneration()-FirstValidKeyGen+1))
for keyGen := FirstValidKeyGen; keyGen <= rmd.LatestKeyGeneration(); keyGen++ {
wkb, rkb, err := rmd.getTLFKeyBundles(keyGen)
require.NoError(t, err)
expectedWriterPubKeys := make(UserDevicePublicKeys)
expectedReaderPubKeys := make(UserDevicePublicKeys)
var expectedWriterEPublicKeys,
expectedReaderEPublicKeys kbfscrypto.TLFEphemeralPublicKeys
for _, expected := range expectedRekeyInfos {
expectedWriterPubKeys = accumulatePublicKeys(
expectedWriterPubKeys,
expected.writerPrivKeys.toPublicKeys())
expectedReaderPubKeys = accumulatePublicKeys(
expectedReaderPubKeys,
expected.readerPrivKeys.toPublicKeys())
if expected.writerPrivKeys.hasKeys() ||
expected.readerPrivKeys.hasKeys() {
if expected.ePubKeyIndex >= 0 {
require.Equal(t, expected.ePubKeyIndex,
len(expectedWriterEPublicKeys))
expectedWriterEPublicKeys = append(
expectedWriterEPublicKeys,
expected.ePubKey)
} else {
i := -1 - expected.ePubKeyIndex
require.Equal(t, i,
len(expectedReaderEPublicKeys))
expectedReaderEPublicKeys = append(
expectedReaderEPublicKeys,
expected.ePubKey)
}
}
}
writerPubKeys := userDeviceKeyInfoMapV2ToPublicKeys(wkb.WKeys)
readerPubKeys := userDeviceKeyInfoMapV2ToPublicKeys(rkb.RKeys)
require.Equal(t, expectedWriterPubKeys, writerPubKeys)
require.Equal(t, expectedReaderPubKeys, readerPubKeys)
require.Equal(t, expectedWriterEPublicKeys,
wkb.TLFEphemeralPublicKeys)
require.Equal(t, expectedReaderEPublicKeys,
rkb.TLFReaderEphemeralPublicKeys)
require.Equal(t, expectedPubKeys[keyGen-FirstValidKeyGen],
wkb.TLFPublicKey)
for _, expected := range expectedRekeyInfos {
require.Equal(t, len(expectedTLFCryptKeys),
len(expected.serverHalves))
expectedUserPubKeys := unionPublicKeyUsers(
expected.writerPrivKeys.toPublicKeys(),
expected.readerPrivKeys.toPublicKeys())
userPubKeys := userDeviceServerHalvesToPublicKeys(
expected.serverHalves[keyGen-FirstValidKeyGen])
require.Equal(t, expectedUserPubKeys.RemoveKeylessUsersForTest(), userPubKeys)
checkGetTLFCryptKeyV2(t, keyGen, expected,
expectedTLFCryptKeys[keyGen-FirstValidKeyGen],
wkb, rkb)
}
}
}
func TestRootMetadataV2UpdateKeyBundles(t *testing.T) {
uid1 := keybase1.MakeTestUID(1)
uid2 := keybase1.MakeTestUID(2)
uid3 := keybase1.MakeTestUID(3)
privKey1 := kbfscrypto.MakeFakeCryptPrivateKeyOrBust("key1")
privKey2 := kbfscrypto.MakeFakeCryptPrivateKeyOrBust("key2")
privKey3 := kbfscrypto.MakeFakeCryptPrivateKeyOrBust("key3")
updatedWriterKeys := UserDevicePublicKeys{
uid1: {privKey1.GetPublicKey(): true},
uid2: {privKey2.GetPublicKey(): true},
}
updatedReaderKeys := UserDevicePublicKeys{
uid3: {privKey3.GetPublicKey(): true},
}
tlfID := tlf.FakeID(1, tlf.Private)
bh, err := tlf.MakeHandle(
[]keybase1.UserOrTeamID{uid1.AsUserOrTeam(), uid2.AsUserOrTeam()},
[]keybase1.UserOrTeamID{uid3.AsUserOrTeam()},
[]keybase1.SocialAssertion{{}},
nil, nil)
require.NoError(t, err)
rmd, err := MakeInitialRootMetadataV2(tlfID, bh)
require.NoError(t, err)
codec := kbfscodec.NewMsgpack()
ePubKey1, ePrivKey1, err := kbfscrypto.MakeRandomTLFEphemeralKeys()
require.NoError(t, err)
// Add first key generations.
latestKeyGen := FirstValidKeyGen + 5
var pubKeys []kbfscrypto.TLFPublicKey
var tlfCryptKeys []kbfscrypto.TLFCryptKey
var serverHalves1 []UserDeviceKeyServerHalves
for keyGen := FirstValidKeyGen; keyGen <= latestKeyGen; keyGen++ {
fakeKeyData := [32]byte{byte(keyGen)}
pubKey := kbfscrypto.MakeTLFPublicKey(fakeKeyData)
tlfCryptKey := kbfscrypto.MakeTLFCryptKey(fakeKeyData)
// Use the same ephemeral keys for initial key
// generations, even though that can't happen in
// practice.
_, serverHalves1Gen, err := rmd.AddKeyGeneration(codec,
nil, updatedWriterKeys, updatedReaderKeys,
ePubKey1, ePrivKey1,
pubKey, kbfscrypto.TLFCryptKey{}, tlfCryptKey)
require.NoError(t, err)
serverHalves1 = append(serverHalves1, serverHalves1Gen)
pubKeys = append(pubKeys, pubKey)
tlfCryptKeys = append(tlfCryptKeys, tlfCryptKey)
}
expectedRekeyInfo1 := expectedRekeyInfoV2{
writerPrivKeys: userDevicePrivateKeys{
uid1: {privKey1: true},
uid2: {privKey2: true},
},
readerPrivKeys: userDevicePrivateKeys{
uid3: {privKey3: true},
},
serverHalves: serverHalves1,
ePubKeyIndex: 0,
ePubKey: ePubKey1,
}
expectedRekeyInfos := []expectedRekeyInfoV2{expectedRekeyInfo1}