2015-11-27 00:03:10 +01:00
|
|
|
package cryptfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-27 22:18:36 +01:00
|
|
|
"io/ioutil"
|
2016-01-25 00:51:28 +01:00
|
|
|
"os"
|
2015-11-27 22:18:36 +01:00
|
|
|
"path/filepath"
|
2015-11-27 00:03:10 +01:00
|
|
|
"strings"
|
2015-11-29 21:41:38 +01:00
|
|
|
"sync"
|
2015-11-27 00:03:10 +01:00
|
|
|
)
|
|
|
|
|
2015-11-29 21:41:38 +01:00
|
|
|
// A simple one-entry DirIV cache
|
|
|
|
type DirIVCache struct {
|
|
|
|
// Invalidated?
|
2015-11-29 21:55:20 +01:00
|
|
|
cleared bool
|
2015-11-29 21:41:38 +01:00
|
|
|
// The DirIV
|
2015-11-29 21:55:20 +01:00
|
|
|
iv []byte
|
2015-11-29 21:41:38 +01:00
|
|
|
// Directory the DirIV belongs to
|
2015-11-29 21:55:20 +01:00
|
|
|
dir string
|
2015-11-29 21:41:38 +01:00
|
|
|
// Ecrypted version of "dir"
|
|
|
|
translatedDir string
|
|
|
|
// Synchronisation
|
2015-11-29 21:55:20 +01:00
|
|
|
lock sync.RWMutex
|
2015-11-29 21:41:38 +01:00
|
|
|
}
|
|
|
|
|
2015-12-06 15:05:52 +01:00
|
|
|
// lookup - fetch entry for "dir" from the cache
|
2015-11-29 21:41:38 +01:00
|
|
|
func (c *DirIVCache) lookup(dir string) (bool, []byte, string) {
|
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
if !c.cleared && c.dir == dir {
|
|
|
|
return true, c.iv, c.translatedDir
|
|
|
|
}
|
|
|
|
return false, nil, ""
|
|
|
|
}
|
|
|
|
|
2015-12-06 15:05:52 +01:00
|
|
|
// store - write entry for "dir" into the caches
|
2015-11-29 21:41:38 +01:00
|
|
|
func (c *DirIVCache) store(dir string, iv []byte, translatedDir string) {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
c.cleared = false
|
|
|
|
c.iv = iv
|
|
|
|
c.dir = dir
|
|
|
|
c.translatedDir = translatedDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DirIVCache) Clear() {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
c.cleared = true
|
|
|
|
}
|
|
|
|
|
2015-11-29 19:57:48 +01:00
|
|
|
// readDirIV - read the "gocryptfs.diriv" file from "dir" (absolute ciphertext path)
|
2016-01-24 19:40:53 +01:00
|
|
|
func (be *CryptFS) ReadDirIV(dir string) (iv []byte, readErr error) {
|
2015-11-27 22:20:01 +01:00
|
|
|
ivfile := filepath.Join(dir, DIRIV_FILENAME)
|
2016-01-24 19:40:53 +01:00
|
|
|
Debug.Printf("ReadDirIV: reading %s\n", ivfile)
|
|
|
|
iv, readErr = ioutil.ReadFile(ivfile)
|
|
|
|
if readErr != nil {
|
|
|
|
// The directory may have been concurrently deleted or moved. Failure to
|
|
|
|
// read the diriv is not an error in that case.
|
|
|
|
_, statErr := os.Stat(dir)
|
|
|
|
if os.IsNotExist(statErr) {
|
|
|
|
Debug.Printf("ReadDirIV: Dir %s was deleted under our feet", dir)
|
|
|
|
} else {
|
|
|
|
// This should not happen
|
|
|
|
Warn.Printf("ReadDirIV: Dir exists but diriv does not: %v\n", readErr)
|
|
|
|
}
|
|
|
|
return nil, readErr
|
2015-11-27 22:20:01 +01:00
|
|
|
}
|
|
|
|
if len(iv) != DIRIV_LEN {
|
2016-01-24 19:40:53 +01:00
|
|
|
return nil, fmt.Errorf("ReadDirIV: Invalid length %d\n", len(iv))
|
2015-11-27 22:20:01 +01:00
|
|
|
}
|
|
|
|
return iv, nil
|
2015-11-27 00:03:10 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 19:57:48 +01:00
|
|
|
// WriteDirIV - create diriv file inside "dir" (absolute ciphertext path)
|
2015-11-27 23:34:55 +01:00
|
|
|
// This function is exported because it is used from pathfs_frontend, main,
|
|
|
|
// and also the automated tests.
|
|
|
|
func WriteDirIV(dir string) error {
|
2015-11-27 21:48:58 +01:00
|
|
|
iv := RandBytes(DIRIV_LEN)
|
|
|
|
file := filepath.Join(dir, DIRIV_FILENAME)
|
|
|
|
// 0444 permissions: the file is not secret but should not be written to
|
|
|
|
return ioutil.WriteFile(file, iv, 0444)
|
|
|
|
}
|
|
|
|
|
2016-01-24 19:40:53 +01:00
|
|
|
// EncryptPathDirIV - encrypt path using EME with DirIV
|
2015-12-08 16:13:29 +01:00
|
|
|
func (be *CryptFS) EncryptPathDirIV(plainPath string, rootDir string, eme bool) (cipherPath string, err error) {
|
2015-11-27 00:03:10 +01:00
|
|
|
// Empty string means root directory
|
|
|
|
if plainPath == "" {
|
|
|
|
return plainPath, nil
|
|
|
|
}
|
2015-11-29 21:41:38 +01:00
|
|
|
// Check if the DirIV is cached
|
|
|
|
parentDir := filepath.Dir(plainPath)
|
|
|
|
found, iv, cParentDir := be.DirIVCacheEnc.lookup(parentDir)
|
|
|
|
if found {
|
|
|
|
//fmt.Print("h")
|
|
|
|
baseName := filepath.Base(plainPath)
|
2015-12-08 16:13:29 +01:00
|
|
|
cBaseName := be.encryptName(baseName, iv, eme)
|
|
|
|
cipherPath = cParentDir + "/" + cBaseName
|
|
|
|
return cipherPath, nil
|
2015-11-29 21:41:38 +01:00
|
|
|
}
|
|
|
|
// Walk the directory tree
|
2015-11-27 00:03:10 +01:00
|
|
|
var wd = rootDir
|
|
|
|
var encryptedNames []string
|
|
|
|
plainNames := strings.Split(plainPath, "/")
|
|
|
|
for _, plainName := range plainNames {
|
2015-11-29 21:41:38 +01:00
|
|
|
iv, err = be.ReadDirIV(wd)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 16:13:29 +01:00
|
|
|
encryptedName := be.encryptName(plainName, iv, eme)
|
2015-11-27 00:03:10 +01:00
|
|
|
encryptedNames = append(encryptedNames, encryptedName)
|
|
|
|
wd = filepath.Join(wd, encryptedName)
|
|
|
|
}
|
2015-11-29 21:41:38 +01:00
|
|
|
// Cache the final DirIV
|
2015-12-08 16:13:29 +01:00
|
|
|
cipherPath = strings.Join(encryptedNames, "/")
|
|
|
|
cParentDir = filepath.Dir(cipherPath)
|
2015-11-29 21:41:38 +01:00
|
|
|
be.DirIVCacheEnc.store(parentDir, iv, cParentDir)
|
2015-12-08 16:13:29 +01:00
|
|
|
return cipherPath, nil
|
2015-11-27 00:03:10 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 19:40:53 +01:00
|
|
|
// DecryptPathDirIV - decrypt path using EME with DirIV
|
2015-12-08 16:13:29 +01:00
|
|
|
func (be *CryptFS) DecryptPathDirIV(encryptedPath string, rootDir string, eme bool) (string, error) {
|
2015-11-27 00:03:10 +01:00
|
|
|
var wd = rootDir
|
|
|
|
var plainNames []string
|
|
|
|
encryptedNames := strings.Split(encryptedPath, "/")
|
2015-11-28 00:07:03 +01:00
|
|
|
Debug.Printf("DecryptPathDirIV: decrypting %v\n", encryptedNames)
|
2015-11-27 00:03:10 +01:00
|
|
|
for _, encryptedName := range encryptedNames {
|
2015-11-29 19:57:48 +01:00
|
|
|
iv, err := be.ReadDirIV(wd)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 16:13:29 +01:00
|
|
|
plainName, err := be.decryptName(encryptedName, iv, eme)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
plainNames = append(plainNames, plainName)
|
|
|
|
wd = filepath.Join(wd, encryptedName)
|
|
|
|
}
|
|
|
|
return filepath.Join(plainNames...), nil
|
|
|
|
}
|