2015-09-13 17:55:07 +02:00
|
|
|
package cryptfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-11-01 12:11:36 +01:00
|
|
|
"fmt"
|
2015-10-04 14:36:20 +02:00
|
|
|
"io/ioutil"
|
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-14 17:16:17 +01:00
|
|
|
ConfDefaultName = "gocryptfs.conf"
|
2015-11-03 21:05:47 +01:00
|
|
|
FlagPlaintextNames = "PlaintextNames"
|
2015-09-13 17:55:07 +02:00
|
|
|
)
|
|
|
|
|
2015-10-07 21:26:17 +02:00
|
|
|
type ConfFile struct {
|
2015-09-13 21:47:18 +02:00
|
|
|
// File the config is saved to. Not exported to JSON.
|
2015-09-13 17:55:07 +02:00
|
|
|
filename 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
|
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
|
|
|
|
// "password" and write it to "filename"
|
2015-11-02 23:08:51 +01:00
|
|
|
func CreateConfFile(filename string, password string, plaintextNames bool) error {
|
2015-10-07 21:26:17 +02:00
|
|
|
var cf ConfFile
|
2015-09-13 17:55:07 +02:00
|
|
|
cf.filename = filename
|
|
|
|
|
2015-09-13 21:47:18 +02:00
|
|
|
// Generate new random master key
|
|
|
|
key := RandBytes(KEY_LEN)
|
|
|
|
|
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-10-07 21:26:17 +02:00
|
|
|
cf.EncryptKey(key, password)
|
2015-09-13 17:55:07 +02:00
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
cf.Version = HEADER_CURRENT_VERSION
|
|
|
|
|
2015-11-03 21:05:47 +01:00
|
|
|
if plaintextNames {
|
|
|
|
cf.FeatureFlags = append(cf.FeatureFlags, FlagPlaintextNames)
|
|
|
|
}
|
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 {
|
2015-09-13 21:47:18 +02:00
|
|
|
Warn.Printf("Failed to unmarshal config file\n")
|
2015-10-07 21:26:17 +02:00
|
|
|
return nil, nil, err
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
if cf.Version != HEADER_CURRENT_VERSION {
|
2015-11-03 21:05:47 +01:00
|
|
|
return nil, nil, fmt.Errorf("Unsupported on-disk format %d\n", cf.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that we know all feature flags
|
2015-11-14 17:16:17 +01:00
|
|
|
for _, flag := range cf.FeatureFlags {
|
|
|
|
switch flag {
|
|
|
|
case FlagPlaintextNames:
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("Unsupported feature flag %s\n", flag)
|
2015-11-03 21:05:47 +01:00
|
|
|
}
|
2015-11-01 01:32:33 +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
|
|
|
|
// We use stock go GCM instead of OpenSSL here as speed is not important
|
|
|
|
// and we get better error messages
|
2015-11-03 00:00:13 +01:00
|
|
|
cfs := NewCryptFS(scryptHash, false, false)
|
2015-11-01 01:32:33 +01:00
|
|
|
key, err := cfs.DecryptBlock(cf.EncryptedKey, 0, nil)
|
2015-09-13 17:55:07 +02:00
|
|
|
if err != nil {
|
2015-10-11 18:33:28 +02:00
|
|
|
Warn.Printf("failed to unlock master key: %s\n", err.Error())
|
|
|
|
Warn.Printf("Password incorrect.\n")
|
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
|
|
|
|
2015-10-07 21:26:17 +02:00
|
|
|
return key, &cf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptKey - encrypt "key" using an scrypt hash generated from "password"
|
|
|
|
// and store it in cf.EncryptedKey
|
|
|
|
func (cf *ConfFile) EncryptKey(key []byte, password string) {
|
|
|
|
// Generate derived key from password
|
|
|
|
cf.ScryptObject = NewScryptKdf()
|
|
|
|
scryptHash := cf.ScryptObject.DeriveKey(password)
|
|
|
|
|
|
|
|
// Lock master key using password-based key
|
2015-11-03 00:00:13 +01:00
|
|
|
cfs := NewCryptFS(scryptHash, false, false)
|
2015-11-01 01:32:33 +01:00
|
|
|
cf.EncryptedKey = cfs.EncryptBlock(key, 0, nil)
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFile - write out config in JSON format to file "filename.tmp"
|
|
|
|
// then rename over "filename"
|
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"
|
|
|
|
fd, err := os.Create(tmp)
|
|
|
|
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
|
|
|
|
}
|
2015-11-03 21:05:47 +01:00
|
|
|
|
|
|
|
// isFeatureFlagSet - is the feature flag "flagWant" enabled?
|
|
|
|
func (cf *ConfFile) isFeatureFlagSet(flagWant string) bool {
|
2015-11-14 17:16:17 +01:00
|
|
|
for _, flag := range cf.FeatureFlags {
|
2015-11-03 21:05:47 +01:00
|
|
|
if flag == flagWant {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *ConfFile) PlaintextNames() bool {
|
|
|
|
return cf.isFeatureFlagSet(FlagPlaintextNames)
|
|
|
|
}
|