2018-11-08 03:22:31 +00:00
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-04-30 15:00:37 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2018-11-08 03:22:31 +00:00
|
|
|
"os"
|
2020-04-30 15:00:37 +00:00
|
|
|
"path"
|
2018-11-08 03:22:31 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
2019-02-15 18:31:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-02-15 19:04:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-08-25 10:18:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2018-11-08 03:22:31 +00:00
|
|
|
)
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestStoreAndGetKey(t *testing.T) {
|
2020-04-30 15:00:37 +00:00
|
|
|
tempDir, teardown := setupTempKeystoreDir(t)
|
|
|
|
defer teardown()
|
2020-09-23 08:59:49 +00:00
|
|
|
ks := &Keystore{
|
2020-04-30 15:00:37 +00:00
|
|
|
keysDirPath: tempDir,
|
2018-11-08 03:22:31 +00:00
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:45:29 +00:00
|
|
|
key, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir, key, "password"))
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKey, err := ks.GetKey(tempDir, "password")
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), key.SecretKey.Marshal()))
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
2019-03-29 22:26:41 +00:00
|
|
|
|
|
|
|
func TestStoreAndGetKeys(t *testing.T) {
|
2020-04-30 15:00:37 +00:00
|
|
|
tempDir, teardown := setupTempKeystoreDir(t)
|
|
|
|
defer teardown()
|
2020-09-23 08:59:49 +00:00
|
|
|
ks := &Keystore{
|
2020-04-30 15:00:37 +00:00
|
|
|
keysDirPath: tempDir,
|
2019-03-29 22:26:41 +00:00
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:45:29 +00:00
|
|
|
key, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir+"/test-1", key, "password"))
|
2020-01-07 23:45:29 +00:00
|
|
|
key2, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir+"/test-2", key, "password"))
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-30 15:00:37 +00:00
|
|
|
for _, s := range decryptedKeys {
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()) && !bytes.Equal(s.SecretKey.Marshal(), key2.SecretKey.Marshal()))
|
2019-03-29 22:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 03:22:31 +00:00
|
|
|
func TestEncryptDecryptKey(t *testing.T) {
|
|
|
|
newID := uuid.NewRandom()
|
2019-02-19 15:09:50 +00:00
|
|
|
b := []byte("hi")
|
|
|
|
b32 := bytesutil.ToBytes32(b)
|
2018-11-08 03:22:31 +00:00
|
|
|
password := "test"
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
pk, err := bls.SecretKeyFromBytes(b32[:])
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
key := &Key{
|
2019-02-19 15:09:50 +00:00
|
|
|
ID: newID,
|
|
|
|
SecretKey: pk,
|
|
|
|
PublicKey: pk.PublicKey(),
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:00:37 +00:00
|
|
|
keyJSON, err := EncryptKey(key, password, LightScryptN, LightScryptP)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKey, err := DecryptKey(keyJSON, password)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, bytes.Equal(decryptedKey.ID, newID))
|
2019-02-19 15:09:50 +00:00
|
|
|
expected := pk.Marshal()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), expected))
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
2020-04-29 18:31:22 +00:00
|
|
|
|
|
|
|
func TestGetSymlinkedKeys(t *testing.T) {
|
2020-04-30 15:00:37 +00:00
|
|
|
tempDir, teardown := setupTempKeystoreDir(t)
|
|
|
|
defer teardown()
|
2020-09-23 08:59:49 +00:00
|
|
|
ks := &Keystore{
|
2020-04-29 18:31:22 +00:00
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir+"/files/test-1", key, "password"))
|
|
|
|
require.NoError(t, os.Symlink(tempDir+"/files/test-1", tempDir+"/test-1"))
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(decryptedKeys))
|
2020-04-30 15:00:37 +00:00
|
|
|
for _, s := range decryptedKeys {
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()))
|
2020-04-29 18:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 15:00:37 +00:00
|
|
|
|
|
|
|
// setupTempKeystoreDir creates temporary directory for storing keystore files.
|
|
|
|
func setupTempKeystoreDir(t *testing.T) (string, func()) {
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-30 15:00:37 +00:00
|
|
|
tempDir := path.Join(testutil.TempDir(), fmt.Sprintf("%d", randPath), "keystore")
|
|
|
|
|
|
|
|
return tempDir, func() {
|
2020-08-25 15:23:06 +00:00
|
|
|
assert.NoError(t, os.RemoveAll(tempDir))
|
2020-04-30 15:00:37 +00:00
|
|
|
}
|
|
|
|
}
|