From 6ee01a2e7495b6982adf566d7935e1688abfd675 Mon Sep 17 00:00:00 2001 From: Gilbert Chen Date: Sun, 20 Sep 2020 20:03:52 -0400 Subject: [PATCH] Allow RSA keys to be passed directly via CML instead of a file This change is intended to be used by the web GUI to create an RSA encrypted storage. --- src/duplicacy_config.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/duplicacy_config.go b/src/duplicacy_config.go index 6692fea..6db9325 100644 --- a/src/duplicacy_config.go +++ b/src/duplicacy_config.go @@ -18,6 +18,7 @@ import ( "fmt" "hash" "os" + "strings" "runtime" "runtime/debug" "sync/atomic" @@ -554,10 +555,16 @@ func ConfigStorage(storage Storage, iterations int, compressionLevel int, averag } func (config *Config) loadRSAPublicKey(keyFile string) { - encodedKey, err := ioutil.ReadFile(keyFile) - if err != nil { - LOG_ERROR("BACKUP_KEY", "Failed to read the public key file: %v", err) - return + encodedKey := []byte(keyFile) + var err error + + // keyFile may be the actually key, in which case we don't need to read from a file + if !strings.Contains(keyFile, "-----BEGIN") { + encodedKey, err = ioutil.ReadFile(keyFile) + if err != nil { + LOG_ERROR("BACKUP_KEY", "Failed to read the public key file: %v", err) + return + } } decodedKey, _ := pem.Decode(encodedKey) @@ -593,10 +600,16 @@ func (config *Config) loadRSAPrivateKey(keyFile string, passphrase string) { return } - encodedKey, err := ioutil.ReadFile(keyFile) - if err != nil { - LOG_ERROR("RSA_PRIVATE", "Failed to read the private key file: %v", err) - return + encodedKey := []byte(keyFile) + var err error + + // keyFile may be the actually key, in which case we don't need to read from a file + if !strings.Contains(keyFile, "-----BEGIN") { + encodedKey, err = ioutil.ReadFile(keyFile) + if err != nil { + LOG_ERROR("RSA_PRIVATE", "Failed to read the private key file: %v", err) + return + } } decodedKey, _ := pem.Decode(encodedKey)