1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
package fusefrontend
import (
"path"
"path/filepath"
"strings"
"syscall"
"github.com/rfjakob/gocryptfs/internal/ctlsocksrv"
"github.com/rfjakob/gocryptfs/internal/nametransform"
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
"github.com/rfjakob/gocryptfs/internal/tlog"
)
var _ ctlsocksrv.Interface = &RootNode{} // Verify that interface is implemented.
// EncryptPath implements ctlsock.Backend
//
// Symlink-safe through openBackingDir().
func (rn *RootNode) EncryptPath(plainPath string) (cipherPath string, err error) {
if plainPath == "" {
// Empty string gets encrypted as empty string
return plainPath, nil
}
if rn.args.PlaintextNames {
return plainPath, nil
}
dirfd, _, errno := rn.prepareAtSyscallMyself()
if errno != 0 {
return "", errno
}
defer syscall.Close(dirfd)
// Encrypt path level by level
parts := strings.Split(plainPath, "/")
wd := dirfd
for i, part := range parts {
dirIV, err := nametransform.ReadDirIVAt(wd)
if err != nil {
return "", err
}
cPart, err := rn.nameTransform.EncryptAndHashName(part, dirIV)
if err != nil {
return "", err
}
cipherPath = filepath.Join(cipherPath, cPart)
// Last path component? We are done.
if i == len(parts)-1 {
break
}
// Descend into next directory
wd, err = syscallcompat.Openat(wd, cPart, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
if err != nil {
return "", err
}
// Yes this is somewhat wasteful in terms of used file descriptors:
// we keep them all open until the function returns. But it is simple
// and reliable.
defer syscall.Close(wd)
}
tlog.Debug.Printf("EncryptPath %q -> %q", plainPath, cipherPath)
return cipherPath, nil
}
// DecryptPath implements ctlsock.Backend
//
// DecryptPath is symlink-safe because openBackingDir() and decryptPathAt()
// are symlink-safe.
func (rn *RootNode) DecryptPath(cipherPath string) (plainPath string, err error) {
dirfd, _, errno := rn.prepareAtSyscallMyself()
if errno != 0 {
return "", errno
}
defer syscall.Close(dirfd)
return rn.decryptPathAt(dirfd, cipherPath)
}
// decryptPathAt decrypts a ciphertext path relative to dirfd.
//
// Symlink-safe through ReadDirIVAt() and ReadLongNameAt().
func (rn *RootNode) decryptPathAt(dirfd int, cipherPath string) (plainPath string, err error) {
if rn.args.PlaintextNames || cipherPath == "" {
return cipherPath, nil
}
parts := strings.Split(cipherPath, "/")
wd := dirfd
for i, part := range parts {
dirIV, err := nametransform.ReadDirIVAt(wd)
if err != nil {
return "", err
}
longPart := part
if nametransform.IsLongContent(part) {
longPart, err = nametransform.ReadLongNameAt(wd, part)
if err != nil {
return "", err
}
}
name, err := rn.nameTransform.DecryptName(longPart, dirIV)
if err != nil {
return "", err
}
plainPath = path.Join(plainPath, name)
// Last path component? We are done.
if i == len(parts)-1 {
break
}
// Descend into next directory
wd, err = syscallcompat.Openat(wd, part, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
if err != nil {
return "", err
}
// Yes this is somewhat wasteful in terms of used file descriptors:
// we keep them all open until the function returns. But it is simple
// and reliable.
defer syscall.Close(wd)
}
return plainPath, nil
}
|