diff --git a/app/src/androidTest/java/com/owncloud/android/util/EncryptionTestIT.java b/app/src/androidTest/java/com/owncloud/android/util/EncryptionTestIT.java index edf45c138d70..86582ec14288 100644 --- a/app/src/androidTest/java/com/owncloud/android/util/EncryptionTestIT.java +++ b/app/src/androidTest/java/com/owncloud/android/util/EncryptionTestIT.java @@ -22,6 +22,7 @@ import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedFolderMetadataFileV1; import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedMetadata; import com.owncloud.android.datamodel.e2e.v1.decrypted.Encrypted; +import com.owncloud.android.datamodel.e2e.v1.encrypted.EncryptedFile; import com.owncloud.android.datamodel.e2e.v1.encrypted.EncryptedFolderMetadataFileV1; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.e2ee.CsrHelper; @@ -832,8 +833,8 @@ private boolean cryptFile(String fileName, String md5, byte[] key, byte[] iv) // Encryption Cipher encryptorCipher = EncryptionUtils.getCipher(Cipher.ENCRYPT_MODE, key, iv); - EncryptionUtils.encryptFile(user.getAccountName(), file, encryptorCipher); - String encryptorCipherAuthTag = EncryptionUtils.getAuthenticationTag(encryptorCipher); + EncryptedFile encryptedFile = EncryptionUtils.encryptFile(user.getAccountName(), file, encryptorCipher); + String encryptorCipherAuthTag = EncryptionUtils.getAuthenticationTag(encryptedFile.getEncryptedFile()); // Decryption Cipher decryptorCipher = EncryptionUtils.getCipher(Cipher.DECRYPT_MODE, key, iv); diff --git a/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java b/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java index 8ed1907a113f..2c436591a4c9 100644 --- a/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java @@ -97,7 +97,6 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; -import java.security.spec.InvalidParameterSpecException; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -786,7 +785,7 @@ private RemoteOperationResult performE2EUpload(E2EClientData data) throws Operat return result; } - private E2EData getE2EData(Object object) throws InvalidAlgorithmParameterException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidParameterSpecException, IOException { + private E2EData getE2EData(Object object) throws InvalidAlgorithmParameterException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException { byte[] key = EncryptionUtils.generateKey(); byte[] iv = EncryptionUtils.randomBytes(EncryptionUtils.ivLength); Cipher cipher = EncryptionUtils.getCipher(Cipher.ENCRYPT_MODE, key, iv); 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 941b3346be29..2f8313fe7643 100644 --- a/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java +++ b/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java @@ -65,6 +65,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.RandomAccessFile; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; @@ -72,7 +73,6 @@ import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; -import java.security.KeyPairGenerator; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; @@ -84,7 +84,6 @@ import java.security.interfaces.RSAPrivateCrtKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; -import java.security.spec.InvalidParameterSpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.util.ArrayList; import java.util.Arrays; @@ -120,6 +119,7 @@ public final class EncryptionUtils { public static final String PRIVATE_KEY = "PRIVATE_KEY"; public static final String MNEMONIC = "MNEMONIC"; + private static final int GCM_AUTHENTICATION_TAG_LENGTH = 16; public static final int ivLength = 16; public static final int saltLength = 40; public static final String ivDelimiter = "|"; // not base64 encoded @@ -516,16 +516,26 @@ public static byte[] decodeStringToBase64Bytes(String string) { return Base64.decode(string, Base64.NO_WRAP); } - public static EncryptedFile encryptFile(String accountName, File file, Cipher cipher) throws InvalidParameterSpecException, IOException { + public static EncryptedFile encryptFile(String accountName, File file, Cipher cipher) throws IOException { File tempEncryptedFolder = FileDataStorageManager.createTempEncryptedFolder(accountName); File tempEncryptedFile = File.createTempFile(file.getName(), null, tempEncryptedFolder); encryptFileWithGivenCipher(file, tempEncryptedFile, cipher); - String authenticationTagString = getAuthenticationTag(cipher); + String authenticationTagString = getAuthenticationTag(tempEncryptedFile); return new EncryptedFile(tempEncryptedFile, authenticationTagString); } - public static String getAuthenticationTag(Cipher cipher) throws InvalidParameterSpecException { - byte[] authenticationTag = cipher.getParameters().getParameterSpec(GCMParameterSpec.class).getIV(); + public static String getAuthenticationTag(File encryptedFile) throws IOException { + long fileLength = encryptedFile.length(); + if (fileLength < GCM_AUTHENTICATION_TAG_LENGTH) { + throw new IOException("Encrypted file is too small to contain a GCM authentication tag"); + } + + byte[] authenticationTag = new byte[GCM_AUTHENTICATION_TAG_LENGTH]; + try (RandomAccessFile randomAccessFile = new RandomAccessFile(encryptedFile, "r")) { + randomAccessFile.seek(fileLength - GCM_AUTHENTICATION_TAG_LENGTH); + randomAccessFile.readFully(authenticationTag); + } + return encodeBytesToBase64String(authenticationTag); } @@ -581,13 +591,13 @@ public static void decryptFile(Cipher cipher, inputStream.close(); outputStream.close(); - if (!getAuthenticationTag(cipher).equals(authenticationTag)) { + if (!getAuthenticationTag(encryptedFile).equals(authenticationTag)) { reportE2eError(arbitraryDataProvider, user); throw new SecurityException("Tag not correct"); } Log_OC.d(TAG, encryptedFile.getName() + "decrypted successfully"); - } catch (IOException | BadPaddingException | IllegalBlockSizeException | InvalidParameterSpecException | + } catch (IOException | BadPaddingException | IllegalBlockSizeException | SecurityException exception) { Log_OC.d(TAG, "Error caught at decryptFile(): " + exception.getLocalizedMessage()); }