-
Notifications
You must be signed in to change notification settings - Fork 12
/
CipherFileChannel.java
332 lines (264 loc) · 10.3 KB
/
CipherFileChannel.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Delayed encrypting/decrypting SeekableByteChannel
*
* Copyright 2014 Agitos GmbH, Florian Sager, sager@agitos.de, http://www.agitos.de
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* A licence was granted to the ASF by Florian Sager on 31 December 2014
*/
package de.agitos.encfs4j;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.security.DigestException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Set;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/*
* CipherFileChannel has the following properties:
*
* - transparent encryption/decryption of a SeekableByteChannel.
*
* - either read/write encrypted data to/from a persistent file while processing decrypted data (reverse mode off). Sample: you want to read/write encrypted data to your probably insecure local file store.
*
* - or read/write decrypted data to/from a persistent file while processing encrypted data (reverse mode on). Sample: sync locally unencrypted files to a remote store with transparent encryption on the fly.
*
* - encrypted/decrypted temporary data is written to a temp file in the default temporary file directory (e.g. /tmp/). In case of APPEND/WRITE, the data is persisted if you call close(). The required store space for the temp file is equal to the persistent file size.
*
* - the initialization vector (IV) of the encryption is unique per relativeFilename. A relativeFilename is relative to the provided fileSystemRoot. A fileSystemRoot is similar to a mountpoint that contains all files that can be encrypted/decrypted with the same symmetric key.
*/
public class CipherFileChannel implements SeekableByteChannel {
// path of the underlying file
private Path path;
// temp file for random access operations on encrypted/decrypted data
private File tmpFile;
// transformed (volatile) file
private RandomAccessFile trafoFile;
// persistent file
private RandomAccessFile persistentFile;
// transformed (volatile) channel
private FileChannel trafoChannel;
// persistent channel
private FileChannel persistentChannel;
// mode of operation: if isReverse the the persistent file is unencrypted,
// else the persistent file is encrypted
private boolean isReverse;
// if isReadingRequired then the temp file has to contain transformed data
private boolean isReadingRequired = true;
// is the file channel open?
private boolean isOpen = false;
// if isModified the persistent file has to be encrypted/decrypted on
// close()
private boolean isModified = false;
// cipher transformation like 'AES'
private String cipherTransformation;
// the secret symmetric key
private SecretKeySpec secretKeySpec;
// a relative filename to calculate the IV
private String relativeFilename;
public CipherFileChannel(Path path, String cipherTransformation,
SecretKeySpec secretKeySpec, Path fileSystemRoot,
boolean isReverse, Set<? extends OpenOption> options,
FileAttribute<?>... attrs) throws IOException {
this(path, cipherTransformation, secretKeySpec, fileSystemRoot,
isReverse);
// FSTODO: more options required?
if (options.size() > 0 && !options.contains(StandardOpenOption.READ)
&& !options.contains(StandardOpenOption.APPEND)) {
this.isReadingRequired = false;
}
}
public CipherFileChannel(Path path, String cipherTransformation,
SecretKeySpec secretKeySpec, Path fileSystemRoot, boolean isReverse)
throws IOException {
try {
this.path = path.normalize();
// this.encrypt =
// "encfs".equals(path.getFileSystem().provider().getScheme());
this.persistentFile = new RandomAccessFile(path.toFile(), "rw");
this.persistentChannel = this.persistentFile.getChannel();
this.isOpen = true;
this.cipherTransformation = cipherTransformation;
this.secretKeySpec = secretKeySpec;
this.relativeFilename = fileSystemRoot.relativize(this.path)
.toString();
this.isReverse = isReverse;
} catch (FileNotFoundException e) {
throw new IOException(e.getMessage(), e);
}
}
@Override
public boolean isOpen() {
return this.isOpen;
}
@Override
public long position() throws IOException {
// as long as the transformed channel is not initialized get the
// position from the persistent channel
return this.trafoChannel == null ? this.persistentChannel.position()
: this.trafoChannel.position();
}
@Override
public SeekableByteChannel position(long newPosition) throws IOException {
// as long as the transformed channel is not initialized set the
// position on the persistent channel
SeekableByteChannel ch = this.trafoChannel == null ? this.persistentChannel
.position(newPosition) : this.trafoChannel
.position(newPosition);
return this;
}
@Override
public int read(ByteBuffer dst) throws IOException {
if (this.trafoChannel == null) {
// create the transformed temp file now
this.initTrafoChannel();
}
return this.trafoChannel.read(dst);
}
@Override
public long size() throws IOException {
// as long as the transformed channel is not initialized get the
// information from the persistent channel
return this.trafoChannel == null ? this.persistentChannel.size()
: this.trafoChannel.size();
}
@Override
public SeekableByteChannel truncate(long length) throws IOException {
// as long as the transformed channel is not initialized truncate the
// persistent channel
SeekableByteChannel ch = this.trafoChannel == null ? this.persistentChannel
.truncate(length) : this.trafoChannel.truncate(length);
return this;
}
@Override
public int write(ByteBuffer src) throws IOException {
if (this.trafoChannel == null) {
// create the transformed temp file now
this.initTrafoChannel();
}
int bytesWritten = this.trafoChannel.write(src);
if (!this.isModified)
this.isModified = bytesWritten > 0;
return bytesWritten;
}
@Override
public void close() throws IOException {
if (this.trafoChannel != null) {
this.closeTrafoChannel();
}
this.persistentChannel.close();
this.persistentFile.close();
this.isOpen = false;
}
private void initTrafoChannel() throws IOException {
try {
// create temp file in default temp directory
this.tmpFile = File.createTempFile("encfs-", ".dat");
this.tmpFile.deleteOnExit();
if (this.isReadingRequired) {
// encrypt/decrypt everything from persistentChannel if the
// persistent file content should be read
Cipher cipher = this
.getCipher(this.isReverse ? Cipher.ENCRYPT_MODE
: Cipher.DECRYPT_MODE);
this.persistentChannel.position(0);
// this is where encryption/decryption takes place in the
// beginning based on CipherOutputStream
this.copyStream(
Channels.newInputStream(this.persistentChannel),
new CipherOutputStream(new FileOutputStream(
this.tmpFile), cipher));
}
this.trafoFile = new RandomAccessFile(this.tmpFile, "rw");
this.trafoChannel = this.trafoFile.getChannel();
// read/write on trafoChannel now
} catch (InvalidKeyException | NoSuchAlgorithmException
| DigestException | NoSuchPaddingException
| InvalidAlgorithmParameterException e) {
throw new IOException(e.getMessage(), e);
}
}
private void closeTrafoChannel() throws IOException {
try {
if (!this.isModified)
// nothing to write back to the persistent file
return;
// reset persistentChannel, encrypt/decrypt everything in
// trafoChannel to
// persistentChannel
this.persistentChannel.truncate(0);
Cipher cipher = this.getCipher(this.isReverse ? Cipher.DECRYPT_MODE
: Cipher.ENCRYPT_MODE);
this.trafoChannel.position(0);
// this is where encryption/decryption takes place in the end based
// on CipherOutputStream
this.copyStream(
Channels.newInputStream(this.trafoChannel),
new CipherOutputStream(Channels
.newOutputStream(this.persistentChannel), cipher));
} catch (InvalidKeyException | NoSuchAlgorithmException
| DigestException | NoSuchPaddingException
| InvalidAlgorithmParameterException e) {
throw new IOException(e.getMessage(), e);
} finally {
// finally remove the temp file
this.trafoChannel.close();
this.trafoFile.close();
this.tmpFile.delete();
}
}
private void copyStream(InputStream from, OutputStream to)
throws IOException {
byte[] buffer = new byte[4096];
int bytes_read;
while ((bytes_read = from.read(buffer)) != -1)
// Read until EOF
to.write(buffer, 0, bytes_read);
to.flush();
// streams have to remain open
}
private Cipher getCipher(int cipherMode) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException, DigestException {
Cipher cipher = Cipher.getInstance(this.cipherTransformation);
// calculate a file specific IV based on the unique relative filename
byte[] iv = new byte[cipher.getBlockSize()];
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(this.relativeFilename.getBytes());
md.digest(iv, 0, cipher.getBlockSize());
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
// load the mode, symmetric key and IV into the Cipher
cipher.init(cipherMode, this.secretKeySpec, ivParameterSpec);
return cipher;
}
}