forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader2.go
810 lines (684 loc) · 26.1 KB
/
loader2.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
package teams
import (
"context"
"fmt"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
// loader2.go contains methods on TeamLoader.
// It would be normal for them to be in loader.go but:
// These functions do not call any functions in loader.go except for load2.
// They are here so that the files can be more manageable in size and
// people can work on loader.go and loader2.go simultaneously with less conflict.
// If links are needed in full that are stubbed in state, go out and get them from the server.
// Does not ask for any links above state's seqno, those will be fetched by getNewLinksFromServer.
func (l *TeamLoader) fillInStubbedLinks(ctx context.Context,
me keybase1.UserVersion, teamID keybase1.TeamID, state *keybase1.TeamData,
needSeqnos []keybase1.Seqno, readSubteamID keybase1.TeamID,
proofSet *proofSetT, parentChildOperations []*parentChildOperation) (
*keybase1.TeamData, *proofSetT, []*parentChildOperation, error) {
upperLimit := keybase1.Seqno(0)
if state != nil {
upperLimit = state.Chain.LastSeqno
}
// seqnos needed from the server
var requestSeqnos []keybase1.Seqno
for _, seqno := range needSeqnos {
linkIsAlreadyFilled := TeamSigChainState{inner: state.Chain}.IsLinkFullyPresent(seqno)
if seqno <= upperLimit && !linkIsAlreadyFilled {
requestSeqnos = append(requestSeqnos, seqno)
}
}
if len(requestSeqnos) == 0 {
// early out
return state, proofSet, parentChildOperations, nil
}
teamUpdate, err := l.world.getLinksFromServer(ctx, state.Chain.Id, requestSeqnos, &readSubteamID)
if err != nil {
return state, proofSet, parentChildOperations, err
}
newLinks, err := l.unpackLinks(ctx, teamUpdate)
if err != nil {
return state, proofSet, parentChildOperations, err
}
for _, link := range newLinks {
if link.isStubbed() {
return state, proofSet, parentChildOperations, NewStubbedErrorWithNote(
link, "filling stubbed link")
}
var signer *signerX
signer, proofSet, err = l.verifyLink(ctx, teamID, state, me, link, readSubteamID, proofSet)
if err != nil {
return state, proofSet, parentChildOperations, err
}
if signer == nil || !signer.signer.Uid.Exists() {
return state, proofSet, parentChildOperations, fmt.Errorf("blank signer for full link: %v", signer)
}
state, err = l.inflateLink(ctx, state, link, *signer, me)
if err != nil {
return state, proofSet, parentChildOperations, err
}
if l.isParentChildOperation(ctx, link) {
pco, err := l.toParentChildOperation(ctx, link)
if err != nil {
return state, proofSet, parentChildOperations, err
}
parentChildOperations = append(parentChildOperations, pco)
}
}
return state, proofSet, parentChildOperations, nil
}
type getLinksLows struct {
// Latest seqno on file
Seqno keybase1.Seqno
// Latest PTK generation on file
PerTeamKey keybase1.PerTeamKeyGeneration
// Latest RKM generation on file
ReaderKeyMask keybase1.PerTeamKeyGeneration
}
// checkStubbed checks if it's OK that a link is stubbed.
func (l *TeamLoader) checkStubbed(ctx context.Context, arg load2ArgT, link *chainLinkUnpacked) error {
if !link.isStubbed() {
return nil
}
if l.seqnosContains(arg.needSeqnos, link.Seqno()) {
return NewStubbedErrorWithNote(link, "Need seqno")
}
if arg.needAdmin || !link.outerLink.LinkType.TeamAllowStubWithAdminFlag(arg.needAdmin) {
return NewStubbedErrorWithNote(link, "Need admin privilege for this action")
}
return nil
}
func (l *TeamLoader) loadUserAndKeyFromLinkInner(ctx context.Context,
inner SCChainLinkPayload) (
signerUV keybase1.UserVersion, key *keybase1.PublicKeyV2NaCl, linkMap map[keybase1.Seqno]keybase1.LinkID, err error) {
defer l.G().CTrace(ctx, fmt.Sprintf("TeamLoader#loadUserForSigVerification(%d)", int(inner.Seqno)), func() error { return err })()
keySection := inner.Body.Key
if keySection == nil {
return signerUV, nil, nil, libkb.NoUIDError{}
}
uid := keySection.UID
kid := keySection.KID
signerUV, key, linkMap, err = l.world.loadKeyV2(ctx, uid, kid)
if err != nil {
return signerUV, nil, nil, err
}
return signerUV, key, linkMap, nil
}
func (l *TeamLoader) verifySignatureAndExtractKID(ctx context.Context, outer libkb.OuterLinkV2WithMetadata) (keybase1.KID, error) {
return outer.Verify(l.G().Log)
}
func addProofsForKeyInUserSigchain(teamID keybase1.TeamID, teamLinkMap map[keybase1.Seqno]keybase1.LinkID, link *chainLinkUnpacked, uid keybase1.UID, key *keybase1.PublicKeyV2NaCl, userLinkMap map[keybase1.Seqno]keybase1.LinkID, proofSet *proofSetT) *proofSetT {
a := newProofTerm(uid.AsUserOrTeam(), key.Base.Provisioning, userLinkMap)
b := newProofTerm(teamID.AsUserOrTeam(), link.SignatureMetadata(), teamLinkMap)
c := key.Base.Revocation
proofSet = proofSet.AddNeededHappensBeforeProof(a, b, "user key provisioned before team link")
if c != nil {
proofSet = proofSet.AddNeededHappensBeforeProof(b, newProofTerm(uid.AsUserOrTeam(), *c, userLinkMap), "team link before user key revocation")
}
return proofSet
}
// Verify aspects of a link:
// - Signature must match the outer link
// - Signature must match the inner link if not stubbed
// - Was signed by a key valid for the user at the time of signing
// - Was signed by a user with permissions to make the link at the time of signing
// - Checks outer-inner match
// Some checks are deferred as entries in the returned proofSet
// Does not:
// - Apply the link nor modify state
// - Check the rest of the format of the inner link
// Returns the signer, or nil if the link was stubbed
func (l *TeamLoader) verifyLink(ctx context.Context,
teamID keybase1.TeamID, state *keybase1.TeamData, me keybase1.UserVersion, link *chainLinkUnpacked,
readSubteamID keybase1.TeamID, proofSet *proofSetT) (*signerX, *proofSetT, error) {
if link.isStubbed() {
return nil, proofSet, nil
}
err := link.AssertInnerOuterMatch()
if err != nil {
return nil, proofSet, err
}
if !teamID.Eq(link.innerTeamID) {
return nil, proofSet, fmt.Errorf("team ID mismatch: %s != %s", teamID, link.innerTeamID)
}
kid, err := l.verifySignatureAndExtractKID(ctx, *link.outerLink)
if err != nil {
return nil, proofSet, err
}
signerUV, key, linkMap, err := l.loadUserAndKeyFromLinkInner(ctx, *link.inner)
if err != nil {
return nil, proofSet, err
}
if !kid.Equal(key.Base.Kid) {
return nil, proofSet, libkb.NewWrongKidError(kid, key.Base.Kid)
}
var teamLinkMap map[keybase1.Seqno]keybase1.LinkID
if state != nil {
teamLinkMap = state.Chain.LinkIDs
}
proofSet = addProofsForKeyInUserSigchain(teamID, teamLinkMap, link, signerUV.Uid, key, linkMap, proofSet)
signer := signerX{signer: signerUV}
// For a root team link, or a subteam_head, there is no reason to check adminship
// or writership (or readership) for the team.
if state == nil {
return &signer, proofSet, nil
}
var isReaderOrAbove bool
if !link.outerLink.LinkType.RequiresAdminPermission() {
err = l.verifyWriterOrReaderPermissions(ctx, state, link, signerUV)
isReaderOrAbove = (err == nil)
}
if link.outerLink.LinkType.RequiresAdminPermission() || !isReaderOrAbove {
// Check for admin permissions if they are not an on-chain reader/writer
// because they might be an implicit admin.
// Reassigns signer, might set implicitAdmin.
proofSet, signer, err = l.verifyAdminPermissions(ctx, state, me, link, readSubteamID, signerUV, proofSet)
}
return &signer, proofSet, err
}
func (l *TeamLoader) verifyWriterOrReaderPermissions(ctx context.Context,
state *keybase1.TeamData, link *chainLinkUnpacked, uv keybase1.UserVersion) error {
return (TeamSigChainState{state.Chain}).AssertWasReaderAt(uv, link.SigChainLocation())
}
// Does not return a full TeamData because it might get a subteam-reader version.
func (l *TeamLoader) walkUpToAdmin(
ctx context.Context, team *keybase1.TeamData, me keybase1.UserVersion, readSubteamID keybase1.TeamID,
uv keybase1.UserVersion, admin SCTeamAdmin) (*TeamSigChainState, error) {
target, err := admin.TeamID.ToTeamID()
if err != nil {
return nil, err
}
for team != nil && !team.Chain.Id.Eq(target) {
parent := team.Chain.ParentID
if parent == nil {
return nil, NewAdminNotFoundError(admin)
}
arg := load2ArgT{
teamID: *parent,
me: me,
staleOK: true,
readSubteamID: &readSubteamID,
}
if target.Eq(*parent) {
arg.needSeqnos = []keybase1.Seqno{admin.Seqno}
}
team, err = l.load2(ctx, arg)
if err != nil {
return nil, err
}
}
if team == nil {
return nil, fmt.Errorf("teamloader fault: nil team after admin walk")
}
return &TeamSigChainState{inner: team.Chain}, nil
}
func addProofsForAdminPermission(t keybase1.TeamSigChainState, link *chainLinkUnpacked, bookends proofTermBookends, proofSet *proofSetT) *proofSetT {
a := bookends.left
b := newProofTerm(t.Id.AsUserOrTeam(), link.SignatureMetadata(), t.LinkIDs)
c := bookends.right
proofSet = proofSet.AddNeededHappensBeforeProof(a, b, "became admin before team link")
if c != nil {
proofSet = proofSet.AddNeededHappensBeforeProof(b, *c, "team link before adminship demotion")
}
return proofSet
}
func (l *TeamLoader) verifyAdminPermissions(ctx context.Context,
state *keybase1.TeamData, me keybase1.UserVersion, link *chainLinkUnpacked, readSubteamID keybase1.TeamID,
uv keybase1.UserVersion, proofSet *proofSetT) (*proofSetT, signerX, error) {
signer := signerX{signer: uv}
explicitAdmin := link.inner.TeamAdmin()
teamChain := TeamSigChainState{inner: state.Chain}
// In the simple case, we don't ask for explicit adminship, so we have to be admins of
// the current chain at or before the signature in question.
if explicitAdmin == nil {
err := teamChain.AssertWasAdminAt(uv, link.SigChainLocation())
return proofSet, signer, err
}
// The more complicated case is that there's an explicit admin permssion given, perhaps
// of a parent team.
adminTeam, err := l.walkUpToAdmin(ctx, state, me, readSubteamID, uv, *explicitAdmin)
if err != nil {
return proofSet, signer, err
}
adminBookends, err := adminTeam.assertBecameAdminAt(uv, explicitAdmin.SigChainLocation())
if err != nil {
return proofSet, signer, err
}
// This was an implicit admin action if the team from which admin-power was derived (adminTeam)
// is not the link's team (state).
if !adminTeam.GetID().Eq(teamChain.GetID()) {
signer.implicitAdmin = true
}
proofSet = addProofsForAdminPermission(state.Chain, link, adminBookends, proofSet)
return proofSet, signer, nil
}
// Whether the chain link is of a (child-half) type
// that affects a parent and child chain in lockstep.
// So far these events: subteam create, and subteam rename
func (l *TeamLoader) isParentChildOperation(ctx context.Context,
link *chainLinkUnpacked) bool {
switch link.LinkType() {
case libkb.SigchainV2TypeTeamSubteamHead, libkb.SigchainV2TypeTeamRenameUpPointer:
return true
default:
return false
}
}
func (l *TeamLoader) toParentChildOperation(ctx context.Context,
link *chainLinkUnpacked) (*parentChildOperation, error) {
if !l.isParentChildOperation(ctx, link) {
return nil, fmt.Errorf("link is not a parent-child operation: (seqno:%v, type:%v)",
link.Seqno(), link.LinkType())
}
if link.isStubbed() {
return nil, fmt.Errorf("child half of parent-child operation cannot be stubbed: (seqno:%v, type:%v)",
link.Seqno(), link.LinkType())
}
switch link.LinkType() {
case libkb.SigchainV2TypeTeamSubteamHead, libkb.SigchainV2TypeTeamRenameUpPointer:
if link.inner.Body.Team == nil {
return nil, fmt.Errorf("bad parent-child operation missing team section: (seqno:%v, type:%v)",
link.Seqno(), link.LinkType())
}
if link.inner.Body.Team.Parent == nil {
return nil, fmt.Errorf("parent-child operation missing team parent: (seqno:%v, type:%v)",
link.Seqno(), link.LinkType())
}
parentSeqno := link.inner.Body.Team.Parent.Seqno
if parentSeqno < 1 {
return nil, fmt.Errorf("bad parent-child up seqno: %v", parentSeqno)
}
if link.inner.Body.Team.Name == nil {
return nil, fmt.Errorf("parent-child operation %v missing new name", link.LinkType())
}
newName, err := keybase1.TeamNameFromString((string)(*link.inner.Body.Team.Name))
if err != nil {
return nil, fmt.Errorf("parent-child operation %v has invalid new name: %v",
link.LinkType(), *link.inner.Body.Team.Name)
}
return &parentChildOperation{
parentSeqno: parentSeqno,
linkType: link.LinkType(),
newName: newName,
}, nil
default:
return nil, fmt.Errorf("unsupported parent-child operation: %v", link.LinkType())
}
}
// Apply a new link to the sigchain state.
// `signer` may be nil iff link is stubbed.
func (l *TeamLoader) applyNewLink(ctx context.Context,
state *keybase1.TeamData, link *chainLinkUnpacked,
signer *signerX, me keybase1.UserVersion) (*keybase1.TeamData, error) {
l.G().Log.CDebugf(ctx, "TeamLoader applying link seqno:%v", link.Seqno())
var player *TeamSigChainPlayer
if state == nil {
player = NewTeamSigChainPlayer(l.G(), me)
} else {
player = NewTeamSigChainPlayerWithState(l.G(), me, TeamSigChainState{inner: state.Chain})
}
err := player.AppendChainLink(ctx, link, signer)
if err != nil {
return nil, err
}
newChainState, err := player.GetState()
if err != nil {
return nil, err
}
var newState *keybase1.TeamData
if state == nil {
newState = &keybase1.TeamData{
// Name is left blank until calculateName updates it.
// It shall not be blank by the time it is returned from load2.
Name: keybase1.TeamName{},
Chain: newChainState.inner,
PerTeamKeySeeds: make(map[keybase1.PerTeamKeyGeneration]keybase1.PerTeamKeySeedItem),
ReaderKeyMasks: make(map[keybase1.TeamApplication]map[keybase1.PerTeamKeyGeneration]keybase1.MaskB64),
}
} else {
newState2 := state.DeepCopy()
newState2.Chain = newChainState.inner
newState = &newState2
}
return newState, nil
}
// Inflate a link that was stubbed with its non-stubbed data.
func (l *TeamLoader) inflateLink(ctx context.Context,
state *keybase1.TeamData, link *chainLinkUnpacked,
signer signerX, me keybase1.UserVersion) (
*keybase1.TeamData, error) {
l.G().Log.CDebugf(ctx, "TeamLoader inflating link seqno:%v", link.Seqno())
if state == nil {
// The only reason state would be nil is if this is link 1.
// But link 1 can't be stubbed.
return nil, NewInflateErrorWithNote(link, "no prior state")
}
player := NewTeamSigChainPlayerWithState(l.G(), me, TeamSigChainState{inner: state.Chain})
err := player.InflateLink(link, signer)
if err != nil {
return nil, err
}
newChainState, err := player.GetState()
if err != nil {
return nil, err
}
newState := state.DeepCopy()
newState.Chain = newChainState.inner
return &newState, nil
}
// Check that the parent-child operations appear in the parent sigchains.
func (l *TeamLoader) checkParentChildOperations(ctx context.Context,
me keybase1.UserVersion, loadingTeamID keybase1.TeamID, parentID *keybase1.TeamID, readSubteamID keybase1.TeamID,
parentChildOperations []*parentChildOperation) error {
if len(parentChildOperations) == 0 {
return nil
}
if parentID == nil {
return fmt.Errorf("cannot check parent-child operations with no parent")
}
var needParentSeqnos []keybase1.Seqno
for _, pco := range parentChildOperations {
needParentSeqnos = append(needParentSeqnos, pco.parentSeqno)
}
parent, err := l.load2(ctx, load2ArgT{
teamID: *parentID,
needAdmin: false,
needKeyGeneration: 0,
wantMembers: nil,
wantMembersRole: keybase1.TeamRole_NONE,
forceFullReload: false,
forceRepoll: false,
staleOK: true, // stale is fine, as long as get those seqnos.
needSeqnos: needParentSeqnos,
readSubteamID: &readSubteamID,
me: me,
})
if err != nil {
return fmt.Errorf("error loading parent: %v", err)
}
for _, pco := range parentChildOperations {
parentChain := TeamSigChainState{inner: parent.Chain}
err = l.checkOneParentChildOperation(ctx, pco, loadingTeamID, &parentChain)
if err != nil {
return err
}
}
return nil
}
func (l *TeamLoader) checkOneParentChildOperation(ctx context.Context,
pco *parentChildOperation, teamID keybase1.TeamID, parent *TeamSigChainState) error {
switch pco.linkType {
case libkb.SigchainV2TypeTeamSubteamHead:
return parent.SubteamRenameOccurred(teamID, pco.newName, pco.parentSeqno)
case libkb.SigchainV2TypeTeamRenameUpPointer:
return parent.SubteamRenameOccurred(teamID, pco.newName, pco.parentSeqno)
}
return fmt.Errorf("unrecognized parent-child operation could not be checked: %v", pco.linkType)
}
// Check all the proofs and ordering constraints in proofSet
func (l *TeamLoader) checkProofs(ctx context.Context,
state *keybase1.TeamData, proofSet *proofSetT) error {
return proofSet.check(ctx, l.G(), l.world)
}
// Add data to the state that is not included in the sigchain:
// - per team keys
// - reader key masks
// Checks that the team keys match the published values on the chain.
// Checks that the off-chain data ends up exactly in sync with the chain, generation-wise.
func (l *TeamLoader) addSecrets(ctx context.Context,
state *keybase1.TeamData, me keybase1.UserVersion, box *TeamBox, prevs map[keybase1.PerTeamKeyGeneration]prevKeySealedEncoded,
readerKeyMasks []keybase1.ReaderKeyMask) (*keybase1.TeamData, error) {
latestReceivedGen, seeds, err := l.unboxPerTeamSecrets(ctx, box, prevs)
if err != nil {
return nil, err
}
// Earliest generation received. If there were gaps, the earliest in the consecutive run from the box.
earliestReceivedGen := latestReceivedGen - keybase1.PerTeamKeyGeneration(len(seeds)-1)
// Latest generation from the sigchain
latestChainGen := keybase1.PerTeamKeyGeneration(len(state.Chain.PerTeamKeys))
if latestReceivedGen != latestChainGen {
return nil, fmt.Errorf("wrong latest key generation: %v != %v",
latestReceivedGen, latestChainGen)
}
ret := state.DeepCopy()
// Check that each key matches the chain.
for i, seed := range seeds {
gen := int(latestReceivedGen) + i + 1 - len(seeds)
if gen < 1 {
return nil, fmt.Errorf("gen < 1")
}
if gen <= int(latestChainGen) {
l.G().Log.CDebugf(ctx, "TeamLoader got old key, re-checking as if new")
}
item, err := l.checkPerTeamKeyAgainstChain(ctx, state, keybase1.PerTeamKeyGeneration(gen), seed)
if err != nil {
return nil, err
}
// Add it to the snapshot
ret.PerTeamKeySeeds[item.Generation] = *item
}
// Make sure there is not a gap between the latest local key and the earliest received key.
if earliestReceivedGen != keybase1.PerTeamKeyGeneration(1) {
if _, ok := ret.PerTeamKeySeeds[earliestReceivedGen]; !ok {
return nil, fmt.Errorf("gap in per-team-keys: latestRecvd:%v earliestRecvd:%v",
latestReceivedGen, earliestReceivedGen)
}
}
chain := TeamSigChainState{inner: state.Chain}
role, err := chain.GetUserRole(me)
if err != nil {
role = keybase1.TeamRole_NONE
}
if role.IsReaderOrAbove() {
// Insert all reader key masks
// Then scan to make sure there are no gaps in generations and no missing application masks.
checkMaskGens := make(map[keybase1.PerTeamKeyGeneration]bool)
for _, rkm := range readerKeyMasks {
if rkm.Generation < 1 {
return nil, fmt.Errorf("reader key mask has generation: %v < 0", rkm.Generation)
}
if _, ok := ret.ReaderKeyMasks[rkm.Application]; !ok {
ret.ReaderKeyMasks[rkm.Application] = make(
map[keybase1.PerTeamKeyGeneration]keybase1.MaskB64)
}
ret.ReaderKeyMasks[rkm.Application][rkm.Generation] = rkm.Mask
checkMaskGens[rkm.Generation] = true
if rkm.Generation > 1 {
// Check for the previous rkm to make sure there are no gaps
checkMaskGens[rkm.Generation-1] = true
}
}
// Check that we are all the way up to date
checkMaskGens[latestChainGen] = true
for gen := range checkMaskGens {
err = l.checkReaderKeyMaskCoverage(ctx, &ret, gen)
if err != nil {
return nil, err
}
}
} else {
// Discard all cached reader key masks if we are not an explicit member of the team.
ret.ReaderKeyMasks = make(map[keybase1.TeamApplication]map[keybase1.PerTeamKeyGeneration]keybase1.MaskB64)
// Also we shouldn't have gotten any from the server.
if len(readerKeyMasks) > 0 {
l.G().Log.CWarningf(ctx, "TeamLoader got %v reader-key-masks but not an explicit member",
len(readerKeyMasks))
}
}
return &ret, nil
}
// Check that the RKMs for a generation are covered for all apps.
func (l *TeamLoader) checkReaderKeyMaskCoverage(ctx context.Context,
state *keybase1.TeamData, gen keybase1.PerTeamKeyGeneration) error {
for _, app := range keybase1.TeamApplicationMap {
if _, ok := state.ReaderKeyMasks[app]; !ok {
return fmt.Errorf("missing reader key mask for gen:%v app:%v", gen, app)
}
if _, ok := state.ReaderKeyMasks[app][gen]; !ok {
return fmt.Errorf("missing reader key mask for gen:%v app:%v", gen, app)
}
}
return nil
}
func (l *TeamLoader) checkPerTeamKeyAgainstChain(ctx context.Context,
state *keybase1.TeamData, gen keybase1.PerTeamKeyGeneration, seed keybase1.PerTeamKeySeed) (*keybase1.PerTeamKeySeedItem, error) {
km, err := NewTeamKeyManagerWithSecret(l.G(), seed, gen)
if err != nil {
return nil, err
}
chainKey, err := TeamSigChainState{inner: state.Chain}.GetPerTeamKeyAtGeneration(gen)
if err != nil {
return nil, err
}
newSigKey, err := km.SigningKey()
if err != nil {
return nil, err
}
newEncKey, err := km.EncryptionKey()
if err != nil {
return nil, err
}
if !chainKey.SigKID.SecureEqual(newSigKey.GetKID()) {
return nil, fmt.Errorf("import per-team-key: wrong sigKID expected: %v", chainKey.SigKID.String())
}
if !chainKey.EncKID.SecureEqual(newEncKey.GetKID()) {
return nil, fmt.Errorf("import per-team-key: wrong encKID expected: %v", chainKey.EncKID.String())
}
return &keybase1.PerTeamKeySeedItem{
Seed: seed,
Generation: gen,
Seqno: chainKey.Seqno,
}, nil
}
// Unbox per team keys
// Does not check that the keys match the chain
// TODO: return the signer and have the caller check it. Not critical because the public half is checked anyway.
// Returns the generation of the box (the greatest generation),
// and a list of the seeds in ascending generation order.
func (l *TeamLoader) unboxPerTeamSecrets(ctx context.Context,
box *TeamBox, prevs map[keybase1.PerTeamKeyGeneration]prevKeySealedEncoded) (keybase1.PerTeamKeyGeneration, []keybase1.PerTeamKeySeed, error) {
if box == nil {
return 0, nil, fmt.Errorf("no key box from server")
}
userKey, err := l.perUserEncryptionKey(ctx, box.PerUserKeySeqno)
if err != nil {
return 0, nil, err
}
secret1, err := box.Open(userKey)
if err != nil {
return 0, nil, err
}
// Secrets starts as descending
secrets := []keybase1.PerTeamKeySeed{secret1}
// The generation to work on opening
openGeneration := box.Generation - keybase1.PerTeamKeyGeneration(1)
// Walk down generations until
// - the map is exhausted
// - if malformed, the map has a gap
// - reach generation 0
for {
if int(openGeneration) == 0 || int(openGeneration) < 0 {
break
}
// Prevs is keyed by the generation that can decrypt, not the generation contained.
prev, ok := prevs[openGeneration+1]
if !ok {
break
}
secret, err := decryptPrevSingle(ctx, prev, secrets[len(secrets)-1])
if err != nil {
return box.Generation, nil, fmt.Errorf("gen %v: %v", openGeneration, err)
}
secrets = append(secrets, *secret)
openGeneration--
}
// Reverse the list
// After this secrets is ascending
// https://github.com/golang/go/wiki/SliceTricks#reversing
for i := len(secrets)/2 - 1; i >= 0; i-- {
// opp is the index of the opposite element
opp := len(secrets) - 1 - i
secrets[i], secrets[opp] = secrets[opp], secrets[i]
}
return box.Generation, secrets, nil
}
func (l *TeamLoader) perUserEncryptionKey(ctx context.Context, userSeqno keybase1.Seqno) (*libkb.NaclDHKeyPair, error) {
return l.world.perUserEncryptionKey(ctx, userSeqno)
}
func (l *TeamLoader) unpackLinks(ctx context.Context, teamUpdate *rawTeam) ([]*chainLinkUnpacked, error) {
if teamUpdate == nil {
return nil, nil
}
parsedLinks, err := teamUpdate.parseLinks(ctx)
if err != nil {
return nil, err
}
var links []*chainLinkUnpacked
for _, pLink := range parsedLinks {
pLink2 := pLink
link, err := unpackChainLink(&pLink2)
if err != nil {
return nil, err
}
if !link.isStubbed() {
if !link.innerTeamID.Eq(teamUpdate.ID) {
return nil, fmt.Errorf("link has wrong team ID in response: %v != %v",
link.innerTeamID, teamUpdate.ID)
}
}
links = append(links, link)
}
return links, nil
}
// Whether the snapshot has fully loaded, non-stubbed, all of the links.
func (l *TeamLoader) checkNeededSeqnos(ctx context.Context,
state *keybase1.TeamData, needSeqnos []keybase1.Seqno) error {
if len(needSeqnos) == 0 {
return nil
}
if state == nil {
return fmt.Errorf("nil team does not contain needed seqnos")
}
for _, seqno := range needSeqnos {
if (TeamSigChainState{inner: state.Chain}).HasStubbedSeqno(seqno) {
return fmt.Errorf("needed seqno is stubbed: %v", seqno)
}
}
return nil
}
// Calculates the latest name of the team.
// The last part will be as up to date as the sigchain in state.
// The mid-team parts can be as old as the cache time, unless staleOK is false in which case they will be fetched.
func (l *TeamLoader) calculateName(ctx context.Context,
state *keybase1.TeamData, me keybase1.UserVersion, readSubteamID keybase1.TeamID, staleOK bool) (newName keybase1.TeamName, err error) {
chain := TeamSigChainState{inner: state.Chain}
if !chain.IsSubteam() {
return chain.inner.RootAncestor, nil
}
// Load the parent. The parent load will recalculate it's own name,
// so this name recalculation is recursive.
parent, err := l.load2(ctx, load2ArgT{
teamID: *chain.GetParentID(),
staleOK: staleOK,
readSubteamID: &readSubteamID,
me: me,
})
if err != nil {
return newName, err
}
// Swap out the parent name as the base of this name.
// Check that the root ancestor name and depth still match the subteam chain.
newName, err = parent.Name.Append(string(chain.LatestLastNamePart()))
if err != nil {
return newName, fmt.Errorf("invalid new subteam name: %v", err)
}
if !newName.RootAncestorName().Eq(chain.inner.RootAncestor) {
return newName, fmt.Errorf("subteam changed root ancestor: %v -> %v",
chain.inner.RootAncestor, newName.RootAncestorName())
}
if newName.Depth() != chain.inner.NameDepth {
return newName, fmt.Errorf("subteam changed depth: %v -> %v", chain.inner.NameDepth, newName.Depth())
}
return newName, nil
}