Search in sources :

Example 1 with GetMetadataRemoteOperation

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

the class EncryptionUtils method retrieveMetadata.

/**
 * @param parentFile file metadata should be retrieved for
 * @return Pair: boolean: true: metadata already exists, false: metadata new created
 */
public static Pair<Boolean, DecryptedFolderMetadata> retrieveMetadata(OCFile parentFile, OwnCloudClient client, String privateKey, String publicKey) throws UploadException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException, CertificateException {
    GetMetadataRemoteOperation getMetadataOperation = new GetMetadataRemoteOperation(parentFile.getLocalId());
    RemoteOperationResult getMetadataOperationResult = getMetadataOperation.execute(client);
    DecryptedFolderMetadata metadata;
    if (getMetadataOperationResult.isSuccess()) {
        // decrypt metadata
        String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
        EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
        });
        return new Pair<>(Boolean.TRUE, EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey));
    } else if (getMetadataOperationResult.getHttpCode() == HttpStatus.SC_NOT_FOUND) {
        // new metadata
        metadata = new DecryptedFolderMetadata();
        metadata.setMetadata(new DecryptedFolderMetadata.Metadata());
        metadata.getMetadata().setMetadataKeys(new HashMap<>());
        String metadataKey = EncryptionUtils.encodeBytesToBase64String(EncryptionUtils.generateKey());
        String encryptedMetadataKey = EncryptionUtils.encryptStringAsymmetric(metadataKey, publicKey);
        metadata.getMetadata().getMetadataKeys().put(0, encryptedMetadataKey);
        return new Pair<>(Boolean.FALSE, metadata);
    } else {
        // TODO error
        throw new UploadException("something wrong");
    }
}
Also used : HashMap(java.util.HashMap) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) GetMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation) UploadException(com.owncloud.android.operations.UploadException) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) KeyPair(java.security.KeyPair) Pair(android.util.Pair)

Example 2 with GetMetadataRemoteOperation

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

the class EncryptionUtils method downloadFolderMetadata.

/**
 * Download metadata for folder and decrypt it
 *
 * @return decrypted metadata or null
 */
@Nullable
public static DecryptedFolderMetadata downloadFolderMetadata(OCFile folder, OwnCloudClient client, Context context, Account account) {
    RemoteOperationResult getMetadataOperationResult = new GetMetadataRemoteOperation(folder.getLocalId()).execute(client);
    if (!getMetadataOperationResult.isSuccess()) {
        return null;
    }
    // decrypt metadata
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
    String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
    String privateKey = arbitraryDataProvider.getValue(account.name, EncryptionUtils.PRIVATE_KEY);
    EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
    });
    try {
        return EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey);
    } catch (Exception e) {
        Log_OC.e(TAG, e.getMessage());
        return null;
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) GetMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) UploadException(com.owncloud.android.operations.UploadException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BadPaddingException(javax.crypto.BadPaddingException) Nullable(androidx.annotation.Nullable)

Example 3 with GetMetadataRemoteOperation

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

the class RemoveRemoteEncryptedFileOperation method run.

/**
 * Performs the remove operation.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    DeleteMethod delete = null;
    String token = null;
    DecryptedFolderMetadata metadata;
    String privateKey = arbitraryDataProvider.getValue(account.name, EncryptionUtils.PRIVATE_KEY);
    try {
        // Lock folder
        RemoteOperationResult lockFileOperationResult = new LockFileRemoteOperation(parentId).execute(client);
        if (lockFileOperationResult.isSuccess()) {
            token = (String) lockFileOperationResult.getData().get(0);
        } else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
            throw new RemoteOperationFailedException("Forbidden! Please try again later.)");
        } else {
            throw new RemoteOperationFailedException("Unknown error!");
        }
        // refresh metadata
        RemoteOperationResult getMetadataOperationResult = new GetMetadataRemoteOperation(parentId).execute(client);
        if (getMetadataOperationResult.isSuccess()) {
            // decrypt metadata
            String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
            });
            metadata = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey);
        } else {
            throw new RemoteOperationFailedException("No Metadata found!");
        }
        // delete file remote
        delete = new DeleteMethod(client.getFilesDavUri(remotePath));
        delete.setQueryString(new NameValuePair[] { new NameValuePair(E2E_TOKEN, token) });
        int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
        // exhaust the response, although not interesting
        delete.getResponseBodyAsString();
        result = new RemoteOperationResult(delete.succeeded() || status == HttpStatus.SC_NOT_FOUND, delete);
        Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage());
        // remove file from metadata
        metadata.getFiles().remove(fileName);
        EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
        String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
        // upload metadata
        RemoteOperationResult uploadMetadataOperationResult = new UpdateMetadataRemoteOperation(parentId, serializedFolderMetadata, token).execute(client);
        if (!uploadMetadataOperationResult.isSuccess()) {
            throw new RemoteOperationFailedException("Metadata not uploaded!");
        }
        // return success
        return result;
    } catch (NoSuchAlgorithmException | IOException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException | InvalidKeySpecException e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e);
    } finally {
        if (delete != null) {
            delete.releaseConnection();
        }
        // unlock file
        if (token != null) {
            RemoteOperationResult unlockFileOperationResult = new UnlockFileRemoteOperation(parentId, token).execute(client);
            if (!unlockFileOperationResult.isSuccess()) {
                Log_OC.e(TAG, "Failed to unlock " + parentId);
            }
        }
    }
    return result;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) UnlockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation) UpdateMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.UpdateMetadataRemoteOperation) GetMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) LockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.LockFileRemoteOperation)

Aggregations

EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)3 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 GetMetadataRemoteOperation (com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation)3 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)2 UploadException (com.owncloud.android.operations.UploadException)2 IOException (java.io.IOException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 Pair (android.util.Pair)1 Nullable (androidx.annotation.Nullable)1 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)1 LockFileRemoteOperation (com.owncloud.android.lib.resources.e2ee.LockFileRemoteOperation)1 UnlockFileRemoteOperation (com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation)1 UpdateMetadataRemoteOperation (com.owncloud.android.lib.resources.e2ee.UpdateMetadataRemoteOperation)1 KeyPair (java.security.KeyPair)1