- report authentication error on the UI

- reduced visibility of some classes
This commit is contained in:
Sebastian Stenzel
2016-02-18 16:39:34 +01:00
parent 5a84228678
commit b85a110a24
10 changed files with 35 additions and 22 deletions
@@ -28,7 +28,6 @@ import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Hex;
import org.cryptomator.crypto.engine.FileContentCryptor;
import org.cryptomator.crypto.engine.FileContentEncryptor;
import org.cryptomator.io.ByteBuffers;
@@ -165,7 +164,6 @@ class FileContentEncryptorImpl implements FileContentEncryptor {
mac.update(nonce);
mac.update(ciphertextBuf);
byte[] authenticationCode = mac.doFinal();
Hex.encodeHexString(authenticationCode);
outBuf.put(authenticationCode);
// flip and return:
@@ -35,7 +35,11 @@ public class CryptoFile extends CryptoNode implements File {
@Override
public ReadableFile openReadable() {
boolean authenticate = !fileSystem().delegate().shouldSkipAuthentication(toString());
return new CryptoReadableFile(cryptor.getFileContentCryptor(), forceGetPhysicalFile().openReadable(), authenticate);
return new CryptoReadableFile(cryptor.getFileContentCryptor(), forceGetPhysicalFile().openReadable(), authenticate, this::reportAuthError);
}
private void reportAuthError() {
fileSystem().delegate().authenticationFailed(this.toString());
}
@Override
@@ -8,6 +8,7 @@
*******************************************************************************/
package org.cryptomator.filesystem.crypto;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
@@ -15,6 +16,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.cryptomator.crypto.engine.AuthenticationFailedException;
import org.cryptomator.crypto.engine.FileContentCryptor;
import org.cryptomator.crypto.engine.FileContentDecryptor;
import org.cryptomator.filesystem.ReadableFile;
@@ -29,15 +31,17 @@ class CryptoReadableFile implements ReadableFile {
private final FileContentCryptor cryptor;
private final ReadableFile file;
private final boolean authenticate;
private final Runnable onAuthError;
private FileContentDecryptor decryptor;
private Future<Void> readAheadTask;
private ByteBuffer bufferedCleartext = EMPTY_BUFFER;
public CryptoReadableFile(FileContentCryptor cryptor, ReadableFile file, boolean authenticate) {
public CryptoReadableFile(FileContentCryptor cryptor, ReadableFile file, boolean authenticate, Runnable onAuthError) {
this.header = ByteBuffer.allocate(cryptor.getHeaderSize());
this.cryptor = cryptor;
this.file = file;
this.authenticate = authenticate;
this.onAuthError = onAuthError;
file.position(0);
file.read(header);
header.flip();
@@ -58,6 +62,8 @@ class CryptoReadableFile implements ReadableFile {
return bytesRead;
} catch (InterruptedException e) {
throw new UncheckedIOException(new InterruptedIOException("Task interrupted while waiting for cleartext"));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@@ -78,9 +84,14 @@ class CryptoReadableFile implements ReadableFile {
readAheadTask = executorService.submit(new CiphertextReader(file, decryptor, header.remaining() + ciphertextPos));
}
private void bufferCleartext() throws InterruptedException {
private void bufferCleartext() throws InterruptedException, IOException {
if (!bufferedCleartext.hasRemaining()) {
bufferedCleartext = decryptor.cleartext();
try {
bufferedCleartext = decryptor.cleartext();
} catch (AuthenticationFailedException e) {
onAuthError.run();
throw new IOException("Failed to decrypt file due to an authentication error.", e);
}
}
}
@@ -37,8 +37,11 @@ public class CryptoReadableFileTest {
}
}).thenThrow(new UncheckedIOException(new IOException("failed.")));
Runnable noop = () -> {
};
@SuppressWarnings("resource")
ReadableFile cryptoReadableFile = new CryptoReadableFile(fileContentCryptor, underlyingFile, true);
ReadableFile cryptoReadableFile = new CryptoReadableFile(fileContentCryptor, underlyingFile, true, noop);
cryptoReadableFile.read(ByteBuffer.allocate(1));
}