forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegatekey.go
276 lines (221 loc) · 6.69 KB
/
delegatekey.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"encoding/hex"
"time"
keybase1 "github.com/keybase/client/go/protocol"
jsonw "github.com/keybase/go-jsonw"
)
// Delegator
//
// Delegates authority to another key.
//
type Delegator struct {
Contextified
// Set these fields
NewKey GenericKey
ExistingKey GenericKey
EldestKID keybase1.KID
Me *User
Expire int
Device *Device
RevSig string
ServerHalf []byte
EncodedPrivateKey string
Ctime int64
DelegationType DelegationType
Aggregated bool // During aggregation we skip some steps (posting, updating some state)
// Optional precalculated values used by KeyProof
LastSeqno Seqno // kex2 HandleDidCounterSign needs to sign subkey without a user but we know what the last seqno was
PrevLinkID LinkID // kex2 HandleDidCounterSign calculates previous link id without a user
SigningUser UserBasic // kex2 doesn't have a full user, but does have basic user info
// Internal fields
sig string
sigID keybase1.SigID
merkleTriple MerkleTriple
postArg APIArg
}
func (d Delegator) getSigningKID() keybase1.KID { return d.GetSigningKey().GetKID() }
func (d Delegator) getExistingKID() (kid keybase1.KID) {
if d.ExistingKey == nil {
return
}
return d.ExistingKey.GetKID()
}
func (d Delegator) GetSigningKey() GenericKey {
if d.ExistingKey != nil {
return d.ExistingKey
}
return d.NewKey
}
func (d Delegator) IsSibkeyOrEldest() bool {
return d.DelegationType == SibkeyType || d.DelegationType == EldestType
}
func (d Delegator) IsEldest() bool { return d.DelegationType == EldestType }
// GetMerkleTriple gets the new MerkleTriple that came about as a result
// of performing the key delegation.
func (d Delegator) GetMerkleTriple() MerkleTriple { return d.merkleTriple }
func (d *Delegator) CheckArgs() (err error) {
G.Log.Debug("+ Delegator::checkArgs()")
if d.NewKey == nil {
err = NoSecretKeyError{}
return
}
if d.DelegationType == "" {
err = MissingDelegationTypeError{}
}
if d.ExistingKey != nil {
G.Log.Debug("| Picked passed-in signing key")
} else {
G.Log.Debug("| Picking new key for an eldest self-sig")
d.DelegationType = EldestType
}
if d.EldestKID.Exists() || d.IsEldest() {
} else if kid := d.Me.GetEldestKID(); kid.IsNil() {
err = NoEldestKeyError{}
return err
} else {
d.EldestKID = kid
}
G.Log.Debug("| Picked key %s for signing", d.getSigningKID())
G.Log.Debug("- Delegator::checkArgs()")
return nil
}
// LoadSigningKey can be called before Run() to load the signing key into
// the delegator. This will check the given key first, then a device Key if we have one,
// and otherwise will leave the signing key unset so that we will set it
// as the eldest key on upload.
// lctx can be nil.
func (d *Delegator) LoadSigningKey(lctx LoginContext, ui SecretUI) (err error) {
G.Log.Debug("+ Delegator::LoadSigningKey")
defer func() {
G.Log.Debug("+ Delegator::LoadSigningKey -> %s, (found=%v)", ErrToOk(err), (d.GetSigningKey() != nil))
}()
if d.ExistingKey != nil {
G.Log.Debug("| Was set ahead of time")
return
}
if d.Me == nil {
d.Me, err = LoadMe(NewLoadUserPubOptionalArg(d.G()))
if err != nil {
return
} else if d.Me == nil {
G.Log.Debug("| Me didn't load")
return
}
}
if !d.Me.HasActiveKey() {
G.Log.Debug("| PGPKeyImportEngine: no active key found, so assuming set of eldest key")
return
}
d.ExistingKey, err = d.G().Keyrings.GetSecretKeyWithPrompt(lctx, SecretKeyArg{
Me: d.Me,
KeyType: DeviceSigningKeyType,
}, ui, "sign new key")
return err
}
// Run the Delegator, performing all necessary internal operations. Return err
// on failure and nil on success.
func (d *Delegator) Run(lctx LoginContext) (err error) {
var jw *jsonw.Wrapper
G.Log.Debug("+ Delegator.Run()")
defer func() {
G.Log.Debug("- Delegator.Run() -> %s", ErrToOk(err))
}()
if err = d.CheckArgs(); err != nil {
return
}
// We'll need to generate two proofs, so set the Ctime
// so that we get the same time both times
d.Ctime = time.Now().Unix()
// For a sibkey signature, we first sign the blob with the
// sibkey, and then embed that signature for the delegating key
if d.DelegationType == SibkeyType {
if jw, err = KeyProof(*d); err != nil {
G.Log.Debug("| Failure in intermediate KeyProof()")
return err
}
if d.RevSig, _, _, err = SignJSON(jw, d.NewKey); err != nil {
G.Log.Debug("| Failure in intermediate SignJson()")
return err
}
}
if d.GStrict() == nil {
panic("null g strict")
}
if d.GStrict().LocalDb == nil {
panic("should have a local DB")
}
if jw, err = KeyProof(*d); err != nil {
G.Log.Debug("| Failure in KeyProof()")
return
}
return d.SignAndPost(lctx, jw)
}
func (d *Delegator) SignAndPost(lctx LoginContext, jw *jsonw.Wrapper) (err error) {
var linkid LinkID
if d.sig, d.sigID, linkid, err = SignJSON(jw, d.GetSigningKey()); err != nil {
G.Log.Debug("| Failure in SignJson()")
return err
}
if err = d.post(lctx); err != nil {
G.Log.Debug("| Failure in post()")
return
}
if err = d.updateLocalState(linkid); err != nil {
return
}
return nil
}
func (d *Delegator) updateLocalState(linkid LinkID) (err error) {
d.Me.SigChainBump(linkid, d.sigID)
d.merkleTriple = MerkleTriple{LinkID: linkid, SigID: d.sigID}
return d.Me.localDelegateKey(d.NewKey, d.sigID, d.getExistingKID(), d.IsSibkeyOrEldest(), d.IsEldest())
}
func (d *Delegator) post(lctx LoginContext) (err error) {
var pub string
if pub, err = d.NewKey.Encode(); err != nil {
return
}
hargs := HTTPArgs{
"sig_id_base": S{Val: d.sigID.ToString(false)},
"sig_id_short": S{Val: d.sigID.ToShortID()},
"sig": S{Val: d.sig},
"type": S{Val: string(d.DelegationType)},
"is_remote_proof": B{Val: false},
"public_key": S{Val: pub},
}
if len(string(d.ServerHalf)) > 0 {
hargs["server_half"] = S{Val: hex.EncodeToString(d.ServerHalf)}
}
if d.DelegationType == PGPUpdateType {
hargs["is_update"] = B{Val: true}
}
if d.IsEldest() {
hargs["is_primary"] = I{Val: 1}
hargs["new_eldest"] = B{Val: true}
hargs["signing_kid"] = S{Val: d.NewKey.GetKID().String()}
} else {
hargs["eldest_kid"] = d.EldestKID
hargs["signing_kid"] = d.getSigningKID()
}
if len(d.EncodedPrivateKey) > 0 {
hargs["private_key"] = S{Val: d.EncodedPrivateKey}
}
arg := APIArg{
Endpoint: "key/add",
NeedSession: true,
Args: hargs,
Contextified: NewContextified(d.G()),
}
if lctx != nil {
arg.SessionR = lctx.LocalSession()
}
if d.Aggregated {
d.postArg = arg
return nil
}
_, err = d.G().API.Post(arg)
return err
}