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"
|
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 (
|
|
|
|
// 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-08-29 22:05:39 +02:00
|
|
|
// 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.
|
|
|
|
ConfReverseName = ".gocryptfs.reverse.conf"
|
2015-09-13 17:55:07 +02:00
|
|
|
)
|
|
|
|
|
2015-10-07 21:26:17 +02:00
|
|
|
type ConfFile struct {
|
2016-06-05 11:33:54 +02:00
|
|
|
// gocryptfs version string
|
|
|
|
// This only documents the config file for humans who look at it. The actual
|
|
|
|
// technical info is contained in FeatureFlags.
|
|
|
|
Creator string
|
2015-09-13 21:47:18 +02:00
|
|
|
// Encrypted AES key, unlocked using a password hashed with scrypt
|
|
|
|
EncryptedKey []byte
|
|
|
|
// Stores parameters for scrypt hashing (key derivation)
|
|
|
|
ScryptObject scryptKdf
|
2015-11-01 01:32:33 +01:00
|
|
|
// The On-Disk-Format version this filesystem uses
|
|
|
|
Version uint16
|
2015-11-03 21:05:47 +01:00
|
|
|
// List of feature flags this filesystem has enabled.
|
|
|
|
// 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-06-05 11:33:54 +02:00
|
|
|
// File the config is saved to. Not exported to JSON.
|
|
|
|
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.
|
2016-06-05 11:33:54 +02:00
|
|
|
func CreateConfFile(filename string, password string, plaintextNames bool, logN int, creator string) 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-09-13 21:47:18 +02:00
|
|
|
// Generate new random master key
|
2016-02-06 19:20:54 +01:00
|
|
|
key := cryptocore.RandBytes(cryptocore.KeyLen)
|
2015-09-13 21:47:18 +02:00
|
|
|
|
2015-10-07 21:26:17 +02:00
|
|
|
// Encrypt it using the password
|
2015-11-01 01:32:33 +01:00
|
|
|
// This sets ScryptObject and EncryptedKey
|
2015-11-29 18:52:58 +01:00
|
|
|
cf.EncryptKey(key, password, logN)
|
2015-09-13 17:55:07 +02:00
|
|
|
|
2015-12-10 19:38:02 +01:00
|
|
|
// Set feature flags
|
2016-06-05 12:53:10 +02:00
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagGCMIV128])
|
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])
|
2015-11-03 21:05:47 +01:00
|
|
|
}
|
2015-09-13 17:55:07 +02:00
|
|
|
|
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
|
2015-11-03 21:05:47 +01:00
|
|
|
// contained key using password.
|
|
|
|
//
|
|
|
|
// Returns the decrypted key and the ConfFile object
|
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
|
|
|
}
|
|
|
|
|
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
|
2016-03-03 00:58:12 +01:00
|
|
|
// We use stock go GCM instead of OpenSSL here as we only use 96-bit IVs,
|
|
|
|
// speed is not important and we get better error messages
|
2016-02-06 19:20:54 +01:00
|
|
|
cc := cryptocore.New(scryptHash, false, false)
|
|
|
|
ce := contentenc.New(cc, 4096)
|
|
|
|
|
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())
|
2016-06-05 13:44:22 +02:00
|
|
|
return nil, nil, fmt.Errorf("Password incorrect.")
|
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
|
2015-11-29 18:52:58 +01: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
|
2016-02-06 19:20:54 +01:00
|
|
|
cc := cryptocore.New(scryptHash, false, false)
|
|
|
|
ce := contentenc.New(cc, 4096)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
_, 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)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|