Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 18 additions & 8 deletions app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
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;
import java.security.InvalidKeyException;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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());
}
Expand Down
Loading