Skip to content

Commit

Permalink
Fixed e2ee dialog crash due to null context.
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder-tsys committed Jun 20, 2023
1 parent e70e262 commit 385320d
Showing 1 changed file with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import android.accounts.AccountManager;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
Expand Down Expand Up @@ -50,6 +51,7 @@
import com.owncloud.android.utils.theme.ViewThemeUtils;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.util.ArrayList;
Expand Down Expand Up @@ -129,7 +131,7 @@ public void onStart() {
viewThemeUtils.platform.colorTextButtons(positiveButton, neutralButton);
}

task = new DownloadKeysAsyncTask();
task = new DownloadKeysAsyncTask(requireContext());
task.execute();
}

Expand Down Expand Up @@ -239,7 +241,7 @@ private Dialog createDialog(View v) {
neutralButton.setVisibility(View.GONE);
getDialog().setTitle(R.string.end_to_end_encryption_storing_keys);

GenerateNewKeysAsyncTask newKeysTask = new GenerateNewKeysAsyncTask();
GenerateNewKeysAsyncTask newKeysTask = new GenerateNewKeysAsyncTask(requireContext());
newKeysTask.execute();
break;

Expand Down Expand Up @@ -293,6 +295,12 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
}

public class DownloadKeysAsyncTask extends AsyncTask<Void, Void, String> {
private final WeakReference<Context> mWeakContext;

public DownloadKeysAsyncTask(Context context) {
mWeakContext = new WeakReference<>(context);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
Expand All @@ -303,47 +311,58 @@ protected void onPreExecute() {

@Override
protected String doInBackground(Void... voids) {
Context context = mWeakContext.get();
// fetch private/public key
// if available
// - store public key
// - decrypt private key, store unencrypted private key in database

GetPublicKeyOperation publicKeyOperation = new GetPublicKeyOperation();
RemoteOperationResult<String> publicKeyResult = publicKeyOperation.execute(user, getContext());

if (publicKeyResult.isSuccess()) {
Log_OC.d(TAG, "public key successful downloaded for " + user.getAccountName());

String publicKeyFromServer = publicKeyResult.getResultData();
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(),
EncryptionUtils.PUBLIC_KEY,
publicKeyFromServer);
} else {
return null;
}
if (user != null) {
RemoteOperationResult<String> publicKeyResult = publicKeyOperation.execute(user, context);

if (publicKeyResult.isSuccess()) {
Log_OC.d(TAG, "public key successful downloaded for " + user.getAccountName());

String publicKeyFromServer = publicKeyResult.getResultData();
if (arbitraryDataProvider != null) {
arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(),
EncryptionUtils.PUBLIC_KEY,
publicKeyFromServer);
} else {
return null;
}
} else {
return null;
}

RemoteOperationResult<com.owncloud.android.lib.ocs.responses.PrivateKey> privateKeyResult =
new GetPrivateKeyOperation().execute(user, getContext());
RemoteOperationResult<com.owncloud.android.lib.ocs.responses.PrivateKey> privateKeyResult =
new GetPrivateKeyOperation().execute(user, context);

if (privateKeyResult.isSuccess()) {
Log_OC.d(TAG, "private key successful downloaded for " + user.getAccountName());
if (privateKeyResult.isSuccess()) {
Log_OC.d(TAG, "private key successful downloaded for " + user.getAccountName());

keyResult = KEY_EXISTING_USED;
return privateKeyResult.getResultData().getKey();
} else {
return null;
keyResult = KEY_EXISTING_USED;
return privateKeyResult.getResultData().getKey();
}
}
return null;
}

@Override
protected void onPostExecute(String privateKey) {
super.onPostExecute(privateKey);
Context context = mWeakContext.get();
if (context == null) {
Log_OC.e(TAG, "Context lost after fetching private keys.");
return;
}

if (privateKey == null) {
// first show info
try {
if (keyWords == null || keyWords.isEmpty()) {
keyWords = EncryptionUtils.getRandomWords(12, requireContext());
keyWords = EncryptionUtils.getRandomWords(12, context);
}
showMnemonicInfo();
} catch (IOException e) {
Expand All @@ -360,6 +379,13 @@ protected void onPostExecute(String privateKey) {
}

public class GenerateNewKeysAsyncTask extends AsyncTask<Void, Void, String> {

private final WeakReference<Context> mWeakContext;

public GenerateNewKeysAsyncTask(Context context) {
mWeakContext = new WeakReference<>(context);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
Expand All @@ -373,18 +399,20 @@ protected String doInBackground(Void... voids) {
// - encrypt private key, push key to server, store unencrypted private key in database

try {
Context context = mWeakContext.get();

String publicKeyString;

// Create public/private key pair
KeyPair keyPair = EncryptionUtils.generateKeyPair();

// create CSR
AccountManager accountManager = AccountManager.get(getContext());
AccountManager accountManager = AccountManager.get(context);
String userId = accountManager.getUserData(user.toPlatformAccount(), AccountUtils.Constants.KEY_USER_ID);
String urlEncoded = CsrHelper.generateCsrPemEncodedString(keyPair, userId);

SendCSROperation operation = new SendCSROperation(urlEncoded);
RemoteOperationResult result = operation.execute(user, getContext());
RemoteOperationResult result = operation.execute(user, context);

if (result.isSuccess()) {
publicKeyString = (String) result.getData().get(0);
Expand All @@ -407,7 +435,7 @@ protected String doInBackground(Void... voids) {

// upload encryptedPrivateKey
StorePrivateKeyOperation storePrivateKeyOperation = new StorePrivateKeyOperation(encryptedPrivateKey);
RemoteOperationResult storePrivateKeyResult = storePrivateKeyOperation.execute(user, getContext());
RemoteOperationResult storePrivateKeyResult = storePrivateKeyOperation.execute(user, context);

if (storePrivateKeyResult.isSuccess()) {
Log_OC.d(TAG, "private key success");
Expand All @@ -426,7 +454,7 @@ protected String doInBackground(Void... voids) {
return (String) storePrivateKeyResult.getData().get(0);
} else {
DeletePublicKeyOperation deletePublicKeyOperation = new DeletePublicKeyOperation();
deletePublicKeyOperation.execute(user, getContext());
deletePublicKeyOperation.execute(user, context);
}
} catch (Exception e) {
Log_OC.e(TAG, e.getMessage());
Expand All @@ -440,9 +468,20 @@ protected String doInBackground(Void... voids) {
protected void onPostExecute(String s) {
super.onPostExecute(s);

Context context = mWeakContext.get();
if (context == null) {
Log_OC.e(TAG, "Context lost after generating new private keys.");
return;
}

if (s.isEmpty()) {
errorSavingKeys();
} else {
if (getDialog() == null) {
Log_OC.e(TAG, "Dialog is null cannot proceed further.");
return;
}

requireDialog().dismiss();
notifyResult();
}
Expand All @@ -464,6 +503,10 @@ private String generateMnemonicString(boolean withWhitespace) {

@VisibleForTesting
public void showMnemonicInfo() {
if (getDialog() == null) {
Log_OC.e(TAG, "Dialog is null cannot proceed further.");
return;
}
requireDialog().setTitle(R.string.end_to_end_encryption_passphrase_title);

binding.encryptionStatus.setText(R.string.end_to_end_encryption_keywords_description);
Expand All @@ -483,6 +526,11 @@ public void showMnemonicInfo() {

@VisibleForTesting
public void errorSavingKeys() {
if (getDialog() == null) {
Log_OC.e(TAG, "Dialog is null cannot proceed further.");
return;
}

keyResult = KEY_FAILED;

requireDialog().setTitle(R.string.common_error);
Expand Down

0 comments on commit 385320d

Please sign in to comment.