forked from ethereumproject/go-ethereum
/
keystore.go
475 lines (405 loc) · 11.7 KB
/
keystore.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
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package accounts
import (
"bytes"
"crypto/aes"
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt"
"github.com/ethereumproject/go-ethereum/common"
"github.com/ethereumproject/go-ethereum/crypto"
"github.com/ethereumproject/go-ethereum/crypto/randentropy"
"github.com/ethereumproject/go-ethereum/crypto/secp256k1"
)
type key struct {
UUID string
// to simplify lookups we also store the address
Address common.Address
// we only store privkey as pubkey/address can be derived from it
// privkey in this struct is always in plaintext
PrivateKey *ecdsa.PrivateKey
}
type plainKeyJSON struct {
ID string `json:"id"`
Address string `json:"address"`
PrivateKey string `json:"privatekey"`
Version int `json:"version"`
}
func (k *key) MarshalJSON() (j []byte, err error) {
jStruct := plainKeyJSON{
ID: k.UUID,
Address: hex.EncodeToString(k.Address[:]),
PrivateKey: hex.EncodeToString(crypto.FromECDSA(k.PrivateKey)),
Version: 3,
}
j, err = json.Marshal(jStruct)
return j, err
}
func (k *key) UnmarshalJSON(j []byte) (err error) {
keyJSON := new(plainKeyJSON)
err = json.Unmarshal(j, &keyJSON)
if err != nil {
return err
}
k.UUID = keyJSON.ID
addr, err := hex.DecodeString(keyJSON.Address)
if err != nil {
return err
}
k.Address = common.BytesToAddress(addr)
privkey, err := hex.DecodeString(keyJSON.PrivateKey)
if err != nil {
return err
}
k.PrivateKey = crypto.ToECDSA(privkey)
return nil
}
// newKeyUUID returns an identifier for key.
func newKeyUUID() (string, error) {
var u [16]byte
if _, err := rand.Read(u[:]); err != nil {
return "", err
}
u[6] = (u[6] & 0x0f) | 0x40 // version 4
u[8] = (u[8] & 0x3f) | 0x80 // variant 10
return fmt.Sprintf("%x-%x-%x-%x-%x", u[:4], u[4:6], u[6:8], u[8:10], u[10:]), nil
}
func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) (*key, error) {
id, err := newKeyUUID()
if err != nil {
return nil, err
}
return &key{
UUID: id,
Address: crypto.PubkeyToAddress(privateKeyECDSA.PublicKey),
PrivateKey: privateKeyECDSA,
}, nil
}
func storeNewKey(store *keyStore, secret string) (*key, Account, error) {
privateKeyECDSA, err := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
if err != nil {
return nil, Account{}, err
}
key, err := newKeyFromECDSA(privateKeyECDSA)
if err != nil {
return nil, Account{}, err
}
file, err := store.Insert(key, secret)
if err != nil {
return nil, Account{}, err
}
return key, Account{Address: key.Address, File: file}, err
}
type keyStore struct {
baseDir string // absolute filepath to default/flagged value for eg datadir/mainnet/keystore
scryptN int
scryptP int
}
func newKeyStore(dir string, scryptN, scryptP int) (*keyStore, error) {
if !filepath.IsAbs(dir) {
var err error
dir, err = filepath.Abs(dir)
if err != nil {
return nil, err
}
}
if err := os.MkdirAll(dir, 0700); err != nil {
return nil, err
}
return &keyStore{
baseDir: dir,
scryptN: scryptN,
scryptP: scryptP,
}, nil
}
func (store *keyStore) DecryptKey(data []byte, secret string) (*key, error) {
key, err := decryptKey(data, secret)
if err != nil {
return nil, err
}
return key, nil
}
func (store *keyStore) Lookup(file string, secret string) (*key, error) {
if !filepath.IsAbs(file) {
file = filepath.Join(store.baseDir, file)
}
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
key, err := decryptKey(data, secret)
if err != nil {
return nil, err
}
return key, nil
}
func (store *keyStore) Insert(key *key, secret string) (file string, err error) {
data, err := encryptKey(key, secret, store.scryptN, store.scryptP)
if err != nil {
return "", err
}
timestamp := time.Now().UTC().Format("2006-01-02T15-04-05.999999999")
file = fmt.Sprintf("UTC--%sZ--%x", timestamp, key.Address[:])
file = filepath.Join(store.baseDir, file)
if err := writeKeyFile(file, data); err != nil {
return "", err
}
return file, nil
}
func (store keyStore) Update(file string, key *key, secret string) error {
data, err := encryptKey(key, secret, store.scryptN, store.scryptP)
if err != nil {
return err
}
if !filepath.IsAbs(file) {
file = filepath.Join(store.baseDir, file)
}
return writeKeyFile(file, data)
}
// https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
// web3v3 is a version 3 encrypted key store record.
type web3v3 struct {
ID string `json:"id"`
Address string `json:"address"`
Crypto cryptoJSON `json:"crypto"`
Version int `json:"version"`
}
// web3v3 is a version 1 encrypted key store record.
type web3v1 struct {
ID string `json:"id"`
Address string `json:"address"`
Crypto cryptoJSON `json:"crypto"`
Version string `json:"version"`
}
type cryptoJSON struct {
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherparamsJSON `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams map[string]interface{} `json:"kdfparams"`
MAC string `json:"mac"`
}
type cipherparamsJSON struct {
IV string `json:"iv"`
}
// encryptKey encrypts key as version 3.
func encryptKey(key *key, secret string, scryptN, scryptP int) ([]byte, error) {
salt := randentropy.GetEntropyCSPRNG(32)
derivedKey, err := scrypt.Key([]byte(secret), salt, scryptN, scryptR, scryptP, scryptDKLen)
if err != nil {
return nil, err
}
encryptKey := derivedKey[:16]
keyBytes := crypto.FromECDSA(key.PrivateKey)
iv := randentropy.GetEntropyCSPRNG(aes.BlockSize) // 16
cipherText, err := aesCTRXOR(encryptKey, keyBytes, iv)
if err != nil {
return nil, err
}
mac := crypto.Keccak256(derivedKey[16:32], cipherText)
return json.Marshal(web3v3{
ID: key.UUID,
Address: hex.EncodeToString(key.Address[:]),
Crypto: cryptoJSON{
Cipher: "aes-128-ctr",
CipherText: hex.EncodeToString(cipherText),
CipherParams: cipherparamsJSON{
IV: hex.EncodeToString(iv),
},
KDF: "scrypt",
KDFParams: map[string]interface{}{
"n": scryptN,
"r": scryptR,
"p": scryptP,
"dklen": scryptDKLen,
"salt": hex.EncodeToString(salt),
},
MAC: hex.EncodeToString(mac),
},
Version: 3,
})
}
// Web3PrivateKey decrypts the record with secret and returns the private key.
func Web3PrivateKey(web3JSON []byte, secret string) (*ecdsa.PrivateKey, error) {
k, err := decryptKey(web3JSON, secret)
if err != nil {
return nil, err
}
return k.PrivateKey, nil
}
// decryptKey decrypts a key from a JSON blob, returning the private key itself.
func decryptKey(web3JSON []byte, secret string) (*key, error) {
// Parse the JSON into a simple map to fetch the key version
m := make(map[string]interface{})
if err := json.Unmarshal(web3JSON, &m); err != nil {
return nil, err
}
// Depending on the version try to parse one way or another
var (
keyBytes []byte
keyUUID string
)
if version, ok := m["version"].(string); ok && version == "1" {
w := new(web3v1)
if err := json.Unmarshal(web3JSON, w); err != nil {
return nil, err
}
keyUUID = w.ID
var err error
keyBytes, err = decryptKeyV1(w, secret)
if err != nil {
return nil, err
}
} else {
w := new(web3v3)
if err := json.Unmarshal(web3JSON, w); err != nil {
return nil, err
}
if w.Version != 3 {
return nil, fmt.Errorf("unsupported Web3 version: %v", version)
}
keyUUID = w.ID
var err error
keyBytes, err = decryptKeyV3(w, secret)
if err != nil {
return nil, err
}
}
k := crypto.ToECDSA(keyBytes)
return &key{
UUID: keyUUID,
Address: crypto.PubkeyToAddress(k.PublicKey),
PrivateKey: k,
}, nil
}
func decryptKeyV3(keyProtected *web3v3, secret string) (keyBytes []byte, err error) {
if keyProtected.Crypto.Cipher != "aes-128-ctr" {
return nil, fmt.Errorf("Cipher not supported: %v", keyProtected.Crypto.Cipher)
}
mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
if err != nil {
return nil, err
}
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
if err != nil {
return nil, err
}
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
if err != nil {
return nil, err
}
derivedKey, err := getKDFKey(keyProtected.Crypto, secret)
if err != nil {
return nil, err
}
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
if !bytes.Equal(calculatedMAC, mac) {
return nil, ErrDecrypt
}
plainText, err := aesCTRXOR(derivedKey[:16], cipherText, iv)
if err != nil {
return nil, err
}
return plainText, err
}
func decryptKeyV1(keyProtected *web3v1, secret string) (keyBytes []byte, err error) {
mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
if err != nil {
return nil, err
}
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
if err != nil {
return nil, err
}
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
if err != nil {
return nil, err
}
derivedKey, err := getKDFKey(keyProtected.Crypto, secret)
if err != nil {
return nil, err
}
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
if !bytes.Equal(calculatedMAC, mac) {
return nil, ErrDecrypt
}
plainText, err := aesCBCDecrypt(crypto.Keccak256(derivedKey[:16])[:16], cipherText, iv)
if err != nil {
return nil, err
}
return plainText, err
}
func getKDFKey(cryptoJSON cryptoJSON, secret string) ([]byte, error) {
salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
if err != nil {
return nil, err
}
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
if cryptoJSON.KDF == "scrypt" {
n := ensureInt(cryptoJSON.KDFParams["n"])
r := ensureInt(cryptoJSON.KDFParams["r"])
p := ensureInt(cryptoJSON.KDFParams["p"])
return scrypt.Key([]byte(secret), salt, n, r, p, dkLen)
} else if cryptoJSON.KDF == "pbkdf2" {
c := ensureInt(cryptoJSON.KDFParams["c"])
prf := cryptoJSON.KDFParams["prf"].(string)
if prf != "hmac-sha256" {
return nil, fmt.Errorf("Unsupported PBKDF2 PRF: %s", prf)
}
key := pbkdf2.Key([]byte(secret), salt, c, dkLen, sha256.New)
return key, nil
}
return nil, fmt.Errorf("Unsupported KDF: %s", cryptoJSON.KDF)
}
// TODO: can we do without this when unmarshalling dynamic JSON?
// why do integers in KDF params end up as float64 and not int after
// unmarshal?
func ensureInt(x interface{}) int {
res, ok := x.(int)
if !ok {
res = int(x.(float64))
}
return res
}
func writeKeyFile(file string, content []byte) error {
dir, basename := filepath.Split(file)
// Atomic write: create a temporary hidden file first
// then move it into place. TempFile assigns mode 0600.
f, err := ioutil.TempFile(dir, "."+basename+".tmp")
if err != nil {
return err
}
if _, err := f.Write(content); err != nil {
f.Close()
os.Remove(f.Name())
return err
}
if err := f.Close(); err != nil {
return err
}
// BUG(pascaldekloe): Windows won't allow updates to a keyfile when it is being read.
return os.Rename(f.Name(), file)
}