This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lksec.go
582 lines (496 loc) · 14.2 KB
/
lksec.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"crypto/hmac"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/crypto/nacl/secretbox"
"strings"
)
const LKSecVersion = 100
const LKSecLen = 32
type LKSecClientHalf struct {
c *[LKSecLen]byte
}
func (c LKSecClientHalf) IsNil() bool {
return c.c == nil
}
func (c LKSecClientHalf) Bytes() []byte {
if c.c == nil {
return nil
}
return c.c[:]
}
type LKSecServerHalf struct {
s *[LKSecLen]byte
}
func (s LKSecServerHalf) IsNil() bool {
return s.s == nil
}
func (s LKSecServerHalf) Bytes() []byte {
if s.s == nil {
return nil
}
return s.s[:]
}
type LKSecFullSecret struct {
f *[LKSecLen]byte
}
type LKSecMask struct {
m *[LKSecLen]byte
}
func (f LKSecFullSecret) IsNil() bool {
return f.f == nil
}
func (f LKSecFullSecret) Bytes() []byte {
if f.f == nil {
return nil
}
return f.f[:]
}
func NewLKSecServerHalfFromHex(s string) (ret LKSecServerHalf, err error) {
var b []byte
b, err = hex.DecodeString(s)
if err != nil {
return ret, err
}
if len(b) != LKSecLen {
err = fmt.Errorf("Wrong LKSec server length: %d != %d", len(b), LKSecLen)
return ret, err
}
var v [LKSecLen]byte
copy(v[:], b)
ret = LKSecServerHalf{s: &v}
return ret, nil
}
func newLKSecFullSecretFromBytes(b []byte) (ret LKSecFullSecret, err error) {
if len(b) != LKSecLen {
err = fmt.Errorf("Wrong LKSecFullSecret len: %d != %d", len(b), LKSecLen)
return ret, err
}
var v [LKSecLen]byte
copy(v[:], b)
ret = LKSecFullSecret{f: &v}
return ret, nil
}
func NewLKSecClientHalfFromBytes(b []byte) (ret LKSecClientHalf, err error) {
if len(b) != LKSecLen {
err = fmt.Errorf("Wrong LKSecClientHalf len: %d != %d", len(b), LKSecLen)
return ret, err
}
var v [LKSecLen]byte
copy(v[:], b)
ret = LKSecClientHalf{c: &v}
return ret, nil
}
func (f LKSecFullSecret) Equal(f2 LKSecFullSecret) bool {
if f.IsNil() {
return false
}
if f2.IsNil() {
return false
}
return hmac.Equal(f.f[:], f2.f[:])
}
func (c LKSecClientHalf) Equal(c2 LKSecClientHalf) bool {
if c.IsNil() {
return false
}
if c2.IsNil() {
return false
}
return hmac.Equal(c.c[:], c2.c[:])
}
func (s LKSecServerHalf) EncodeToHex() string {
if s.IsNil() {
return ""
}
return hex.EncodeToString(s.s[:])
}
func (m LKSecMask) IsNil() bool {
return m.m == nil
}
func (m LKSecMask) EncodeToHex() string {
if m.IsNil() {
return ""
}
return hex.EncodeToString(m.m[:])
}
func NewLKSecServerHalfZeros() LKSecServerHalf {
var z [LKSecLen]byte
return LKSecServerHalf{s: &z}
}
type LKSec struct {
serverHalf LKSecServerHalf
clientHalf LKSecClientHalf
secret LKSecFullSecret
ppGen PassphraseGeneration
uid keybase1.UID
deviceID keybase1.DeviceID
Contextified
}
func xorBytes(x *[LKSecLen]byte, y *[LKSecLen]byte) *[LKSecLen]byte {
var ret [LKSecLen]byte
for i := 0; i < LKSecLen; i++ {
ret[i] = x[i] ^ y[i]
}
return &ret
}
func (s LKSecServerHalf) ComputeFullSecret(c LKSecClientHalf) LKSecFullSecret {
return LKSecFullSecret{f: xorBytes(s.s, c.c)}
}
func (s LKSecServerHalf) ComputeClientHalf(f LKSecFullSecret) LKSecClientHalf {
return LKSecClientHalf{c: xorBytes(s.s, f.f)}
}
func (f LKSecFullSecret) bug3964Remask(s LKSecServerHalf) LKSecFullSecret {
return LKSecFullSecret{f: xorBytes(s.s, f.f)}
}
func (c LKSecClientHalf) ComputeMask(c2 LKSecClientHalf) LKSecMask {
if c.IsNil() || c2.IsNil() {
return LKSecMask{}
}
return LKSecMask{m: xorBytes(c.c, c2.c)}
}
func NewLKSec(pps *PassphraseStream, uid keybase1.UID, gc *GlobalContext) *LKSec {
return NewLKSecWithDeviceID(pps, uid, keybase1.DeviceID(""), gc)
}
func NewLKSecWithDeviceID(pps *PassphraseStream, uid keybase1.UID, deviceID keybase1.DeviceID, gc *GlobalContext) *LKSec {
res := &LKSec{
uid: uid,
deviceID: deviceID,
Contextified: NewContextified(gc),
}
if pps != nil {
res.clientHalf = pps.LksClientHalf()
res.ppGen = pps.Generation()
}
return res
}
func NewLKSecWithClientHalf(clientHalf LKSecClientHalf, ppgen PassphraseGeneration, uid keybase1.UID, gc *GlobalContext) *LKSec {
return &LKSec{
clientHalf: clientHalf,
ppGen: ppgen,
uid: uid,
Contextified: NewContextified(gc),
}
}
func NewLKSecWithFullSecret(secret LKSecFullSecret, uid keybase1.UID, gc *GlobalContext) *LKSec {
return &LKSec{
secret: secret,
ppGen: PassphraseGeneration(-1),
uid: uid,
Contextified: NewContextified(gc),
}
}
func (s *LKSec) CorruptedFullSecretForBug3964Testing(srv LKSecServerHalf) LKSecFullSecret {
return s.FullSecret().bug3964Remask(srv)
}
func (s *LKSec) SetUID(u keybase1.UID) {
s.uid = u
}
func (s *LKSec) SetClientHalf(b LKSecClientHalf) {
s.clientHalf = b
}
func (s *LKSec) SetServerHalf(b LKSecServerHalf) {
s.serverHalf = b
}
// Generation returns the passphrase generation that this local key security
// object is derived from.
func (s LKSec) Generation() PassphraseGeneration {
return s.ppGen
}
func (s *LKSec) GenerateServerHalf() error {
if s.clientHalf.IsNil() {
return errors.New("Can't generate server half without a client half")
}
if !s.serverHalf.IsNil() {
return nil
}
var v [LKSecLen]byte
var n int
var err error
if n, err = rand.Read(v[:]); err != nil {
return err
}
if n != LKSecLen {
return fmt.Errorf("short random read; wanted %d bytes but only got %d", LKSecLen, n)
}
s.serverHalf = LKSecServerHalf{s: &v}
return nil
}
func (s *LKSec) GetServerHalf() LKSecServerHalf {
return s.serverHalf
}
func (s *LKSec) Load(m MetaContext) (err error) {
defer m.CTrace("LKSec::Load()", func() error { return err })()
if !s.secret.IsNil() {
m.CDebugf("| Short-circuit; we already know the full secret")
return nil
}
if s.clientHalf.IsNil() {
err = fmt.Errorf("client half not set")
return err
}
if err = s.LoadServerHalf(m); err != nil {
return err
}
s.SetFullSecret()
return nil
}
func (s *LKSec) SetFullSecret() {
s.G().Log.Debug("| Making XOR'ed secret key for Local Key Security (LKS)")
s.secret = s.serverHalf.ComputeFullSecret(s.clientHalf)
}
func (s *LKSec) LoadServerHalf(m MetaContext) (err error) {
defer m.CTrace("LKSec::LoadServerHalf()", func() error { return err })()
if !s.serverHalf.IsNil() {
m.CDebugf("| short-circuit: already have serverHalf")
return nil
}
_, err = s.LoadServerDetails(m)
return err
}
func (s *LKSec) LoadServerDetails(m MetaContext) (ret DeviceKeyMap, err error) {
defer m.CTrace("LKSec#LoadServerDetails", func() error { return err })()
devid := s.deviceID
if devid.IsNil() {
devid = m.G().Env.GetDeviceIDForUID(s.uid)
}
if devid.IsNil() {
return ret, fmt.Errorf("lksec load: no device id set, thus can't fetch server half")
}
if ret, err = s.apiServerHalf(m, devid); err != nil {
m.CDebugf("apiServerHalf(%s) error: %s", devid, err)
return ret, err
}
if s.serverHalf.IsNil() {
return ret, fmt.Errorf("after apiServerHalf(%s), serverHalf still empty", devid)
}
return ret, nil
}
func (s *LKSec) GetSecret(m MetaContext) (secret LKSecFullSecret, err error) {
defer m.CTrace("LKsec:GetSecret()", func() error { return err })()
if err = s.Load(m); err != nil {
return secret, err
}
secret = s.secret
return secret, nil
}
func (s *LKSec) Encrypt(m MetaContext, src []byte) (res []byte, err error) {
defer m.CTrace("LKsec:Encrypt()", func() error { return err })()
if err = s.Load(m); err != nil {
return nil, err
}
var nonce []byte
nonce, err = RandBytes(24)
if err != nil {
return nil, err
}
var fnonce [24]byte
copy(fnonce[:], nonce)
box := secretbox.Seal(nil, src, &fnonce, s.secret.f)
ret := append(nonce, box...)
return ret, nil
}
func (s *LKSec) attemptBug3964Recovery(m MetaContext, data []byte, nonce *[24]byte) (res []byte, gen PassphraseGeneration, erroneousMask LKSecServerHalf, err error) {
ss, err := s.loadSecretSyncer(m)
if err != nil {
return nil, 0, LKSecServerHalf{}, err
}
devices := ss.AllDevices()
res, serverHalf, err := s.tryAllDevicesForBug3964Recovery(devices, data, nonce)
return res, s.ppGen, serverHalf, err
}
func (s *LKSec) tryAllDevicesForBug3964Recovery(devices DeviceKeyMap, data []byte, nonce *[24]byte) (res []byte, erroneousMask LKSecServerHalf, err error) {
// This logline is asserted in testing in bug_3964_repairman_test
defer s.G().Trace("LKSec#tryAllDevicesForBug3964Recovery()", func() error { return err })()
for devid, dev := range devices {
// This logline is asserted in testing in bug_3964_repairman_test
s.G().Log.Debug("| Trying Bug 3964 Recovery w/ device %q {id: %s, lks: %s...}", dev.Description, devid, dev.LksServerHalf[0:8])
serverHalf, err := dev.ToLKSec()
if err != nil {
s.G().Log.Debug("| Failed with error: %s\n", err)
continue
}
fs := s.secret.bug3964Remask(serverHalf)
res, ok := secretbox.Open(nil, data, nonce, fs.f)
if ok {
// This logline is asserted in testing in bug_3964_repairman_test
s.G().Log.Debug("| Success")
return res, serverHalf, nil
}
}
err = PassphraseError{"failed to open secretbox"}
return nil, LKSecServerHalf{}, err
}
func splitCiphertext(src []byte) ([]byte, *[24]byte) {
var nonce [24]byte
copy(nonce[:], src[0:24])
data := src[24:]
return data, &nonce
}
func (s *LKSec) Decrypt(m MetaContext, src []byte) (res []byte, gen PassphraseGeneration, erroneousMask LKSecServerHalf, err error) {
// This logline is asserted in testing in bug_3964_repairman_test
defer m.CTrace("LKSec#Decrypt()", func() error { return err })()
if err = s.Load(m); err != nil {
return nil, 0, LKSecServerHalf{}, err
}
var ok bool
data, nonce := splitCiphertext(src)
res, ok = secretbox.Open(nil, data, nonce, s.secret.f)
if !ok {
m.CDebugf("secretbox.Open failed: attempting recovery")
return s.attemptBug3964Recovery(m, data, nonce)
}
return res, s.ppGen, LKSecServerHalf{}, nil
}
func (s *LKSec) decryptForBug3964Repair(src []byte, dkm DeviceKeyMap) (res []byte, erroneousMask LKSecServerHalf, err error) {
defer s.G().Trace("LKSec#decryptForBug3964Repair()", func() error { return err })()
data, nonce := splitCiphertext(src)
res, ok := secretbox.Open(nil, data, nonce, s.secret.f)
if ok {
s.G().Log.Debug("| Succeeded with intended mask")
return res, LKSecServerHalf{}, nil
}
return s.tryAllDevicesForBug3964Recovery(dkm, data, nonce)
}
func (s *LKSec) ComputeClientHalf() (ret LKSecClientHalf, err error) {
if !s.clientHalf.IsNil() {
return s.clientHalf, nil
}
if s.serverHalf.IsNil() {
return ret, errors.New("LKSec: tried to compute client half, but no server half loaded")
}
if s.secret.IsNil() {
return ret, errors.New("LKSec: tried to compute client half, but no full secret loaded")
}
return s.serverHalf.ComputeClientHalf(s.secret), nil
}
func (s *LKSec) loadSecretSyncer(m MetaContext) (ss *SecretSyncer, err error) {
if lctx := m.LoginContext(); lctx != nil {
if err := lctx.RunSecretSyncer(m, s.uid); err != nil {
return nil, err
}
return lctx.SecretSyncer(), nil
}
aerr := m.G().LoginStateDeprecated().Account(func(a *Account) {
if err = RunSyncer(m, a.SecretSyncer(), s.uid, a.LoggedIn(), a.LocalSession()); err != nil {
return
}
ss = a.SecretSyncer()
}, "LKSec#loadSecretSyncer")
if aerr != nil {
return nil, aerr
}
return ss, err
}
func (s *LKSec) apiServerHalf(m MetaContext, devid keybase1.DeviceID) (dkm DeviceKeyMap, err error) {
var dev DeviceKey
ss, err := s.loadSecretSyncer(m)
if err != nil {
return dkm, err
}
dev, err = ss.FindDevice(devid)
if err != nil {
return dkm, err
}
s.serverHalf, err = dev.ToLKSec()
if err != nil {
return dkm, err
}
s.ppGen = dev.PPGen
return ss.AllDevices(), nil
}
// NewLKSForEncrypt gets a verified passphrase stream, and returns
// an LKS that works for encryption.
func NewLKSecForEncrypt(m MetaContext, ui SecretUI, uid keybase1.UID) (ret *LKSec, err error) {
var pps *PassphraseStream
if pps, err = m.G().LoginStateDeprecated().GetPassphraseStream(m, ui); err != nil {
return
}
ret = NewLKSec(pps, uid, m.G())
return
}
// EncryptClientHalfRecovery takes the client half of the LKS secret
// and encrypts it for the given key. This is for recovery of passphrases
// on device recovery operations.
func (s *LKSec) EncryptClientHalfRecovery(key GenericKey) (string, error) {
if s.clientHalf.IsNil() {
return "", errors.New("Nil LKS Client Half")
}
return key.EncryptToString(s.clientHalf.Bytes(), nil)
}
// ToSKB exports a generic key with the given LKSec to a SecretKeyBundle,
// performing all necessary encryption.
func (s *LKSec) ToSKB(m MetaContext, key GenericKey) (ret *SKB, err error) {
defer m.CTrace("LKSec#ToSKB", func() error { return err })()
if s == nil {
return nil, errors.New("nil lks")
}
ret = NewSKBWithGlobalContext(s.G())
var publicKey RawPublicKey
var privateKey RawPrivateKey
publicKey, privateKey, err = key.ExportPublicAndPrivate()
if err != nil {
return nil, err
}
ret.Priv.Data, err = s.Encrypt(m, []byte(privateKey))
if err != nil {
return nil, err
}
ret.Priv.Encryption = LKSecVersion
ret.Priv.PassphraseGeneration = int(s.Generation())
ret.Pub = []byte(publicKey)
ret.Type = key.GetAlgoType()
return ret, nil
}
func WriteLksSKBToKeyring(m MetaContext, k GenericKey, lks *LKSec) (skb *SKB, err error) {
defer m.CTrace("WriteLksSKBToKeyring", func() error { return err })()
skb, err = lks.ToSKB(m, k)
if err != nil {
return nil, fmt.Errorf("k.ToLksSKB() error: %s", err)
}
if err = skbPushAndSave(m, skb); err != nil {
return nil, err
}
return skb, nil
}
func (s *LKSec) FullSecret() LKSecFullSecret {
if !s.secret.IsNil() {
return s.secret
}
if s.serverHalf.IsNil() || s.clientHalf.IsNil() {
return LKSecFullSecret{}
}
return s.serverHalf.ComputeFullSecret(s.clientHalf)
}
func (s LKSec) ServerHalf() LKSecServerHalf {
return s.serverHalf
}
func (s LKSec) ClientHalf() LKSecClientHalf {
return s.clientHalf
}
type LKSecServerHalfSet struct {
index map[[LKSecLen]byte]bool
}
func NewLKSecServerHalfSet() *LKSecServerHalfSet {
return &LKSecServerHalfSet{
index: make(map[[LKSecLen]byte]bool),
}
}
func (l *LKSecServerHalfSet) Add(s LKSecServerHalf) {
if !s.IsNil() {
l.index[*s.s] = true
}
}
func (l *LKSecServerHalfSet) EncodeToHexList() string {
var s []string
for k := range l.index {
s = append(s, hex.EncodeToString(k[:]))
}
return strings.Join(s, ",")
}