GenerateNewKeysAsyncTask.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.ui.asynctasks;
  22. import android.accounts.Account;
  23. import android.content.res.Resources;
  24. import android.os.AsyncTask;
  25. import com.owncloud.android.lib.common.OwnCloudClient;
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  27. import com.owncloud.android.lib.common.utils.Log_OC;
  28. import com.owncloud.android.lib.resources.users.SendCSROperation;
  29. import com.owncloud.android.lib.resources.users.StorePrivateKeyOperation;
  30. import com.owncloud.android.utils.CsrHelper;
  31. import com.owncloud.android.utils.EncryptionUtils;
  32. import java.security.KeyPair;
  33. import java.security.PrivateKey;
  34. import java.util.ArrayList;
  35. /**
  36. * Async task for generating new e2e keys
  37. */
  38. public class GenerateNewKeysAsyncTask extends AsyncTask<Void, Void, String> {
  39. private static final String TAG = GenerateNewKeysAsyncTask.class.getSimpleName();
  40. private ArrayList<String> keyWords;
  41. private OwnCloudClient client;
  42. private Account account;
  43. private Resources resources;
  44. private String packageName;
  45. public GenerateNewKeysAsyncTask(OwnCloudClient client, Account account, Resources resources, String packageName) {
  46. this.client = client;
  47. this.account = account;
  48. this.resources = resources;
  49. this.packageName = packageName;
  50. }
  51. @Override
  52. protected void onPreExecute() {
  53. super.onPreExecute();
  54. // textView.setText(R.string.end_to_end_encryption_generating_keys);
  55. }
  56. @Override
  57. protected String doInBackground(Void... voids) {
  58. // - create CSR, push to server, store returned public key in database
  59. // - encrypt private key, push key to server, store unencrypted private key in database
  60. try {
  61. String publicKey;
  62. // Create public/private key pair
  63. KeyPair keyPair = EncryptionUtils.generateKeyPair();
  64. PrivateKey privateKey = keyPair.getPrivate();
  65. // create CSR
  66. String urlEncoded = CsrHelper.generateCsrPemEncodedString(keyPair, account.name);
  67. SendCSROperation operation = new SendCSROperation(urlEncoded);
  68. RemoteOperationResult result = operation.execute(client);
  69. if (result.isSuccess()) {
  70. Log_OC.d(TAG, "public key success");
  71. publicKey = (String) result.getData().get(0);
  72. } else {
  73. // keyResult = KEY_FAILED;
  74. return "";
  75. }
  76. keyWords = EncryptionUtils.getRandomWords(12, null);
  77. StringBuilder stringBuilder = new StringBuilder();
  78. for (String string : keyWords) {
  79. stringBuilder.append(string);
  80. }
  81. String keyPhrase = stringBuilder.toString();
  82. String privateKeyString = EncryptionUtils.encodeBytesToBase64String(privateKey.getEncoded());
  83. String privatePemKeyString = EncryptionUtils.privateKeyToPEM(privateKey);
  84. String encryptedPrivateKey = EncryptionUtils.encryptPrivateKey(privatePemKeyString, keyPhrase);
  85. // upload encryptedPrivateKey
  86. StorePrivateKeyOperation storePrivateKeyOperation = new StorePrivateKeyOperation(encryptedPrivateKey);
  87. RemoteOperationResult storePrivateKeyResult = storePrivateKeyOperation.execute(client);
  88. if (storePrivateKeyResult.isSuccess()) {
  89. Log_OC.d(TAG, "private key success");
  90. // arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PRIVATE_KEY,
  91. // privateKeyString);
  92. // arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PUBLIC_KEY, publicKey);
  93. // keyResult = KEY_CREATED;
  94. return (String) storePrivateKeyResult.getData().get(0);
  95. }
  96. } catch (Exception e) {
  97. Log_OC.e(TAG, e.getMessage());
  98. e.printStackTrace();
  99. }
  100. // keyResult = KEY_FAILED;
  101. return "";
  102. }
  103. @Override
  104. protected void onPostExecute(String s) {
  105. super.onPostExecute(s);
  106. // if (s.isEmpty()) {
  107. // keyResult = KEY_FAILED;
  108. //
  109. // getDialog().setTitle(R.string.common_error);
  110. // textView.setText(R.string.end_to_end_encryption_unsuccessful);
  111. // positiveButton.setText(R.string.end_to_end_encryption_dialog_close);
  112. // positiveButton.setVisibility(View.VISIBLE);
  113. // } else {
  114. // getDialog().setTitle(R.string.end_to_end_encryption_passphrase_title);
  115. //
  116. // textView.setText(R.string.end_to_end_encryption_keywords_description);
  117. //
  118. // StringBuilder stringBuilder = new StringBuilder();
  119. //
  120. // for (String string: keyWords) {
  121. // stringBuilder.append(string).append(" ");
  122. // }
  123. // String keys = stringBuilder.toString();
  124. //
  125. // passphraseTextView.setText(keys);
  126. //
  127. // passphraseTextView.setVisibility(View.VISIBLE);
  128. // positiveButton.setText(R.string.end_to_end_encryption_confirm_button);
  129. // positiveButton.setVisibility(View.VISIBLE);
  130. // }
  131. }
  132. }