2016-07-06 21:51:25 +02:00
|
|
|
// Package configfile reads and writes gocryptfs.conf does the key
|
|
|
|
// wrapping.
|
2016-02-06 19:20:54 +01:00
|
|
|
package configfile
|
2015-09-13 17:55:07 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-11-01 12:11:36 +01:00
|
|
|
"fmt"
|
2015-10-04 14:36:20 +02:00
|
|
|
"io/ioutil"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
2016-02-06 20:23:36 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
2017-02-26 19:25:23 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2015-09-13 17:55:07 +02:00
|
|
|
)
|
|
|
|
import "os"
|
|
|
|
|
|
|
|
const (
|
2016-10-02 06:14:18 +02:00
|
|
|
// ConfDefaultName is the default configuration file name.
|
2015-09-13 17:55:07 +02:00
|
|
|
// The dot "." is not used in base64url (RFC4648), hence
|
|
|
|
// we can never clash with an encrypted file.
|
2015-11-27 22:18:36 +01:00
|
|
|
ConfDefaultName = "gocryptfs.conf"
|
2016-10-02 06:14:18 +02:00
|
|
|
// ConfReverseName is the default configuration file name in reverse mode,
|
|
|
|
// the config file gets stored next to the plain-text files. Make it hidden
|
|
|
|
// (start with dot) to not annoy the user.
|
2016-08-29 22:05:39 +02:00
|
|
|
ConfReverseName = ".gocryptfs.reverse.conf"
|
2015-09-13 17:55:07 +02:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// ConfFile is the content of a config file.
|
2015-10-07 21:26:17 +02:00
|
|
|
type ConfFile struct {
|
2016-10-02 06:14:18 +02:00
|
|
|
// Creator is the gocryptfs version string.
|
2016-06-05 11:33:54 +02:00
|
|
|
// This only documents the config file for humans who look at it. The actual
|
|
|
|
// technical info is contained in FeatureFlags.
|
|
|
|
Creator string
|
2016-10-02 06:14:18 +02:00
|
|
|
// EncryptedKey holds an encrypted AES key, unlocked using a password
|
|
|
|
// hashed with scrypt
|
2015-09-13 21:47:18 +02:00
|
|
|
EncryptedKey []byte
|
2016-10-02 06:14:18 +02:00
|
|
|
// ScryptObject stores parameters for scrypt hashing (key derivation)
|
|
|
|
ScryptObject ScryptKDF
|
|
|
|
// Version is the On-Disk-Format version this filesystem uses
|
2015-11-01 01:32:33 +01:00
|
|
|
Version uint16
|
2016-10-02 06:14:18 +02:00
|
|
|
// FeatureFlags is a list of feature flags this filesystem has enabled.
|
2015-11-03 21:05:47 +01:00
|
|
|
// If gocryptfs encounters a feature flag it does not support, it will refuse
|
|
|
|
// mounting. This mechanism is analogous to the ext4 feature flags that are
|
|
|
|
// stored in the superblock.
|
|
|
|
FeatureFlags []string
|
2016-10-02 06:14:18 +02:00
|
|
|
// Filename is the name of the config file. Not exported to JSON.
|
2016-06-05 11:33:54 +02:00
|
|
|
filename string
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
|
|
|
|
2015-09-13 21:47:18 +02:00
|
|
|
// CreateConfFile - create a new config with a random key encrypted with
|
2015-11-29 18:52:58 +01:00
|
|
|
// "password" and write it to "filename".
|
|
|
|
// Uses scrypt with cost parameter logN.
|
2017-03-05 18:13:56 +01:00
|
|
|
func CreateConfFile(filename string, password string, plaintextNames bool, logN int, creator string, aessiv bool) error {
|
2015-10-07 21:26:17 +02:00
|
|
|
var cf ConfFile
|
2015-09-13 17:55:07 +02:00
|
|
|
cf.filename = filename
|
2016-06-05 11:33:54 +02:00
|
|
|
cf.Creator = creator
|
2016-02-06 19:20:54 +01:00
|
|
|
cf.Version = contentenc.CurrentVersion
|
2015-09-13 17:55:07 +02:00
|
|
|
|
2015-12-10 19:38:02 +01:00
|
|
|
// Set feature flags
|
2016-09-26 23:25:13 +02:00
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagGCMIV128])
|
2017-03-07 21:03:05 +01:00
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagHKDF])
|
2015-11-03 21:05:47 +01:00
|
|
|
if plaintextNames {
|
2016-06-05 12:53:10 +02:00
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagPlaintextNames])
|
2015-12-08 16:13:29 +01:00
|
|
|
} else {
|
2016-06-05 12:53:10 +02:00
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagDirIV])
|
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagEMENames])
|
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagLongNames])
|
2017-03-06 22:20:28 +01:00
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagRaw64])
|
2015-11-03 21:05:47 +01:00
|
|
|
}
|
2016-09-26 23:25:13 +02:00
|
|
|
if aessiv {
|
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagAESSIV])
|
2016-09-20 22:59:10 +02:00
|
|
|
}
|
2015-09-13 17:55:07 +02:00
|
|
|
|
2017-03-05 21:59:55 +01:00
|
|
|
// Generate new random master key
|
|
|
|
key := cryptocore.RandBytes(cryptocore.KeyLen)
|
|
|
|
|
|
|
|
// Encrypt it using the password
|
|
|
|
// This sets ScryptObject and EncryptedKey
|
|
|
|
// Note: this looks at the FeatureFlags, so call it AFTER setting them.
|
|
|
|
cf.EncryptKey(key, password, logN)
|
|
|
|
|
2015-11-02 23:08:51 +01:00
|
|
|
// Write file to disk
|
|
|
|
return cf.WriteFile()
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
|
|
|
|
2015-09-13 21:47:18 +02:00
|
|
|
// LoadConfFile - read config file from disk and decrypt the
|
2017-01-26 21:32:08 +01:00
|
|
|
// contained key using "password".
|
2015-11-03 21:05:47 +01:00
|
|
|
// Returns the decrypted key and the ConfFile object
|
2017-01-26 21:32:08 +01:00
|
|
|
//
|
|
|
|
// If "password" is empty, the config file is read
|
|
|
|
// but the key is not decrypted (returns nil in its place).
|
2015-10-07 21:26:17 +02:00
|
|
|
func LoadConfFile(filename string, password string) ([]byte, *ConfFile, error) {
|
|
|
|
var cf ConfFile
|
2015-09-13 21:47:18 +02:00
|
|
|
cf.filename = filename
|
|
|
|
|
2015-09-13 17:55:07 +02:00
|
|
|
// Read from disk
|
|
|
|
js, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2015-10-07 21:26:17 +02:00
|
|
|
return nil, nil, err
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
2015-09-13 21:47:18 +02:00
|
|
|
|
|
|
|
// Unmarshal
|
2015-09-13 17:55:07 +02:00
|
|
|
err = json.Unmarshal(js, &cf)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("Failed to unmarshal config file")
|
2015-10-07 21:26:17 +02:00
|
|
|
return nil, nil, err
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
if cf.Version != contentenc.CurrentVersion {
|
2016-01-20 20:55:56 +01:00
|
|
|
return nil, nil, fmt.Errorf("Unsupported on-disk format %d", cf.Version)
|
2015-11-03 21:05:47 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 23:13:10 +02:00
|
|
|
// Check that all set feature flags are known
|
2015-11-14 17:16:17 +01:00
|
|
|
for _, flag := range cf.FeatureFlags {
|
2016-06-06 23:13:10 +02:00
|
|
|
if !cf.isFeatureFlagKnown(flag) {
|
|
|
|
return nil, nil, fmt.Errorf("Unsupported feature flag %q", flag)
|
2015-11-03 21:05:47 +01:00
|
|
|
}
|
2015-11-01 01:32:33 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 23:13:10 +02:00
|
|
|
// Check that all required feature flags are set
|
|
|
|
var requiredFlags []flagIota
|
|
|
|
if cf.IsFeatureFlagSet(FlagPlaintextNames) {
|
|
|
|
requiredFlags = requiredFlagsPlaintextNames
|
|
|
|
} else {
|
|
|
|
requiredFlags = requiredFlagsNormal
|
|
|
|
}
|
|
|
|
deprecatedFs := false
|
|
|
|
for _, i := range requiredFlags {
|
|
|
|
if !cf.IsFeatureFlagSet(i) {
|
2016-06-19 20:01:04 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "Required feature flag %q is missing\n", knownFlags[i])
|
2016-06-06 23:13:10 +02:00
|
|
|
deprecatedFs = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deprecatedFs {
|
2016-06-19 20:01:04 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "\033[33m"+`
|
|
|
|
The filesystem was created by gocryptfs v0.6 or earlier. This version of
|
|
|
|
gocryptfs can no longer mount the filesystem.
|
|
|
|
Please download gocryptfs v0.11 and upgrade your filesystem,
|
|
|
|
see https://github.com/rfjakob/gocryptfs/wiki/Upgrading for instructions.
|
2016-06-16 23:23:09 +02:00
|
|
|
|
2016-06-19 20:01:04 +02:00
|
|
|
If you have trouble upgrading, join the discussion at
|
|
|
|
https://github.com/rfjakob/gocryptfs/issues/29 .
|
2016-06-06 23:13:10 +02:00
|
|
|
|
2016-06-19 20:01:04 +02:00
|
|
|
`+"\033[0m")
|
|
|
|
|
|
|
|
return nil, nil, fmt.Errorf("Deprecated filesystem")
|
2016-06-06 23:13:10 +02:00
|
|
|
}
|
2016-10-16 18:17:28 +02:00
|
|
|
if password == "" {
|
|
|
|
// 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
|
|
|
|
}
|
2017-03-05 21:59:55 +01:00
|
|
|
|
2015-09-13 21:47:18 +02:00
|
|
|
// Generate derived key from password
|
|
|
|
scryptHash := cf.ScryptObject.DeriveKey(password)
|
|
|
|
|
|
|
|
// Unlock master key using password-based key
|
2017-03-05 21:59:55 +01:00
|
|
|
useHKDF := cf.IsFeatureFlagSet(FlagHKDF)
|
|
|
|
ce := getKeyEncrypter(scryptHash, useHKDF)
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Enabled = false // Silence DecryptBlock() error messages on incorrect password
|
2016-02-06 19:20:54 +01:00
|
|
|
key, err := ce.DecryptBlock(cf.EncryptedKey, 0, nil)
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Enabled = true
|
2015-09-13 17:55:07 +02:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("failed to unlock master key: %s", err.Error())
|
2017-02-26 19:25:23 +01:00
|
|
|
return nil, nil, exitcodes.NewErr("Password incorrect.", exitcodes.PasswordIncorrect)
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
2015-09-13 21:47:18 +02:00
|
|
|
|
2016-06-16 23:23:09 +02:00
|
|
|
return key, &cf, err
|
2015-10-07 21:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptKey - encrypt "key" using an scrypt hash generated from "password"
|
2015-11-29 18:52:58 +01:00
|
|
|
// and store it in cf.EncryptedKey.
|
|
|
|
// Uses scrypt with cost parameter logN and stores the scrypt parameters in
|
|
|
|
// cf.ScryptObject.
|
|
|
|
func (cf *ConfFile) EncryptKey(key []byte, password string, logN int) {
|
2015-10-07 21:26:17 +02:00
|
|
|
// Generate derived key from password
|
2016-10-02 06:14:18 +02:00
|
|
|
cf.ScryptObject = NewScryptKDF(logN)
|
2015-10-07 21:26:17 +02:00
|
|
|
scryptHash := cf.ScryptObject.DeriveKey(password)
|
|
|
|
|
|
|
|
// Lock master key using password-based key
|
2017-03-05 21:59:55 +01:00
|
|
|
useHKDF := cf.IsFeatureFlagSet(FlagHKDF)
|
|
|
|
ce := getKeyEncrypter(scryptHash, useHKDF)
|
2016-09-29 21:29:45 +02:00
|
|
|
cf.EncryptedKey = ce.EncryptBlock(key, 0, nil)
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFile - write out config in JSON format to file "filename.tmp"
|
2015-11-25 20:09:48 +01:00
|
|
|
// then rename over "filename".
|
|
|
|
// This way a password change atomically replaces the file.
|
2015-10-07 21:26:17 +02:00
|
|
|
func (cf *ConfFile) WriteFile() error {
|
2015-09-13 17:55:07 +02:00
|
|
|
tmp := cf.filename + ".tmp"
|
2015-11-25 20:09:48 +01:00
|
|
|
// 0400 permissions: gocryptfs.conf should be kept secret and never be written to.
|
|
|
|
fd, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400)
|
2015-09-13 17:55:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-14 00:45:41 +02:00
|
|
|
js, err := json.MarshalIndent(cf, "", "\t")
|
2015-09-13 17:55:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-21 08:43:39 +02:00
|
|
|
// For convenience for the user, add a newline at the end.
|
|
|
|
js = append(js, '\n')
|
2015-09-13 17:55:07 +02:00
|
|
|
_, err = fd.Write(js)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fd.Sync()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Rename(tmp, cf.filename)
|
2016-09-25 19:48:21 +02:00
|
|
|
return err
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
2017-03-05 21:59:55 +01:00
|
|
|
|
|
|
|
// getKeyEncrypter is a helper function that returns the right ContentEnc
|
|
|
|
// instance for the "useHKDF" setting.
|
|
|
|
func getKeyEncrypter(scryptHash []byte, useHKDF bool) *contentenc.ContentEnc {
|
|
|
|
IVLen := 96
|
|
|
|
// gocryptfs v1.2 and older used 96-bit IVs for master key encryption.
|
|
|
|
// v1.3 adds the "HKDF" feature flag, which also enables 128-bit nonces.
|
|
|
|
if useHKDF {
|
|
|
|
IVLen = contentenc.DefaultIVBits
|
|
|
|
}
|
2017-04-08 02:09:28 +02:00
|
|
|
cc := cryptocore.New(scryptHash, cryptocore.BackendGoGCM, IVLen, useHKDF, false)
|
|
|
|
ce := contentenc.New(cc, 4096, false)
|
2017-03-05 21:59:55 +01:00
|
|
|
return ce
|
|
|
|
}
|