From 67ddd0ba38cff9b6483ae511557df6421f38c146 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 22 Mar 2024 10:05:31 +0100 Subject: [PATCH] Fix decryption Signed-off-by: alperozturk --- .../operations/DownloadFileOperation.java | 8 +--- .../android/utils/EncryptionUtils.java | 37 ++++++++++--------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java index fc286dfd4a..def606f504 100644 --- a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java @@ -267,18 +267,14 @@ public class DownloadFileOperation extends RemoteOperation { try { 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) { return new RemoteOperationResult(e); } } 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) { new FileExportUtils().exportFile(file.getFileName(), file.getMimeType(), diff --git a/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java b/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java index 8a03cd7b3e..a47073458f 100644 --- a/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java +++ b/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java @@ -568,6 +568,7 @@ public final class EncryptionUtils { File encryptedFile = new File(file.getAbsolutePath() + ".enc"); encryptFileWithGivenCipher(file, encryptedFile, cipher); String authenticationTagString = getAuthenticationTag(cipher); + Log_OC.d("", "KAVGAM!!: " + authenticationTagString); return new EncryptedFile(encryptedFile, authenticationTagString); } @@ -600,33 +601,35 @@ public final class EncryptionUtils { inputStream.close(); } - public static File decryptFile(File encryptedFile, + public static void decryptFile(Cipher cipher, + File encryptedFile, + File decryptedFile, String authenticationTag, - Cipher cipher, ArbitraryDataProvider arbitraryDataProvider, - User user) throws InvalidParameterSpecException { - File decryptedFile = new File(encryptedFile.getAbsolutePath().replace(".enc", "_decrypted")); + User user) throws IOException, + BadPaddingException, IllegalBlockSizeException, InvalidParameterSpecException { - try (FileInputStream inputStream = new FileInputStream(encryptedFile); - FileOutputStream fileOutputStream = new FileOutputStream(decryptedFile); - CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher)) { - - byte[] buffer = new byte[4096]; - int bytesRead; - - while ((bytesRead = cipherInputStream.read(buffer)) != -1) { - fileOutputStream.write(buffer, 0, bytesRead); + FileInputStream inputStream = new FileInputStream(encryptedFile); + FileOutputStream outputStream = new FileOutputStream(decryptedFile); + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + byte[] output = cipher.update(buffer, 0, bytesRead); + if (output != null) { + outputStream.write(output); } - } 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)) { reportE2eError(arbitraryDataProvider, user); throw new SecurityException("Tag not correct"); } - - return decryptedFile; } // FIXME Decryption is broken