/
device_keyfinder.go
150 lines (126 loc) · 3.6 KB
/
device_keyfinder.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"fmt"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
)
// DeviceKeyfinder is an engine to find device keys for users (loaded by
// assertions), possibly tracking them if necessary.
type DeviceKeyfinder struct {
libkb.Contextified
arg DeviceKeyfinderArg
userMap map[keybase1.UID](*keybase1.UserPlusKeys)
}
type DeviceKeyfinderArg struct {
Users []string
NeedEncryptKeys bool
NeedVerifyKeys bool
Self *libkb.User
}
// NewDeviceKeyfinder creates a DeviceKeyfinder engine.
func NewDeviceKeyfinder(g *libkb.GlobalContext, arg DeviceKeyfinderArg) *DeviceKeyfinder {
return &DeviceKeyfinder{
Contextified: libkb.NewContextified(g),
arg: arg,
userMap: make(map[keybase1.UID](*keybase1.UserPlusKeys)),
}
}
// Name is the unique engine name.
func (e *DeviceKeyfinder) Name() string {
return "DeviceKeyfinder"
}
// GetPrereqs returns the engine prereqs.
func (e *DeviceKeyfinder) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *DeviceKeyfinder) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *DeviceKeyfinder) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{
&ResolveThenIdentify2{},
}
}
// Run starts the engine.
func (e *DeviceKeyfinder) Run(ctx *Context) (err error) {
defer libkb.Trace(e.G().Log, "DeviceKeyfinder::Run", func() error { return err })()
err = e.identifyUsers(ctx)
if err != nil {
return err
}
return nil
}
// UsersPlusDeviceKeys returns the users found while running the engine,
// plus their device keys.
func (e *DeviceKeyfinder) UsersPlusKeys() map[keybase1.UID](*keybase1.UserPlusKeys) {
return e.userMap
}
func (e *DeviceKeyfinder) identifyUsers(ctx *Context) error {
for _, u := range e.arg.Users {
if err := e.identifyUser(ctx, u); err != nil {
return err
}
}
return nil
}
func (e *DeviceKeyfinder) identifyUser(ctx *Context, user string) error {
arg := keybase1.Identify2Arg{
UserAssertion: user,
Reason: keybase1.IdentifyReason{
Type: keybase1.IdentifyReasonType_ENCRYPT,
},
AlwaysBlock: true,
}
eng := NewResolveThenIdentify2(e.G(), &arg)
if err := RunEngine(eng, ctx); err != nil {
return libkb.IdentifyFailedError{Assertion: user, Reason: err.Error()}
}
return e.addUser(eng.Result())
}
func (e *DeviceKeyfinder) hasUser(upk *keybase1.UserPlusKeys) bool {
_, ok := e.userMap[upk.Uid]
return ok
}
func (e *DeviceKeyfinder) filterKeys(upk *keybase1.UserPlusKeys) error {
var keys []keybase1.PublicKey
for _, key := range upk.DeviceKeys {
if len(key.PGPFingerprint) != 0 {
// this shouldn't happen
continue
}
if e.arg.NeedVerifyKeys && !libkb.KIDIsDeviceVerify(key.KID) {
continue
}
if e.arg.NeedEncryptKeys && !libkb.KIDIsDeviceEncrypt(key.KID) {
continue
}
keys = append(keys, key)
}
if len(keys) == 0 {
return libkb.NoNaClEncryptionKeyError{User: upk.Username, HasPGPKey: upk.PGPKeyCount > 0}
}
upk.DeviceKeys = keys
return nil
}
func (e *DeviceKeyfinder) addUser(ir *keybase1.Identify2Res) error {
if ir == nil {
return fmt.Errorf("Null result from Identify2")
}
upk := &ir.Upk
if e.hasUser(upk) {
return nil
}
if e.arg.Self != nil && e.arg.Self.GetUID().Equal(upk.Uid) {
e.G().Log.Debug("skipping self in DevicekeyFinder (uid=%s)", upk.Uid)
return nil
}
if err := e.filterKeys(upk); err != nil {
return err
}
e.userMap[upk.Uid] = upk
return nil
}