-
Notifications
You must be signed in to change notification settings - Fork 43
/
keychain.go
415 lines (335 loc) · 9.94 KB
/
keychain.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
package keychain
import (
"crypto/ed25519"
"crypto/sha512"
"encoding/hex"
"os"
"path"
"strings"
"golang.org/x/crypto/pbkdf2"
"errors"
"github.com/99designs/keyring"
ri "github.com/FleekHQ/space-daemon/core/keychain/keyring"
"github.com/FleekHQ/space-daemon/core/permissions"
"github.com/FleekHQ/space-daemon/core/store"
"github.com/FleekHQ/space-daemon/log"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/textileio/go-threads/core/thread"
sym "github.com/textileio/go-threads/crypto/symmetric"
)
const PrivateKeyStoreKey = "key"
const PublicKeyStoreKey = "pub"
const privKeyMnemonicSeparator = "___"
var (
ErrKeyPairNotFound = errors.New("No key pair found in the local db.")
)
type keychain struct {
fileDir string
st store.Store
ring ri.Keyring
privKey *crypto.PrivKey
}
type Keychain interface {
GenerateKeyPair() (pub []byte, priv []byte, err error)
GenerateKeyFromMnemonic(...GenerateKeyFromMnemonicOpts) (mnemonic string, err error)
GetStoredKeyPairInLibP2PFormat() (crypto.PrivKey, crypto.PubKey, error)
GetStoredPublicKey() (crypto.PubKey, error)
GetStoredMnemonic() (string, error)
GetManagedThreadKey(threadKeyName string) (thread.Key, error)
GenerateKeyPairWithForce() (pub []byte, priv []byte, err error)
Sign([]byte) ([]byte, error)
ImportExistingKeyPair(priv crypto.PrivKey, mnemonic string) error
DeleteKeypair() error
StoreAppToken(tok *permissions.AppToken) error
GetAppToken(key string) (*permissions.AppToken, error)
}
type keychainOptions struct {
fileDir string
store store.Store
// Don't use kc.ring directly, use getKeyRing() instead
ring ri.Keyring
}
var defaultKeychainOptions = keychainOptions{
fileDir: store.DefaultRootDir,
}
// Helper function for setting keychain file path for Windows/Linux
func WithPath(path string) Option {
return func(o *keychainOptions) {
if path != "" {
o.fileDir = path
}
}
}
func WithStore(st store.Store) Option {
return func(o *keychainOptions) {
if st != nil {
o.store = st
}
}
}
// Used to inject a mock keyring in tests or in case you want to use a custom keyring implementation
func WithKeyring(ring ri.Keyring) Option {
return func(o *keychainOptions) {
if ring != nil {
o.ring = ring
}
}
}
type Option func(o *keychainOptions)
func New(opts ...Option) *keychain {
o := defaultKeychainOptions
for _, opt := range opts {
opt(&o)
}
if o.store == nil {
defaultStore := store.New(store.WithPath(o.fileDir))
o.store = defaultStore
}
return &keychain{
fileDir: o.fileDir,
st: o.store,
ring: o.ring,
}
}
// Generates a public/private key pair using ed25519 algorithm.
// It stores it in the local db and returns the key pair key.
// If there's already a key pair stored, it returns an error.
// Use GenerateKeyPairWithForce if you want to override existing keys
func (kc *keychain) GenerateKeyPair() ([]byte, []byte, error) {
if val, _ := kc.GetStoredPublicKey(); val != nil {
newErr := errors.New("Error while executing GenerateKeyPair. Key pair already exists. Use GenerateKeyPairWithForce if you want to override it.")
return nil, nil, newErr
}
return kc.generateAndStoreKeyPair(nil, "")
}
// Returns the stored key pair using the same signature than libp2p's GenerateEd25519Key function
func (kc *keychain) GetStoredKeyPairInLibP2PFormat() (crypto.PrivKey, crypto.PubKey, error) {
var priv []byte
var err error
_, err = kc.GetStoredPublicKey()
if err != nil {
return nil, nil, err
}
if kc.privKey != nil {
return *kc.privKey, (*kc.privKey).GetPublic(), nil
}
if priv, _, err = kc.retrieveKeyPair(); err != nil {
newErr := ErrKeyPairNotFound
return nil, nil, newErr
}
var unmarshalledPriv crypto.PrivKey
if unmarshalledPriv, err = crypto.UnmarshalEd25519PrivateKey(priv); err != nil {
return nil, nil, err
}
kc.privKey = &unmarshalledPriv
unmarshalledPub := unmarshalledPriv.GetPublic()
return unmarshalledPriv, unmarshalledPub, nil
}
// Generates a public/private key pair using ed25519 algorithm.
// It stores it in the local db and returns the key pair.
// Warning: If there's already a key pair stored, it overrides it.
func (kc *keychain) GenerateKeyPairWithForce() ([]byte, []byte, error) {
return kc.generateAndStoreKeyPair(nil, "")
}
// Returns the public key currently in use in LibP2P format.
// Returns an error if there's no public key set.
// Unlike GetStoredKeyPairInLibP2PFormat, this method does not access the keychain
func (kc *keychain) GetStoredPublicKey() (crypto.PubKey, error) {
ring, err := kc.getKeyRing()
if err != nil {
return nil, err
}
_, err = ring.GetMetadata(PrivateKeyStoreKey)
if err == keyring.ErrKeyNotFound {
return nil, ErrKeyPairNotFound
}
pubInBytes, err := kc.st.Get([]byte(PublicKeyStoreKey))
if err != nil {
return nil, err
}
if pubInBytes == nil {
return nil, ErrKeyPairNotFound
}
pub, err := crypto.UnmarshalEd25519PublicKey(pubInBytes)
if err != nil {
return nil, err
}
return pub, nil
}
func (kc *keychain) GetStoredMnemonic() (string, error) {
_, mnemonic, err := kc.retrieveKeyPair()
if err != nil {
return "", err
}
return mnemonic, nil
}
// Stores an existing private key in the keychain
// Warning: If there's already a key pair stored, this will override it.
func (kc *keychain) ImportExistingKeyPair(priv crypto.PrivKey, mnemonic string) error {
privInBytes, err := priv.Raw()
if err != nil {
return err
}
pubInBytes, err := priv.GetPublic().Raw()
if err != nil {
return err
}
// Store the key pair in the db
if err := kc.storeKeyPair(privInBytes, pubInBytes, mnemonic); err != nil {
return err
}
kc.privKey = &priv
return nil
}
func (kc *keychain) DeleteKeypair() error {
ring, err := kc.getKeyRing()
if err != nil {
return err
}
// Note: currently ignoring error on keychain removal because it's failing randomly.
// Use GenerateKeyPair with override option instead.
err = ring.Remove(PrivateKeyStoreKey)
err = kc.st.Remove([]byte(PublicKeyStoreKey))
if err != nil {
return err
}
kc.privKey = nil
return nil
}
func (kc *keychain) generateKeyPair(seed []byte) ([]byte, []byte, error) {
if seed != nil {
priv := ed25519.NewKeyFromSeed(seed)
publicKey := priv.Public()
pub, ok := publicKey.(ed25519.PublicKey)
if !ok {
return nil, nil, errors.New("Error while generating key pair from seed")
}
return pub, priv, nil
}
// Compute the key from a random seed
pub, priv, err := ed25519.GenerateKey(nil)
return pub, priv, err
}
func (kc *keychain) generateAndStoreKeyPair(seed []byte, mnemonic string) ([]byte, []byte, error) {
// Compute the key from a random seed
pub, priv, err := kc.generateKeyPair(seed)
if err != nil {
return nil, nil, err
}
// Store the key pair in the db
if err := kc.storeKeyPair(priv, pub, mnemonic); err != nil {
return nil, nil, err
}
privkey, err := crypto.UnmarshalEd25519PrivateKey(priv)
if err != nil {
log.Warn("Unable to cache priv key")
}
kc.privKey = &privkey
return pub, priv, nil
}
// Signs a message using the stored private key.
// Returns an error if the private key cannot be found.
func (kc *keychain) Sign(message []byte) ([]byte, error) {
if priv, _, err := kc.retrieveKeyPair(); err != nil {
return nil, err
} else {
signedBytes := ed25519.Sign(priv, message)
return signedBytes, nil
}
}
func (kc *keychain) getKeyRing() (ri.Keyring, error) {
if kc.ring != nil {
return kc.ring, nil
}
ucd, err := os.UserConfigDir()
if err != nil {
return nil, err
}
return keyring.Open(keyring.Config{
ServiceName: "space",
// MacOS keychain
KeychainTrustApplication: true,
KeychainAccessibleWhenUnlocked: true,
// KDE Wallet
KWalletAppID: "space",
KWalletFolder: "space",
// Windows
WinCredPrefix: "space",
// freedesktop.org's Secret Service
LibSecretCollectionName: "space",
// Pass (https://www.passwordstore.org/)
PassPrefix: "space",
PassDir: kc.fileDir + "/kcpw",
// Fallback encrypted file
FileDir: path.Join(ucd, "space", "keyring"),
})
}
func (kc *keychain) storeKeyPair(privKey []byte, pubKey []byte, mnemonic string) error {
ring, err := kc.getKeyRing()
if err != nil {
return err
}
privAsHex := hex.EncodeToString(privKey)
privWithMnemonic := privAsHex + privKeyMnemonicSeparator + mnemonic
// Store private key together with mnemonic
// Priv key is stored as 0x1234...890___some mnemonic
// The idea behind storing them together is that we avoid asking for keychain access twice
if err := ring.Set(keyring.Item{
Key: PrivateKeyStoreKey,
Data: []byte(privWithMnemonic),
Label: "Space App",
}); err != nil {
return err
}
// Store pub key outside of the key ring for quick access
if err := kc.st.Set([]byte(PublicKeyStoreKey), pubKey); err != nil {
return err
}
return nil
}
func (kc *keychain) retrieveKeyPair() (privKey []byte, mnemonic string, err error) {
ring, err := kc.getKeyRing()
if err != nil {
return nil, "", err
}
privKeyItem, err := ring.Get(PrivateKeyStoreKey)
if err != nil {
return nil, "", err
}
// Priv key is stored as 0x1234...890___some mnemonic
// Here we split it to return priv key and mnemonic separately
privKeyAsStr := string(privKeyItem.Data)
privKeyParts := strings.Split(privKeyAsStr, privKeyMnemonicSeparator)
mnemonic = privKeyParts[1]
privKey, err = hex.DecodeString(privKeyParts[0])
if err != nil {
return nil, "", err
}
return privKey, mnemonic, nil
}
func (kc *keychain) GetManagedThreadKey(threadKeyName string) (thread.Key, error) {
// Check if there's a key stored before continuing
_, err := kc.GetStoredPublicKey()
if err != nil {
return thread.Key{}, err
}
size := 32
priv, _, err := kc.GetStoredKeyPairInLibP2PFormat()
if err != nil {
return thread.Key{}, err
}
privBytes, err := priv.Raw()
if err != nil {
return thread.Key{}, err
}
num := pbkdf2.Key(privBytes, []byte("threadKey"+threadKeyName), 256, size, sha512.New)
if err != nil {
return thread.Key{}, err
}
truncated := num[:sym.KeyBytes*2]
managedKey, err := thread.KeyFromBytes(truncated)
if err != nil {
return thread.Key{}, err
}
return managedKey, nil
}