Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-03-22 09:38:33 +01:00 committed by Alper Öztürk
parent f9e50338ee
commit c0f23c435a
3 changed files with 34 additions and 19 deletions

View File

@ -264,19 +264,10 @@ public class DownloadFileOperation extends RemoteOperation {
byte[] key = decodeStringToBase64Bytes(keyString);
byte[] iv = decodeStringToBase64Bytes(nonceString);
byte[] authenticationTag = decodeStringToBase64Bytes(authenticationTagString);
try {
Cipher cipher = EncryptionUtils.getCipher(Cipher.DECRYPT_MODE, key, iv);
byte[] decryptedBytes = EncryptionUtils.decryptFile(cipher,
tmpFile,
authenticationTag,
new ArbitraryDataProviderImpl(operationContext),
user);
try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
fileOutputStream.write(decryptedBytes);
}
tmpFile = EncryptionUtils.decryptFile(tmpFile, authenticationTagString, cipher, new ArbitraryDataProviderImpl(operationContext), user);
} catch (Exception e) {
return new RemoteOperationResult(e);
}

View File

@ -560,16 +560,10 @@ public class UploadFileOperation extends SyncOperation {
Long creationTimestamp = FileUtil.getCreationTimestamp(originalFile);
/***** E2E *****/
// Key, always generate new one
byte[] key = EncryptionUtils.generateKey();
// IV, always generate new one
byte[] iv = EncryptionUtils.randomBytes(EncryptionUtils.ivLength);
Cipher cipher = EncryptionUtils.getCipher(Cipher.ENCRYPT_MODE, key, iv);
File file = new File(mFile.getStoragePath());
EncryptedFile encryptedFile = EncryptionUtils.encryptFile(file, cipher);
// new random file name, check if it exists in metadata

View File

@ -114,6 +114,7 @@ import java.util.UUID;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
@ -563,9 +564,6 @@ public final class EncryptionUtils {
return Base64.decode(string, Base64.NO_WRAP);
}
/*
ENCRYPTION
*/
public static EncryptedFile encryptFile(File file, Cipher cipher) throws IOException, InvalidParameterSpecException {
File encryptedFile = new File(file.getAbsolutePath() + ".enc");
encryptFileWithGivenCipher(file, encryptedFile, cipher);
@ -602,7 +600,37 @@ public final class EncryptionUtils {
inputStream.close();
}
public static File decryptFile(File encryptedFile,
String authenticationTag,
Cipher cipher,
ArbitraryDataProvider arbitraryDataProvider,
User user) throws InvalidParameterSpecException {
File decryptedFile = new File(encryptedFile.getAbsolutePath().replace(".enc", "_decrypted"));
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);
}
} catch (Exception e) {
Log_OC.d(TAG, "Error caught at decryptFile(): " + e.getLocalizedMessage());
}
if (!getAuthenticationTag(cipher).equals(authenticationTag)) {
reportE2eError(arbitraryDataProvider, user);
throw new SecurityException("Tag not correct");
}
return decryptedFile;
}
// FIXME Decryption is broken
/*
public static byte[] decryptFile(
Cipher cipher,
File file,
@ -627,6 +655,8 @@ public final class EncryptionUtils {
return cipher.doFinal(fileBytes);
}
*/
/**
* Encrypt string with RSA algorithm, ECB mode, OAEPWithSHA-256AndMGF1 padding Asymmetric encryption, with private