This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
key_store.go
425 lines (374 loc) · 9.86 KB
/
key_store.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
package keys
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"sync"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
hex "github.com/tmthrgd/go-hex"
"golang.org/x/crypto/scrypt"
)
const (
scryptN = 1 << 18
scryptr = 8
scryptp = 1
scryptdkLen = 32
CryptoNone = "none"
CryptoAESGCM = "scrypt-aes-gcm"
HashEd25519 = "go-crypto-0.5.0"
HashSecp256k1 = "btc"
)
//-----------------------------------------------------------------------------
// json encodings
// addresses should be hex encoded
type keyJSON struct {
CurveType string
Address string
PublicKey string
AddressHash string
PrivateKey privateKeyJSON
}
type privateKeyJSON struct {
Crypto string
Plain string `json:",omitempty"`
Salt []byte `json:",omitempty"`
Nonce []byte `json:",omitempty"`
CipherText []byte `json:",omitempty"`
}
func (k *Key) MarshalJSON() (j []byte, err error) {
jStruct := keyJSON{
CurveType: k.CurveType.String(),
Address: hex.EncodeUpperToString(k.Address[:]),
PublicKey: hex.EncodeUpperToString(k.Pubkey()),
AddressHash: k.PublicKey.AddressHashType(),
PrivateKey: privateKeyJSON{Crypto: CryptoNone, Plain: hex.EncodeUpperToString(k.PrivateKey.RawBytes())},
}
j, err = json.Marshal(jStruct)
return j, err
}
func (k *Key) UnmarshalJSON(j []byte) (err error) {
keyJ := new(keyJSON)
err = json.Unmarshal(j, &keyJ)
if err != nil {
return err
}
if len(keyJ.PrivateKey.Plain) == 0 {
return fmt.Errorf("no private key")
}
curveType, err := crypto.CurveTypeFromString(keyJ.CurveType)
if err != nil {
curveType = crypto.CurveTypeEd25519
}
privKey, err := hex.DecodeString(keyJ.PrivateKey.Plain)
if err != nil {
return err
}
k2, err := NewKeyFromPriv(curveType, privKey)
if err != nil {
return err
}
k.Address = k2.Address
k.CurveType = curveType
k.PublicKey = k2.PrivateKey.GetPublicKey()
k.PrivateKey = k2.PrivateKey
return nil
}
// returns the address if valid, nil otherwise
func IsValidKeyJson(j []byte) []byte {
j1 := new(keyJSON)
e1 := json.Unmarshal(j, &j1)
if e1 == nil {
addr, _ := hex.DecodeString(j1.Address)
return addr
}
return nil
}
func NewKeyStore(dir string, AllowBadFilePermissions bool, logger *logging.Logger) *KeyStore {
return &KeyStore{
keysDirPath: dir,
AllowBadFilePermissions: AllowBadFilePermissions,
logger: logger.With(structure.ComponentKey, "keys").WithScope("NewKeyStore"),
}
}
type KeyStore struct {
sync.Mutex
AllowBadFilePermissions bool
keysDirPath string
logger *logging.Logger
}
func (ks *KeyStore) Gen(passphrase string, curveType crypto.CurveType) (key *Key, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("GenerateNewKey error: %v", r)
}
}()
key, err = NewKey(curveType)
if err != nil {
return nil, err
}
err = ks.StoreKey(passphrase, key)
return key, err
}
func (ks *KeyStore) GetKey(passphrase string, keyAddr []byte) (*Key, error) {
ks.Lock()
defer ks.Unlock()
dataDirPath, err := returnDataDir(ks.keysDirPath)
if err != nil {
return nil, err
}
fileContent, err := ks.GetKeyFile(dataDirPath, keyAddr)
if err != nil {
return nil, err
}
key := new(keyJSON)
if err = json.Unmarshal(fileContent, key); err != nil {
return nil, err
}
if len(key.PrivateKey.CipherText) > 0 {
return DecryptKey(passphrase, key)
} else {
key := new(Key)
err = key.UnmarshalJSON(fileContent)
return key, err
}
}
func (ks *KeyStore) AllKeys() ([]*Key, error) {
dataDirPath, err := returnDataDir(ks.keysDirPath)
if err != nil {
return nil, err
}
addrs, err := GetAllAddresses(dataDirPath)
if err != nil {
return nil, err
}
var list []*Key
for _, addr := range addrs {
addrB, err := crypto.AddressFromHexString(addr)
if err != nil {
return nil, err
}
k, err := ks.GetKey("", addrB[:])
if err != nil {
return nil, err
}
list = append(list, k)
}
return list, nil
}
func DecryptKey(passphrase string, keyProtected *keyJSON) (*Key, error) {
salt := keyProtected.PrivateKey.Salt
nonce := keyProtected.PrivateKey.Nonce
cipherText := keyProtected.PrivateKey.CipherText
curveType, err := crypto.CurveTypeFromString(keyProtected.CurveType)
if err != nil {
return nil, err
}
authArray := []byte(passphrase)
derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptr, scryptp, scryptdkLen)
if err != nil {
return nil, err
}
aesBlock, err := aes.NewCipher(derivedKey)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(aesBlock)
if err != nil {
return nil, err
}
pubKey, err := hex.DecodeString(keyProtected.PublicKey)
if err != nil {
return nil, err
}
plainText, err := gcm.Open(nil, nonce, cipherText, nil)
if err != nil {
pkey, _ := NewKeyFromPub(curveType, pubKey)
return pkey, err
}
address, err := crypto.AddressFromHexString(keyProtected.Address)
if err != nil {
return nil, err
}
k, err := NewKeyFromPriv(curveType, plainText)
if err != nil {
return nil, err
}
if address != k.Address {
return nil, fmt.Errorf("address does not match")
}
return k, nil
}
func (ks *KeyStore) GetAllAddresses() (addresses []string, err error) {
ks.Lock()
defer ks.Unlock()
return GetAllAddresses(ks.keysDirPath)
}
func (ks *KeyStore) StoreKey(passphrase string, key *Key) error {
ks.Lock()
defer ks.Unlock()
if passphrase != "" {
return ks.StoreKeyEncrypted(passphrase, key)
} else {
return ks.StoreKeyPlain(key)
}
}
func (ks *KeyStore) StoreKeyPlain(key *Key) (err error) {
keyJSON, err := json.Marshal(key)
if err != nil {
return err
}
dataDirPath, err := returnDataDir(ks.keysDirPath)
if err != nil {
return err
}
err = WriteKeyFile(key.Address[:], dataDirPath, keyJSON)
return err
}
func (ks *KeyStore) StoreKeyEncrypted(passphrase string, key *Key) error {
authArray := []byte(passphrase)
salt := make([]byte, 32)
_, err := rand.Read(salt)
if err != nil {
return err
}
derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptr, scryptp, scryptdkLen)
if err != nil {
return err
}
toEncrypt := key.PrivateKey.RawBytes()
AES256Block, err := aes.NewCipher(derivedKey)
if err != nil {
return err
}
gcm, err := cipher.NewGCM(AES256Block)
if err != nil {
return err
}
// XXX: a GCM nonce may only be used once per key ever!
nonce := make([]byte, gcm.NonceSize())
_, err = rand.Read(nonce)
if err != nil {
return err
}
// (dst, nonce, plaintext, extradata)
cipherText := gcm.Seal(nil, nonce, toEncrypt, nil)
cipherStruct := privateKeyJSON{
Crypto: CryptoAESGCM, Salt: salt, Nonce: nonce, CipherText: cipherText,
}
keyStruct := keyJSON{
CurveType: key.CurveType.String(),
Address: hex.EncodeUpperToString(key.Address[:]),
PublicKey: hex.EncodeUpperToString(key.Pubkey()),
AddressHash: key.PublicKey.AddressHashType(),
PrivateKey: cipherStruct,
}
keyJSON, err := json.Marshal(keyStruct)
if err != nil {
return err
}
dataDirPath, err := returnDataDir(ks.keysDirPath)
if err != nil {
return err
}
return WriteKeyFile(key.Address[:], dataDirPath, keyJSON)
}
func (ks *KeyStore) DeleteKey(passphrase string, keyAddr []byte) (err error) {
dataDirPath, err := returnDataDir(ks.keysDirPath)
if err != nil {
return err
}
keyDirPath := path.Join(dataDirPath, strings.ToUpper(hex.EncodeToString(keyAddr))+".json")
return os.Remove(keyDirPath)
}
func (ks *KeyStore) GetKeyFile(dataDirPath string, keyAddr []byte) (fileContent []byte, err error) {
filename := path.Join(dataDirPath, strings.ToUpper(hex.EncodeToString(keyAddr))+".json")
fileInfo, err := os.Stat(filename)
if err != nil {
return nil, err
}
if (uint32(fileInfo.Mode()) & 0077) != 0 {
ks.logger.InfoMsg("file should be accessible by user only", "file", filename)
if !ks.AllowBadFilePermissions {
return nil, fmt.Errorf("file %s should be accessible by user only", filename)
}
}
return ioutil.ReadFile(filename)
}
func WriteKeyFile(addr []byte, dataDirPath string, content []byte) (err error) {
addrHex := strings.ToUpper(hex.EncodeToString(addr))
keyFilePath := path.Join(dataDirPath, addrHex+".json")
err = os.MkdirAll(dataDirPath, 0700) // read, write and dir search for user
if err != nil {
return err
}
return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user
}
func (ks *KeyStore) GetAllNames() (map[string]string, error) {
return coreNameList(ks.keysDirPath)
}
func GetAllAddresses(dataDirPath string) (addresses []string, err error) {
fileInfos, err := ioutil.ReadDir(dataDirPath)
if err != nil {
return nil, err
}
addresses = make([]string, len(fileInfos))
for i, fileInfo := range fileInfos {
addr := strings.TrimSuffix(fileInfo.Name(), ".json")
addresses[i] = addr
}
return addresses, err
}
type Key struct {
CurveType crypto.CurveType
Address crypto.Address
PublicKey crypto.PublicKey
PrivateKey crypto.PrivateKey
}
func NewKey(typ crypto.CurveType) (*Key, error) {
privKey, err := crypto.GeneratePrivateKey(nil, typ)
if err != nil {
return nil, err
}
pubKey := privKey.GetPublicKey()
return &Key{
CurveType: typ,
PublicKey: pubKey,
Address: pubKey.GetAddress(),
PrivateKey: privKey,
}, nil
}
func (k *Key) Pubkey() []byte {
return k.PublicKey.PublicKey
}
func NewKeyFromPub(curveType crypto.CurveType, PubKeyBytes []byte) (*Key, error) {
pubKey, err := crypto.PublicKeyFromBytes(PubKeyBytes, curveType)
if err != nil {
return nil, err
}
return &Key{
CurveType: curveType,
PublicKey: pubKey,
Address: pubKey.GetAddress(),
}, nil
}
func NewKeyFromPriv(curveType crypto.CurveType, PrivKeyBytes []byte) (*Key, error) {
privKey, err := crypto.PrivateKeyFromRawBytes(PrivKeyBytes, curveType)
if err != nil {
return nil, err
}
pubKey := privKey.GetPublicKey()
return &Key{
CurveType: curveType,
Address: pubKey.GetAddress(),
PublicKey: pubKey,
PrivateKey: privKey,
}, nil
}