Fix decryption

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-03-22 10:05:31 +01:00 committed by Alper Öztürk
parent c0f23c435a
commit 67ddd0ba38
2 changed files with 22 additions and 23 deletions

View file

@ -267,18 +267,14 @@ public class DownloadFileOperation extends RemoteOperation {
try { try {
Cipher cipher = EncryptionUtils.getCipher(Cipher.DECRYPT_MODE, key, iv); Cipher cipher = EncryptionUtils.getCipher(Cipher.DECRYPT_MODE, key, iv);
tmpFile = EncryptionUtils.decryptFile(tmpFile, authenticationTagString, cipher, new ArbitraryDataProviderImpl(operationContext), user); EncryptionUtils.decryptFile(cipher, tmpFile, newFile, authenticationTagString, new ArbitraryDataProviderImpl(operationContext), user);
} catch (Exception e) { } catch (Exception e) {
return new RemoteOperationResult(e); return new RemoteOperationResult(e);
} }
} }
if (downloadType == DownloadType.DOWNLOAD) { if (downloadType == DownloadType.DOWNLOAD) {
moved = tmpFile.renameTo(newFile);
newFile.setLastModified(file.getModificationTimestamp());
if (!moved) {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
}
} else if (downloadType == DownloadType.EXPORT) { } else if (downloadType == DownloadType.EXPORT) {
new FileExportUtils().exportFile(file.getFileName(), new FileExportUtils().exportFile(file.getFileName(),
file.getMimeType(), file.getMimeType(),

View file

@ -568,6 +568,7 @@ public final class EncryptionUtils {
File encryptedFile = new File(file.getAbsolutePath() + ".enc"); File encryptedFile = new File(file.getAbsolutePath() + ".enc");
encryptFileWithGivenCipher(file, encryptedFile, cipher); encryptFileWithGivenCipher(file, encryptedFile, cipher);
String authenticationTagString = getAuthenticationTag(cipher); String authenticationTagString = getAuthenticationTag(cipher);
Log_OC.d("", "KAVGAM!!: " + authenticationTagString);
return new EncryptedFile(encryptedFile, authenticationTagString); return new EncryptedFile(encryptedFile, authenticationTagString);
} }
@ -600,33 +601,35 @@ public final class EncryptionUtils {
inputStream.close(); inputStream.close();
} }
public static File decryptFile(File encryptedFile, public static void decryptFile(Cipher cipher,
File encryptedFile,
File decryptedFile,
String authenticationTag, String authenticationTag,
Cipher cipher,
ArbitraryDataProvider arbitraryDataProvider, ArbitraryDataProvider arbitraryDataProvider,
User user) throws InvalidParameterSpecException { User user) throws IOException,
File decryptedFile = new File(encryptedFile.getAbsolutePath().replace(".enc", "_decrypted")); BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException {
try (FileInputStream inputStream = new FileInputStream(encryptedFile); FileInputStream inputStream = new FileInputStream(encryptedFile);
FileOutputStream fileOutputStream = new FileOutputStream(decryptedFile); FileOutputStream outputStream = new FileOutputStream(decryptedFile);
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher)) { byte[] buffer = new byte[4096];
int bytesRead;
byte[] buffer = new byte[4096]; while ((bytesRead = inputStream.read(buffer)) != -1) {
int bytesRead; byte[] output = cipher.update(buffer, 0, bytesRead);
if (output != null) {
while ((bytesRead = cipherInputStream.read(buffer)) != -1) { outputStream.write(output);
fileOutputStream.write(buffer, 0, bytesRead);
} }
} catch (Exception e) {
Log_OC.d(TAG, "Error caught at decryptFile(): " + e.getLocalizedMessage());
} }
byte[] output = cipher.doFinal();
if (output != null) {
outputStream.write(output);
}
inputStream.close();
outputStream.close();
if (!getAuthenticationTag(cipher).equals(authenticationTag)) { if (!getAuthenticationTag(cipher).equals(authenticationTag)) {
reportE2eError(arbitraryDataProvider, user); reportE2eError(arbitraryDataProvider, user);
throw new SecurityException("Tag not correct"); throw new SecurityException("Tag not correct");
} }
return decryptedFile;
} }
// FIXME Decryption is broken // FIXME Decryption is broken