mirror of https://github.com/M66B/FairEmail.git
Auto select S/MIME encryption keys
This commit is contained in:
parent
13ece09df0
commit
7d01466c6d
|
@ -42,7 +42,6 @@ import java.util.List;
|
|||
import java.util.Objects;
|
||||
|
||||
public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.ViewHolder> {
|
||||
private ICertificate intf;
|
||||
private Context context;
|
||||
private LifecycleOwner owner;
|
||||
private LayoutInflater inflater;
|
||||
|
@ -50,7 +49,7 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
|
|||
private String email;
|
||||
private List<EntityCertificate> items = new ArrayList<>();
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
|
||||
private View view;
|
||||
private TextView tvEmail;
|
||||
private TextView tvSubject;
|
||||
|
@ -65,16 +64,6 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
|
|||
tvSubject = itemView.findViewById(R.id.tvSubject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int pos = getAdapterPosition();
|
||||
if (pos == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
EntityCertificate certificate = items.get(pos);
|
||||
intf.onSelected(certificate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
int pos = getAdapterPosition();
|
||||
|
@ -108,7 +97,7 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
|
|||
|
||||
new SimpleTask<Void>() {
|
||||
@Override
|
||||
protected Void onExecute(Context context, Bundle args) throws Throwable {
|
||||
protected Void onExecute(Context context, Bundle args) {
|
||||
long id = args.getLong("id");
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
|
@ -131,14 +120,10 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
|
|||
}
|
||||
|
||||
private void wire() {
|
||||
if (intf != null)
|
||||
view.setOnClickListener(this);
|
||||
view.setOnLongClickListener(this);
|
||||
}
|
||||
|
||||
private void unwire() {
|
||||
if (intf != null)
|
||||
view.setOnClickListener(null);
|
||||
view.setOnLongClickListener(null);
|
||||
}
|
||||
|
||||
|
@ -152,8 +137,7 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
|
|||
}
|
||||
}
|
||||
|
||||
AdapterCertificate(Fragment parentFragment, ICertificate intf) {
|
||||
this.intf = intf;
|
||||
AdapterCertificate(Fragment parentFragment) {
|
||||
this.context = parentFragment.getContext();
|
||||
this.owner = parentFragment.getViewLifecycleOwner();
|
||||
this.inflater = LayoutInflater.from(parentFragment.getContext());
|
||||
|
@ -257,8 +241,4 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
|
|||
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
|
||||
holder.powner.recreate();
|
||||
}
|
||||
|
||||
interface ICertificate {
|
||||
void onSelected(EntityCertificate certificate);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2700,12 +2700,17 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
|
||||
private void onActionDecrypt(TupleMessageEx message, boolean auto) {
|
||||
int encrypt = (message.encrypt == null ? EntityMessage.PGP_SIGNENCRYPT /* Inline */ : message.encrypt);
|
||||
String recipient = null;
|
||||
if (message.to != null && message.to.length == 1)
|
||||
recipient = ((InternetAddress) message.to[0]).getAddress();
|
||||
|
||||
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
||||
lbm.sendBroadcast(
|
||||
new Intent(FragmentMessages.ACTION_DECRYPT)
|
||||
.putExtra("id", message.id)
|
||||
.putExtra("auto", auto)
|
||||
.putExtra("type", encrypt));
|
||||
.putExtra("type", encrypt)
|
||||
.putExtra("recipient", recipient));
|
||||
}
|
||||
|
||||
private void onActionReplyMenu(TupleMessageEx message) {
|
||||
|
|
|
@ -71,7 +71,7 @@ public class FragmentCertificates extends FragmentBase {
|
|||
itemDecorator.setDrawable(getContext().getDrawable(R.drawable.divider));
|
||||
rvCertificate.addItemDecoration(itemDecorator);
|
||||
|
||||
adapter = new AdapterCertificate(this, null);
|
||||
adapter = new AdapterCertificate(this);
|
||||
rvCertificate.setAdapter(adapter);
|
||||
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
|
|
|
@ -53,7 +53,6 @@ import android.os.OperationCanceledException;
|
|||
import android.provider.ContactsContract;
|
||||
import android.provider.MediaStore;
|
||||
import android.security.KeyChain;
|
||||
import android.security.KeyChainAliasCallback;
|
||||
import android.text.Html;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
|
@ -77,7 +76,6 @@ import android.widget.EditText;
|
|||
import android.widget.FilterQueryProvider;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.MultiAutoCompleteTextView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
@ -93,7 +91,6 @@ import androidx.documentfile.provider.DocumentFile;
|
|||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
|
@ -1252,42 +1249,38 @@ public class FragmentCompose extends FragmentBase {
|
|||
private void onEncrypt(final EntityMessage draft) {
|
||||
if (EntityMessage.SMIME_SIGNONLY.equals(draft.encrypt) ||
|
||||
EntityMessage.SMIME_SIGNENCRYPT.equals(draft.encrypt)) {
|
||||
Handler handler = new Handler();
|
||||
KeyChain.choosePrivateKeyAlias(getActivity(), new KeyChainAliasCallback() {
|
||||
@Override
|
||||
public void alias(@Nullable String alias) {
|
||||
Log.i("Selected key alias=" + alias);
|
||||
if (alias != null) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String email = null;
|
||||
if (draft.to != null && draft.to.length == 1)
|
||||
email = ((InternetAddress) draft.to[0]).getAddress();
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", draft.id);
|
||||
args.putInt("type", draft.encrypt);
|
||||
args.putString("email", email);
|
||||
args.putString("alias", alias);
|
||||
String sender = null;
|
||||
if (draft.from != null && draft.from.length > 0)
|
||||
sender = ((InternetAddress) draft.from[0]).getAddress();
|
||||
Log.i("Alias sender=" + sender);
|
||||
|
||||
if (EntityMessage.SMIME_SIGNENCRYPT.equals(draft.encrypt)) {
|
||||
FragmentDialogCertificate fragment = new FragmentDialogCertificate();
|
||||
fragment.setArguments(args);
|
||||
fragment.setTargetFragment(FragmentCompose.this, REQUEST_CERTIFICATE);
|
||||
fragment.show(getParentFragmentManager(), "compose:certificate");
|
||||
} else
|
||||
onSmime(args);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", draft.id);
|
||||
args.putInt("type", draft.encrypt);
|
||||
args.putString("sender", sender);
|
||||
|
||||
Helper.selectKeyAlias(getActivity(), sender, new Helper.IKeyAlias() {
|
||||
@Override
|
||||
public void onSelected(String alias) {
|
||||
args.putString("alias", alias);
|
||||
onSmime(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected() {
|
||||
Snackbar snackbar = Snackbar.make(view, R.string.title_invalid_key, Snackbar.LENGTH_LONG);
|
||||
final Intent intent = KeyChain.createInstallIntent();
|
||||
if (intent.resolveActivity(getContext().getPackageManager()) != null)
|
||||
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
},
|
||||
null, null, null, -1, null);
|
||||
});
|
||||
snackbar.show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (pgpService.isBound())
|
||||
try {
|
||||
|
@ -1886,7 +1879,6 @@ public class FragmentCompose extends FragmentBase {
|
|||
long id = args.getLong("id");
|
||||
int type = args.getInt("type");
|
||||
String alias = args.getString("alias");
|
||||
long cid = args.getLong("certificate", -1);
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
|
||||
|
@ -1897,9 +1889,6 @@ public class FragmentCompose extends FragmentBase {
|
|||
EntityIdentity identity = db.identity().getIdentity(draft.identity);
|
||||
if (identity == null)
|
||||
throw new IllegalArgumentException(getString(R.string.title_from_missing));
|
||||
EntityCertificate certificate = db.certificate().getCertificate(cid);
|
||||
if (certificate == null && EntityMessage.SMIME_SIGNENCRYPT.equals(type))
|
||||
throw new IllegalArgumentException("Certificate missing");
|
||||
|
||||
// Get/clean attachments
|
||||
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
|
||||
|
@ -2008,8 +1997,24 @@ public class FragmentCompose extends FragmentBase {
|
|||
return null;
|
||||
}
|
||||
|
||||
X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509")
|
||||
.generateCertificate(new ByteArrayInputStream(certificate.getEncoded()));
|
||||
List<Address> addresses = new ArrayList<>();
|
||||
if (draft.to != null)
|
||||
addresses.addAll(Arrays.asList(draft.to));
|
||||
if (draft.cc != null)
|
||||
addresses.addAll(Arrays.asList(draft.cc));
|
||||
if (draft.bcc != null)
|
||||
addresses.addAll(Arrays.asList(draft.bcc));
|
||||
|
||||
List<X509Certificate> certs = new ArrayList<>();
|
||||
for (Address address : addresses) {
|
||||
String email = ((InternetAddress) address).getAddress();
|
||||
List<EntityCertificate> e = db.certificate().getCertificateByEmail(email);
|
||||
if (e == null || e.size() < 1)
|
||||
throw new IllegalArgumentException(context.getString(R.string.title_certificate_missing, email));
|
||||
X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509")
|
||||
.generateCertificate(new ByteArrayInputStream(e.get(0).getEncoded()));
|
||||
certs.add(cert);
|
||||
}
|
||||
|
||||
// Build signature
|
||||
BodyPart bpSignature = new MimeBodyPart();
|
||||
|
@ -2032,8 +2037,10 @@ public class FragmentCompose extends FragmentBase {
|
|||
|
||||
// Encrypt
|
||||
CMSEnvelopedDataGenerator cmsEnvelopedDataGenerator = new CMSEnvelopedDataGenerator();
|
||||
RecipientInfoGenerator gen = new JceKeyTransRecipientInfoGenerator(cert);
|
||||
cmsEnvelopedDataGenerator.addRecipientInfoGenerator(gen);
|
||||
for (X509Certificate cert : certs) {
|
||||
RecipientInfoGenerator gen = new JceKeyTransRecipientInfoGenerator(cert);
|
||||
cmsEnvelopedDataGenerator.addRecipientInfoGenerator(gen);
|
||||
}
|
||||
|
||||
ByteArrayOutputStream osMessage = new ByteArrayOutputStream();
|
||||
imessage.writeTo(osMessage);
|
||||
|
@ -3963,60 +3970,6 @@ public class FragmentCompose extends FragmentBase {
|
|||
}
|
||||
}
|
||||
|
||||
public static class FragmentDialogCertificate extends FragmentDialogBase {
|
||||
private String email;
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
email = getArguments().getString("email");
|
||||
|
||||
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_select_certificate, null);
|
||||
final RecyclerView rvCertificate = dview.findViewById(R.id.rvCertificate);
|
||||
final ProgressBar pbWait = dview.findViewById(R.id.pbWait);
|
||||
|
||||
final Dialog dialog = new AlertDialog.Builder(getContext())
|
||||
.setTitle(R.string.title_select_certificate)
|
||||
.setView(dview)
|
||||
.setNegativeButton(android.R.string.cancel, null).create();
|
||||
|
||||
rvCertificate.setHasFixedSize(false);
|
||||
LinearLayoutManager llm = new LinearLayoutManager(getContext());
|
||||
rvCertificate.setLayoutManager(llm);
|
||||
|
||||
DividerItemDecoration itemDecorator = new DividerItemDecoration(getContext(), llm.getOrientation());
|
||||
Drawable divider = getContext().getDrawable(R.drawable.divider);
|
||||
divider.mutate().setTint(getContext().getResources().getColor(R.color.lightColorSeparator));
|
||||
itemDecorator.setDrawable(divider);
|
||||
rvCertificate.addItemDecoration(itemDecorator);
|
||||
|
||||
final AdapterCertificate adapter = new AdapterCertificate(this, new AdapterCertificate.ICertificate() {
|
||||
@Override
|
||||
public void onSelected(EntityCertificate certificate) {
|
||||
dialog.dismiss();
|
||||
getArguments().putLong("certificate", certificate.id);
|
||||
sendResult(RESULT_OK);
|
||||
}
|
||||
});
|
||||
rvCertificate.setAdapter(adapter);
|
||||
|
||||
rvCertificate.setVisibility(View.GONE);
|
||||
pbWait.setVisibility(View.VISIBLE);
|
||||
|
||||
DB db = DB.getInstance(getContext());
|
||||
db.certificate().liveCertificates(email).observe(getViewLifecycleOwner(), new Observer<List<EntityCertificate>>() {
|
||||
@Override
|
||||
public void onChanged(List<EntityCertificate> certificates) {
|
||||
pbWait.setVisibility(View.GONE);
|
||||
rvCertificate.setVisibility(View.VISIBLE);
|
||||
adapter.set(email, certificates);
|
||||
}
|
||||
});
|
||||
|
||||
return dialog;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FragmentDialogSend extends FragmentDialogBase {
|
||||
@NonNull
|
||||
@Override
|
||||
|
|
|
@ -49,7 +49,6 @@ import android.print.PrintAttributes;
|
|||
import android.print.PrintDocumentAdapter;
|
||||
import android.print.PrintManager;
|
||||
import android.security.KeyChain;
|
||||
import android.security.KeyChainAliasCallback;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.LongSparseArray;
|
||||
|
@ -3896,6 +3895,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
|
|||
long id = intent.getLongExtra("id", -1);
|
||||
boolean auto = intent.getBooleanExtra("auto", false);
|
||||
int type = intent.getIntExtra("type", EntityMessage.ENCRYPT_NONE);
|
||||
String recipient = intent.getStringExtra("recipient");
|
||||
|
||||
final Bundle args = new Bundle();
|
||||
args.putLong("id", id);
|
||||
|
@ -3904,27 +3904,27 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
|
|||
if (EntityMessage.SMIME_SIGNONLY.equals(type))
|
||||
onSmime(args);
|
||||
else if (EntityMessage.SMIME_SIGNENCRYPT.equals(type)) {
|
||||
Handler handler = new Handler();
|
||||
KeyChain.choosePrivateKeyAlias(getActivity(), new KeyChainAliasCallback() {
|
||||
@Override
|
||||
public void alias(@Nullable String alias) {
|
||||
Log.i("Selected key alias=" + alias);
|
||||
if (alias != null) {
|
||||
args.putString("alias", alias);
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
onSmime(args);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
Helper.selectKeyAlias(getActivity(), recipient, new Helper.IKeyAlias() {
|
||||
@Override
|
||||
public void onSelected(String alias) {
|
||||
args.putString("alias", alias);
|
||||
onSmime(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected() {
|
||||
Snackbar snackbar = Snackbar.make(view, R.string.title_invalid_key, Snackbar.LENGTH_LONG);
|
||||
final Intent intent = KeyChain.createInstallIntent();
|
||||
if (intent.resolveActivity(getContext().getPackageManager()) != null)
|
||||
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
},
|
||||
null, null, null, -1, null);
|
||||
});
|
||||
snackbar.show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (pgpService.isBound()) {
|
||||
Intent data = new Intent();
|
||||
|
|
|
@ -19,6 +19,7 @@ package eu.faircode.email;
|
|||
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.app.KeyguardManager;
|
||||
import android.content.ActivityNotFoundException;
|
||||
|
@ -42,6 +43,9 @@ import android.os.LocaleList;
|
|||
import android.os.Parcel;
|
||||
import android.os.PowerManager;
|
||||
import android.os.StatFs;
|
||||
import android.security.KeyChain;
|
||||
import android.security.KeyChainAliasCallback;
|
||||
import android.security.KeyChainException;
|
||||
import android.text.Spannable;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
|
@ -974,6 +978,62 @@ public class Helper {
|
|||
return "?";
|
||||
}
|
||||
|
||||
static void selectKeyAlias(final Activity activity, final String email, final IKeyAlias intf) {
|
||||
final Context context = activity.getApplicationContext();
|
||||
final Handler handler = new Handler();
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (email != null)
|
||||
try {
|
||||
if (KeyChain.getPrivateKey(context, email) != null) {
|
||||
Log.i("Private key available alias=" + email);
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
intf.onSelected(email);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch (KeyChainException ex) {
|
||||
Log.w(ex);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
KeyChain.choosePrivateKeyAlias(activity, new KeyChainAliasCallback() {
|
||||
@Override
|
||||
public void alias(@Nullable final String alias) {
|
||||
Log.i("Selected key alias=" + alias);
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (alias == null)
|
||||
intf.onNothingSelected();
|
||||
else
|
||||
intf.onSelected(alias);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
null, null, null, -1, email);
|
||||
}
|
||||
});
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
interface IKeyAlias {
|
||||
void onSelected(String alias);
|
||||
|
||||
void onNothingSelected();
|
||||
}
|
||||
|
||||
// Miscellaneous
|
||||
|
||||
static <T> List<List<T>> chunkList(List<T> list, int size) {
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/rvCertificate"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="12dp"
|
||||
android:scrollbarStyle="outsideOverlay"
|
||||
android:scrollbars="vertical"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<eu.faircode.email.ContentLoadingProgressBar
|
||||
android:id="@+id/pbWait"
|
||||
style="@style/Base.Widget.AppCompat.ProgressBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminate="true"
|
||||
android:padding="24dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue