Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - Utils Code Cleanup #13455

Merged
merged 1 commit into from
Aug 23, 2024
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 @@ -118,7 +118,7 @@ nDO4ew==
)

val users = mutableListOf(
DecryptedUser(userId, cert)
DecryptedUser(userId, cert, null)
)

// val filedrop = mutableMapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package com.owncloud.android.utils
import com.google.gson.reflect.TypeToken
import com.nextcloud.client.account.MockUser
import com.nextcloud.common.User
import com.nextcloud.utils.extensions.findMetadataKeyByUserId
import com.owncloud.android.EncryptionIT
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.datamodel.e2e.v1.decrypted.Data
Expand Down Expand Up @@ -221,7 +222,7 @@ class EncryptionUtilsV2IT : EncryptionIT() {
val metadataKeyBase64 = EncryptionUtils.generateKeyString()
val metadataKey = EncryptionUtils.decodeStringToBase64Bytes(metadataKeyBase64)

val user = DecryptedUser("t1", encryptionTestUtils.t1PublicKey)
val user = DecryptedUser("t1", encryptionTestUtils.t1PublicKey, null)

val encryptedUser = encryptionUtilsV2.encryptUser(user, metadataKey)
assertNotEquals(encryptedUser.encryptedMetadataKey, metadataKeyBase64)
Expand Down Expand Up @@ -274,6 +275,11 @@ class EncryptionUtilsV2IT : EncryptionIT() {
arbitraryDataProvider
)

// V1 doesn't have decryptedMetadataKey so that we can ignore it for comparison
for (user in decrypted.users) {
user.decryptedMetadataKey = null
}

assertEquals(metadataFile, decrypted)
}

Expand Down Expand Up @@ -489,7 +495,7 @@ class EncryptionUtilsV2IT : EncryptionIT() {

var metadataFile = generateDecryptedFolderMetadataFile(enc1, enc1Cert)

metadataFile = encryptionUtilsV2.addShareeToMetadata(metadataFile, enc2.accountName, enc2Cert)
metadataFile = encryptionUtilsV2.addShareeToMetadata(metadataFile, enc2.accountName, enc2Cert, null)

val encryptedMetadataFile = encryptionUtilsV2.encryptFolderMetadataFile(
metadataFile,
Expand Down Expand Up @@ -541,7 +547,12 @@ class EncryptionUtilsV2IT : EncryptionIT() {
val enc1 = MockUser("enc1", "Nextcloud")
val enc2 = MockUser("enc2", "Nextcloud")
var metadataFile = generateDecryptedFolderMetadataFile(enc1, enc1Cert)
metadataFile = encryptionUtilsV2.addShareeToMetadata(metadataFile, enc2.accountName, enc2Cert)
metadataFile = encryptionUtilsV2.addShareeToMetadata(
metadataFile,
enc2.accountName,
enc2Cert,
metadataFile.users.findMetadataKeyByUserId(enc2.accountName)
)

assertEquals(2, metadataFile.users.size)

Expand Down Expand Up @@ -586,7 +597,7 @@ class EncryptionUtilsV2IT : EncryptionIT() {
)

val users = mutableListOf(
DecryptedUser(user.accountName, cert)
DecryptedUser(user.accountName, cert, null)
)

metadata.keyChecksums.add(encryptionUtilsV2.hashMetadataKey(metadata.metadataKey))
Expand Down Expand Up @@ -734,8 +745,6 @@ class EncryptionUtilsV2IT : EncryptionIT() {
|Rei/RGBQ==","userId": "john"}],"version": "2"}
""".trimMargin()

val base64Metadata = EncryptionUtils.encodeStringToBase64String(metadata)

val privateKey = EncryptionUtils.PEMtoPrivateKey(encryptionTestUtils.t1PrivateKey)
val certificateT1 = EncryptionUtils.convertCertFromString(encryptionTestUtils.t1PublicKey)
val certificateEnc2 = EncryptionUtils.convertCertFromString(enc2Cert)
Expand All @@ -746,23 +755,18 @@ class EncryptionUtilsV2IT : EncryptionIT() {
metadata
)

val base64Ans = encryptionUtilsV2.extractSignedString(signed)

// verify
val certs = listOf(
certificateEnc2,
certificateT1
)
assertTrue(encryptionUtilsV2.verifySignedMessage(signed, certs))
assertTrue(encryptionUtilsV2.verifySignedMessage(base64Ans, base64Metadata, certs))

assertTrue(encryptionUtilsV2.verifySignedData(signed, certs))
}

@Throws(Throwable::class)
@Test
fun sign() {
val sut = "randomstring123"
val json = "randomstring123"
val jsonBase64 = EncryptionUtils.encodeStringToBase64String(json)

val privateKey = EncryptionUtils.PEMtoPrivateKey(encryptionTestUtils.t1PrivateKey)
val certificate = EncryptionUtils.convertCertFromString(encryptionTestUtils.t1PublicKey)
Expand All @@ -773,15 +777,12 @@ class EncryptionUtilsV2IT : EncryptionIT() {
sut
)

val base64Ans = encryptionUtilsV2.extractSignedString(signed)

// verify
val certs = listOf(
EncryptionUtils.convertCertFromString(enc2Cert),
certificate
)
assertTrue(encryptionUtilsV2.verifySignedMessage(signed, certs))
assertTrue(encryptionUtilsV2.verifySignedMessage(base64Ans, jsonBase64, certs))

assertTrue(encryptionUtilsV2.verifySignedData(signed, certs))
}

@Test
Expand Down Expand Up @@ -857,6 +858,11 @@ class EncryptionUtilsV2IT : EncryptionIT() {
arbitraryDataProvider
)

// V1 doesn't have decryptedMetadataKey so that we can ignore it for comparison
for (user in decryptedFolderMetadata2.users) {
user.decryptedMetadataKey = null
}

// compare
assertTrue(
EncryptionTestIT.compareJsonStrings(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.nextcloud.utils.extensions

import com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedUser

fun List<DecryptedUser?>.findMetadataKeyByUserId(userId: String): String? {
var result: String? = null

for (decryptedUser in this) {
if (decryptedUser != null && decryptedUser.userId == userId) {
result = decryptedUser.decryptedMetadataKey
}
}

return result
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,10 @@ public static String getUserAgent() {
return getUserAgent(R.string.nextcloud_user_agent);
}

public static void showMessage(int messageId) {
ContextExtensionsKt.showToast(getAppContext(), messageId);
}

// user agent
private static String getUserAgent(@StringRes int agent) {
String appString = string(agent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ package com.owncloud.android.datamodel.e2e.v2.decrypted

data class DecryptedUser(
val userId: String,
val certificate: String
val certificate: String,
var decryptedMetadataKey: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.client.network.ClientFactoryImpl;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.utils.extensions.DecryptedUserExtensionsKt;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.FileDataStorageManager;
Expand Down Expand Up @@ -183,7 +184,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
if (metadata == null) {
String cert = EncryptionUtils.retrievePublicKeyForUser(user, context);
metadata = new EncryptionUtilsV2().createDecryptedFolderMetadataFile();
metadata.getUsers().add(new DecryptedUser(client.getUserId(), cert));
metadata.getUsers().add(new DecryptedUser(client.getUserId(), cert, null));

metadataExists = false;
} else {
Expand All @@ -194,9 +195,12 @@ protected RemoteOperationResult run(OwnCloudClient client) {

// add sharee to metadata
String publicKey = EncryptionUtils.getPublicKey(user, shareeName, arbitraryDataProvider);

String decryptedMetadataKey = DecryptedUserExtensionsKt.findMetadataKeyByUserId(metadata.getUsers(), shareeName);
DecryptedFolderMetadataFile newMetadata = encryptionUtilsV2.addShareeToMetadata(metadata,
shareeName,
publicKey);
publicKey,
decryptedMetadataKey);

// upload metadata
metadata.getMetadata().setCounter(newCounter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ public static Pair<Boolean, DecryptedFolderMetadataFile> retrieveMetadata(OCFile
new ArrayList<>(),
new HashMap<>(),
E2EVersion.V2_0.getValue());
metadata.getUsers().add(new DecryptedUser(client.getUserId(), publicKey));
metadata.getUsers().add(new DecryptedUser(client.getUserId(), publicKey, null));
byte[] metadataKey = EncryptionUtils.generateKey();

if (metadataKey == null) {
Expand Down
Loading
Loading