Use temporary files for encryption/decryption

pull/164/head
M66B 5 years ago
parent 3f4d192512
commit f3fa2d0038

@ -112,9 +112,8 @@ import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
@ -1557,20 +1556,29 @@ public class FragmentCompose extends FragmentBase {
MimeMessage imessage = new MimeMessage(isession);
MessageHelper.build(context, message, attachments, identity, imessage);
// Create temporary files
File plain = File.createTempFile("plain", "." + id, context.getCacheDir());
File encrypted = File.createTempFile("encrypted", "." + id, context.getCacheDir());
// Serialize message
ByteArrayOutputStream os = new ByteArrayOutputStream();
imessage.writeTo(os);
ByteArrayInputStream decrypted = new ByteArrayInputStream(os.toByteArray());
ByteArrayOutputStream encrypted = null;
if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction()) ||
OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()))
encrypted = new ByteArrayOutputStream();
try (OutputStream out = new FileOutputStream(plain)) {
imessage.writeTo(out);
}
// Encrypt message
// Call OpenPGP
Intent result;
try (InputStream in = new FileInputStream(plain)) {
try (OutputStream out = new FileOutputStream(encrypted)) {
Log.i("Executing " + data.getAction());
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
Intent result = api.executeApi(data, decrypted, encrypted);
result = api.executeApi(data, in, out);
}
}
plain.delete();
// Process result
try {
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
Log.i("Result action=" + data.getAction() + " code=" + resultCode);
switch (resultCode) {
@ -1605,18 +1613,20 @@ public class FragmentCompose extends FragmentBase {
attachment.encryption = encryption;
attachment.id = db.attachment().insertAttachment(attachment);
byte[] bytes;
if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction()))
bytes = result.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE);
else
bytes = encrypted.toByteArray();
File file = attachment.getFile(context);
if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) {
byte[] bytes = result.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE);
Log.i("Writing " + file + " size=" + bytes.length);
try (OutputStream out = new FileOutputStream(file)) {
out.write(bytes);
}
db.attachment().setDownloaded(attachment.id, (long) bytes.length);
} else {
Log.i("Writing " + file + " size=" + encrypted.length());
Helper.copy(encrypted, file);
db.attachment().setDownloaded(attachment.id, file.length());
}
db.setTransactionSuccessful();
} finally {
@ -1682,6 +1692,9 @@ public class FragmentCompose extends FragmentBase {
default:
throw new IllegalArgumentException("Unknown result code=" + resultCode);
}
} finally {
encrypted.delete();
}
}
@Override

@ -115,7 +115,6 @@ import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -3841,23 +3840,23 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
Intent data = args.getParcelable("data");
DB db = DB.getInstance(context);
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
InputStream in = null;
boolean inline = false;
InputStream encrypted = null;
// Find encrypted data
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
for (EntityAttachment attachment : attachments)
if (EntityAttachment.PGP_MESSAGE.equals(attachment.encryption)) {
if (!attachment.available)
throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing));
File file = attachment.getFile(context);
encrypted = new FileInputStream(file);
in = new FileInputStream(file);
break;
}
if (encrypted == null) {
if (in == null) {
EntityMessage message = db.message().getMessage(id);
if (message != null && message.content) {
File file = message.getFile(context);
@ -3876,20 +3875,27 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
section = TextUtils.join("\n\r", disarmored);
inline = true;
encrypted = new ByteArrayInputStream(section.getBytes());
in = new ByteArrayInputStream(section.getBytes());
}
}
}
}
if (encrypted == null)
if (in == null)
throw new IllegalArgumentException(context.getString(R.string.title_not_encrypted));
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
Intent result;
File plain = File.createTempFile("plain", "." + id, context.getCacheDir());
try {
// Decrypt message
try {
try (OutputStream out = new FileOutputStream(plain)) {
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
Intent result = api.executeApi(data, encrypted, decrypted);
result = api.executeApi(data, in, out);
}
} finally {
in.close();
}
Log.i("PGP result=" + result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR));
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
@ -3903,8 +3909,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
db.beginTransaction();
// Write decrypted body
Helper.writeText(message.getFile(context), decrypted.toString());
Helper.copy(plain, message.getFile(context));
db.message().setMessageStored(id, new Date().getTime());
db.setTransactionSuccessful();
@ -3914,12 +3919,14 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
} else {
// Decode message
MessageHelper.MessageParts parts;
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
ByteArrayInputStream is = new ByteArrayInputStream(decrypted.toByteArray());
MimeMessage imessage = new MimeMessage(isession, is);
try (InputStream fis = new FileInputStream(plain)) {
MimeMessage imessage = new MimeMessage(isession, fis);
MessageHelper helper = new MessageHelper(imessage);
MessageHelper.MessageParts parts = helper.getMessageParts();
parts = helper.getMessageParts();
}
try {
db.beginTransaction();
@ -3977,6 +3984,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new IllegalArgumentException(error.getMessage());
}
} finally {
plain.delete();
}
return null;
}

Loading…
Cancel
Save