From 8ced867c4f2a26e00e32d7703fe13aa47d9df923 Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Sat, 9 Mar 2024 15:11:24 +0000 Subject: init_dir: use masterkey arg --- cli_args.go | 2 +- init_dir.go | 1 + internal/configfile/config_file.go | 8 ++++++-- tests/cli/cli_test.go | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cli_args.go b/cli_args.go index 48f1cdd..75df4d1 100644 --- a/cli_args.go +++ b/cli_args.go @@ -280,7 +280,7 @@ func parseCliOpts(osArgs []string) (args argContainer) { tlog.Fatal.Printf("The options -passfile and -masterkey cannot be used at the same time") os.Exit(exitcodes.Usage) } - if len(args.extpass) > 0 && args.masterkey != "" { + if len(args.extpass) > 0 && args.masterkey != "" && !args.init { tlog.Fatal.Printf("The options -extpass and -masterkey cannot be used at the same time") os.Exit(exitcodes.Usage) } diff --git a/init_dir.go b/init_dir.go index 5ade692..738590f 100644 --- a/init_dir.go +++ b/init_dir.go @@ -108,6 +108,7 @@ func initDir(args *argContainer) { DeterministicNames: args.deterministic_names, XChaCha20Poly1305: args.xchacha, LongNameMax: args.longnamemax, + Masterkey: handleArgsMasterkey(args), }) if err != nil { tlog.Fatal.Println(err) diff --git a/internal/configfile/config_file.go b/internal/configfile/config_file.go index 2d11346..3d59dc5 100644 --- a/internal/configfile/config_file.go +++ b/internal/configfile/config_file.go @@ -74,6 +74,7 @@ type CreateArgs struct { DeterministicNames bool XChaCha20Poly1305 bool LongNameMax uint8 + Masterkey []byte } // Create - create a new config with a random key encrypted with @@ -126,8 +127,11 @@ func Create(args *CreateArgs) error { return err } { - // Generate new random master key - key := cryptocore.RandBytes(cryptocore.KeyLen) + key := args.Masterkey + if key == nil { + // Generate new random master key + key = cryptocore.RandBytes(cryptocore.KeyLen) + } tlog.PrintMasterkeyReminder(key) // Encrypt it using the password // This sets ScryptObject and EncryptedKey diff --git a/tests/cli/cli_test.go b/tests/cli/cli_test.go index 94a8b16..686d14c 100644 --- a/tests/cli/cli_test.go +++ b/tests/cli/cli_test.go @@ -2,6 +2,8 @@ package cli import ( + "bytes" + "encoding/hex" "fmt" "io/ioutil" "os" @@ -97,6 +99,19 @@ func TestInitReverse(t *testing.T) { } } +// Test -init with -masterkey +func TestInitMasterkey(t *testing.T) { + var testMk = make([]byte, 32) + dir := test_helpers.InitFS(t, fmt.Sprintf("-masterkey=%s", hex.EncodeToString(testMk))) + m, _, err := configfile.LoadAndDecrypt(dir+"/"+configfile.ConfDefaultName, testPw) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(testMk, m) { + t.Error("masterkey does not match") + } +} + // testPasswd changes the password from "test" to "test" using // the -extpass method, then from "test" to "newpasswd" using the // stdin method. -- cgit v1.2.3