forked from keybase/client
/
identify2_with_uid.go
662 lines (549 loc) · 17.5 KB
/
identify2_with_uid.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"fmt"
"sync"
"time"
gregor "github.com/keybase/client/go/gregor"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
var locktab libkb.LockTable
type Identify2WithUIDTestArgs struct {
noMe bool // don't load ME
tcl *libkb.TrackChainLink // the track chainlink to use
selfLoad bool // on if this is a self load
noCache bool // on if we shouldn't use the cache
cache libkb.Identify2Cacher
clock func() time.Time
}
type identify2TrackType int
const (
identify2NoTrack identify2TrackType = iota
identify2TrackOK
identify2TrackBroke
)
//
// TODOs:
// - think harder about what we're caching in failure cases; right now we're only
// caching full successes.
// - Better error typing for various failures.
// - Work back in the identify card
//
// Identify2WithUID is the Identify engine used in KBFS and as a subroutine
// of command-line crypto.
type Identify2WithUID struct {
libkb.Contextified
arg *keybase1.Identify2Arg
testArgs *Identify2WithUIDTestArgs
trackToken keybase1.TrackToken
confirmResult keybase1.ConfirmResult
cachedRes *keybase1.UserPlusKeys
// If we just resolved a user, then we can plumb this through to loadUser()
ResolveBody *jsonw.Wrapper
me *libkb.User
them *libkb.User
themAssertion libkb.AssertionExpression
remoteAssertion libkb.AssertionAnd
localAssertion libkb.AssertionAnd
state libkb.IdentifyState
useTracking bool
identifyKeys []keybase1.IdentifyKey
resultCh chan<- error
// For eagerly checking remote Assertions as they come in, these
// member variables maintain state, protected by the remotesMutex.
remotesMutex sync.Mutex
remotesReceived *libkb.ProofSet
remotesError error
remotesCompleted bool
responsibleGregorItem gregor.Item
// When tracking is being performed, the identify engine is used with a tracking ui.
// These options are sent to the ui based on command line options.
// For normal identify, safe to leave these in their default zero state.
trackOptions keybase1.TrackOptions
// When called from chat, we should just collect breaking tracking failures, but
// not fail track. This is where we collect them
trackBreaks *keybase1.IdentifyTrackBreaks
}
var _ (Engine) = (*Identify2WithUID)(nil)
var _ (libkb.CheckCompletedListener) = (*Identify2WithUID)(nil)
// Name is the unique engine name.
func (e *Identify2WithUID) Name() string {
return "Identify2WithUID"
}
func NewIdentify2WithUID(g *libkb.GlobalContext, arg *keybase1.Identify2Arg) *Identify2WithUID {
return &Identify2WithUID{
Contextified: libkb.NewContextified(g),
arg: arg,
}
}
// GetPrereqs returns the engine prereqs.
func (e *Identify2WithUID) Prereqs() Prereqs {
return Prereqs{}
}
func (e *Identify2WithUID) WantDelegate(k libkb.UIKind) bool {
return k == libkb.IdentifyUIKind && e.arg.UseDelegateUI
}
func (e *Identify2WithUID) calledFromChatGUI() bool {
return e.arg.ChatGUIMode
}
func (e *Identify2WithUID) resetError(err error) error {
if err == nil {
return nil
}
switch err.(type) {
case libkb.ProofError:
case libkb.IdentifySummaryError:
default:
return err
}
if e.calledFromChatGUI() {
e.G().Log.Debug("| Reset err from %v -> nil since caller is 'CHAT_GUI'", err)
return nil
}
return err
}
// Run then engine
func (e *Identify2WithUID) Run(ctx *Context) (err error) {
defer libkb.TimeLog(fmt.Sprintf("Identify2WithUID.Run(UID=%v, Assertion=%s", e.arg.Uid, e.arg.UserAssertion), e.G().Clock().Now(), e.G().Log.Debug)
e.G().Log.Debug("+ Identify2WithUID.Run(UID=%v, Assertion=%s)", e.arg.Uid, e.arg.UserAssertion)
if e.arg.Uid.IsNil() {
return libkb.NoUIDError{}
}
// Only the first send matters, but we don't want to block the subsequent no-op
// sends. This code will break when we have more than 100 unblocking opportunities.
ch := make(chan error, 100)
e.resultCh = ch
go e.run(ctx)
err = <-ch
// Potentially reset the error based on the error and the calling context.
err = e.resetError(err)
e.G().Log.Debug("- Identify2WithUID.Run() -> %v", err)
return err
}
func (e *Identify2WithUID) run(ctx *Context) {
err := e.runReturnError(ctx)
e.unblock( /* isFinal */ true, err)
ctx.IdentifyUI.Cancel() // always cancel IdentifyUI to allow clients to clean up
}
func (e *Identify2WithUID) runReturnError(ctx *Context) (err error) {
e.G().Log.Debug("+ acquire singleflight lock for %s", e.arg.Uid)
lock := locktab.AcquireOnName(e.arg.Uid.String())
e.G().Log.Debug("- acquired singleflight lock")
defer func() {
e.G().Log.Debug("+ Releasing singleflight lock for %s", e.arg.Uid)
lock.Release()
e.G().Log.Debug("- Released singleflight lock")
}()
if e.loadAssertion(); err != nil {
return err
}
if !e.useAnyAssertions() && e.allowEarlyOuts() && e.checkFastCacheHit() {
e.G().Log.Debug("| hit fast cache")
return nil
}
e.G().Log.Debug("| Identify2WithUID.loadUsers")
if err = e.loadUsers(ctx); err != nil {
return err
}
if err = e.checkLocalAssertions(); err != nil {
return err
}
if e.isSelfLoad() && !e.arg.NoSkipSelf {
e.G().Log.Debug("| was a self load, short-circuiting")
return nil
}
if !e.useRemoteAssertions() && e.allowEarlyOuts() && e.checkSlowCacheHit() {
e.G().Log.Debug("| hit slow cache, first check")
return nil
}
e.G().Log.Debug("| Identify2WithUID.createIdentifyState")
if err = e.createIdentifyState(); err != nil {
return err
}
if err = e.runIdentifyPrecomputation(); err != nil {
return err
}
// First we check that all remote assertions as present for the user,
// whether or not the remote check actually suceeds (hence the
// ProofState_NONE check).
okStates := []keybase1.ProofState{keybase1.ProofState_NONE, keybase1.ProofState_OK}
if err = e.checkRemoteAssertions(okStates); err != nil {
e.G().Log.Debug("| Early fail due to missing remote assertions")
return err
}
if e.useRemoteAssertions() && e.allowEarlyOuts() && e.checkSlowCacheHit() {
e.G().Log.Debug("| hit slow cache, second check")
return nil
}
// If we're not using tracking and we're not using remote assertions,
// we can unblock the RPC caller here, and perform the identifyUI operations
// in the background.
if !e.useTracking && !e.useRemoteAssertions() && e.allowEarlyOuts() {
e.unblock( /* isFinal */ false, nil)
}
if err = e.runIdentifyUI(ctx); err != nil {
return err
}
return nil
}
func (e *Identify2WithUID) allowEarlyOuts() bool {
return !e.arg.NeedProofSet
}
func (e *Identify2WithUID) getNow() time.Time {
if e.testArgs != nil && e.testArgs.clock != nil {
return e.testArgs.clock()
}
return time.Now()
}
func (e *Identify2WithUID) unblock(isFinal bool, err error) {
e.G().Log.Debug("| unblocking...")
if e.arg.AlwaysBlock && !isFinal {
e.G().Log.Debug("| skipping unblock; isFinal=%v; AlwaysBlock=%v...", isFinal, e.arg.AlwaysBlock)
} else {
e.resultCh <- err
e.G().Log.Debug("| unblock sent...")
}
}
func (e *Identify2WithUID) maybeCacheResult() {
if e.state.Result().IsOK() && e.getCache() != nil {
v := e.toUserPlusKeys()
e.getCache().Insert(&v)
}
}
func (e *Identify2WithUID) insertTrackToken(ctx *Context, outcome *libkb.IdentifyOutcome, ui libkb.IdentifyUI) (err error) {
e.G().Log.Debug("+ insertTrackToken")
defer func() {
e.G().Log.Debug("- insertTrackToken -> %v", err)
}()
e.trackToken, err = e.G().TrackCache.Insert(outcome)
if err != nil {
return err
}
if err = ui.ReportTrackToken(e.trackToken); err != nil {
return err
}
return nil
}
// CCLCheckCompleted is triggered whenever a remote proof check completes.
// We get these calls as a result of being a "CheckCompletedListener".
// When each result comes in, we check against our pool of needed remote
// assertions. If the set is complete, or if one that we need errors,
// we can unblock the caller.
func (e *Identify2WithUID) CCLCheckCompleted(lcr *libkb.LinkCheckResult) {
e.remotesMutex.Lock()
defer e.remotesMutex.Unlock()
e.G().Log.Debug("+ CheckCompleted for %s", lcr.GetLink().ToIDString())
defer e.G().Log.Debug("- CheckCompleted")
// Always add to remotesReceived list, so that we have a full ProofSet.
pf := libkb.RemoteProofChainLinkToProof(lcr.GetLink())
e.remotesReceived.Add(pf)
if !e.useRemoteAssertions() || e.useTracking {
e.G().Log.Debug("| Early out, since not using remote assertions or is tracking")
return
}
if !e.remoteAssertion.HasFactor(pf) {
e.G().Log.Debug("| Proof isn't needed in our remote-assertion early-out check: %v", pf)
return
}
if err := lcr.GetError(); err != nil {
e.G().Log.Debug("| got error -> %v", err)
e.remotesError = err
}
// note(maxtaco): this is a little ugly in that it's O(n^2) where n is the number
// of identities in the assertion. But I can't imagine n > 3, so this is fine
// for now.
matched := e.remoteAssertion.MatchSet(*e.remotesReceived)
e.G().Log.Debug("| matched -> %v", matched)
if matched {
e.remotesCompleted = true
}
if e.remotesError != nil || e.remotesCompleted {
e.G().Log.Debug("| unblocking, with err = %v", e.remotesError)
e.unblock(false, e.remotesError)
}
}
func (e *Identify2WithUID) checkLocalAssertions() error {
if !e.localAssertion.MatchSet(*e.them.BaseProofSet()) {
return libkb.UnmetAssertionError{User: e.them.GetName(), Remote: false}
}
return nil
}
func (e *Identify2WithUID) checkRemoteAssertions(okStates []keybase1.ProofState) error {
ps := libkb.NewProofSet(nil)
e.state.Result().AddProofsToSet(ps, okStates)
if !e.remoteAssertion.MatchSet(*ps) {
return libkb.UnmetAssertionError{User: e.them.GetName(), Remote: true}
}
return nil
}
func (e *Identify2WithUID) loadAssertion() (err error) {
e.themAssertion, err = libkb.AssertionParseAndOnly(e.G().MakeAssertionContext(), e.arg.UserAssertion)
if err == nil {
e.remoteAssertion, e.localAssertion = libkb.CollectAssertions(e.themAssertion)
}
return err
}
func (e *Identify2WithUID) useAnyAssertions() bool {
return e.useLocalAssertions() || e.useRemoteAssertions()
}
func (e *Identify2WithUID) useLocalAssertions() bool {
return e.localAssertion.Len() > 0
}
// If we need a ProofSet, it's as if we need remote assertions.
func (e *Identify2WithUID) useRemoteAssertions() bool {
return (e.remoteAssertion.Len() > 0)
}
func (e *Identify2WithUID) runIdentifyPrecomputation() (err error) {
f := func(k keybase1.IdentifyKey) error {
e.identifyKeys = append(e.identifyKeys, k)
return nil
}
e.state.Precompute(f)
return nil
}
func (e *Identify2WithUID) displayUserCardAsync(ctx *Context) <-chan error {
if e.calledFromChatGUI() {
return nil
}
return displayUserCardAsync(e.G(), ctx, e.them.GetUID(), (e.me != nil))
}
func (e *Identify2WithUID) runIdentifyUI(ctx *Context) (err error) {
e.G().Log.Debug("+ runIdentifyUI(%s)", e.them.GetName())
// RemoteReceived, start with the baseProofSet that has PGP
// fingerprints and the user's UID and username.
e.remotesReceived = e.them.BaseProofSet()
iui := ctx.IdentifyUI
if e.useTracking && e.arg.CanSuppressUI && !e.arg.ForceDisplay {
iui = newBufferedIdentifyUI(e.G(), iui, keybase1.ConfirmResult{
IdentityConfirmed: true,
})
} else if e.calledFromChatGUI() {
iui = newLoopbackIdentifyUI(e.G(), &e.trackBreaks)
}
e.G().Log.Debug("| IdentifyUI.Start(%s)", e.them.GetName())
if err = iui.Start(e.them.GetName(), e.arg.Reason, e.arg.ForceDisplay); err != nil {
return err
}
for _, k := range e.identifyKeys {
if err = iui.DisplayKey(k); err != nil {
return err
}
}
e.G().Log.Debug("| IdentifyUI.ReportLastTrack(%s)", e.them.GetName())
if err = iui.ReportLastTrack(libkb.ExportTrackSummary(e.state.TrackLookup(), e.them.GetName())); err != nil {
return err
}
e.G().Log.Debug("| IdentifyUI.LaunchNetworkChecks(%s)", e.them.GetName())
if err = iui.LaunchNetworkChecks(e.state.ExportToUncheckedIdentity(), e.them.Export()); err != nil {
return err
}
waiter := e.displayUserCardAsync(ctx)
e.G().Log.Debug("| IdentifyUI.Identify(%s)", e.them.GetName())
if err = e.them.IDTable().Identify(e.state, e.arg.ForceRemoteCheck, iui, e); err != nil {
return err
}
if waiter != nil {
if err = <-waiter; err != nil {
return err
}
e.G().Log.Debug("| IdentifyUI waited for waiter (%s)", e.them.GetName())
}
// use Confirm to display the IdentifyOutcome
outcome := e.state.Result()
outcome.TrackOptions = e.trackOptions
e.confirmResult, err = iui.Confirm(outcome.Export())
if err != nil {
return err
}
e.insertTrackToken(ctx, outcome, iui)
if err = iui.Finish(); err != nil {
return err
}
e.G().Log.Debug("| IdentifyUI.Finished(%s)", e.them.GetName())
err = e.checkRemoteAssertions([]keybase1.ProofState{keybase1.ProofState_OK})
e.maybeCacheResult()
if err == nil && !e.arg.NoErrorOnTrackFailure {
// We only care about tracking errors in this case; hence GetErrorLax
_, err = e.state.Result().GetErrorLax()
}
e.G().Log.Debug("- runIdentifyUI(%s) -> %v", e.them.GetName(), err)
return err
}
func (e *Identify2WithUID) getTrackChainLink(tmp bool) (*libkb.TrackChainLink, error) {
if e.testArgs != nil && e.testArgs.tcl != nil {
return e.testArgs.tcl, nil
}
if e.me == nil {
return nil, nil
}
if tmp {
return e.me.TmpTrackChainLinkFor(e.them.GetName(), e.them.GetUID())
}
return e.me.TrackChainLinkFor(e.them.GetName(), e.them.GetUID())
}
func (e *Identify2WithUID) createIdentifyState() (err error) {
e.state = libkb.NewIdentifyStateWithGregorItem(e.responsibleGregorItem, e.them)
tcl, err := e.getTrackChainLink(false)
if err != nil {
return err
}
if tcl != nil {
e.useTracking = true
e.state.SetTrackLookup(tcl)
if ttcl, _ := e.getTrackChainLink(true); ttcl != nil {
e.state.SetTmpTrackLookup(ttcl)
}
}
return nil
}
// RequiredUIs returns the required UIs.
func (e *Identify2WithUID) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{
libkb.IdentifyUIKind,
}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *Identify2WithUID) SubConsumers() []libkb.UIConsumer {
return nil
}
func (e *Identify2WithUID) isSelfLoad() bool {
if e.testArgs != nil && e.testArgs.selfLoad {
return true
}
return e.me != nil && e.them != nil && e.me.Equal(e.them)
}
func (e *Identify2WithUID) loadMe(ctx *Context) (err error) {
// Short circuit loadMe for testing
if e.testArgs != nil && e.testArgs.noMe {
return nil
}
var ok bool
var uid keybase1.UID
ok, uid, err = IsLoggedIn(e, ctx)
if err != nil || !ok {
return err
}
e.me, err = libkb.LoadMeByUID(e.G(), uid)
return err
}
func (e *Identify2WithUID) loadThem(ctx *Context) (err error) {
arg := libkb.NewLoadUserArg(e.G())
arg.UID = e.arg.Uid
arg.ResolveBody = e.ResolveBody
e.them, err = libkb.LoadUser(arg)
if err != nil {
switch err.(type) {
case libkb.NoKeyError:
// convert this error to NoSigChainError
return libkb.NoSigChainError{}
case libkb.DeletedError:
return err
case libkb.NotFoundError:
return libkb.UserNotFoundError{UID: arg.UID, Msg: "in Identify2WithUID"}
}
}
if e.them == nil {
return libkb.UserNotFoundError{UID: arg.UID, Msg: "in Identify2WithUID"}
}
return err
}
func (e *Identify2WithUID) loadUsers(ctx *Context) error {
var loadMeErr, loadThemErr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
loadMeErr = e.loadMe(ctx)
wg.Done()
}()
wg.Add(1)
go func() {
loadThemErr = e.loadThem(ctx)
wg.Done()
}()
wg.Wait()
if loadMeErr != nil {
return loadMeErr
}
if loadThemErr != nil {
return loadThemErr
}
return nil
}
func (e *Identify2WithUID) checkFastCacheHit() bool {
if e.getCache() == nil {
return false
}
fn := func(u keybase1.UserPlusKeys) keybase1.Time { return u.Uvv.CachedAt }
u, err := e.getCache().Get(e.arg.Uid, fn, libkb.Identify2CacheShortTimeout)
if err != nil {
e.G().Log.Debug("fast cache error for %s: %s", e.arg.Uid, err)
}
if u == nil {
return false
}
e.cachedRes = u
return true
}
func (e *Identify2WithUID) checkSlowCacheHit() bool {
if e.getCache() == nil {
return false
}
fn := func(u keybase1.UserPlusKeys) keybase1.Time { return u.Uvv.LastIdentifiedAt }
u, _ := e.getCache().Get(e.them.GetUID(), fn, libkb.Identify2CacheLongTimeout)
if u == nil {
return false
}
if !e.them.IsCachedIdentifyFresh(u) {
return false
}
e.cachedRes = u
return true
}
func (e *Identify2WithUID) Result() *keybase1.Identify2Res {
res := &keybase1.Identify2Res{}
if e.cachedRes != nil {
res.Upk = *e.cachedRes
} else if e.them != nil {
res.Upk = e.toUserPlusKeys()
}
// This will be a no-op unless we're being called from the chat GUI
res.TrackBreaks = e.trackBreaks
return res
}
func (e *Identify2WithUID) GetProofSet() *libkb.ProofSet {
return e.remotesReceived
}
func (e *Identify2WithUID) toUserPlusKeys() keybase1.UserPlusKeys {
return e.them.ExportToUserPlusKeys(keybase1.ToTime(e.getNow()))
}
func (e *Identify2WithUID) getCache() libkb.Identify2Cacher {
if e.testArgs != nil && e.testArgs.cache != nil {
return e.testArgs.cache
}
if e.testArgs != nil && e.testArgs.noCache {
return nil
}
return e.G().Identify2Cache
}
func (e *Identify2WithUID) getTrackType() identify2TrackType {
switch {
case !e.useTracking || e.state.Result() == nil:
return identify2NoTrack
case e.state.Result().IsOK():
return identify2TrackOK
default:
return identify2TrackBroke
}
}
func (e *Identify2WithUID) SetResponsibleGregorItem(item gregor.Item) {
e.responsibleGregorItem = item
}
func (e *Identify2WithUID) TrackToken() keybase1.TrackToken {
return e.trackToken
}
func (e *Identify2WithUID) ConfirmResult() keybase1.ConfirmResult {
return e.confirmResult
}