|
@@ -1,160 +0,0 @@
|
|
|
-
|
|
|
- * Nextcloud Android client application
|
|
|
- *
|
|
|
- * @author Tobias Kaminsky
|
|
|
- * Copyright (C) 2017 Tobias Kaminsky
|
|
|
- * Copyright (C) 2017 Nextcloud GmbH.
|
|
|
- *
|
|
|
- * This program is free software: you can redistribute it and/or modify
|
|
|
- * it under the terms of the GNU Affero General Public License as published by
|
|
|
- * the Free Software Foundation, either version 3 of the License, or
|
|
|
- * at your option) any later version.
|
|
|
- *
|
|
|
- * This program is distributed in the hope that it will be useful,
|
|
|
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
- * GNU Affero General Public License for more details.
|
|
|
- *
|
|
|
- * You should have received a copy of the GNU Affero General Public License
|
|
|
- * along with this program. If not, see <http:
|
|
|
- */
|
|
|
-
|
|
|
-package com.owncloud.android.ui.asynctasks;
|
|
|
-
|
|
|
-import android.accounts.Account;
|
|
|
-import android.content.res.Resources;
|
|
|
-import android.os.AsyncTask;
|
|
|
-
|
|
|
-import com.owncloud.android.lib.common.OwnCloudClient;
|
|
|
-import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
|
|
-import com.owncloud.android.lib.common.utils.Log_OC;
|
|
|
-import com.owncloud.android.lib.resources.users.SendCSROperation;
|
|
|
-import com.owncloud.android.lib.resources.users.StorePrivateKeyOperation;
|
|
|
-import com.owncloud.android.utils.CsrHelper;
|
|
|
-import com.owncloud.android.utils.EncryptionUtils;
|
|
|
-
|
|
|
-import java.security.KeyPair;
|
|
|
-import java.security.PrivateKey;
|
|
|
-import java.util.ArrayList;
|
|
|
-
|
|
|
-
|
|
|
- * Async task for generating new e2e keys
|
|
|
- */
|
|
|
-
|
|
|
-public class GenerateNewKeysAsyncTask extends AsyncTask<Void, Void, String> {
|
|
|
- private static final String TAG = GenerateNewKeysAsyncTask.class.getSimpleName();
|
|
|
- private ArrayList<String> keyWords;
|
|
|
-
|
|
|
- private OwnCloudClient client;
|
|
|
- private Account account;
|
|
|
- private Resources resources;
|
|
|
- private String packageName;
|
|
|
-
|
|
|
- public GenerateNewKeysAsyncTask(OwnCloudClient client, Account account, Resources resources, String packageName) {
|
|
|
- this.client = client;
|
|
|
- this.account = account;
|
|
|
- this.resources = resources;
|
|
|
- this.packageName = packageName;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void onPreExecute() {
|
|
|
- super.onPreExecute();
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected String doInBackground(Void... voids) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- try {
|
|
|
- String publicKey;
|
|
|
-
|
|
|
-
|
|
|
- KeyPair keyPair = EncryptionUtils.generateKeyPair();
|
|
|
- PrivateKey privateKey = keyPair.getPrivate();
|
|
|
-
|
|
|
-
|
|
|
- String urlEncoded = CsrHelper.generateCsrPemEncodedString(keyPair, account.name);
|
|
|
-
|
|
|
- SendCSROperation operation = new SendCSROperation(urlEncoded);
|
|
|
- RemoteOperationResult result = operation.execute(client);
|
|
|
-
|
|
|
- if (result.isSuccess()) {
|
|
|
- Log_OC.d(TAG, "public key success");
|
|
|
-
|
|
|
- publicKey = (String) result.getData().get(0);
|
|
|
- } else {
|
|
|
-
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
- keyWords = EncryptionUtils.getRandomWords(12, null);
|
|
|
-
|
|
|
- StringBuilder stringBuilder = new StringBuilder();
|
|
|
- for (String string : keyWords) {
|
|
|
- stringBuilder.append(string);
|
|
|
- }
|
|
|
- String keyPhrase = stringBuilder.toString();
|
|
|
-
|
|
|
- String privateKeyString = EncryptionUtils.encodeBytesToBase64String(privateKey.getEncoded());
|
|
|
- String privatePemKeyString = EncryptionUtils.privateKeyToPEM(privateKey);
|
|
|
- String encryptedPrivateKey = EncryptionUtils.encryptPrivateKey(privatePemKeyString, keyPhrase);
|
|
|
-
|
|
|
-
|
|
|
- StorePrivateKeyOperation storePrivateKeyOperation = new StorePrivateKeyOperation(encryptedPrivateKey);
|
|
|
- RemoteOperationResult storePrivateKeyResult = storePrivateKeyOperation.execute(client);
|
|
|
-
|
|
|
- if (storePrivateKeyResult.isSuccess()) {
|
|
|
- Log_OC.d(TAG, "private key success");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- return (String) storePrivateKeyResult.getData().get(0);
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- Log_OC.e(TAG, e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void onPostExecute(String s) {
|
|
|
- super.onPostExecute(s);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-}
|