Fixed autocrypt keydata

pull/157/head
M66B 6 years ago
parent eb009db29f
commit a1434ca3cf

@ -56,6 +56,7 @@ public class EntityAttachment {
static final Integer PGP_MESSAGE = 1; static final Integer PGP_MESSAGE = 1;
static final Integer PGP_SIGNATURE = 2; static final Integer PGP_SIGNATURE = 2;
static final Integer PGP_KEY = 3;
// https://developer.android.com/guide/topics/media/media-formats#image-formats // https://developer.android.com/guide/topics/media/media-formats#image-formats
private static final List<String> IMAGE_TYPES = Collections.unmodifiableList(Arrays.asList( private static final List<String> IMAGE_TYPES = Collections.unmodifiableList(Arrays.asList(

@ -1387,10 +1387,12 @@ public class FragmentCompose extends FragmentBase {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
imessage.writeTo(os); imessage.writeTo(os);
ByteArrayInputStream decrypted = new ByteArrayInputStream(os.toByteArray()); ByteArrayInputStream decrypted = new ByteArrayInputStream(os.toByteArray());
ByteArrayOutputStream encrypted = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()) ByteArrayOutputStream encrypted = null;
? new ByteArrayOutputStream() : null); if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction()) ||
OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()))
encrypted = new ByteArrayOutputStream();
if (BuildConfig.BETA_RELEASE) { if (BuildConfig.DEBUG || BuildConfig.BETA_RELEASE) {
Log.i("Execute " + data); Log.i("Execute " + data);
Log.logExtras(data); Log.logExtras(data);
} }
@ -1399,7 +1401,7 @@ public class FragmentCompose extends FragmentBase {
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService()); OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
Intent result = api.executeApi(data, decrypted, encrypted); Intent result = api.executeApi(data, decrypted, encrypted);
if (BuildConfig.BETA_RELEASE) { if (BuildConfig.DEBUG || BuildConfig.BETA_RELEASE) {
Log.i("Result " + result); Log.i("Result " + result);
Log.logExtras(result); Log.logExtras(result);
} }
@ -1408,28 +1410,43 @@ public class FragmentCompose extends FragmentBase {
switch (resultCode) { switch (resultCode) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
// Attach encrypted data / signature // Attach encrypted data / signature
if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()) || if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction()) ||
OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()) ||
OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction()))
try { try {
db.beginTransaction(); db.beginTransaction();
String name;
int encryption;
if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction())) {
name = "keydata.asc";
encryption = EntityAttachment.PGP_KEY;
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
name = "encrypted.asc";
encryption = EntityAttachment.PGP_MESSAGE;
} else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) {
name = "signature.asc";
encryption = EntityAttachment.PGP_SIGNATURE;
} else
throw new IllegalStateException(data.getAction());
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
attachment.message = id; attachment.message = id;
attachment.sequence = db.attachment().getAttachmentSequence(id) + 1; attachment.sequence = db.attachment().getAttachmentSequence(id) + 1;
attachment.name = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()) attachment.name = name;
? "encrypted.asc" : "signature.asc");
attachment.type = "application/octet-stream"; attachment.type = "application/octet-stream";
attachment.disposition = Part.INLINE; attachment.disposition = Part.INLINE;
attachment.encryption = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()) attachment.encryption = encryption;
? EntityAttachment.PGP_MESSAGE : EntityAttachment.PGP_SIGNATURE);
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
byte[] bytes = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()) byte[] bytes;
? encrypted.toByteArray() if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction()))
: result.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE)); bytes = result.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE);
else
bytes = encrypted.toByteArray();
File file = attachment.getFile(context); File file = attachment.getFile(context);
if (BuildConfig.BETA_RELEASE) if (BuildConfig.DEBUG || BuildConfig.BETA_RELEASE)
Log.i("Writing " + file + " size=" + bytes.length); Log.i("Writing " + file + " size=" + bytes.length);
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
out.write(bytes); out.write(bytes);
@ -1443,8 +1460,16 @@ 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);
if (BuildConfig.DEBUG || BuildConfig.BETA_RELEASE)
Log.i("Keys=" + pgpKeyIds.length);
// Encrypt message // Get encrypt key
Intent intent = new Intent(OpenPgpApi.ACTION_GET_KEY);
intent.putExtra(OpenPgpApi.EXTRA_KEY_ID, pgpKeyIds[0]);
intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
return intent;
} else if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction())) {
// Get sign key
Intent intent = new Intent(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); Intent intent = new Intent(OpenPgpApi.ACTION_GET_SIGN_KEY_ID);
return intent; return intent;
} else if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) { } else if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) {
@ -1457,10 +1482,10 @@ public class FragmentCompose extends FragmentBase {
intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
return intent; return intent;
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) { } else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
// Sign message // 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);
return intent; return null;
} else { } else {
// send message // send message
return null; return null;
@ -1544,7 +1569,7 @@ public class FragmentCompose extends FragmentBase {
} }
} else if (requestCode == ActivityCompose.REQUEST_ENCRYPT) { } else if (requestCode == ActivityCompose.REQUEST_ENCRYPT) {
if (data != null) { if (data != null) {
if (BuildConfig.BETA_RELEASE) if (BuildConfig.DEBUG || BuildConfig.BETA_RELEASE)
Log.logExtras(data); Log.logExtras(data);
doPgp(data); doPgp(data);
} }

@ -252,7 +252,7 @@ public class MessageHelper {
if (message.from != null && message.from.length > 0) if (message.from != null && message.from.length > 0)
for (EntityAttachment attachment : attachments) for (EntityAttachment attachment : attachments)
if (attachment.available && EntityAttachment.PGP_SIGNATURE.equals(attachment.encryption)) { if (attachment.available && EntityAttachment.PGP_KEY.equals(attachment.encryption)) {
InternetAddress from = (InternetAddress) message.from[0]; InternetAddress from = (InternetAddress) message.from[0];
File file = attachment.getFile(context); File file = attachment.getFile(context);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

Loading…
Cancel
Save