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
|
2021-06-17 08:11:33 +02:00
|
|
|
//BadNameFlag is appended to filenames in plain mode if a ciphername is inavlid but is shown
|
|
|
|
BadNameFlag = " GOCRYPTFS_BAD_NAME"
|
2019-02-17 17:05:05 +01:00
|
|
|
)
|
|
|
|
|
2019-02-16 21:55:54 +01:00
|
|
|
// NameTransformer is an interface used to transform filenames.
|
|
|
|
type NameTransformer interface {
|
|
|
|
DecryptName(cipherName string, iv []byte) (string, error)
|
2021-06-02 14:21:30 +02:00
|
|
|
EncryptName(plainName string, iv []byte) (string, error)
|
2019-02-16 21:55:54 +01:00
|
|
|
EncryptAndHashName(name string, iv []byte) (string, error)
|
2021-06-17 08:11:33 +02:00
|
|
|
EncryptAndHashBadName(name string, iv []byte, dirfd int) (string, error)
|
2020-08-02 19:33:12 +02:00
|
|
|
// HashLongName - take the hash of a long string "name" and return
|
|
|
|
// "gocryptfs.longname.[sha256]"
|
|
|
|
//
|
|
|
|
// This function does not do any I/O.
|
2019-02-16 21:55:54 +01:00
|
|
|
HashLongName(name string) string
|
2021-06-17 08:11:33 +02:00
|
|
|
HaveBadnamePatterns() bool
|
2019-02-16 21:55:54 +01:00
|
|
|
WriteLongNameAt(dirfd int, hashName string, plainName string) error
|
|
|
|
B64EncodeToString(src []byte) string
|
|
|
|
B64DecodeString(s string) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
BadnamePatterns []string
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
for _, pattern := range n.BadnamePatterns {
|
|
|
|
match, err := filepath.Match(pattern, cipherName)
|
|
|
|
if err == nil && match { // Pattern should have been validated already
|
2020-06-06 12:49:45 +02:00
|
|
|
// Find longest decryptable substring
|
|
|
|
// At least 16 bytes due to AES --> at least 22 characters in base64
|
|
|
|
nameMin := n.B64.EncodedLen(aes.BlockSize)
|
|
|
|
for charpos := len(cipherName) - 1; charpos >= nameMin; charpos-- {
|
2020-05-19 13:34:58 +02:00
|
|
|
res, err = n.decryptName(cipherName[:charpos], iv)
|
|
|
|
if err == nil {
|
2021-06-17 08:11:33 +02:00
|
|
|
return res + cipherName[charpos:] + BadNameFlag, nil
|
2020-05-19 13:34:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-17 08:11:33 +02:00
|
|
|
return cipherName + BadNameFlag, nil
|
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
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// HaveBadnamePatterns returns true if BadName patterns were provided
|
|
|
|
func (n *NameTransform) HaveBadnamePatterns() bool {
|
|
|
|
return len(n.BadnamePatterns) > 0
|
|
|
|
}
|