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 (
|
2017-05-07 20:58:27 +02:00
|
|
|
"bytes"
|
2016-02-06 19:20:54 +01:00
|
|
|
"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 {
|
2019-01-02 22:32:21 +01:00
|
|
|
emeCipher *eme.EMECipher
|
|
|
|
longNames bool
|
2018-12-27 12:03:00 +01:00
|
|
|
// B64 = either base64.URLEncoding or base64.RawURLEncoding, depending
|
2017-03-05 22:59:25 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:58:27 +02:00
|
|
|
// DecryptName decrypts a base64-encoded encrypted filename "cipherName" using the
|
|
|
|
// initialization vector "iv".
|
2016-02-06 19:20:54 +01:00
|
|
|
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
|
|
|
|
}
|
2017-05-23 20:46:24 +02:00
|
|
|
if len(bin) == 0 {
|
|
|
|
tlog.Warn.Printf("DecryptName: empty input")
|
|
|
|
return "", syscall.EBADMSG
|
|
|
|
}
|
2016-02-06 19:20:54 +01:00
|
|
|
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))
|
2017-05-07 20:58:27 +02:00
|
|
|
return "", syscall.EBADMSG
|
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 {
|
2017-05-23 20:46:24 +02:00
|
|
|
tlog.Debug.Printf("DecryptName: unPad16 error detail: %v", err)
|
2016-07-03 15:35:58 +02:00
|
|
|
// unPad16 returns detailed errors including the position of the
|
|
|
|
// incorrect bytes. Kill the padding oracle by lumping everything into
|
|
|
|
// a generic error.
|
2017-05-07 20:58:27 +02:00
|
|
|
return "", syscall.EBADMSG
|
|
|
|
}
|
|
|
|
// A name can never contain a null byte or "/". Make sure we never return those
|
2017-05-23 20:46:24 +02:00
|
|
|
// to the kernel, even when we read a corrupted (or fuzzed) filesystem.
|
2017-05-07 20:58:27 +02:00
|
|
|
if bytes.Contains(bin, []byte{0}) || bytes.Contains(bin, []byte("/")) {
|
|
|
|
return "", syscall.EBADMSG
|
2016-02-06 19:20:54 +01:00
|
|
|
}
|
2017-11-22 06:11:19 +01:00
|
|
|
// The name should never be "." or "..".
|
|
|
|
if bytes.Equal(bin, []byte(".")) || bytes.Equal(bin, []byte("..")) {
|
|
|
|
return "", syscall.EBADMSG
|
|
|
|
}
|
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
|
|
|
// The encryption is either CBC or EME, depending on "useEME".
|
2016-02-06 22:54:14 +01:00
|
|
|
//
|
2019-01-02 21:45:40 +01:00
|
|
|
// This function is exported because in some cases, fusefrontend needs access
|
|
|
|
// to the full (not hashed) name if longname is used.
|
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
|
|
|
|
}
|