RemoveRemoteEncryptedFileOperation.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.operations;
  22. import android.content.Context;
  23. import com.google.gson.reflect.TypeToken;
  24. import com.nextcloud.client.account.User;
  25. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  26. import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
  27. import com.owncloud.android.datamodel.DecryptedFolderMetadata;
  28. import com.owncloud.android.datamodel.EncryptedFolderMetadata;
  29. import com.owncloud.android.lib.common.OwnCloudClient;
  30. import com.owncloud.android.lib.common.operations.RemoteOperation;
  31. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. import com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation;
  34. import com.owncloud.android.lib.resources.e2ee.LockFileRemoteOperation;
  35. import com.owncloud.android.lib.resources.e2ee.UnlockFileRemoteOperation;
  36. import com.owncloud.android.lib.resources.e2ee.UpdateMetadataRemoteOperation;
  37. import com.owncloud.android.utils.EncryptionUtils;
  38. import org.apache.commons.httpclient.HttpStatus;
  39. import org.apache.commons.httpclient.NameValuePair;
  40. import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
  41. import java.io.IOException;
  42. import java.security.InvalidAlgorithmParameterException;
  43. import java.security.InvalidKeyException;
  44. import java.security.NoSuchAlgorithmException;
  45. import java.security.cert.CertificateException;
  46. import java.security.spec.InvalidKeySpecException;
  47. import javax.crypto.BadPaddingException;
  48. import javax.crypto.IllegalBlockSizeException;
  49. import javax.crypto.NoSuchPaddingException;
  50. /**
  51. * Remote operation performing the removal of a remote encrypted file or folder
  52. */
  53. public class RemoveRemoteEncryptedFileOperation extends RemoteOperation {
  54. private static final String TAG = RemoveRemoteEncryptedFileOperation.class.getSimpleName();
  55. private static final int REMOVE_READ_TIMEOUT = 30000;
  56. private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
  57. private final String remotePath;
  58. private final long parentId;
  59. private User user;
  60. private final ArbitraryDataProvider arbitraryDataProvider;
  61. private final String fileName;
  62. /**
  63. * Constructor
  64. *
  65. * @param remotePath RemotePath of the remote file or folder to remove from the server
  66. * @param parentId local id of parent folder
  67. */
  68. RemoveRemoteEncryptedFileOperation(String remotePath,
  69. long parentId,
  70. User user,
  71. Context context,
  72. String fileName) {
  73. this.remotePath = remotePath;
  74. this.parentId = parentId;
  75. this.user = user;
  76. this.fileName = fileName;
  77. arbitraryDataProvider = new ArbitraryDataProviderImpl(context);
  78. }
  79. /**
  80. * Performs the remove operation.
  81. */
  82. @Override
  83. protected RemoteOperationResult run(OwnCloudClient client) {
  84. RemoteOperationResult result;
  85. DeleteMethod delete = null;
  86. String token = null;
  87. DecryptedFolderMetadata metadata;
  88. String privateKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PRIVATE_KEY);
  89. String publicKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PUBLIC_KEY);
  90. try {
  91. // Lock folder
  92. RemoteOperationResult lockFileOperationResult = new LockFileRemoteOperation(parentId).execute(client);
  93. if (lockFileOperationResult.isSuccess()) {
  94. token = (String) lockFileOperationResult.getData().get(0);
  95. } else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
  96. throw new RemoteOperationFailedException("Forbidden! Please try again later.)");
  97. } else {
  98. throw new RemoteOperationFailedException("Unknown error!");
  99. }
  100. // refresh metadata
  101. RemoteOperationResult getMetadataOperationResult = new GetMetadataRemoteOperation(parentId).execute(client);
  102. if (getMetadataOperationResult.isSuccess()) {
  103. // decrypt metadata
  104. String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
  105. EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(
  106. serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
  107. });
  108. metadata = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata,
  109. privateKey,
  110. arbitraryDataProvider,
  111. user,
  112. parentId);
  113. } else {
  114. throw new RemoteOperationFailedException("No Metadata found!");
  115. }
  116. // delete file remote
  117. delete = new DeleteMethod(client.getFilesDavUri(remotePath));
  118. delete.setQueryString(new NameValuePair[]{new NameValuePair(E2E_TOKEN, token)});
  119. int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
  120. delete.getResponseBodyAsString(); // exhaust the response, although not interesting
  121. result = new RemoteOperationResult(delete.succeeded() || status == HttpStatus.SC_NOT_FOUND, delete);
  122. Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage());
  123. // remove file from metadata
  124. metadata.getFiles().remove(fileName);
  125. EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(
  126. metadata,
  127. privateKey,
  128. publicKey,
  129. arbitraryDataProvider,
  130. user,
  131. parentId);
  132. String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
  133. // upload metadata
  134. RemoteOperationResult uploadMetadataOperationResult =
  135. new UpdateMetadataRemoteOperation(parentId,
  136. serializedFolderMetadata, token).execute(client);
  137. if (!uploadMetadataOperationResult.isSuccess()) {
  138. throw new RemoteOperationFailedException("Metadata not uploaded!");
  139. }
  140. // return success
  141. return result;
  142. } catch (NoSuchAlgorithmException | IOException | InvalidKeyException | InvalidAlgorithmParameterException |
  143. NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException | InvalidKeySpecException |
  144. CertificateException e) {
  145. result = new RemoteOperationResult(e);
  146. Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e);
  147. } finally {
  148. if (delete != null) {
  149. delete.releaseConnection();
  150. }
  151. // unlock file
  152. if (token != null) {
  153. RemoteOperationResult unlockFileOperationResult = new UnlockFileRemoteOperation(parentId, token)
  154. .execute(client);
  155. if (!unlockFileOperationResult.isSuccess()) {
  156. Log_OC.e(TAG, "Failed to unlock " + parentId);
  157. }
  158. }
  159. }
  160. return result;
  161. }
  162. }