Encryption improvements

pull/168/head
M66B 6 years ago
parent 8ec3e01a5e
commit 922921dc67

@ -1556,9 +1556,9 @@ public class FragmentCompose extends FragmentBase {
attachments.remove(attachment); attachments.remove(attachment);
} }
// Create temporary files // Create files
File plain = new File(context.getCacheDir(), "plain." + id); File input = new File(context.getCacheDir(), "input." + id);
File encrypted = new File(context.getCacheDir(), "encrypted." + id); File output = new File(context.getCacheDir(), "output." + id);
// Serializing messages is NOT reproducible // Serializing messages is NOT reproducible
if (OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction())) { if (OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction())) {
@ -1569,7 +1569,7 @@ public class FragmentCompose extends FragmentBase {
MessageHelper.build(context, message, attachments, identity, imessage); MessageHelper.build(context, message, attachments, identity, imessage);
// Serialize message // Serialize message
try (OutputStream out = new FileOutputStream(plain)) { try (OutputStream out = new FileOutputStream(input)) {
imessage.writeTo(out); imessage.writeTo(out);
} }
} }
@ -1578,7 +1578,7 @@ public class FragmentCompose extends FragmentBase {
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());
Intent result = api.executeApi(data, new FileInputStream(plain), new FileOutputStream(encrypted)); Intent result = api.executeApi(data, new FileInputStream(input), new FileOutputStream(output));
// Process result // Process result
try { try {
@ -1627,8 +1627,8 @@ public class FragmentCompose extends FragmentBase {
} }
db.attachment().setDownloaded(attachment.id, (long) bytes.length); db.attachment().setDownloaded(attachment.id, (long) bytes.length);
} else { } else {
Log.i("Writing " + file + " size=" + encrypted.length()); Log.i("Writing " + file + " size=" + output.length());
Helper.copy(encrypted, file); Helper.copy(output, file);
db.attachment().setDownloaded(attachment.id, file.length()); db.attachment().setDownloaded(attachment.id, file.length());
} }
@ -1640,10 +1640,8 @@ public class FragmentCompose extends FragmentBase {
if (OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction())) { if (OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction())) {
pgpKeyIds = result.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS); pgpKeyIds = result.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS);
Log.i("Keys=" + pgpKeyIds.length); Log.i("Keys=" + pgpKeyIds.length);
// Send without encryption
if (pgpKeyIds.length == 0) if (pgpKeyIds.length == 0)
return null; throw new IllegalStateException("Got no key");
// Get encrypt key // Get encrypt key
if (pgpKeyIds.length == 1) { if (pgpKeyIds.length == 1) {
@ -1684,23 +1682,26 @@ public class FragmentCompose extends FragmentBase {
intent.putExtra(BuildConfig.APPLICATION_ID, id); intent.putExtra(BuildConfig.APPLICATION_ID, id);
return intent; return intent;
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) { } else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
plain.delete(); input.delete();
// Get signature // Get signature
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);
intent.putExtra(BuildConfig.APPLICATION_ID, id); //intent.putExtra(BuildConfig.APPLICATION_ID, id);
// send message
return null; return null;
} else { } else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) {
// send message // send message
return null; return null;
} } else
throw new IllegalStateException("Unknown action=" + data.getAction());
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
return (PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
plain.delete(); input.delete();
db.identity().setIdentitySignKey(identity.id, null); db.identity().setIdentitySignKey(identity.id, null);
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -1712,7 +1713,7 @@ public class FragmentCompose extends FragmentBase {
throw new IllegalStateException("OpenPgp unknown result code=" + resultCode); throw new IllegalStateException("OpenPgp unknown result code=" + resultCode);
} }
} finally { } finally {
encrypted.delete(); output.delete();
} }
} }

@ -245,6 +245,7 @@ public class MessageHelper {
for (final EntityAttachment attachment : attachments) for (final EntityAttachment attachment : attachments)
if (attachment.available && EntityAttachment.PGP_MESSAGE.equals(attachment.encryption)) { if (attachment.available && EntityAttachment.PGP_MESSAGE.equals(attachment.encryption)) {
// https://tools.ietf.org/html/rfc3156
Multipart multipart = new MimeMultipart("encrypted; protocol=\"application/pgp-encrypted\""); Multipart multipart = new MimeMultipart("encrypted; protocol=\"application/pgp-encrypted\"");
BodyPart pgp = new MimeBodyPart(); BodyPart pgp = new MimeBodyPart();

Loading…
Cancel
Save