diff --git a/app/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java b/app/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java index 4d3e5fbf8a03..d8a22e6974e9 100644 --- a/app/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java @@ -475,11 +475,14 @@ private void synchronizeData(List folderAndFiles) { // update size mLocalFolder.setFileLength(remoteFolder.getFileLength()); - Object object = getDecryptedFolderMetadata(encryptedAncestor, - mLocalFolder, - getClient(), - user, - mContext); + Object object = null; + if (mLocalFolder.isEncrypted()) { + object = getDecryptedFolderMetadata(encryptedAncestor, + mLocalFolder, + getClient(), + user, + mContext); + } if (encryptedAncestor && object == null) { throw new IllegalStateException("metadata is null!"); 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 f70869d770bf..5542e9b4a850 100644 --- a/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java +++ b/app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java @@ -416,11 +416,18 @@ public static DecryptedFolderMetadataFileV1 decryptFolderMetaData(EncryptedFolde } // decrypt metadata + EncryptionUtilsV2 encryptionUtilsV2 = new EncryptionUtilsV2(); String serializedEncryptedMetadata = getMetadataOperationResult.getResultData().getMetadata(); + return encryptionUtilsV2.parseAnyMetadata(getMetadataOperationResult.getResultData(), + user, + client, + context, + folder); + /* E2EVersion version = determinateVersion(serializedEncryptedMetadata); - EncryptionUtilsV2 encryptionUtilsV2 = new EncryptionUtilsV2(); + switch (version) { case UNKNOWN: @@ -454,6 +461,8 @@ public static DecryptedFolderMetadataFileV1 decryptFolderMetaData(EncryptedFolde folder); } return null; + + */ } public static E2EVersion determinateVersion(String metadata) { diff --git a/app/src/main/java/com/owncloud/android/utils/EncryptionUtilsV2.kt b/app/src/main/java/com/owncloud/android/utils/EncryptionUtilsV2.kt index dda0301010f2..9e845d17608a 100644 --- a/app/src/main/java/com/owncloud/android/utils/EncryptionUtilsV2.kt +++ b/app/src/main/java/com/owncloud/android/utils/EncryptionUtilsV2.kt @@ -555,7 +555,12 @@ class EncryptionUtilsV2 { migrateV1ToV2( decryptedV1, client.userIdPlain, - publicKey + publicKey, + folder, + storageManager, + client, + user, + context ) } catch (e: Exception) { // TODO do better @@ -655,26 +660,73 @@ class EncryptionUtilsV2 { fun migrateV1ToV2( v1: DecryptedFolderMetadataFileV1, userId: String, - cert: String + cert: String, + folder: OCFile, + storageManager: FileDataStorageManager, + client: OwnCloudClient, + user: User, + context: Context ): DecryptedFolderMetadataFile { + // key + val key = if (v1.metadata.metadataKeys != null && v1.metadata.metadataKeys.size > 1) { + v1.metadata.metadataKeys[0] + } else { + v1.metadata.metadataKey + } + // create new metadata val metadataV2 = DecryptedMetadata( mutableListOf(), false, 0, - mutableMapOf(), - v1.files.mapValues { migrateDecryptedFileV1ToV2(it.value) }.toMutableMap(), - EncryptionUtils.decodeStringToBase64Bytes(v1.metadata.metadataKeys[0]) - ?: throw IllegalStateException("Metadata key not found!") + v1 + .files + .filter { it.value.encrypted.mimetype == MimeType.WEBDAV_FOLDER } + .mapValues { it.value.encrypted.filename } + .toMutableMap(), + v1 + .files + .filter { it.value.encrypted.mimetype != MimeType.WEBDAV_FOLDER } + .mapValues { migrateDecryptedFileV1ToV2(it.value) } + .toMutableMap(), + EncryptionUtils.decodeStringToBase64Bytes(key) ?: throw IllegalStateException("Metadata key not found!") ) // upon migration there can only be one user, as there is no sharing yet in place - val users = mutableListOf(DecryptedUser(userId, cert)) + val users = if (storageManager.getFileById(folder.parentId)?.isEncrypted == false) { + mutableListOf(DecryptedUser(userId, cert)) + } else { + mutableListOf() + } // TODO val filedrop = mutableMapOf() - return DecryptedFolderMetadataFile(metadataV2, users, filedrop) + val newMetadata = DecryptedFolderMetadataFile(metadataV2, users, filedrop) + val metadataKey = EncryptionUtils.generateKey() ?: throw UploadException("Could not encrypt folder!") + + newMetadata.metadata.metadataKey = metadataKey + newMetadata.metadata.keyChecksums.add(EncryptionUtilsV2().hashMetadataKey(metadataKey)) + + // lock + val token = EncryptionUtils.lockFolder(folder, client) + + // upload + serializeAndUploadMetadata( + folder, + newMetadata, + token, + client, + true, + context, + user, + storageManager + ) + + // unlock + EncryptionUtils.unlockFolder(folder, client, token) + + return newMetadata } @VisibleForTesting