2016-10-02 06:14:18 +02:00
|
|
|
// Package nametransform encrypts and decrypts filenames.
|
2016-02-06 19:20:54 +01:00
|
|
|
package nametransform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"encoding/base64"
|
2016-09-21 21:43:56 +02:00
|
|
|
"syscall"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
|
|
|
"github.com/rfjakob/eme"
|
2016-06-16 19:02:47 +02:00
|
|
|
|
2016-07-03 15:35:58 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-02-06 19:20:54 +01:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// NameTransform is used to transform filenames.
|
2016-06-16 19:02:47 +02:00
|
|
|
type NameTransform struct {
|
2017-03-05 13:58:24 +01:00
|
|
|
emeCipher *eme.EMECipher
|
2016-06-16 19:02:47 +02:00
|
|
|
longNames bool
|
|
|
|
DirIVCache dirIVCache
|
2017-03-05 22:59:25 +01:00
|
|
|
// B64 = either base64.URLEncoding or base64.RawURLEncoding, depeding
|
|
|
|
// on the Raw64 feature flag
|
|
|
|
B64 *base64.Encoding
|
2016-06-16 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// New returns a new NameTransform instance.
|
2017-03-05 13:58:24 +01:00
|
|
|
func New(e *eme.EMECipher, longNames bool, raw64 bool) *NameTransform {
|
2016-11-01 18:43:22 +01:00
|
|
|
b64 := base64.URLEncoding
|
|
|
|
if raw64 {
|
2017-03-05 17:44:14 +01:00
|
|
|
b64 = base64.RawURLEncoding
|
2016-11-01 18:43:22 +01:00
|
|
|
}
|
2016-06-16 19:02:47 +02:00
|
|
|
return &NameTransform{
|
2017-03-05 13:58:24 +01:00
|
|
|
emeCipher: e,
|
|
|
|
longNames: longNames,
|
2017-03-05 22:59:25 +01:00
|
|
|
B64: b64,
|
2016-06-16 19:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
// DecryptName - decrypt base64-encoded encrypted filename "cipherName"
|
|
|
|
//
|
|
|
|
// This function is exported because it allows for a very efficient readdir
|
|
|
|
// implementation (read IV once, decrypt all names using this function).
|
|
|
|
func (n *NameTransform) DecryptName(cipherName string, iv []byte) (string, error) {
|
2017-03-05 22:59:25 +01:00
|
|
|
bin, err := n.B64.DecodeString(cipherName)
|
2016-02-06 19:20:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(bin)%aes.BlockSize != 0 {
|
2016-09-25 19:50:16 +02:00
|
|
|
tlog.Debug.Printf("DecryptName %q: decoded length %d is not a multiple of 16", cipherName, len(bin))
|
2016-09-25 18:01:24 +02:00
|
|
|
return "", syscall.EINVAL
|
2016-02-06 19:20:54 +01:00
|
|
|
}
|
2017-03-05 13:58:24 +01:00
|
|
|
bin = n.emeCipher.Decrypt(iv, bin)
|
2016-02-06 19:20:54 +01:00
|
|
|
bin, err = unPad16(bin)
|
|
|
|
if err != nil {
|
2016-07-03 15:35:58 +02:00
|
|
|
tlog.Debug.Printf("pad16 error detail: %v", err)
|
|
|
|
// unPad16 returns detailed errors including the position of the
|
|
|
|
// incorrect bytes. Kill the padding oracle by lumping everything into
|
|
|
|
// a generic error.
|
2016-09-21 21:43:56 +02:00
|
|
|
return "", syscall.EINVAL
|
2016-02-06 19:20:54 +01:00
|
|
|
}
|
|
|
|
plain := string(bin)
|
|
|
|
return plain, err
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// EncryptName encrypts "plainName", returns a base64-encoded "cipherName64".
|
2016-06-16 19:02:47 +02:00
|
|
|
// Used internally by EncryptPathDirIV().
|
|
|
|
// The encryption is either CBC or EME, depending on "useEME".
|
2016-02-06 22:54:14 +01:00
|
|
|
//
|
|
|
|
// This function is exported because fusefrontend needs access to the full (not hashed)
|
2016-06-16 19:02:47 +02:00
|
|
|
// name if longname is used. Otherwise you should use EncryptPathDirIV()
|
2016-02-06 22:54:14 +01:00
|
|
|
func (n *NameTransform) EncryptName(plainName string, iv []byte) (cipherName64 string) {
|
2016-02-06 19:20:54 +01:00
|
|
|
bin := []byte(plainName)
|
|
|
|
bin = pad16(bin)
|
2017-03-05 13:58:24 +01:00
|
|
|
bin = n.emeCipher.Encrypt(iv, bin)
|
2017-03-05 22:59:25 +01:00
|
|
|
cipherName64 = n.B64.EncodeToString(bin)
|
2016-02-06 19:20:54 +01:00
|
|
|
return cipherName64
|
|
|
|
}
|