Skip to content

Commit

Permalink
configfile: add LoadAndDecrypt wrapper
Browse files Browse the repository at this point in the history
Callers that do not want to decrypt the masterkey should
call plain Load().

#258
  • Loading branch information
rfjakob committed Sep 8, 2018
1 parent 09d28c2 commit 2bdf7d5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 36 deletions.
2 changes: 1 addition & 1 deletion gocryptfs-xray/xray_main.go
Expand Up @@ -61,7 +61,7 @@ func main() {
func dumpMasterKey(fn string) {
tlog.Info.Enabled = false
pw := readpassword.Once("", "")
masterkey, _, err := configfile.Load(fn, pw)
masterkey, _, err := configfile.LoadAndDecrypt(fn, pw)
if err != nil {
fmt.Fprintln(os.Stderr, err)
exitcodes.Exit(err)
Expand Down
52 changes: 33 additions & 19 deletions internal/configfile/config_file.go
Expand Up @@ -118,40 +118,63 @@ func Create(filename string, password []byte, plaintextNames bool,
return cf.WriteFile()
}

// Load - read config file from disk and decrypt the
// LoadAndDecrypt - read config file from disk and decrypt the
// contained key using "password".
// Returns the decrypted key and the ConfFile object
//
// If "password" is empty, the config file is read
// but the key is not decrypted (returns nil in its place).
func Load(filename string, password []byte) ([]byte, *ConfFile, error) {
func LoadAndDecrypt(filename string, password []byte) ([]byte, *ConfFile, error) {
cf, err := Load(filename)
if err != nil {
return nil, nil, err
}
if len(password) == 0 {
// We have validated the config file, but without a password we cannot
// decrypt the master key. Return only the parsed config.
return nil, cf, nil
// TODO: Make this an error in gocryptfs v1.7. All code should now call
// Load() instead of calling LoadAndDecrypt() with an empty password.
}

// Decrypt the masterkey using the password
key, err := cf.DecryptMasterKey(password)
if err != nil {
return nil, nil, err
}

return key, cf, err
}

// Load loads and parses the config file at "filename".
func Load(filename string) (*ConfFile, error) {
var cf ConfFile
cf.filename = filename

// Read from disk
js, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, err
return nil, err
}
if len(js) == 0 {
return nil, nil, fmt.Errorf("Config file is empty")
return nil, fmt.Errorf("Config file is empty")
}

// Unmarshal
err = json.Unmarshal(js, &cf)
if err != nil {
tlog.Warn.Printf("Failed to unmarshal config file")
return nil, nil, err
return nil, err
}

if cf.Version != contentenc.CurrentVersion {
return nil, nil, fmt.Errorf("Unsupported on-disk format %d", cf.Version)
return nil, fmt.Errorf("Unsupported on-disk format %d", cf.Version)
}

// Check that all set feature flags are known
for _, flag := range cf.FeatureFlags {
if !cf.isFeatureFlagKnown(flag) {
return nil, nil, fmt.Errorf("Unsupported feature flag %q", flag)
return nil, fmt.Errorf("Unsupported feature flag %q", flag)
}
}

Expand Down Expand Up @@ -181,20 +204,11 @@ func Load(filename string, password []byte) ([]byte, *ConfFile, error) {
`+tlog.ColorReset)

return nil, nil, exitcodes.NewErr("Deprecated filesystem", exitcodes.DeprecatedFS)
}
if len(password) == 0 {
// We have validated the config file, but without a password we cannot
// decrypt the master key. Return only the parsed config.
return nil, &cf, nil
}

key, err := cf.DecryptMasterKey(password)
if err != nil {
return nil, nil, err
return nil, exitcodes.NewErr("Deprecated filesystem", exitcodes.DeprecatedFS)
}

return key, &cf, err
// All good
return &cf, nil
}

// DecryptMasterKey decrypts the masterkey stored in cf.EncryptedKey using
Expand Down
16 changes: 8 additions & 8 deletions internal/configfile/config_test.go
Expand Up @@ -11,7 +11,7 @@ import (
var testPw = []byte("test")

func TestLoadV1(t *testing.T) {
_, _, err := Load("config_test/v1.conf", testPw)
_, _, err := LoadAndDecrypt("config_test/v1.conf", testPw)
if err == nil {
t.Errorf("Outdated v1 config file must fail to load but it didn't")
} else if testing.Verbose() {
Expand All @@ -24,7 +24,7 @@ func TestLoadV1(t *testing.T) {
func TestLoadV2(t *testing.T) {
t1 := time.Now()

_, _, err := Load("config_test/v2.conf", testPw)
_, _, err := LoadAndDecrypt("config_test/v2.conf", testPw)
if err != nil {
t.Errorf("Could not load v2 config file: %v", err)
}
Expand All @@ -39,21 +39,21 @@ func TestLoadV2PwdError(t *testing.T) {
if !testing.Verbose() {
tlog.Warn.Enabled = false
}
_, _, err := Load("config_test/v2.conf", []byte("wrongpassword"))
_, _, err := LoadAndDecrypt("config_test/v2.conf", []byte("wrongpassword"))
if err == nil {
t.Errorf("Loading with wrong password must fail but it didn't")
}
}

func TestLoadV2Feature(t *testing.T) {
_, _, err := Load("config_test/PlaintextNames.conf", testPw)
_, _, err := LoadAndDecrypt("config_test/PlaintextNames.conf", testPw)
if err != nil {
t.Errorf("Could not load v2 PlaintextNames config file: %v", err)
}
}

func TestLoadV2StrangeFeature(t *testing.T) {
_, _, err := Load("config_test/StrangeFeature.conf", testPw)
_, _, err := LoadAndDecrypt("config_test/StrangeFeature.conf", testPw)
if err == nil {
t.Errorf("Loading unknown feature must fail but it didn't")
} else if testing.Verbose() {
Expand All @@ -66,7 +66,7 @@ func TestCreateConfDefault(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, c, err := Load("config_test/tmp.conf", testPw)
_, c, err := LoadAndDecrypt("config_test/tmp.conf", testPw)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestCreateConfPlaintextnames(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, c, err := Load("config_test/tmp.conf", testPw)
_, c, err := LoadAndDecrypt("config_test/tmp.conf", testPw)
if err != nil {
t.Fatal(err)
}
Expand All @@ -115,7 +115,7 @@ func TestCreateConfFileAESSIV(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, c, err := Load("config_test/tmp.conf", testPw)
_, c, err := LoadAndDecrypt("config_test/tmp.conf", testPw)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Expand Up @@ -36,7 +36,7 @@ var raceDetector bool
func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.ConfFile, err error) {
// First check if the file can be read at all, and find out if a Trezor should
// be used instead of a password.
_, cf1, err := configfile.Load(args.config, nil)
cf1, err := configfile.Load(args.config)
if err != nil {
tlog.Fatal.Printf("Cannot open config file: %v", err)
return nil, nil, err
Expand All @@ -56,7 +56,7 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.Conf
pw = readpassword.Once(args.extpass, "")
}
tlog.Info.Println("Decrypting master key")
masterkey, confFile, err = configfile.Load(args.config, pw)
masterkey, confFile, err = configfile.LoadAndDecrypt(args.config, pw)
for i := range pw {
pw[i] = 0
}
Expand All @@ -73,7 +73,7 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.Conf
func changePassword(args *argContainer) {
// Parse the config file, but do not unlock the master key. We only want to
// know if the Trezor flag is set.
_, cf1, err := configfile.Load(args.config, nil)
cf1, err := configfile.Load(args.config)
if err != nil {
tlog.Fatal.Printf("Cannot open config file: %v", err)
os.Exit(exitcodes.LoadConf)
Expand Down
6 changes: 3 additions & 3 deletions tests/cli/cli_test.go
Expand Up @@ -34,7 +34,7 @@ func TestMain(m *testing.M) {
// Test -init flag
func TestInit(t *testing.T) {
dir := test_helpers.InitFS(t)
_, c, err := configfile.Load(dir+"/"+configfile.ConfDefaultName, testPw)
_, c, err := configfile.LoadAndDecrypt(dir+"/"+configfile.ConfDefaultName, testPw)
if err != nil {
t.Fatal(err)
}
Expand All @@ -51,7 +51,7 @@ func TestInitDevRandom(t *testing.T) {
// Test -init with -aessiv
func TestInitAessiv(t *testing.T) {
dir := test_helpers.InitFS(t, "-aessiv")
_, c, err := configfile.Load(dir+"/"+configfile.ConfDefaultName, testPw)
_, c, err := configfile.LoadAndDecrypt(dir+"/"+configfile.ConfDefaultName, testPw)
if err != nil {
t.Fatal(err)
}
Expand All @@ -63,7 +63,7 @@ func TestInitAessiv(t *testing.T) {
// Test -init with -reverse
func TestInitReverse(t *testing.T) {
dir := test_helpers.InitFS(t, "-reverse")
_, c, err := configfile.Load(dir+"/"+configfile.ConfReverseName, testPw)
_, c, err := configfile.LoadAndDecrypt(dir+"/"+configfile.ConfReverseName, testPw)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/plaintextnames/plaintextnames_test.go
Expand Up @@ -29,7 +29,7 @@ func TestMain(m *testing.M) {

// Only the PlaintextNames feature flag should be set
func TestFlags(t *testing.T) {
_, cf, err := configfile.Load(cDir+"/gocryptfs.conf", testPw)
_, cf, err := configfile.LoadAndDecrypt(cDir+"/gocryptfs.conf", testPw)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/trezor/trezor_test.go
Expand Up @@ -35,7 +35,7 @@ func TestInitTrezor(t *testing.T) {
// vvvvvvvvvvvvv disable -extpass
dir := test_helpers.InitFS(t, "-trezor", "-extpass", "")
// The freshly created config file should have the Trezor feature flag set.
_, c, err := configfile.Load(dir+"/"+configfile.ConfDefaultName, nil)
c, err := configfile.Load(dir + "/" + configfile.ConfDefaultName)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 2bdf7d5

Please sign in to comment.