Store S/MIME key alias in identity

pull/168/head
M66B 5 years ago
parent 0d1d7f6ae9
commit 043c33ea41

File diff suppressed because it is too large Load Diff

@ -125,9 +125,19 @@ public class AdapterIdentity extends RecyclerView.Adapter<AdapterIdentity.ViewHo
tvHost.setText(String.format("%s:%d", identity.host, identity.port)); tvHost.setText(String.format("%s:%d", identity.host, identity.port));
tvAccount.setText(identity.accountName); tvAccount.setText(identity.accountName);
tvSignKeyId.setText(identity.sign_key == null ? null
: context.getString(R.string.title_sign_key, Long.toHexString(identity.sign_key))); StringBuilder sb = new StringBuilder();
tvSignKeyId.setVisibility(identity.sign_key == null ? View.GONE : View.VISIBLE); if (identity.sign_key != null)
sb.append(Long.toHexString(identity.sign_key));
if (identity.sign_key_alias != null) {
if (sb.length() != 0)
sb.append(", ");
sb.append(identity.sign_key_alias);
}
tvSignKeyId.setText(context.getString(R.string.title_sign_key, sb.toString()));
tvSignKeyId.setVisibility(sb.length() > 0 ? View.VISIBLE : View.GONE);
tvLast.setText(context.getString(R.string.title_last_connected, tvLast.setText(context.getString(R.string.title_last_connected,
identity.last_connected == null ? "-" : DTF.format(identity.last_connected))); identity.last_connected == null ? "-" : DTF.format(identity.last_connected)));
@ -230,7 +240,16 @@ public class AdapterIdentity extends RecyclerView.Adapter<AdapterIdentity.ViewHo
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
db.identity().setIdentitySignKey(id, null); try {
db.beginTransaction();
db.identity().setIdentitySignKey(id, null);
db.identity().setIdentitySignKeyAlias(id, null);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null; return null;
} }

@ -56,7 +56,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
// https://developer.android.com/topic/libraries/architecture/room.html // https://developer.android.com/topic/libraries/architecture/room.html
@Database( @Database(
version = 118, version = 119,
entities = { entities = {
EntityIdentity.class, EntityIdentity.class,
EntityAccount.class, EntityAccount.class,
@ -1155,6 +1155,13 @@ public abstract class DB extends RoomDatabase {
db.execSQL("CREATE INDEX IF NOT EXISTS `index_certificate_email` ON `certificate` (`email`)"); db.execSQL("CREATE INDEX IF NOT EXISTS `index_certificate_email` ON `certificate` (`email`)");
} }
}) })
.addMigrations(new Migration(118, 119) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `identity` ADD COLUMN `sign_key_alias` TEXT");
}
})
.build(); .build();
} }

@ -94,6 +94,9 @@ public interface DaoIdentity {
@Query("UPDATE identity SET sign_key = :sign_key WHERE id = :id") @Query("UPDATE identity SET sign_key = :sign_key WHERE id = :id")
int setIdentitySignKey(long id, Long sign_key); int setIdentitySignKey(long id, Long sign_key);
@Query("UPDATE identity SET sign_key_alias = :alias WHERE id = :id")
int setIdentitySignKeyAlias(long id, String alias);
@Query("UPDATE identity SET error = :error WHERE id = :id") @Query("UPDATE identity SET error = :error WHERE id = :id")
int setIdentityError(long id, String error); int setIdentityError(long id, String error);

@ -100,6 +100,7 @@ public class EntityIdentity {
public Boolean store_sent = false; // obsolete public Boolean store_sent = false; // obsolete
public Long sent_folder = null; // obsolete public Long sent_folder = null; // obsolete
public Long sign_key = null; // OpenPGP public Long sign_key = null; // OpenPGP
public String sign_key_alias = null; // S/MIME
public Boolean tbd; public Boolean tbd;
public String state; public String state;
public String error; public String error;

@ -1249,38 +1249,53 @@ public class FragmentCompose extends FragmentBase {
private void onEncrypt(final EntityMessage draft) { private void onEncrypt(final EntityMessage draft) {
if (EntityMessage.SMIME_SIGNONLY.equals(draft.encrypt) || if (EntityMessage.SMIME_SIGNONLY.equals(draft.encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(draft.encrypt)) { EntityMessage.SMIME_SIGNENCRYPT.equals(draft.encrypt)) {
String sender = null;
if (draft.from != null && draft.from.length > 0)
sender = ((InternetAddress) draft.from[0]).getAddress();
Log.i("Alias sender=" + sender);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", draft.id); args.putLong("id", draft.id);
args.putInt("type", draft.encrypt); args.putInt("type", draft.encrypt);
args.putString("sender", sender);
Helper.selectKeyAlias(getActivity(), sender, new Helper.IKeyAlias() { new SimpleTask<EntityIdentity>() {
@Override @Override
public void onSelected(String alias) { protected EntityIdentity onExecute(Context context, Bundle args) {
args.putString("alias", alias); long id = args.getLong("id");
onSmime(args);
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id);
if (draft == null || draft.identity == null)
return null;
return db.identity().getIdentity(draft.identity);
} }
@Override @Override
public void onNothingSelected() { protected void onExecuted(final Bundle args, EntityIdentity identity) {
Snackbar snackbar = Snackbar.make(view, R.string.title_no_key, Snackbar.LENGTH_LONG); Helper.selectKeyAlias(getActivity(), identity.sign_key_alias, new Helper.IKeyAlias() {
final Intent intent = KeyChain.createInstallIntent(); @Override
if (intent.resolveActivity(getContext().getPackageManager()) != null) public void onSelected(String alias) {
snackbar.setAction(R.string.title_fix, new View.OnClickListener() { args.putString("alias", alias);
@Override onSmime(args);
public void onClick(View v) { }
startActivity(intent);
} @Override
}); public void onNothingSelected() {
snackbar.show(); Snackbar snackbar = Snackbar.make(view, R.string.title_no_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);
}
});
snackbar.show();
}
});
} }
});
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "compose:alias");
} else { } else {
if (pgpService.isBound()) if (pgpService.isBound())
try { try {
@ -1923,8 +1938,10 @@ public class FragmentCompose extends FragmentBase {
}; };
bpContent.setContent(imessage.getContent(), imessage.getContentType()); bpContent.setContent(imessage.getContent(), imessage.getContentType());
// Store selected alias
if (alias == null) if (alias == null)
throw new IllegalArgumentException("Key alias missing"); throw new IllegalArgumentException("Key alias missing");
db.identity().setIdentitySignKeyAlias(identity.id, alias);
// Get private key // Get private key
PrivateKey privkey = KeyChain.getPrivateKey(context, alias); PrivateKey privkey = KeyChain.getPrivateKey(context, alias);

Loading…
Cancel
Save