-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsynocrypto_decrypter.go
253 lines (222 loc) · 8.6 KB
/
synocrypto_decrypter.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package synocrypto
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"hash"
"io"
"github.com/maxlaverse/synocrypto/pkg/compression"
"github.com/maxlaverse/synocrypto/pkg/crypto"
"github.com/maxlaverse/synocrypto/pkg/encoding"
"github.com/maxlaverse/synocrypto/pkg/log"
)
type decrypter struct {
options DecrypterOptions
}
// Metadata only reads the metadata of a file and then stops, returned a map and an error in
// case of troubles.
func (d *decrypter) Metadata(in io.Reader) (map[string]interface{}, error) {
objReader := encoding.NewReader(in)
// Starts reading the encrypted file and return a channel with data objects once the metadata
// have been read.
dataChan, err := objReader.DataChannel()
if err != nil {
return nil, fmt.Errorf("error reading metadata: %w", err)
}
// Discard the data
for range dataChan {
}
return objReader.Metadata(), nil
}
// Decrypt reads an encrypted file and writes it into an output writer after having decrypted it.
// Depending on the file's metadata, Decrypt also decompresses the file and verifies its integrity.
func (d *decrypter) Decrypt(in io.Reader, out io.Writer) error {
objReader := encoding.NewReader(in)
// Starts reading the encrypted file and return a channel with data objects once the metadata
// have been read.
dataChan, err := objReader.DataChannel()
if err != nil {
return fmt.Errorf("error reading metadata: %w", err)
}
// Retrieve the session key required to decrypt the data
sessionKey, err := retrieveSessionKey(d.options.Password, d.options.PrivateKey, objReader.Metadata())
if err != nil {
return fmt.Errorf("unable to initialize the decryption: %w", err)
}
// Pipe a hasher if we have information allowing us to verify its integrity.
var hasher hash.Hash
hashName, ok := objReader.Metadata()[encoding.MetadataFieldDigest]
if !ok {
log.Warningf("Unable to locate hash function field in the metadata. Integrity of the output won't be checked.", encoding.MetadataFieldDigest)
} else if hashName == "md5" {
log.Debugf("Output integrity verified through md5")
hasher = md5.New()
out = io.MultiWriter(hasher, out)
} else {
log.Warningf("Unsupported hash function: '%s'. Integrity of the output won't be checked", hashName)
}
// Pipe a decompressor if the encrypted file is compressed
compressionEnabled, ok := objReader.Metadata()[encoding.MetadataFieldCompress]
if !ok {
log.Warningf("Unable to locate compression field in the metadata. Assuming the data is compressed.", encoding.MetadataFieldDigest)
compressionEnabled = 1
}
if compressionEnabled == 1 {
if d.options.UseExternalLz4Decompressor {
log.Debug("Using external lz4 command for decompression")
out, err = compression.NewLz4DecompExternal(out)
} else {
log.Debug("Using builtin lz4 decompressor")
out, err = compression.NewLz4DecompBuiltin(out)
}
if err != nil {
return fmt.Errorf("unable to initialize the decompression: %w", err)
}
} else {
log.Debug("Compression is disabled")
}
// Pipe a decrypter with this session key
cryptoOut := crypto.NewDecrypterWithPasswordAndSalt(sessionKey, []byte{}, out)
// Read the data
blockCount := 0
for data := range dataChan {
blockCount++
n, err := cryptoOut.Write(data)
if err != nil {
cryptoOut.Close()
return fmt.Errorf("error writing block #%d (%d bytes, written: %d) to output stream: %w", blockCount, len(data), n, err)
}
}
if objReader.Error() != nil {
cryptoOut.Close()
return fmt.Errorf("error reading the encodings: %w", objReader.Error())
}
// Close and finish
err = cryptoOut.Close()
if err != nil {
return fmt.Errorf("error closing stream: %w", err)
}
// Check the file's integrity if we can
if hasher != nil {
actualFileDigest := hex.EncodeToString(hasher.Sum(nil))
expectedHash := objReader.Metadata()[encoding.MetadataFieldMd5Digest]
if actualFileDigest != expectedHash {
if !d.options.IgnoreChecksumMismatch {
return fmt.Errorf("file digest doesn't match (computed: '%s', read:'%s')", actualFileDigest, expectedHash)
}
log.Errorf("file digest doesn't match (computed: '%s', read:'%s')", actualFileDigest, expectedHash)
} else {
log.Debugf("Checksum matched: %s", actualFileDigest)
}
}
return nil
}
func retrieveSessionKey(password string, privateKey []byte, metadata map[string]interface{}) (sessionKey []byte, err error) {
if len(password) > 0 {
sessionKey, err = retrieveSessionKeyByPassword(password, metadata)
if err != nil {
log.Errorf("Unable to retrieve session key by password: %v", err)
}
}
if len(privateKey) > 0 {
sessionKey, err = retrieveSessionKeyByPrivateKey(privateKey, metadata)
if err != nil {
log.Errorf("Unable to retrieve session key by private key: %v", err)
}
}
if len(sessionKey) == 0 {
return nil, fmt.Errorf("not enough information available to decrypt the session key")
}
sessionKeyHash, hasSessionKeyHash := metadata[encoding.MetadataFieldSessionKeyHash]
if !hasSessionKeyHash {
log.Warning("Unable to find the hash of the session key. Won't be able to verify the validity of the session key")
} else {
if !crypto.IsSaltedHashOf(sessionKeyHash.(string), string(sessionKey)) {
return nil, fmt.Errorf("sessionKey hash don't match (expected: '%s')", sessionKeyHash)
}
}
if _, ok := metadata[encoding.MetadataFieldSalt]; ok {
// Special handling when a salt is given, which probably means when the version is > 1.0.0
sessionKey, err = hexToBytes(sessionKey)
if err != nil {
return nil, fmt.Errorf("unable to convert session key from 'hex' to 'bytes': %w", err)
}
}
return
}
func retrieveSessionKeyByPrivateKey(privateKey []byte, metadata map[string]interface{}) ([]byte, error) {
keyHash, hasKeyHash := metadata[encoding.MetadataFieldEncryptionKey2Hash]
if !hasKeyHash {
log.Warning("File is missing the hash of the encrypted key2. Won't be able to check if private key is valid")
} else {
publicKey, err := crypto.PublicKeyFromPrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("error extracting public key from private key: '%w'", err)
}
if !crypto.IsSaltedHashOf(keyHash.(string), publicKey) {
return nil, fmt.Errorf("private key hash don't match (expected: '%s')", keyHash.(string))
}
}
encryptionKey2, hasEncryptionKey := metadata[encoding.MetadataFieldEncryptionKey2]
if !hasEncryptionKey {
return nil, fmt.Errorf("missing session key encrypted by private key")
}
encryptionKey2, err := base64.StdEncoding.DecodeString(encryptionKey2.(string))
if err != nil {
return nil, fmt.Errorf("error decoding encrypted key2: '%w'", err)
}
return crypto.DecryptOnceWithPrivateKey(privateKey, []byte(encryptionKey2.([]byte)))
}
func retrieveSessionKeyByPassword(password string, metadata map[string]interface{}) ([]byte, error) {
passwordHash, hasPasswordHash := metadata[encoding.MetadataFieldEncryptionKey1Hash]
if !hasPasswordHash {
log.Warning("File is missing the hash of the encrypted key1. Won't be able to check if password is valid")
} else {
if !crypto.IsSaltedHashOf(passwordHash.(string), password) {
return nil, fmt.Errorf("password hash don't match (expected: '%s')", passwordHash.(string))
}
}
encryptedSessionKeyByPassword, hasEncryptionKey := metadata[encoding.MetadataFieldEncryptionKey1]
salt, hasSalt := metadata[encoding.MetadataFieldSalt]
if !hasEncryptionKey {
return nil, fmt.Errorf("missing session key encrypted by password")
} else if !hasSalt {
version := retrieveVersion(metadata)
if len(version) > 0 && version[0] > 1 {
return nil, fmt.Errorf("missing expected salt (version: %v)", version)
}
log.Warningf("missing salt (version: %v)", version)
salt = ""
}
encryptedSessionKeyByPassword, err := base64.StdEncoding.DecodeString(encryptedSessionKeyByPassword.(string))
if err != nil {
return nil, fmt.Errorf("error decoding encrypted key1: '%w'", err)
}
sessionKey, err := crypto.DecryptOnceWithPasswordAndSalt([]byte(password), []byte(salt.(string)), encryptedSessionKeyByPassword.([]byte))
if err != nil {
return nil, fmt.Errorf("error decrypting encrypted key1: '%w'", err)
}
return sessionKey, nil
}
func retrieveVersion(metadata map[string]interface{}) []int {
version, hasVersion := metadata[encoding.MetadataFieldVersion]
if hasVersion {
major, hasMajorVersion := version.(map[string]interface{})["major"]
minor, hasMinorVersion := version.(map[string]interface{})["minor"]
if hasMajorVersion && hasMinorVersion {
return []int{major.(int), minor.(int)}
} else if hasMajorVersion {
return []int{major.(int)}
}
}
return []int{}
}
func hexToBytes(str []byte) ([]byte, error) {
data := make([]byte, hex.DecodedLen(len(str)))
_, err := hex.Decode(data, []byte(str))
if err != nil {
return nil, err
}
return data, nil
}