Search in sources :

Example 1 with ToggleEncryptionRemoteOperation

use of com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation in project android by nextcloud.

the class EndToEndRandomIT method init.

private void init() throws Exception {
    // create folder
    createFolder(rootEncFolder);
    OCFile encFolder = createFolder(rootEncFolder + RandomString.make(5) + "/");
    // encrypt it
    assertTrue(new ToggleEncryptionRemoteOperation(encFolder.getLocalId(), encFolder.getRemotePath(), true).execute(client).isSuccess());
    encFolder.setEncrypted(true);
    getStorageManager().saveFolder(encFolder, new ArrayList<>(), new ArrayList<>());
    useExistingKeys();
    rootEncFolder = encFolder.getDecryptedRemotePath();
    currentFolder = encFolder;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)

Example 2 with ToggleEncryptionRemoteOperation

use of com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation in project android by nextcloud.

the class EndToEndRandomIT method reInit.

@Test
public void reInit() throws Exception {
    // create folder
    OCFile encFolder = createFolder(rootEncFolder);
    // encrypt it
    assertTrue(new ToggleEncryptionRemoteOperation(encFolder.getLocalId(), encFolder.getRemotePath(), true).execute(client).isSuccess());
    encFolder.setEncrypted(true);
    getStorageManager().saveFolder(encFolder, new ArrayList<>(), new ArrayList<>());
    createKeys();
    // delete keys
    arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.PRIVATE_KEY);
    arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.PUBLIC_KEY);
    arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.MNEMONIC);
    useExistingKeys();
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation) Test(org.junit.Test)

Example 3 with ToggleEncryptionRemoteOperation

use of com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation in project android by nextcloud.

the class CreateFolderOperation method encryptedCreate.

private RemoteOperationResult encryptedCreate(OCFile parent, OwnCloudClient client) {
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
    String privateKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PRIVATE_KEY);
    String publicKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PUBLIC_KEY);
    String token = null;
    Boolean metadataExists;
    DecryptedFolderMetadata metadata;
    String encryptedRemotePath = null;
    String filename = new File(remotePath).getName();
    try {
        // lock folder
        token = EncryptionUtils.lockFolder(parent, client);
        // get metadata
        Pair<Boolean, DecryptedFolderMetadata> metadataPair = EncryptionUtils.retrieveMetadata(parent, client, privateKey, publicKey);
        metadataExists = metadataPair.first;
        metadata = metadataPair.second;
        // check if filename already exists
        if (isFileExisting(metadata, filename)) {
            return new RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
        }
        // generate new random file name, check if it exists in metadata
        String encryptedFileName = createRandomFileName(metadata);
        encryptedRemotePath = parent.getRemotePath() + encryptedFileName;
        RemoteOperationResult result = new CreateFolderRemoteOperation(encryptedRemotePath, true, token).execute(client);
        if (result.isSuccess()) {
            // update metadata
            metadata.getFiles().put(encryptedFileName, createDecryptedFile(filename));
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
            String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
            // upload metadata
            EncryptionUtils.uploadMetadata(parent, serializedFolderMetadata, token, client, metadataExists);
            // unlock folder
            if (token != null) {
                RemoteOperationResult unlockFolderResult = EncryptionUtils.unlockFolder(parent, client, token);
                if (unlockFolderResult.isSuccess()) {
                    token = null;
                } else {
                    // TODO do better
                    throw new RuntimeException("Could not unlock folder!");
                }
            }
            RemoteOperationResult remoteFolderOperationResult = new ReadFolderRemoteOperation(encryptedRemotePath).execute(client);
            createdRemoteFolder = (RemoteFile) remoteFolderOperationResult.getData().get(0);
            OCFile newDir = createRemoteFolderOcFile(parent, filename, createdRemoteFolder);
            getStorageManager().saveFile(newDir);
            RemoteOperationResult encryptionOperationResult = new ToggleEncryptionRemoteOperation(newDir.getLocalId(), newDir.getRemotePath(), true).execute(client);
            if (!encryptionOperationResult.isSuccess()) {
                throw new RuntimeException("Error creating encrypted subfolder!");
            }
        } else {
            // revert to sane state in case of any error
            Log_OC.e(TAG, remotePath + " hasn't been created");
        }
        return result;
    } catch (Exception e) {
        if (!EncryptionUtils.unlockFolder(parent, client, token).isSuccess()) {
            throw new RuntimeException("Could not clean up after failing folder creation!");
        }
        // remove folder
        if (encryptedRemotePath != null) {
            RemoteOperationResult removeResult = new RemoveRemoteEncryptedFileOperation(encryptedRemotePath, parent.getLocalId(), user.toPlatformAccount(), context, filename).execute(client);
            if (!removeResult.isSuccess()) {
                throw new RuntimeException("Could not clean up after failing folder creation!");
            }
        }
        // TODO do better
        return new RemoteOperationResult(e);
    } finally {
        // unlock folder
        if (token != null) {
            RemoteOperationResult unlockFolderResult = EncryptionUtils.unlockFolder(parent, client, token);
            if (!unlockFolderResult.isSuccess()) {
                // TODO do better
                throw new RuntimeException("Could not unlock folder!");
            }
        }
    }
}
Also used : CreateFolderRemoteOperation(com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) ReadFolderRemoteOperation(com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation) OCFile(com.owncloud.android.datamodel.OCFile) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)

Example 4 with ToggleEncryptionRemoteOperation

use of com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation in project android by nextcloud.

the class EndToEndRandomIT method removeFolder.

private void removeFolder(OCFile folder) {
    Log_OC.d(this, "Start removing content of folder: " + folder.getDecryptedRemotePath());
    List<OCFile> children = fileDataStorageManager.getFolderContent(folder, false);
    // remove children
    for (OCFile child : children) {
        if (child.isFolder()) {
            removeFolder(child);
            // remove folder
            Log_OC.d(this, "Remove folder: " + child.getDecryptedRemotePath());
            if (!folder.isEncrypted() && child.isEncrypted()) {
                assertTrue(new ToggleEncryptionRemoteOperation(child.getLocalId(), child.getRemotePath(), false).execute(client).isSuccess());
                OCFile f = getStorageManager().getFileByEncryptedRemotePath(child.getRemotePath());
                f.setEncrypted(false);
                getStorageManager().saveFile(f);
                child.setEncrypted(false);
            }
        } else {
            Log_OC.d(this, "Remove file: " + child.getDecryptedRemotePath());
        }
        assertTrue(new RemoveFileOperation(child, false, account, false, targetContext, getStorageManager()).execute(client).isSuccess());
    }
    Log_OC.d(this, "Finished removing content of folder: " + folder.getDecryptedRemotePath());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)

Example 5 with ToggleEncryptionRemoteOperation

use of com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation in project android by nextcloud.

the class AbstractOnServerIT method deleteAllFiles.

public static void deleteAllFiles() {
    RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client);
    assertTrue(result.getLogMessage(), result.isSuccess());
    for (Object object : result.getData()) {
        RemoteFile remoteFile = (RemoteFile) object;
        if (!remoteFile.getRemotePath().equals("/")) {
            if (remoteFile.isEncrypted()) {
                assertTrue(new ToggleEncryptionRemoteOperation(remoteFile.getLocalId(), remoteFile.getRemotePath(), false).execute(client).isSuccess());
            }
            assertTrue(new RemoveFileRemoteOperation(remoteFile.getRemotePath()).execute(client).isSuccess());
        }
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadFolderRemoteOperation(com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation) RemoveFileRemoteOperation(com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation)

Aggregations

ToggleEncryptionRemoteOperation (com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation)6 OCFile (com.owncloud.android.datamodel.OCFile)4 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)2 ReadFolderRemoteOperation (com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation)2 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)2 User (com.nextcloud.client.account.User)1 ClientFactory (com.nextcloud.client.network.ClientFactory)1 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)1 CreateFolderRemoteOperation (com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation)1 RemoveFileRemoteOperation (com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation)1 RemoveFileOperation (com.owncloud.android.operations.RemoveFileOperation)1 SetupEncryptionDialogFragment (com.owncloud.android.ui.dialog.SetupEncryptionDialogFragment)1 File (java.io.File)1 Subscribe (org.greenrobot.eventbus.Subscribe)1 Test (org.junit.Test)1