S/MIME: split sign/encrypt key

master
M66B 4 months ago
parent c0776edc9f
commit 932d0efc3e

@ -177,9 +177,18 @@ public class AdapterIdentity extends RecyclerView.Adapter<AdapterIdentity.ViewHo
if (identity.sign_key != null) if (identity.sign_key != null)
sb.append(Long.toHexString(identity.sign_key)); sb.append(Long.toHexString(identity.sign_key));
if (identity.sign_key_alias != null) { if (identity.sign_key_alias != null) {
String s = identity.getAlias(EntityMessage.SMIME_SIGNONLY);
String e = identity.getAlias(EntityMessage.SMIME_SIGNENCRYPT);
if (s != null) {
if (sb.length() != 0) if (sb.length() != 0)
sb.append(", "); sb.append(", ");
sb.append(identity.sign_key_alias); sb.append(s);
}
if (e != null && !e.equals(s)) {
if (sb.length() != 0)
sb.append(", ");
sb.append(e);
}
} }
tvSignKeyId.setText(context.getString(R.string.title_sign_key, sb.toString())); tvSignKeyId.setText(context.getString(R.string.title_sign_key, sb.toString()));

@ -205,6 +205,38 @@ public class EntityIdentity {
return false; return false;
} }
void setAlias(String alias, int type) throws JSONException {
JSONObject jaliases;
if (sign_key_alias == null)
jaliases = new JSONObject();
else
try {
jaliases = new JSONObject(sign_key_alias);
} catch (JSONException ex) {
Log.w(ex);
jaliases = new JSONObject();
jaliases.put("s", sign_key_alias);
jaliases.put("e", sign_key_alias);
}
jaliases.put(type == EntityMessage.SMIME_SIGNONLY ? "s" : "e", alias);
sign_key_alias = jaliases.toString();
}
String getAlias(int type) {
if (sign_key_alias == null)
return null;
try {
JSONObject jaliases = new JSONObject(sign_key_alias);
String key = (type == EntityMessage.SMIME_SIGNONLY ? "s" : "e");
return (jaliases.has(key) ? jaliases.getString(key) : null);
} catch (JSONException ex) {
Log.w(ex);
return sign_key_alias;
}
}
public JSONObject toJSON() throws JSONException { public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("id", id); json.put("id", id);

@ -3213,6 +3213,7 @@ public class FragmentCompose extends FragmentBase {
@Override @Override
protected EntityIdentity onExecute(Context context, Bundle args) { protected EntityIdentity onExecute(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
int type = args.getInt("type");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id); EntityMessage draft = db.message().getMessage(id);
@ -3220,10 +3221,14 @@ public class FragmentCompose extends FragmentBase {
return null; return null;
EntityIdentity identity = db.identity().getIdentity(draft.identity); EntityIdentity identity = db.identity().getIdentity(draft.identity);
if (identity != null && identity.sign_key_alias != null) if (identity != null)
try { try {
PrivateKey key = KeyChain.getPrivateKey(context, identity.sign_key_alias); String alias = identity.getAlias(type);
if (alias != null) {
args.putString("alias", alias);
PrivateKey key = KeyChain.getPrivateKey(context, alias);
args.putBoolean("available", key != null); args.putBoolean("available", key != null);
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
@ -3238,13 +3243,13 @@ public class FragmentCompose extends FragmentBase {
boolean available = args.getBoolean("available"); boolean available = args.getBoolean("available");
if (available) { if (available) {
args.putString("alias", identity.sign_key_alias);
onSmime(args, action, extras); onSmime(args, action, extras);
return; return;
} }
if (interactive) if (interactive) {
Helper.selectKeyAlias(getActivity(), getViewLifecycleOwner(), identity.sign_key_alias, new Helper.IKeyAlias() { String alias = args.getString("alias");
Helper.selectKeyAlias(getActivity(), getViewLifecycleOwner(), alias, new Helper.IKeyAlias() {
@Override @Override
public void onSelected(String alias) { public void onSelected(String alias) {
args.putString("alias", alias); args.putString("alias", alias);
@ -3270,6 +3275,7 @@ public class FragmentCompose extends FragmentBase {
} }
}); });
} }
}
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
@ -4463,7 +4469,8 @@ public class FragmentCompose extends FragmentBase {
} }
// Store selected alias // Store selected alias
db.identity().setIdentitySignKeyAlias(identity.id, alias); identity.setAlias(alias, type);
db.identity().setIdentitySignKeyAlias(identity.id, identity.sign_key_alias);
// Build content // Build content
File sinput = new File(tmp, draft.id + ".smime_sign"); File sinput = new File(tmp, draft.id + ".smime_sign");

@ -9432,11 +9432,12 @@ public class FragmentMessages extends FragmentBase
@Override @Override
protected void onExecuted(Bundle args, EntityIdentity identity) { protected void onExecuted(Bundle args, EntityIdentity identity) {
int type = args.getInt("type");
boolean auto = args.getBoolean("auto"); boolean auto = args.getBoolean("auto");
if (auto && identity == null) if (auto && identity == null)
return; return;
String alias = (identity == null ? null : identity.sign_key_alias); String alias = (identity == null ? null : identity.getAlias(type));
Helper.selectKeyAlias(getActivity(), getViewLifecycleOwner(), alias, new Helper.IKeyAlias() { Helper.selectKeyAlias(getActivity(), getViewLifecycleOwner(), alias, new Helper.IKeyAlias() {
@Override @Override
public void onSelected(String alias) { public void onSelected(String alias) {
@ -10679,6 +10680,7 @@ public class FragmentMessages extends FragmentBase
} }
private void decodeMessage(Context context, InputStream is, EntityMessage message, Bundle args) throws MessagingException, IOException { private void decodeMessage(Context context, InputStream is, EntityMessage message, Bundle args) throws MessagingException, IOException {
int type = args.getInt("type");
String alias = args.getString("alias"); String alias = args.getString("alias");
boolean duplicate = args.getBoolean("duplicate"); boolean duplicate = args.getBoolean("duplicate");
@ -10765,10 +10767,17 @@ public class FragmentMessages extends FragmentBase
db.message().setMessageStored(message.id, new Date().getTime()); db.message().setMessageStored(message.id, new Date().getTime());
db.message().setMessageFts(message.id, false); db.message().setMessageFts(message.id, false);
if (alias != null && !duplicate && message.identity != null) if (alias != null && !duplicate && message.identity != null) {
db.identity().setIdentitySignKeyAlias(message.identity, alias); EntityIdentity identity = db.identity().getIdentity(message.identity);
if (identity != null) {
identity.setAlias(alias, type);
db.identity().setIdentitySignKeyAlias(identity.id, identity.sign_key_alias);
}
}
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} catch (JSONException ex) {
Log.e(ex);
} catch (SQLiteConstraintException ex) { } catch (SQLiteConstraintException ex) {
// Message removed // Message removed
Log.w(ex); Log.w(ex);

Loading…
Cancel
Save