-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
device_wrap.go
198 lines (164 loc) · 5.26 KB
/
device_wrap.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
)
// DeviceWrap is an engine that wraps DeviceRegister and
// DeviceKeygen.
type DeviceWrap struct {
libkb.Contextified
args *DeviceWrapArgs
signingKey libkb.GenericKey
encryptionKey libkb.NaclDHKeyPair
deviceID keybase1.DeviceID
}
type DeviceWrapArgs struct {
Me *libkb.User
DeviceName string
DeviceType keybase1.DeviceTypeV2
Lks *libkb.LKSec
IsEldest bool
IsSelfProvision bool
Signer libkb.GenericKey
EldestKID keybase1.KID
PerUserKeyring *libkb.PerUserKeyring
EkReboxer *ephemeralKeyReboxer
DeviceID keybase1.DeviceID
// Used in tests for reproducible key generation
naclSigningKeyPair libkb.NaclKeyPair
naclEncryptionKeyPair libkb.NaclKeyPair
}
// NewDeviceWrap creates a DeviceWrap engine.
func NewDeviceWrap(g *libkb.GlobalContext, args *DeviceWrapArgs) *DeviceWrap {
return &DeviceWrap{
args: args,
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *DeviceWrap) Name() string {
return "DeviceWrap"
}
// GetPrereqs returns the engine prereqs.
func (e *DeviceWrap) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *DeviceWrap) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *DeviceWrap) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{
&DeviceKeygen{},
}
}
func (e *DeviceWrap) registerDevice(m libkb.MetaContext) (err error) {
defer m.Trace("DeviceWrap#registerDevice", &err)()
if e.args.Me.HasCurrentDeviceInCurrentInstall() && !e.args.IsSelfProvision {
return libkb.DeviceAlreadyProvisionedError{}
}
if e.deviceID, err = libkb.NewDeviceID(); err != nil {
return err
}
m.Debug("Device name: %s", e.args.DeviceName)
m.Debug("Device ID: %s", e.deviceID)
return e.args.Lks.GenerateServerHalf()
}
func (e *DeviceWrap) genKeys(m libkb.MetaContext) (err error) {
defer m.Trace("DeviceWrap#genKeys", &err)()
kgArgs := &DeviceKeygenArgs{
Me: e.args.Me,
DeviceID: e.deviceID,
DeviceName: e.args.DeviceName,
DeviceType: e.args.DeviceType,
Lks: e.args.Lks,
IsEldest: e.args.IsEldest,
IsSelfProvision: e.args.IsSelfProvision,
PerUserKeyring: e.args.PerUserKeyring,
EkReboxer: e.args.EkReboxer,
naclSigningKeyPair: e.args.naclSigningKeyPair,
naclEncryptionKeyPair: e.args.naclEncryptionKeyPair,
}
kgEng := NewDeviceKeygen(m.G(), kgArgs)
if err = RunEngine2(m, kgEng); err != nil {
return err
}
pargs := &DeviceKeygenPushArgs{
Signer: e.args.Signer,
EldestKID: e.args.EldestKID,
}
if err = kgEng.Push(m, pargs); err != nil {
return err
}
e.signingKey = kgEng.SigningKey()
e.encryptionKey = kgEng.EncryptionKey()
return nil
}
func (e *DeviceWrap) refreshMe(m libkb.MetaContext) (err error) {
defer m.Trace("DeviceWrap#refreshMe", &err)()
if !(e.args.IsEldest || e.args.IsSelfProvision) {
return nil
}
m.Debug("reloading Me because we just bumped eldest seqno or self provisioned")
me, err := libkb.LoadMe(libkb.NewLoadUserArgWithMetaContext(m))
if err != nil {
return err
}
e.args.Me = me
return nil
}
func (e *DeviceWrap) deviceWithKeys(m libkb.MetaContext, mode libkb.KeychainMode) *libkb.DeviceWithKeys {
return libkb.NewDeviceWithKeys(e.signingKey, e.encryptionKey, e.deviceID, e.args.DeviceName, mode)
}
// SwitchConfigAndActiveDevice changes active device to the one
// generated by DeviceWrap. It switches UserConfig and sets global
// ActiveDevice.
func (e *DeviceWrap) SwitchConfigAndActiveDevice(m libkb.MetaContext) (err error) {
defer m.Trace("DeviceWrap#SwitchConfigAndActiveDevice", &err)()
if err = e.refreshMe(m); err != nil {
return err
}
salt, err := e.args.Me.GetSalt()
if err != nil {
return err
}
me := e.args.Me
// Atomically swap to the new config and active device
if e.args.DeviceType == keybase1.DeviceTypeV2_PAPER {
err = m.SwitchUserToActiveOneshotDevice(me.ToUserVersion(), me.GetNormalizedName(), e.deviceWithKeys(m, libkb.KeychainModeNone))
} else {
err = m.SwitchUserNewConfigActiveDevice(me.ToUserVersion(), me.GetNormalizedName(), salt,
e.deviceID, e.signingKey, e.encryptionKey, e.args.DeviceName, libkb.KeychainModeOS)
}
if err != nil {
return err
}
// Sync down secrets for future offline login attempts to work.
// This will largely just download what we just uploaded, but it's
// easy to do this way.
_, w := m.ActiveDevice().SyncSecrets(m)
if w != nil {
m.Warning("Error sync secrets: %s", w.Error())
}
return nil
}
// Run starts the engine.
func (e *DeviceWrap) Run(m libkb.MetaContext) (err error) {
defer m.Trace("DeviceWrap#Run", &err)()
if err = e.registerDevice(m); err != nil {
return err
}
return e.genKeys(m)
}
func (e *DeviceWrap) SigningKey() libkb.GenericKey {
return e.signingKey
}
func (e *DeviceWrap) EncryptionKey() libkb.NaclDHKeyPair {
return e.encryptionKey
}
func (e *DeviceWrap) DeviceID() keybase1.DeviceID {
return e.deviceID
}