Skip to content

Commit

Permalink
migration works
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <[email protected]>
  • Loading branch information
tobiasKaminsky committed Sep 25, 2023
1 parent 95cb5d9 commit f773222
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,14 @@ private void synchronizeData(List<Object> 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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -454,6 +461,8 @@ public static DecryptedFolderMetadataFileV1 decryptFolderMetaData(EncryptedFolde
folder);
}
return null;
*/
}

public static E2EVersion determinateVersion(String metadata) {
Expand Down
68 changes: 60 additions & 8 deletions app/src/main/java/com/owncloud/android/utils/EncryptionUtilsV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,12 @@ class EncryptionUtilsV2 {
migrateV1ToV2(
decryptedV1,
client.userIdPlain,
publicKey
publicKey,
folder,
storageManager,
client,
user,
context
)
} catch (e: Exception) {
// TODO do better
Expand Down Expand Up @@ -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<String, DecryptedFile>()

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
Expand Down

0 comments on commit f773222

Please sign in to comment.