2016-12-16 16:12:26 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
2016-11-15 19:00:52 +00:00
|
|
|
|
2018-02-16 12:29:38 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2016-11-28 14:11:44 +00:00
|
|
|
import android.content.Intent;
|
2016-11-15 19:00:52 +00:00
|
|
|
import android.os.Bundle;
|
2017-03-29 16:30:28 +00:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2018-02-17 10:18:23 +00:00
|
|
|
import android.support.v7.widget.SwitchCompat;
|
2016-11-15 19:00:52 +00:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import java.security.cert.X509Certificate;
|
2016-11-28 14:11:44 +00:00
|
|
|
import java.util.Arrays;
|
2016-11-15 19:00:52 +00:00
|
|
|
|
2016-12-16 16:12:26 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.R;
|
2016-11-15 19:00:52 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
|
2016-11-18 20:49:52 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
|
2016-11-15 19:00:52 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.utils.CryptoHelper;
|
2016-11-28 14:11:44 +00:00
|
|
|
import eu.siacs.conversations.utils.XmppUri;
|
2018-02-12 10:22:51 +00:00
|
|
|
import eu.siacs.conversations.utils.zxing.IntentIntegrator;
|
|
|
|
import eu.siacs.conversations.utils.zxing.IntentResult;
|
2016-11-15 19:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
public abstract class OmemoActivity extends XmppActivity {
|
|
|
|
|
|
|
|
private Account mSelectedAccount;
|
|
|
|
private String mSelectedFingerprint;
|
|
|
|
|
2016-11-28 14:11:44 +00:00
|
|
|
protected XmppUri mPendingFingerprintVerificationUri = null;
|
|
|
|
|
2016-11-15 19:00:52 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
super.onCreateContextMenu(menu,v,menuInfo);
|
|
|
|
Object account = v.getTag(R.id.TAG_ACCOUNT);
|
|
|
|
Object fingerprint = v.getTag(R.id.TAG_FINGERPRINT);
|
2017-04-08 05:15:14 +00:00
|
|
|
Object fingerprintStatus = v.getTag(R.id.TAG_FINGERPRINT_STATUS);
|
2016-11-28 14:11:44 +00:00
|
|
|
if (account != null
|
|
|
|
&& fingerprint != null
|
|
|
|
&& account instanceof Account
|
|
|
|
&& fingerprintStatus != null
|
|
|
|
&& fingerprint instanceof String
|
|
|
|
&& fingerprintStatus instanceof FingerprintStatus) {
|
2016-11-15 19:00:52 +00:00
|
|
|
getMenuInflater().inflate(R.menu.omemo_key_context, menu);
|
2017-01-12 14:59:13 +00:00
|
|
|
MenuItem distrust = menu.findItem(R.id.distrust_key);
|
2016-11-28 14:11:44 +00:00
|
|
|
MenuItem verifyScan = menu.findItem(R.id.verify_scan);
|
|
|
|
if (this instanceof TrustKeysActivity) {
|
2017-01-12 14:59:13 +00:00
|
|
|
distrust.setVisible(false);
|
2016-11-28 14:11:44 +00:00
|
|
|
verifyScan.setVisible(false);
|
|
|
|
} else {
|
|
|
|
FingerprintStatus status = (FingerprintStatus) fingerprintStatus;
|
|
|
|
if (!status.isActive() || status.isVerified()) {
|
|
|
|
verifyScan.setVisible(false);
|
|
|
|
}
|
2017-05-06 16:38:23 +00:00
|
|
|
distrust.setVisible(status.isVerified() || (!status.isActive() && status.isTrusted()));
|
2016-11-28 14:11:44 +00:00
|
|
|
}
|
2016-11-15 19:00:52 +00:00
|
|
|
this.mSelectedAccount = (Account) account;
|
|
|
|
this.mSelectedFingerprint = (String) fingerprint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
2017-01-12 14:59:13 +00:00
|
|
|
case R.id.distrust_key:
|
2016-11-15 19:00:52 +00:00
|
|
|
showPurgeKeyDialog(mSelectedAccount,mSelectedFingerprint);
|
|
|
|
break;
|
|
|
|
case R.id.copy_omemo_key:
|
|
|
|
copyOmemoFingerprint(mSelectedFingerprint);
|
|
|
|
break;
|
2016-11-28 14:11:44 +00:00
|
|
|
case R.id.verify_scan:
|
|
|
|
new IntentIntegrator(this).initiateScan(Arrays.asList("AZTEC","QR_CODE"));
|
|
|
|
break;
|
2016-11-15 19:00:52 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-28 14:11:44 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
|
|
|
|
if (scanResult != null && scanResult.getFormatName() != null) {
|
|
|
|
String data = scanResult.getContents();
|
|
|
|
XmppUri uri = new XmppUri(data);
|
|
|
|
if (xmppConnectionServiceBound) {
|
|
|
|
processFingerprintVerification(uri);
|
|
|
|
} else {
|
|
|
|
this.mPendingFingerprintVerificationUri =uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void processFingerprintVerification(XmppUri uri);
|
|
|
|
|
2016-11-15 19:00:52 +00:00
|
|
|
protected void copyOmemoFingerprint(String fingerprint) {
|
|
|
|
if (copyTextToClipboard(CryptoHelper.prettifyFingerprint(fingerprint.substring(2)), R.string.omemo_fingerprint)) {
|
|
|
|
Toast.makeText(
|
|
|
|
this,
|
|
|
|
R.string.toast_message_omemo_fingerprint,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:49:52 +00:00
|
|
|
protected void addFingerprintRow(LinearLayout keys, final XmppAxolotlSession session, boolean highlight) {
|
|
|
|
final Account account = session.getAccount();
|
|
|
|
final String fingerprint = session.getFingerprint();
|
|
|
|
addFingerprintRowWithListeners(keys,
|
|
|
|
session.getAccount(),
|
2017-06-25 16:18:13 +00:00
|
|
|
fingerprint,
|
2016-11-18 20:49:52 +00:00
|
|
|
highlight,
|
|
|
|
session.getTrust(),
|
|
|
|
true,
|
|
|
|
true,
|
2018-02-17 10:18:23 +00:00
|
|
|
(buttonView, isChecked) -> account.getAxolotlService().setFingerprintTrust(fingerprint, FingerprintStatus.createActive(isChecked)));
|
2016-11-15 19:00:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:49:52 +00:00
|
|
|
protected void addFingerprintRowWithListeners(LinearLayout keys, final Account account,
|
2016-11-15 19:00:52 +00:00
|
|
|
final String fingerprint,
|
|
|
|
boolean highlight,
|
|
|
|
FingerprintStatus status,
|
|
|
|
boolean showTag,
|
|
|
|
boolean undecidedNeedEnablement,
|
|
|
|
CompoundButton.OnCheckedChangeListener
|
|
|
|
onCheckedChangeListener) {
|
|
|
|
View view = getLayoutInflater().inflate(R.layout.contact_key, keys, false);
|
2018-02-17 10:18:23 +00:00
|
|
|
TextView key = view.findViewById(R.id.key);
|
|
|
|
TextView keyType = view.findViewById(R.id.key_type);
|
2016-11-15 19:00:52 +00:00
|
|
|
if (Config.X509_VERIFICATION && status.getTrust() == FingerprintStatus.Trust.VERIFIED_X509) {
|
2018-02-17 10:18:23 +00:00
|
|
|
key.setOnClickListener(v -> showX509Certificate(account,fingerprint));
|
|
|
|
keyType.setOnClickListener(v -> showX509Certificate(account,fingerprint));
|
2016-11-15 19:00:52 +00:00
|
|
|
}
|
2018-02-17 10:18:23 +00:00
|
|
|
SwitchCompat trustToggle = view.findViewById(R.id.tgl_trust);
|
|
|
|
ImageView verifiedFingerprintSymbol = view.findViewById(R.id.verified_fingerprint);
|
2016-11-15 19:00:52 +00:00
|
|
|
trustToggle.setVisibility(View.VISIBLE);
|
|
|
|
registerForContextMenu(view);
|
|
|
|
view.setTag(R.id.TAG_ACCOUNT,account);
|
|
|
|
view.setTag(R.id.TAG_FINGERPRINT,fingerprint);
|
2016-11-28 14:11:44 +00:00
|
|
|
view.setTag(R.id.TAG_FINGERPRINT_STATUS,status);
|
2016-11-15 19:00:52 +00:00
|
|
|
boolean x509 = Config.X509_VERIFICATION && status.getTrust() == FingerprintStatus.Trust.VERIFIED_X509;
|
|
|
|
final View.OnClickListener toast;
|
2018-02-17 10:18:23 +00:00
|
|
|
trustToggle.setChecked(status.isTrusted());
|
2016-11-15 19:00:52 +00:00
|
|
|
|
|
|
|
if (status.isActive()){
|
|
|
|
key.setTextColor(getPrimaryTextColor());
|
|
|
|
keyType.setTextColor(getSecondaryTextColor());
|
|
|
|
if (status.isVerified()) {
|
|
|
|
verifiedFingerprintSymbol.setVisibility(View.VISIBLE);
|
2016-11-16 08:39:44 +00:00
|
|
|
verifiedFingerprintSymbol.setAlpha(1.0f);
|
2016-11-15 19:00:52 +00:00
|
|
|
trustToggle.setVisibility(View.GONE);
|
2018-02-17 10:18:23 +00:00
|
|
|
verifiedFingerprintSymbol.setOnClickListener(v -> replaceToast(getString(R.string.this_device_has_been_verified), false));
|
2016-11-15 19:00:52 +00:00
|
|
|
toast = null;
|
|
|
|
} else {
|
|
|
|
verifiedFingerprintSymbol.setVisibility(View.GONE);
|
|
|
|
trustToggle.setVisibility(View.VISIBLE);
|
|
|
|
trustToggle.setOnCheckedChangeListener(onCheckedChangeListener);
|
|
|
|
if (status.getTrust() == FingerprintStatus.Trust.UNDECIDED && undecidedNeedEnablement) {
|
2018-02-17 10:18:23 +00:00
|
|
|
trustToggle.setOnClickListener(v -> {
|
|
|
|
account.getAxolotlService().setFingerprintTrust(fingerprint,FingerprintStatus.createActive(false));
|
|
|
|
v.setEnabled(true);
|
|
|
|
v.setOnClickListener(null);
|
2016-11-15 19:00:52 +00:00
|
|
|
});
|
|
|
|
trustToggle.setEnabled(false);
|
|
|
|
} else {
|
|
|
|
trustToggle.setOnClickListener(null);
|
|
|
|
trustToggle.setEnabled(true);
|
|
|
|
}
|
2018-02-17 10:18:23 +00:00
|
|
|
toast = v -> hideToast();
|
2016-11-15 19:00:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key.setTextColor(getTertiaryTextColor());
|
|
|
|
keyType.setTextColor(getTertiaryTextColor());
|
2018-02-17 10:18:23 +00:00
|
|
|
toast = v -> replaceToast(getString(R.string.this_device_is_no_longer_in_use), false);
|
2016-11-15 19:00:52 +00:00
|
|
|
if (status.isVerified()) {
|
|
|
|
trustToggle.setVisibility(View.GONE);
|
|
|
|
verifiedFingerprintSymbol.setVisibility(View.VISIBLE);
|
2016-11-16 08:39:44 +00:00
|
|
|
verifiedFingerprintSymbol.setAlpha(0.4368f);
|
2016-11-15 19:00:52 +00:00
|
|
|
verifiedFingerprintSymbol.setOnClickListener(toast);
|
|
|
|
} else {
|
|
|
|
trustToggle.setVisibility(View.VISIBLE);
|
|
|
|
verifiedFingerprintSymbol.setVisibility(View.GONE);
|
|
|
|
trustToggle.setOnClickListener(null);
|
|
|
|
trustToggle.setEnabled(false);
|
|
|
|
trustToggle.setOnClickListener(toast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
view.setOnClickListener(toast);
|
|
|
|
key.setOnClickListener(toast);
|
|
|
|
keyType.setOnClickListener(toast);
|
|
|
|
if (showTag) {
|
|
|
|
keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509 : R.string.omemo_fingerprint));
|
|
|
|
} else {
|
|
|
|
keyType.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
if (highlight) {
|
2017-03-29 16:30:28 +00:00
|
|
|
keyType.setTextColor(ContextCompat.getColor(this, R.color.accent));
|
2016-11-15 19:00:52 +00:00
|
|
|
keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509_selected_message : R.string.omemo_fingerprint_selected_message));
|
|
|
|
} else {
|
|
|
|
keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509 : R.string.omemo_fingerprint));
|
|
|
|
}
|
|
|
|
|
|
|
|
key.setText(CryptoHelper.prettifyFingerprint(fingerprint.substring(2)));
|
|
|
|
|
|
|
|
keys.addView(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showPurgeKeyDialog(final Account account, final String fingerprint) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
2017-01-12 14:59:13 +00:00
|
|
|
builder.setTitle(R.string.distrust_omemo_key);
|
|
|
|
builder.setMessage(R.string.distrust_omemo_key_text);
|
2016-11-15 19:00:52 +00:00
|
|
|
builder.setNegativeButton(getString(R.string.cancel), null);
|
2017-01-12 14:59:13 +00:00
|
|
|
builder.setPositiveButton(R.string.confirm,
|
2018-02-17 10:18:23 +00:00
|
|
|
(dialog, which) -> {
|
|
|
|
account.getAxolotlService().distrustFingerprint(fingerprint);
|
|
|
|
refreshUi();
|
2016-11-15 19:00:52 +00:00
|
|
|
});
|
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showX509Certificate(Account account, String fingerprint) {
|
|
|
|
X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
|
|
|
|
if (x509Certificate != null) {
|
|
|
|
showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this,R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showCertificateInformationDialog(Bundle bundle) {
|
|
|
|
View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
|
|
|
|
final String not_available = getString(R.string.certicate_info_not_available);
|
|
|
|
TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
|
|
|
|
TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
|
|
|
|
TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
|
|
|
|
TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
|
|
|
|
TextView sha1 = (TextView) view.findViewById(R.id.sha1);
|
|
|
|
|
|
|
|
subject_cn.setText(bundle.getString("subject_cn", not_available));
|
|
|
|
subject_o.setText(bundle.getString("subject_o", not_available));
|
|
|
|
issuer_cn.setText(bundle.getString("issuer_cn", not_available));
|
|
|
|
issuer_o.setText(bundle.getString("issuer_o", not_available));
|
|
|
|
sha1.setText(bundle.getString("sha1", not_available));
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setTitle(R.string.certificate_information);
|
|
|
|
builder.setView(view);
|
|
|
|
builder.setPositiveButton(R.string.ok, null);
|
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
}
|