Skip to content
Snippets Groups Projects
Commit a97f2e79 authored by Josh Brooks's avatar Josh Brooks
Browse files

Handle file pathing edge cases

parent 3e595537
No related branches found
No related tags found
2 merge requests!23Release,!19Implement mnemonic in client
...@@ -14,9 +14,11 @@ import ( ...@@ -14,9 +14,11 @@ import (
xxMnemonic "gitlab.com/xx_network/crypto/mnemonic" xxMnemonic "gitlab.com/xx_network/crypto/mnemonic"
"gitlab.com/xx_network/primitives/utils" "gitlab.com/xx_network/primitives/utils"
"golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/chacha20poly1305"
"path/filepath"
"strings"
) )
const mnemonicFile = "/.recovery" const mnemonicFile = ".recovery"
// StoreSecretWithMnemonic creates a mnemonic and uses it to encrypt the secret. // StoreSecretWithMnemonic creates a mnemonic and uses it to encrypt the secret.
// This encrypted data saved in storage. // This encrypted data saved in storage.
...@@ -24,12 +26,18 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) { ...@@ -24,12 +26,18 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
// Use fastRNG for RNG ops (AES fortuna based RNG using system RNG) // Use fastRNG for RNG ops (AES fortuna based RNG using system RNG)
rng := fastRNG.NewStreamGenerator(12, 3, csprng.NewSystemRNG).GetStream() rng := fastRNG.NewStreamGenerator(12, 3, csprng.NewSystemRNG).GetStream()
// Ensure path is appended by filepath separator "/"
if !strings.HasSuffix(path, string(filepath.Separator)) {
path = path + string(filepath.Separator)
}
// Create a mnemonic // Create a mnemonic
mnemonic, err := xxMnemonic.GenerateMnemonic(rng, 32) mnemonic, err := xxMnemonic.GenerateMnemonic(rng, 32)
if err != nil { if err != nil {
return "", errors.Errorf("Failed to generate mnemonic: %v", err) return "", errors.Errorf("Failed to generate mnemonic: %v", err)
} }
// Decode mnemonic
decodedMnemonic, err := xxMnemonic.DecodeMnemonic(mnemonic) decodedMnemonic, err := xxMnemonic.DecodeMnemonic(mnemonic)
if err != nil { if err != nil {
return "", errors.Errorf("Failed to decode mnemonic: %v", err) return "", errors.Errorf("Failed to decode mnemonic: %v", err)
...@@ -42,7 +50,8 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) { ...@@ -42,7 +50,8 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
} }
// Save encrypted secret to file // Save encrypted secret to file
err = utils.WriteFileDef(path+mnemonicFile, ciphertext) recoveryFile := path + mnemonicFile
err = utils.WriteFileDef(recoveryFile, ciphertext)
if err != nil { if err != nil {
return "", errors.Errorf("Failed to save mnemonic information to file") return "", errors.Errorf("Failed to save mnemonic information to file")
} }
...@@ -53,16 +62,31 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) { ...@@ -53,16 +62,31 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
// LoadSecretWithMnemonic loads the encrypted secret from storage and decrypts // LoadSecretWithMnemonic loads the encrypted secret from storage and decrypts
// the secret using the given mnemonic. // the secret using the given mnemonic.
func LoadSecretWithMnemonic(mnemonic, path string) (secret []byte, err error) { func LoadSecretWithMnemonic(mnemonic, path string) (secret []byte, err error) {
data, err := utils.ReadFile(path + mnemonicFile) // Ensure path is appended by filepath separator "/"
if !strings.HasSuffix(path, string(filepath.Separator)) {
path = path + string(filepath.Separator)
}
// Ensure that the recovery file exists
recoveryFile := path + mnemonicFile
if !utils.Exists(recoveryFile) {
return nil, errors.Errorf("Recovery file does not exist. " +
"Did you properly set up recovery or provide an incorrect filepath?")
}
// Read file from storage
data, err := utils.ReadFile(recoveryFile)
if err != nil { if err != nil {
return nil, errors.Errorf("Failed to load mnemonic information: %v", err) return nil, errors.Errorf("Failed to load mnemonic information: %v", err)
} }
// Decode mnemonic
decodedMnemonic, err := xxMnemonic.DecodeMnemonic(mnemonic) decodedMnemonic, err := xxMnemonic.DecodeMnemonic(mnemonic)
if err != nil { if err != nil {
return nil, errors.Errorf("Failed to decode mnemonic: %v", err) return nil, errors.Errorf("Failed to decode mnemonic: %v", err)
} }
// Decrypt the stored secret
secret, err = decryptWithMnemonic(data, decodedMnemonic) secret, err = decryptWithMnemonic(data, decodedMnemonic)
if err != nil { if err != nil {
return nil, errors.Errorf("Failed to decrypt secret: %v", err) return nil, errors.Errorf("Failed to decrypt secret: %v", err)
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
func TestStoreSecretWithMnemonic(t *testing.T) { func TestStoreSecretWithMnemonic(t *testing.T) {
secret := []byte("test123") secret := []byte("test123")
storageDir := "ignore.1" storageDir := "ignore.1/"
mnemonic, err := StoreSecretWithMnemonic(secret, storageDir) mnemonic, err := StoreSecretWithMnemonic(secret, storageDir)
if err != nil { if err != nil {
t.Errorf("StoreSecretWithMnemonic error; %v", err) t.Errorf("StoreSecretWithMnemonic error; %v", err)
...@@ -90,6 +90,12 @@ func TestLoadSecretWithMnemonic(t *testing.T) { ...@@ -90,6 +90,12 @@ func TestLoadSecretWithMnemonic(t *testing.T) {
t.Fatalf("Loaded secret does not match original data."+ t.Fatalf("Loaded secret does not match original data."+
"\n\tExpected: %v\n\tReceived: %v", secret, received) "\n\tExpected: %v\n\tReceived: %v", secret, received)
} }
_, err = LoadSecretWithMnemonic(mnemonic, "badDirectory")
if err == nil {
t.Fatalf("LoadSecretWithMnemonic should error when provided a path " +
"where a recovery file does not exist.")
}
} }
// Prng is a PRNG that satisfies the csprng.Source interface. // Prng is a PRNG that satisfies the csprng.Source interface.
......
package bindings
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment