Skip to content
Open
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
25 changes: 16 additions & 9 deletions app/src/main/java/com/nextcloud/utils/e2ee/E2EEKeyInspector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.owncloud.android.datamodel.ArbitraryDataProvider
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.datamodel.e2e.v1.encrypted.EncryptedFolderMetadataFileV1
import com.owncloud.android.datamodel.e2e.v2.encrypted.EncryptedFolderMetadataFile
import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.OwnCloudClientFactory
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation
Expand All @@ -25,6 +25,7 @@ import com.owncloud.android.operations.GetCapabilitiesOperation
import com.owncloud.android.ui.dialog.setupEncryption.CertificateValidator
import com.owncloud.android.ui.dialog.setupEncryption.model.DownloadKeyResult
import com.owncloud.android.utils.EncryptionUtils
import com.owncloud.android.utils.EncryptionUtilsV2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.apache.commons.httpclient.HttpStatus
Expand Down Expand Up @@ -102,23 +103,29 @@ class E2EEKeyInspector @Inject constructor(
val metadata = metadataResult.resultData

return@withContext if (E2EVersionHelper.isV2Plus(capability)) {
decryptsMetadataV2(metadata.metadata, privateKey, client.userId)
decryptsMetadataV2(folder, privateKey, client)
} else {
decryptsMetadataV1(metadata.metadata, privateKey, folder.localId)
}
}

private fun decryptsMetadataV2(serializedMetadata: String, privateKey: String, userId: String): Boolean {
val metadataFile = EncryptionUtils.deserializeJSON(
serializedMetadata,
object : TypeToken<EncryptedFolderMetadataFile>() {}
private fun decryptsMetadataV2(ocFile: OCFile, privateKey: String, client: OwnCloudClient): Boolean {
val userId = client.userId
val metadata = EncryptionUtilsV2().retrieveTopMostMetadata(
ocFile,
storageManager,
client,
userId,
privateKey,
accountManager.user,
context,
arbitraryDataProvider
)

val user = metadataFile.users.find { it.userId == userId }
val user = metadata.users.find { it.userId == userId }
?: throw IllegalStateException("cannot find current user in metadata")

return try {
EncryptionUtils.decryptStringAsymmetricV2(user.encryptedMetadataKey, privateKey)
EncryptionUtils.decryptStringAsymmetricV2(user.decryptedMetadataKey, privateKey)
true
} catch (e: Exception) {
Log_OC.w(TAG, "user tried to decrypt folder's metadata with different private key: $e")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
*/
package com.owncloud.android.datamodel.e2e.v2.encrypted

import com.nextcloud.utils.e2ee.E2EVersionHelper

/**
* Decrypted class representation of metadata json of folder metadata.
*/
data class EncryptedFolderMetadataFile(
val metadata: EncryptedMetadata,
val users: List<EncryptedUser>,
val users: List<EncryptedUser>?,
@Transient val filedrop: MutableMap<String, EncryptedFiledrop>?,
val version: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class EncryptionUtilsV2 {
)
} else {
// Top folder
val encryptedUser = metadataFile.users.find { it.userId == userId }
val encryptedUser = metadataFile.users?.find { it.userId == userId }
?: throw IllegalStateException("Cannot find current user in metadata")

val decryptedMetadataKey = decryptMetadataKey(encryptedUser, privateKey)
Expand Down
Loading