mirror of https://github.com/M66B/FairEmail.git
Get encryption key
This commit is contained in:
parent
ea4453fa16
commit
3fdba49f88
|
@ -46,8 +46,9 @@ public class ActivityCompose extends ActivityBilling implements FragmentManager.
|
||||||
static final int REQUEST_CONTACT_BCC = 3;
|
static final int REQUEST_CONTACT_BCC = 3;
|
||||||
static final int REQUEST_IMAGE = 4;
|
static final int REQUEST_IMAGE = 4;
|
||||||
static final int REQUEST_ATTACHMENT = 5;
|
static final int REQUEST_ATTACHMENT = 5;
|
||||||
static final int REQUEST_ENCRYPT = 6;
|
static final int REQUEST_KEY = 6;
|
||||||
static final int REQUEST_SIGN = 7;
|
static final int REQUEST_ENCRYPT = 7;
|
||||||
|
static final int REQUEST_SIGN = 8;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
|
@ -1101,10 +1101,9 @@ public class FragmentCompose extends FragmentBase {
|
||||||
for (int i = 0; i < ato.length; i++)
|
for (int i = 0; i < ato.length; i++)
|
||||||
tos[i] = ato[i].getAddress();
|
tos[i] = ato[i].getAddress();
|
||||||
|
|
||||||
Intent data = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
|
Intent data = new Intent(OpenPgpApi.ACTION_GET_SIGN_KEY_ID);
|
||||||
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, tos);
|
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, tos);
|
||||||
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
|
encrypt(data);
|
||||||
encrypt(data, false);
|
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
if (ex instanceof IllegalArgumentException)
|
if (ex instanceof IllegalArgumentException)
|
||||||
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
|
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
|
||||||
|
@ -1129,11 +1128,10 @@ public class FragmentCompose extends FragmentBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encrypt(Intent data, boolean sign) {
|
private void encrypt(Intent data) {
|
||||||
final Bundle args = new Bundle();
|
final Bundle args = new Bundle();
|
||||||
args.putLong("id", working);
|
args.putLong("id", working);
|
||||||
args.putParcelable("data", data);
|
args.putParcelable("data", data);
|
||||||
args.putBoolean("sign", sign);
|
|
||||||
|
|
||||||
new SimpleTask<Object>() {
|
new SimpleTask<Object>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -1141,7 +1139,6 @@ public class FragmentCompose extends FragmentBase {
|
||||||
// Get arguments
|
// Get arguments
|
||||||
long id = args.getLong("id");
|
long id = args.getLong("id");
|
||||||
Intent data = args.getParcelable("data");
|
Intent data = args.getParcelable("data");
|
||||||
boolean sign = args.getBoolean("sign");
|
|
||||||
|
|
||||||
DB db = DB.getInstance(context);
|
DB db = DB.getInstance(context);
|
||||||
|
|
||||||
|
@ -1151,7 +1148,7 @@ public class FragmentCompose extends FragmentBase {
|
||||||
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
|
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
|
||||||
for (EntityAttachment attachment : new ArrayList<>(attachments))
|
for (EntityAttachment attachment : new ArrayList<>(attachments))
|
||||||
if (attachment.encryption != null) {
|
if (attachment.encryption != null) {
|
||||||
if (!sign)
|
if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction()))
|
||||||
db.attachment().deleteAttachment(attachment.id);
|
db.attachment().deleteAttachment(attachment.id);
|
||||||
attachments.remove(attachment);
|
attachments.remove(attachment);
|
||||||
}
|
}
|
||||||
|
@ -1169,10 +1166,11 @@ 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 = (sign ? null : new ByteArrayOutputStream());
|
ByteArrayOutputStream encrypted = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())
|
||||||
|
? new ByteArrayOutputStream() : null);
|
||||||
|
|
||||||
if (BuildConfig.BETA_RELEASE) {
|
if (BuildConfig.BETA_RELEASE) {
|
||||||
Log.i((sign ? "Sign " : "Encrypt ") + data);
|
Log.i("Execute " + data);
|
||||||
Log.logExtras(data);
|
Log.logExtras(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1181,7 +1179,7 @@ public class FragmentCompose extends FragmentBase {
|
||||||
Intent result = api.executeApi(data, decrypted, encrypted);
|
Intent result = api.executeApi(data, decrypted, encrypted);
|
||||||
|
|
||||||
if (BuildConfig.BETA_RELEASE) {
|
if (BuildConfig.BETA_RELEASE) {
|
||||||
Log.i((sign ? "Signed " : "Encrypted ") + result);
|
Log.i("Result " + result);
|
||||||
Log.logExtras(result);
|
Log.logExtras(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1189,43 +1187,58 @@ 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
|
||||||
try {
|
if (!OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction()))
|
||||||
db.beginTransaction();
|
try {
|
||||||
|
db.beginTransaction();
|
||||||
|
|
||||||
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 = (sign ? "signature.asc" : "encrypted.asc");
|
attachment.name = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())
|
||||||
attachment.type = "application/octet-stream";
|
? "encrypted.asc" : "signature.asc");
|
||||||
attachment.encryption = (sign ? EntityAttachment.PGP_SIGNATURE : EntityAttachment.PGP_MESSAGE);
|
attachment.type = "application/octet-stream";
|
||||||
attachment.id = db.attachment().insertAttachment(attachment);
|
attachment.encryption = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())
|
||||||
|
? EntityAttachment.PGP_MESSAGE : EntityAttachment.PGP_SIGNATURE);
|
||||||
|
attachment.id = db.attachment().insertAttachment(attachment);
|
||||||
|
|
||||||
byte[] bytes = (sign
|
byte[] bytes = (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())
|
||||||
? result.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE)
|
? encrypted.toByteArray()
|
||||||
: encrypted.toByteArray());
|
: result.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE));
|
||||||
|
|
||||||
File file = attachment.getFile(context);
|
File file = attachment.getFile(context);
|
||||||
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);
|
||||||
db.attachment().setDownloaded(attachment.id, (long) bytes.length);
|
db.attachment().setDownloaded(attachment.id, (long) bytes.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
} finally {
|
||||||
|
db.endTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) {
|
||||||
} finally {
|
// Encrypt message
|
||||||
db.endTransaction();
|
Intent intent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
|
||||||
}
|
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1));
|
||||||
|
return intent;
|
||||||
if (sign)
|
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
|
||||||
return null; // send message
|
|
||||||
else {
|
|
||||||
// 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, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1));
|
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1));
|
||||||
return intent;
|
return intent;
|
||||||
|
} else {
|
||||||
|
// send message
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
|
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
|
||||||
|
if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction()))
|
||||||
|
args.putInt("request", ActivityCompose.REQUEST_KEY);
|
||||||
|
else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()))
|
||||||
|
args.putInt("request", ActivityCompose.REQUEST_ENCRYPT);
|
||||||
|
else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction()))
|
||||||
|
args.putInt("request", ActivityCompose.REQUEST_SIGN);
|
||||||
return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
|
return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
|
||||||
|
|
||||||
case OpenPgpApi.RESULT_CODE_ERROR:
|
case OpenPgpApi.RESULT_CODE_ERROR:
|
||||||
|
@ -1243,14 +1256,13 @@ public class FragmentCompose extends FragmentBase {
|
||||||
onAction(R.id.action_send);
|
onAction(R.id.action_send);
|
||||||
else if (result instanceof Intent) {
|
else if (result instanceof Intent) {
|
||||||
Intent data = (Intent) result;
|
Intent data = (Intent) result;
|
||||||
encrypt(data, true);
|
encrypt(data);
|
||||||
} else if (result instanceof PendingIntent)
|
} else if (result instanceof PendingIntent)
|
||||||
try {
|
try {
|
||||||
boolean sign = args.getBoolean("sign");
|
|
||||||
PendingIntent pi = (PendingIntent) result;
|
PendingIntent pi = (PendingIntent) result;
|
||||||
startIntentSenderForResult(
|
startIntentSenderForResult(
|
||||||
pi.getIntentSender(),
|
pi.getIntentSender(),
|
||||||
sign ? ActivityCompose.REQUEST_SIGN : ActivityCompose.REQUEST_ENCRYPT,
|
args.getInt("request"),
|
||||||
null, 0, 0, 0, null);
|
null, 0, 0, 0, null);
|
||||||
} catch (IntentSender.SendIntentException ex) {
|
} catch (IntentSender.SendIntentException ex) {
|
||||||
Log.e(ex);
|
Log.e(ex);
|
||||||
|
@ -1293,20 +1305,24 @@ public class FragmentCompose extends FragmentBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (requestCode == ActivityCompose.REQUEST_ENCRYPT) {
|
} else if (requestCode == ActivityCompose.REQUEST_KEY) {
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
Intent intent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
|
if (BuildConfig.BETA_RELEASE)
|
||||||
if (data.hasExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID))
|
Log.logExtras(data);
|
||||||
|
if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) {
|
||||||
|
Intent intent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
|
||||||
|
intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS));
|
||||||
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1));
|
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1));
|
||||||
intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
|
intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
|
||||||
encrypt(intent, false);
|
encrypt(intent);
|
||||||
|
} else
|
||||||
|
encrypt(data);
|
||||||
}
|
}
|
||||||
} else if (requestCode == ActivityCompose.REQUEST_SIGN) {
|
} else if (requestCode == ActivityCompose.REQUEST_ENCRYPT || requestCode == ActivityCompose.REQUEST_SIGN) {
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
Intent intent = new Intent(OpenPgpApi.ACTION_DETACHED_SIGN);
|
if (BuildConfig.BETA_RELEASE)
|
||||||
if (data.hasExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID))
|
Log.logExtras(data);
|
||||||
intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1));
|
encrypt(data);
|
||||||
encrypt(intent, true);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (data != null)
|
if (data != null)
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class Log {
|
||||||
stringBuilder.append(key)
|
stringBuilder.append(key)
|
||||||
.append("=")
|
.append("=")
|
||||||
.append(value)
|
.append(value)
|
||||||
.append(value == null ? "" : " (" + value.getClass().getSimpleName() + ")")
|
.append(value == null ? "" : " (" + data.get(key).getClass().getSimpleName() + ")")
|
||||||
.append("\r\n");
|
.append("\r\n");
|
||||||
}
|
}
|
||||||
i(stringBuilder.toString());
|
i(stringBuilder.toString());
|
||||||
|
|
Loading…
Reference in New Issue