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"
|
2019-09-14 18:32:54 +02:00
|
|
|
"path/filepath"
|
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
|
|
|
)
|
|
|
|
|
2019-02-17 17:05:05 +01:00
|
|
|
const (
|
|
|
|
// Like ext4, we allow at most 255 bytes for a file name.
|
|
|
|
NameMax = 255
|
|
|
|
)
|
|
|
|
|
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
|
2019-09-14 18:32:54 +02:00
|
|
|
// Patterns to bypass decryption
|
2021-06-20 19:09:46 +02:00
|
|
|
badnamePatterns []string
|
2016-06-16 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// New returns a new NameTransform instance.
|
2021-06-20 19:09:46 +02:00
|
|
|
func New(e *eme.EMECipher, longNames bool, raw64 bool, badname []string) *NameTransform {
|
|
|
|
tlog.Debug.Printf("nametransform.New: longNames=%v, raw64=%v, badname=%q",
|
|
|
|
longNames, raw64, badname)
|
|
|
|
|
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{
|
2021-06-20 19:09:46 +02:00
|
|
|
emeCipher: e,
|
|
|
|
longNames: longNames,
|
|
|
|
B64: b64,
|
|
|
|
badnamePatterns: badname,
|
2016-06-16 19:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:32:54 +02:00
|
|
|
// DecryptName calls decryptName to try and decrypt a base64-encoded encrypted
|
|
|
|
// filename "cipherName", and failing that checks if it can be bypassed
|
2016-02-06 19:20:54 +01:00
|
|
|
func (n *NameTransform) DecryptName(cipherName string, iv []byte) (string, error) {
|
2019-09-14 18:32:54 +02:00
|
|
|
res, err := n.decryptName(cipherName, iv)
|
2021-06-21 12:08:18 +02:00
|
|
|
if err != nil && n.HaveBadnamePatterns() {
|
|
|
|
return n.decryptBadname(cipherName, iv)
|
2019-09-14 18:32:54 +02:00
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptName decrypts a base64-encoded encrypted filename "cipherName" using the
|
|
|
|
// initialization vector "iv".
|
|
|
|
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 {
|
2021-06-02 14:21:30 +02:00
|
|
|
tlog.Warn.Printf("DecryptName %q: unPad16 error: %v", cipherName, err)
|
2017-05-07 20:58:27 +02:00
|
|
|
return "", syscall.EBADMSG
|
|
|
|
}
|
2021-06-02 14:21:30 +02:00
|
|
|
plain := string(bin)
|
|
|
|
if err := IsValidName(plain); err != nil {
|
|
|
|
tlog.Warn.Printf("DecryptName %q: invalid name after decryption: %v", cipherName, err)
|
2017-11-22 06:11:19 +01:00
|
|
|
return "", syscall.EBADMSG
|
|
|
|
}
|
2016-02-06 19:20:54 +01:00
|
|
|
return plain, err
|
|
|
|
}
|
|
|
|
|
2020-04-18 16:14:48 +02:00
|
|
|
// EncryptName encrypts "plainName", returns a base64-encoded "cipherName64",
|
|
|
|
// encrypted using EME (https://github.com/rfjakob/eme).
|
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.
|
2021-06-02 14:21:30 +02:00
|
|
|
func (n *NameTransform) EncryptName(plainName string, iv []byte) (cipherName64 string, err error) {
|
|
|
|
if err := IsValidName(plainName); err != nil {
|
|
|
|
tlog.Warn.Printf("EncryptName %q: invalid plainName: %v", plainName, err)
|
|
|
|
return "", syscall.EBADMSG
|
|
|
|
}
|
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)
|
2021-06-02 14:21:30 +02:00
|
|
|
return cipherName64, nil
|
2016-02-06 19:20:54 +01:00
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
|
2021-06-21 12:08:18 +02:00
|
|
|
// EncryptAndHashName encrypts "name" and hashes it to a longname if it is
|
|
|
|
// too long.
|
|
|
|
// Returns ENAMETOOLONG if "name" is longer than 255 bytes.
|
|
|
|
func (be *NameTransform) EncryptAndHashName(name string, iv []byte) (string, error) {
|
|
|
|
// Prevent the user from creating files longer than 255 chars.
|
|
|
|
if len(name) > NameMax {
|
|
|
|
return "", syscall.ENAMETOOLONG
|
|
|
|
}
|
|
|
|
cName, err := be.EncryptName(name, iv)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if be.longNames && len(cName) > NameMax {
|
|
|
|
return be.HashLongName(cName), nil
|
|
|
|
}
|
|
|
|
return cName, nil
|
|
|
|
}
|
|
|
|
|
2019-02-16 21:55:54 +01:00
|
|
|
// B64EncodeToString returns a Base64-encoded string
|
|
|
|
func (n *NameTransform) B64EncodeToString(src []byte) string {
|
|
|
|
return n.B64.EncodeToString(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// B64DecodeString decodes a Base64-encoded string
|
|
|
|
func (n *NameTransform) B64DecodeString(s string) ([]byte, error) {
|
|
|
|
return n.B64.DecodeString(s)
|
|
|
|
}
|
2021-06-17 08:11:33 +02:00
|
|
|
|
2021-06-21 12:08:18 +02:00
|
|
|
// Dir is like filepath.Dir but returns "" instead of ".".
|
|
|
|
func Dir(path string) string {
|
|
|
|
d := filepath.Dir(path)
|
|
|
|
if d == "." {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return d
|
2021-06-17 08:11:33 +02:00
|
|
|
}
|