forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scankeys.go
326 lines (290 loc) · 9.19 KB
/
scankeys.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
// 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"
"github.com/keybase/go-crypto/openpgp"
"github.com/keybase/go-crypto/openpgp/packet"
)
// ScanKeys finds pgp decryption keys in SKB and also if there is
// one stored on the server. It satisfies the openpgp.KeyRing
// interface.
//
// It also will find public pgp keys for signature verification.
//
// It is not an engine, but uses an engine and is used by engines,
// so has to be in the engine package. It is a UIConsumer.
type ScanKeys struct {
// keys openpgp.EntityList
skbs []*libkb.SKB // all skb blocks for local keys
secui libkb.SecretUI
idui libkb.IdentifyUI
opts *keybase1.TrackOptions
owner *libkb.User // the owner of the found key(s). Can be `me` or any other keybase user.
me *libkb.User
libkb.Contextified
}
const unlockReason = "PGP Decryption"
// enforce ScanKeys implements openpgp.KeyRing:
var _ openpgp.KeyRing = &ScanKeys{}
// NewScanKeys creates a ScanKeys type. If there is a login
// session, it will load the pgp keys for that user.
func NewScanKeys(secui libkb.SecretUI, idui libkb.IdentifyUI, opts *keybase1.TrackOptions, g *libkb.GlobalContext) (*ScanKeys, error) {
sk := &ScanKeys{
secui: secui,
idui: idui,
opts: opts,
Contextified: libkb.NewContextified(g),
}
var err error
g.Log.Debug("+ NewScanKeys")
defer func() {
g.Log.Debug("- NewScanKeys -> %s", err)
}()
lin, err := g.LoginState().LoggedInLoad()
if err != nil {
return nil, err
}
if !lin {
return sk, nil
}
// logged in:
sk.me, err = libkb.LoadMe(libkb.NewLoadUserArg(sk.G()))
if err != nil {
return nil, fmt.Errorf("loadme error: %s", err)
}
// if user provided, then load their local keys, and their synced secret key:
synced, err := sk.me.GetSyncedSecretKey()
if err != nil {
return nil, fmt.Errorf("getsyncedsecret err: %s", err)
}
aerr := sk.G().LoginState().Account(func(a *libkb.Account) {
var ring *libkb.SKBKeyringFile
ring, err = a.Keyring()
if err != nil {
return
}
err = sk.coalesceBlocks(ring, synced)
}, "NewScanKeys - coalesceBlocks")
if aerr != nil {
return nil, err
}
if err != nil {
return nil, err
}
return sk, nil
}
func (s *ScanKeys) Name() string {
return "ScanKeys"
}
func (s *ScanKeys) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{libkb.SecretUIKind}
}
func (s *ScanKeys) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{
&PGPKeyfinder{},
}
}
// Count returns the number of local keys available.
func (s *ScanKeys) Count() int {
return len(s.skbs)
}
// KeysById returns the set of keys that have the given key id.
// It is only called during decryption by openpgp.
func (s *ScanKeys) KeysById(id uint64) []openpgp.Key {
primaries := s.unlockByID(id)
memres := primaries.KeysById(id)
s.G().Log.Debug("ScanKeys:KeysById(%016x) => %d keys match in memory", id, len(memres))
if len(memres) > 0 {
s.G().Log.Debug("ScanKeys:KeysById(%016x) => owner == me (%s)", id, s.me.GetName())
s.owner = s.me // `me` is the owner of all s.skbs
return memres
}
// KeysById is only used for decryption, so getting public keys from
// API server via s.scan(id) is pointless, so just returning nil.
return nil
}
// KeysByIdAndUsage returns the set of public keys with the given
// id that also meet the key usage given by requiredUsage.
//
// The requiredUsage is expressed as the bitwise-OR of
// packet.KeyFlag* values.
//
// It is only called during signature verification so therefore
// requiredUsage will only equal KeyFlagSign, thus only public
// keys are required. If this ever changes upstream in openpgp,
// this function will panic.
//
func (s *ScanKeys) KeysByIdUsage(id uint64, requiredUsage byte) []openpgp.Key {
if requiredUsage != packet.KeyFlagSign {
panic(fmt.Sprintf("ScanKeys: unexpected requiredUsage flags set: %x", requiredUsage))
}
// check the local keys first.
primaries := s.publicByID(id)
memres := primaries.KeysByIdUsage(id, requiredUsage)
s.G().Log.Debug("ScanKeys:KeysByIdUsage(%016x, %x) => %d keys match in memory", id, requiredUsage, len(memres))
if len(memres) > 0 {
s.G().Log.Debug("ScanKeys:KeysByIdUsage(%016x) => owner == me (%s)", id, s.me.GetName())
s.owner = s.me // `me` is the owner of all s.skbs
return memres
}
// no match, so now lookup the user on the api server by the key id.
list, err := s.scan(id)
if err != nil {
s.G().Log.Debug("error finding keys for %016x: %s", id, err)
return nil
}
// use the list to find the keys correctly
s.G().Log.Debug("ScanKeys:KeysByIdUsage(%d, %x) => %d keys found via api scan", id, requiredUsage, len(list))
return list.KeysByIdUsage(id, requiredUsage)
}
// DecryptionKeys returns all private keys that are valid for
// decryption. It is only used if there is no key id in the
// message.
func (s *ScanKeys) DecryptionKeys() []openpgp.Key {
s.G().Log.Debug("ScanKeys:DecryptionKeys() => %d keys available", s.Count())
all := s.unlockAll()
return all.DecryptionKeys()
}
// Owner returns the owner of the keys found by ScanKeys that were
// used in KeysById or KeysByIdUsage.
func (s *ScanKeys) Owner() *libkb.User {
return s.owner
}
// coalesceBlocks puts the synced pgp key block and all the pgp key
// blocks in ring into s.skbs.
func (s *ScanKeys) coalesceBlocks(ring *libkb.SKBKeyringFile, synced *libkb.SKB) error {
var err error
s.G().Log.Debug("+ ScanKeys::coalesceBlocks")
defer func() {
s.G().Log.Debug("- ScanKeys::coalesceBlocks -> %s", libkb.ErrToOk(err))
}()
if synced != nil {
s.skbs = append(s.skbs, synced)
}
for _, b := range ring.Blocks {
if !libkb.IsPGPAlgo(b.Type) {
continue
}
// make sure uid set on each block:
b.SetUID(s.me.GetUID())
s.skbs = append(s.skbs, b)
}
return nil
}
// scan finds the user on the api server for the key id. Then it
// uses PGPKeyfinder to find the public pgp keys for the user,
// identifying/tracking along the way.
func (s *ScanKeys) scan(id uint64) (openpgp.EntityList, error) {
// lookup the user on the api server by the key id.
username, uid, err := s.apiLookup(id)
if err != nil {
return nil, err
}
s.G().Log.Debug("key id %016x => %s, %s", id, id, username, uid)
if len(username) == 0 || len(uid) == 0 {
return nil, libkb.NoKeyError{}
}
// use PGPKeyfinder engine to get the pgp keys for the user
// could use "uid://xxxxxxx" instead of username here, but the log output
// is more user-friendly with usernames.
arg := &PGPKeyfinderArg{Users: []string{username}}
if s.opts != nil {
arg.TrackOptions = *s.opts
}
ctx := &Context{SecretUI: s.secui, IdentifyUI: s.idui}
eng := NewPGPKeyfinder(arg, s.G())
if err := RunEngine(eng, ctx); err != nil {
return nil, err
}
uplus := eng.UsersPlusKeys()
if len(uplus) != 1 {
s.G().Log.Warning("error getting user plus pgp key from %s", username)
return nil, err
}
// user found is the owner of the keys
s.G().Log.Debug("scan(%016x) => owner of key = (%s)", id, uplus[0].User.GetName())
s.owner = uplus[0].User
// convert the bundles to an openpgp entity list
// (which implements the openpgp.KeyRing interface)
var list openpgp.EntityList
for _, k := range uplus[0].Keys {
list = append(list, k.Entity)
}
return list, nil
}
// apiLookup gets the username and uid from the api server for the
// key id.
func (s *ScanKeys) apiLookup(id uint64) (username, uid string, err error) {
return libkb.PGPLookup(s.G(), id)
}
func (s *ScanKeys) publicByID(id uint64) openpgp.EntityList {
var list openpgp.EntityList
for _, skb := range s.skbs {
pubkey, err := skb.GetPubKey()
if err != nil {
s.G().Log.Warning("error getting pub key from skb: %s", err)
continue
}
bundle, ok := pubkey.(*libkb.PGPKeyBundle)
if !ok {
continue
}
if len(bundle.KeysById(id)) == 0 {
// no match
continue
}
list = append(list, bundle.Entity)
}
return list
}
func (s *ScanKeys) unlockByID(id uint64) openpgp.EntityList {
var list openpgp.EntityList
for _, skb := range s.skbs {
pubkey, err := skb.GetPubKey()
if err != nil {
s.G().Log.Warning("error getting pub key from skb: %s", err)
continue
}
bundle, ok := pubkey.(*libkb.PGPKeyBundle)
if !ok {
continue
}
if len(bundle.KeysById(id)) == 0 {
// no match
continue
}
// some key in the bundle matched, so unlock everything:
unlocked, err := skb.PromptAndUnlock(nil, unlockReason, "", nil, s.secui, nil, s.me)
if err != nil {
s.G().Log.Warning("error unlocking key: %s", err)
continue
}
unlockedBundle, ok := unlocked.(*libkb.PGPKeyBundle)
if !ok {
s.G().Log.Warning("could not convert unlocked key to PGPKeyBundle")
continue
}
list = append(list, unlockedBundle.Entity)
}
return list
}
func (s *ScanKeys) unlockAll() openpgp.EntityList {
var list openpgp.EntityList
for _, skb := range s.skbs {
unlocked, err := skb.PromptAndUnlock(nil, unlockReason, "", nil, s.secui, nil, s.me)
if err != nil {
s.G().Log.Warning("error unlocking key: %s", err)
continue
}
unlockedBundle, ok := unlocked.(*libkb.PGPKeyBundle)
if !ok {
s.G().Log.Warning("could not convert unlocked key to PGPKeyBundle")
continue
}
list = append(list, unlockedBundle.Entity)
}
return list
}