Skip to content

Commit

Permalink
init_dir: use masterkey arg
Browse files Browse the repository at this point in the history
  • Loading branch information
pmazzini authored and rfjakob committed Mar 13, 2024
1 parent 0dfa7f8 commit 8ced867
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cli_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions init_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions internal/configfile/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package cli

import (
"bytes"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 8ced867

Please sign in to comment.