Sign-only fixes

pull/168/head
M66B 6 years ago
parent 8b8c3b539b
commit ec365a4a9b

@ -2182,8 +2182,10 @@ class Core {
attachment.message = message.id; attachment.message = message.id;
attachment.sequence = sequence++; attachment.sequence = sequence++;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
if (EntityAttachment.PGP_MESSAGE.equals(attachment.encryption)) if (EntityAttachment.PGP_SIGNATURE.equals(attachment.encryption))
db.message().setMessageEncrypt(message.id, EntityMessage.ENCRYPTION_SIGNENCRYPT); db.message().setMessageEncrypt(message.id, EntityMessage.PGP_SIGNONLY);
else if (EntityAttachment.PGP_MESSAGE.equals(attachment.encryption))
db.message().setMessageEncrypt(message.id, EntityMessage.PGP_SIGNENCRYPT);
} }
runRules(context, imessage, message, rules); runRules(context, imessage, message, rules);

@ -80,9 +80,9 @@ import static androidx.room.ForeignKey.SET_NULL;
public class EntityMessage implements Serializable { public class EntityMessage implements Serializable {
static final String TABLE_NAME = "message"; static final String TABLE_NAME = "message";
static final Integer ENCRYPTION_NONE = 0; static final Integer ENCRYPT_NONE = 0;
static final Integer ENCRYPTION_SIGNENCRYPT = 1; static final Integer PGP_SIGNENCRYPT = 1;
static final Integer ENCRYPTION_SIGNONLY = 2; static final Integer PGP_SIGNONLY = 2;
static final Integer PRIORITIY_LOW = 0; static final Integer PRIORITIY_LOW = 0;
static final Integer PRIORITIY_NORMAL = 1; static final Integer PRIORITIY_NORMAL = 1;

@ -121,7 +121,7 @@ public class EntityOperation {
for (Object value : values) for (Object value : values)
jargs.put(value); jargs.put(value);
if (MOVE.equals(name) && EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(message.encrypt)) { if (MOVE.equals(name) && EntityMessage.PGP_SIGNENCRYPT.equals(message.encrypt)) {
EntityFolder folder = db.folder().getFolder(message.folder); EntityFolder folder = db.folder().getFolder(message.folder);
if (folder != null && EntityFolder.DRAFTS.equals(folder.type)) if (folder != null && EntityFolder.DRAFTS.equals(folder.type))
name = DELETE; name = DELETE;

@ -936,17 +936,17 @@ public class FragmentCompose extends FragmentBase {
int colorEncrypt = Helper.resolveColor(getContext(), R.attr.colorEncrypt); int colorEncrypt = Helper.resolveColor(getContext(), R.attr.colorEncrypt);
ImageButton ib = (ImageButton) menu.findItem(R.id.menu_encrypt).getActionView(); ImageButton ib = (ImageButton) menu.findItem(R.id.menu_encrypt).getActionView();
ib.setEnabled(!busy); ib.setEnabled(!busy);
ib.setImageResource(EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(encrypt) ib.setImageResource(EntityMessage.PGP_SIGNENCRYPT.equals(encrypt)
? R.drawable.baseline_lock_24 : R.drawable.baseline_lock_open_24); ? R.drawable.baseline_lock_24 : R.drawable.baseline_lock_open_24);
ib.setImageTintList(EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(encrypt) ib.setImageTintList(EntityMessage.PGP_SIGNENCRYPT.equals(encrypt)
? ColorStateList.valueOf(colorEncrypt) : null); ? ColorStateList.valueOf(colorEncrypt) : null);
menu.findItem(R.id.menu_media).setChecked(media); menu.findItem(R.id.menu_media).setChecked(media);
menu.findItem(R.id.menu_compact).setChecked(compact); menu.findItem(R.id.menu_compact).setChecked(compact);
if (EntityMessage.ENCRYPTION_SIGNONLY.equals(encrypt)) if (EntityMessage.PGP_SIGNONLY.equals(encrypt))
bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_sign); bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_sign);
else if (EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(encrypt)) else if (EntityMessage.PGP_SIGNENCRYPT.equals(encrypt))
bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_encrypt); bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_encrypt);
else else
bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_send); bottom_navigation.getMenu().findItem(R.id.action_send).setTitle(R.string.title_send);
@ -999,8 +999,8 @@ public class FragmentCompose extends FragmentBase {
} }
private void onMenuEncrypt() { private void onMenuEncrypt() {
encrypt = (EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(encrypt) encrypt = (EntityMessage.PGP_SIGNENCRYPT.equals(encrypt)
? EntityMessage.ENCRYPTION_NONE : EntityMessage.ENCRYPTION_SIGNENCRYPT); ? EntityMessage.ENCRYPT_NONE : EntityMessage.PGP_SIGNENCRYPT);
getActivity().invalidateOptionsMenu(); getActivity().invalidateOptionsMenu();
Bundle args = new Bundle(); Bundle args = new Bundle();
@ -1014,7 +1014,7 @@ public class FragmentCompose extends FragmentBase {
int encrypt = args.getInt("encrypt"); int encrypt = args.getInt("encrypt");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
if (EntityMessage.ENCRYPTION_NONE.equals(encrypt)) if (EntityMessage.ENCRYPT_NONE.equals(encrypt))
db.message().setMessageEncrypt(id, null); db.message().setMessageEncrypt(id, null);
else else
db.message().setMessageEncrypt(id, encrypt); db.message().setMessageEncrypt(id, encrypt);
@ -1217,10 +1217,10 @@ public class FragmentCompose extends FragmentBase {
} }
Intent intent; Intent intent;
if (EntityMessage.ENCRYPTION_SIGNONLY.equals(draft.encrypt)) { if (EntityMessage.PGP_SIGNONLY.equals(draft.encrypt)) {
intent = new Intent(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); intent = new Intent(OpenPgpApi.ACTION_GET_SIGN_KEY_ID);
intent.putExtra(BuildConfig.APPLICATION_ID, working); intent.putExtra(BuildConfig.APPLICATION_ID, working);
} else if (EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(draft.encrypt)) { } else if (EntityMessage.PGP_SIGNENCRYPT.equals(draft.encrypt)) {
intent = new Intent(OpenPgpApi.ACTION_GET_KEY_IDS); intent = new Intent(OpenPgpApi.ACTION_GET_KEY_IDS);
intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, pgpUserIds); intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, pgpUserIds);
intent.putExtra(BuildConfig.APPLICATION_ID, working); intent.putExtra(BuildConfig.APPLICATION_ID, working);
@ -1515,9 +1515,9 @@ public class FragmentCompose extends FragmentBase {
File output = new File(context.getCacheDir(), "output." + id); File output = new File(context.getCacheDir(), "output." + id);
// Serializing messages is NOT reproducible // Serializing messages is NOT reproducible
if ((EntityMessage.ENCRYPTION_SIGNONLY.equals(draft.encrypt) && if ((EntityMessage.PGP_SIGNONLY.equals(draft.encrypt) &&
OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) || OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) ||
(EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(draft.encrypt) && (EntityMessage.PGP_SIGNENCRYPT.equals(draft.encrypt) &&
OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction()))) { OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction()))) {
// Get attachments // Get attachments
List<EntityAttachment> attachments = db.attachment().getAttachments(id); List<EntityAttachment> attachments = db.attachment().getAttachments(id);
@ -1654,7 +1654,7 @@ public class FragmentCompose extends FragmentBase {
if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction()) || if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction()) ||
(OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction()) && pgpKeyIds.length > 1)) { (OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction()) && pgpKeyIds.length > 1)) {
if (EntityMessage.ENCRYPTION_SIGNONLY.equals(draft.encrypt)) { if (EntityMessage.PGP_SIGNONLY.equals(draft.encrypt)) {
// Sign message // Sign message
Intent intent = new Intent(OpenPgpApi.ACTION_DETACHED_SIGN); Intent intent = new Intent(OpenPgpApi.ACTION_DETACHED_SIGN);
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, pgpSignKeyId); intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, pgpSignKeyId);
@ -1682,14 +1682,14 @@ public class FragmentCompose extends FragmentBase {
pgpSignKeyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1); pgpSignKeyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1);
db.identity().setIdentitySignKey(identity.id, pgpSignKeyId); db.identity().setIdentitySignKey(identity.id, pgpSignKeyId);
if (EntityMessage.ENCRYPTION_SIGNONLY.equals(draft.encrypt)) { if (EntityMessage.PGP_SIGNONLY.equals(draft.encrypt)) {
// Get sign key // Get sign key
Intent intent = new Intent(OpenPgpApi.ACTION_GET_KEY); Intent intent = new Intent(OpenPgpApi.ACTION_GET_KEY);
intent.putExtra(OpenPgpApi.EXTRA_KEY_ID, pgpSignKeyId); intent.putExtra(OpenPgpApi.EXTRA_KEY_ID, pgpSignKeyId);
intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
intent.putExtra(BuildConfig.APPLICATION_ID, id); intent.putExtra(BuildConfig.APPLICATION_ID, id);
return intent; return intent;
} else if (EntityMessage.ENCRYPTION_SIGNENCRYPT.equals(draft.encrypt)) { } else if (EntityMessage.PGP_SIGNENCRYPT.equals(draft.encrypt)) {
// Encrypt message // Encrypt message
Intent intent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); Intent intent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
intent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, pgpKeyIds); intent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, pgpKeyIds);
@ -2232,7 +2232,7 @@ public class FragmentCompose extends FragmentBase {
if (plain_only) if (plain_only)
data.draft.plain_only = true; data.draft.plain_only = true;
if (encrypt_default) if (encrypt_default)
data.draft.encrypt = EntityMessage.ENCRYPTION_SIGNENCRYPT; data.draft.encrypt = EntityMessage.PGP_SIGNENCRYPT;
if (receipt_default) if (receipt_default)
data.draft.receipt_request = true; data.draft.receipt_request = true;
@ -3916,7 +3916,7 @@ public class FragmentCompose extends FragmentBase {
cbPlainOnly.setVisibility(draft.receipt != null && draft.receipt ? View.GONE : View.VISIBLE); cbPlainOnly.setVisibility(draft.receipt != null && draft.receipt ? View.GONE : View.VISIBLE);
cbReceipt.setVisibility(draft.receipt != null && draft.receipt ? View.GONE : View.VISIBLE); cbReceipt.setVisibility(draft.receipt != null && draft.receipt ? View.GONE : View.VISIBLE);
int encrypt = (draft.encrypt == null ? EntityMessage.ENCRYPTION_NONE : draft.encrypt); int encrypt = (draft.encrypt == null ? EntityMessage.ENCRYPT_NONE : draft.encrypt);
for (int i = 0; i < encryptValues.length; i++) for (int i = 0; i < encryptValues.length; i++)
if (encryptValues[i] == encrypt) { if (encryptValues[i] == encrypt) {
spEncrypt.setTag(i); spEncrypt.setTag(i);

@ -4112,12 +4112,14 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
return null; return null;
InputStream in = null; InputStream in = null;
OutputStream out = null;
boolean inline = false; boolean inline = false;
File plain = File.createTempFile("plain", "." + message.id, context.getCacheDir());
// Find encrypted data // Find encrypted data
for (EntityAttachment attachment : attachments) for (EntityAttachment attachment : attachments)
if (EntityAttachment.PGP_MESSAGE.equals(attachment.encryption) || if (EntityAttachment.PGP_CONTENT.equals(attachment.encryption) ||
EntityAttachment.PGP_CONTENT.equals(attachment.encryption)) { EntityAttachment.PGP_MESSAGE.equals(attachment.encryption)) {
if (!attachment.available) if (!attachment.available)
if (auto) if (auto)
return null; return null;
@ -4126,14 +4128,17 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
File file = attachment.getFile(context); File file = attachment.getFile(context);
in = new FileInputStream(file); in = new FileInputStream(file);
break;
if (EntityAttachment.PGP_MESSAGE.equals(attachment.encryption))
out = new FileOutputStream(plain);
} else if (EntityAttachment.PGP_SIGNATURE.equals(attachment.encryption)) { } else if (EntityAttachment.PGP_SIGNATURE.equals(attachment.encryption)) {
File file = attachment.getFile(context); File file = attachment.getFile(context);
byte[] signature = new byte[(int) file.length()]; byte[] signature = new byte[(int) file.length()];
try (FileInputStream fis = new FileInputStream(file)) { try (FileInputStream fis = new FileInputStream(file)) {
fis.read(signature); fis.read(signature);
} }
data.putExtra(OpenPgpApi.ACTION_DETACHED_SIGN, signature); data.putExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE, signature);
} }
if (in == null) { if (in == null) {
@ -4167,74 +4172,74 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
throw new IllegalArgumentException(context.getString(R.string.title_not_encrypted)); throw new IllegalArgumentException(context.getString(R.string.title_not_encrypted));
Intent result; Intent result;
File plain = File.createTempFile("plain", "." + message.id, context.getCacheDir());
try { try {
// Decrypt message // Decrypt message
Log.i("Executing " + data.getAction()); Log.i("Executing " + data.getAction());
Log.logExtras(data); Log.logExtras(data);
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService()); OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
result = api.executeApi(data, in, new FileOutputStream(plain)); result = api.executeApi(data, in, out);
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR); int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
Log.i("Result action=" + data.getAction() + " code=" + resultCode); Log.i("Result action=" + data.getAction() + " code=" + resultCode);
Log.logExtras(data); Log.logExtras(data);
switch (resultCode) { switch (resultCode) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
if (inline) { if (out != null)
try { if (inline) {
db.beginTransaction(); try {
db.beginTransaction();
// Write decrypted body
Helper.copy(plain, message.getFile(context)); // Write decrypted body
db.message().setMessageStored(message.id, new Date().getTime()); Helper.copy(plain, message.getFile(context));
db.message().setMessageStored(message.id, new Date().getTime());
db.setTransactionSuccessful();
} finally { db.setTransactionSuccessful();
db.endTransaction(); } finally {
} db.endTransaction();
}
} else {
// Decode message
MessageHelper.MessageParts parts;
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
try (InputStream fis = new FileInputStream(plain)) {
MimeMessage imessage = new MimeMessage(isession, fis);
MessageHelper helper = new MessageHelper(imessage);
parts = helper.getMessageParts();
}
try { } else {
db.beginTransaction(); // Decode message
MessageHelper.MessageParts parts;
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
try (InputStream fis = new FileInputStream(plain)) {
MimeMessage imessage = new MimeMessage(isession, fis);
MessageHelper helper = new MessageHelper(imessage);
parts = helper.getMessageParts();
}
// Write decrypted body try {
String html = parts.getHtml(context); db.beginTransaction();
Helper.writeText(message.getFile(context), html);
// Write decrypted body
// Remove existing attachments String html = parts.getHtml(context);
db.attachment().deleteAttachments(message.id); Helper.writeText(message.getFile(context), html);
// Add decrypted attachments // Remove existing attachments
List<EntityAttachment> remotes = parts.getAttachments(); db.attachment().deleteAttachments(message.id);
for (int index = 0; index < remotes.size(); index++) {
EntityAttachment remote = remotes.get(index); // Add decrypted attachments
remote.message = message.id; List<EntityAttachment> remotes = parts.getAttachments();
remote.sequence = index + 1; for (int index = 0; index < remotes.size(); index++) {
remote.id = db.attachment().insertAttachment(remote); EntityAttachment remote = remotes.get(index);
try { remote.message = message.id;
parts.downloadAttachment(context, index, remote); remote.sequence = index + 1;
} catch (Throwable ex) { remote.id = db.attachment().insertAttachment(remote);
Log.e(ex); try {
parts.downloadAttachment(context, index, remote);
} catch (Throwable ex) {
Log.e(ex);
}
} }
}
db.message().setMessageStored(message.id, new Date().getTime()); db.message().setMessageStored(message.id, new Date().getTime());
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {
db.endTransaction(); db.endTransaction();
}
} }
}
// Check signature status // Check signature status
OpenPgpSignatureResult sigResult = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); OpenPgpSignatureResult sigResult = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);

@ -345,7 +345,8 @@ public class Log {
elements[i] = (element == null ? null : element.toString()); elements[i] = (element == null ? null : element.toString());
} }
value = TextUtils.join(",", elements); value = TextUtils.join(",", elements);
} } else
value = "[" + length + "]";
} else if (v instanceof Long) } else if (v instanceof Long)
value = "0x" + Long.toHexString((Long) v); value = "0x" + Long.toHexString((Long) v);

@ -1243,7 +1243,7 @@ public class MessageHelper {
AttachmentPart apart = new AttachmentPart(); AttachmentPart apart = new AttachmentPart();
apart.disposition = Part.INLINE; apart.disposition = Part.INLINE;
apart.filename = "content.txt"; apart.filename = "content.asc";
apart.encrypt = EntityAttachment.PGP_CONTENT; apart.encrypt = EntityAttachment.PGP_CONTENT;
apart.part = multipart.getBodyPart(0); apart.part = multipart.getBodyPart(0);
@ -1253,6 +1253,7 @@ public class MessageHelper {
apart.attachment.disposition = apart.disposition; apart.attachment.disposition = apart.disposition;
apart.attachment.name = apart.filename; apart.attachment.name = apart.filename;
apart.attachment.type = ct.getBaseType().toLowerCase(Locale.ROOT); apart.attachment.type = ct.getBaseType().toLowerCase(Locale.ROOT);
apart.attachment.size = (long) apart.part.getSize();
apart.attachment.encryption = apart.encrypt; apart.attachment.encryption = apart.encrypt;
parts.attachments.add(apart); parts.attachments.add(apart);

@ -694,7 +694,7 @@
<string name="title_decrypt">Decrypt</string> <string name="title_decrypt">Decrypt</string>
<string name="title_resync">Resync</string> <string name="title_resync">Resync</string>
<string name="title_no_openpgp">OpenKeychain not found</string> <string name="title_no_openpgp">OpenKeychain not found</string>
<string name="title_not_encrypted">Message is not encrypted</string> <string name="title_not_encrypted">Message is not signed or encrypted</string>
<string name="title_reset_sign_key">Reset sign key</string> <string name="title_reset_sign_key">Reset sign key</string>
<string name="title_import_encrypt_key">Import encryption key</string> <string name="title_import_encrypt_key">Import encryption key</string>

Loading…
Cancel
Save