@ -1104,7 +1104,7 @@ public class FragmentCompose extends FragmentBase {
data . putExtra ( OpenPgpApi . EXTRA_USER_IDS , tos ) ;
data . putExtra ( OpenPgpApi . EXTRA_USER_IDS , tos ) ;
data . putExtra ( OpenPgpApi . EXTRA_REQUEST_ASCII_ARMOR , true ) ;
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,26 +1129,32 @@ public class FragmentCompose extends FragmentBase {
}
}
}
}
private void encrypt ( Intent data ) {
private void encrypt ( Intent data , boolean sign ) {
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 < PendingInten t> ( ) {
new SimpleTask < Objec t> ( ) {
@Override
@Override
protected PendingInten t onExecute ( Context context , Bundle args ) throws Throwable {
protected Objec t onExecute ( Context context , Bundle args ) throws Throwable {
// 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 ) ;
// Get at tachments
// Get d ata
EntityMessage message = db . message ( ) . getMessage ( id ) ;
EntityMessage message = db . message ( ) . getMessage ( id ) ;
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 )
db . attachment ( ) . deleteAttachment ( attachment . id ) ;
attachments . remove ( attachment ) ;
attachments . remove ( attachment ) ;
}
EntityIdentity identity =
EntityIdentity identity =
( message . identity = = null ? null : db . identity ( ) . getIdentity ( message . identity ) ) ;
( message . identity = = null ? null : db . identity ( ) . getIdentity ( message . identity ) ) ;
@ -1157,73 +1163,52 @@ public class FragmentCompose extends FragmentBase {
Properties props = MessageHelper . getSessionProperties ( Helper . AUTH_TYPE_PASSWORD , null , false ) ;
Properties props = MessageHelper . getSessionProperties ( Helper . AUTH_TYPE_PASSWORD , null , false ) ;
Session isession = Session . getInstance ( props , null ) ;
Session isession = Session . getInstance ( props , null ) ;
MimeMessage imessage = new MimeMessage ( isession ) ;
MimeMessage imessage = new MimeMessage ( isession ) ;
MessageHelper . build ( context , message , identity, imessage ) ;
MessageHelper . build ( context , message , attachments, identity, imessage ) ;
// Serialize message
// Serialize message
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 = new ByteArrayOutputStream ( ) ;
ByteArrayOutputStream encrypted = ( sign ? null : new ByteArrayOutputStream ( ) ) ;
if ( BuildConfig . BETA_RELEASE ) {
Log . i ( ( sign ? "Sign " : "Encrypt " ) + data ) ;
Log . logExtras ( data ) ;
}
// Encrypt message
// Encrypt message
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 ) ;
switch ( result . getIntExtra ( OpenPgpApi . RESULT_CODE , OpenPgpApi . RESULT_CODE_ERROR ) ) {
case OpenPgpApi . RESULT_CODE_SUCCESS :
if ( BuildConfig . BETA_RELEASE ) {
// Get public signature
Log . i ( ( sign ? "Signed " : "Encrypted " ) + result ) ;
Intent keyRequest = new Intent ( ) ;
Log . logExtras ( result ) ;
keyRequest . setAction ( OpenPgpApi . ACTION_DETACHED_SIGN ) ;
keyRequest . putExtra ( OpenPgpApi . EXTRA_SIGN_KEY_ID , data . getLongExtra ( OpenPgpApi . EXTRA_SIGN_KEY_ID , - 1 ) ) ;
Intent key = api . executeApi ( keyRequest , decrypted , null ) ;
int r = key . getIntExtra ( OpenPgpApi . RESULT_CODE , OpenPgpApi . RESULT_CODE_ERROR ) ;
if ( r ! = OpenPgpApi . RESULT_CODE_SUCCESS ) {
OpenPgpError error = key . getParcelableExtra ( OpenPgpApi . RESULT_ERROR ) ;
throw new IllegalArgumentException ( error . getMessage ( ) ) ;
}
}
// Attach encrypted data
int resultCode = result . getIntExtra ( OpenPgpApi . RESULT_CODE , OpenPgpApi . RESULT_CODE_ERROR ) ;
switch ( resultCode ) {
case OpenPgpApi . RESULT_CODE_SUCCESS :
// Attach encrypted data / signature
try {
try {
db . beginTransaction ( ) ;
db . beginTransaction ( ) ;
// Delete previously encrypted data
EntityAttachment attachment = new EntityAttachment ( ) ;
for ( EntityAttachment attachment : db . attachment ( ) . getAttachments ( id ) )
attachment . message = id ;
if ( attachment . encryption ! = null )
attachment . sequence = db . attachment ( ) . getAttachmentSequence ( id ) + 1 ;
db . attachment ( ) . deleteAttachment ( attachment . id ) ;
attachment . name = ( sign ? "signature.asc" : "encrypted.asc" ) ;
attachment . type = "application/octet-stream" ;
int seq = db . attachment ( ) . getAttachmentSequence ( id ) ;
attachment . encryption = ( sign ? EntityAttachment . PGP_SIGNATURE : EntityAttachment . PGP_MESSAGE ) ;
attachment . id = db . attachment ( ) . insertAttachment ( attachment ) ;
EntityAttachment attachment1 = new EntityAttachment ( ) ;
attachment1 . message = id ;
attachment1 . sequence = seq + 1 ;
attachment1 . name = "encrypted.asc" ;
attachment1 . type = "application/octet-stream" ;
attachment1 . encryption = EntityAttachment . PGP_MESSAGE ;
attachment1 . id = db . attachment ( ) . insertAttachment ( attachment1 ) ;
File file1 = attachment1 . getFile ( context ) ;
byte [ ] bytes1 = encrypted . toByteArray ( ) ;
try ( OutputStream os1 = new BufferedOutputStream ( new FileOutputStream ( file1 ) ) ) {
os1 . write ( bytes1 ) ;
db . attachment ( ) . setDownloaded ( attachment1 . id , ( long ) bytes1 . length ) ;
}
EntityAttachment attachment2 = new EntityAttachment ( ) ;
attachment2 . message = id ;
attachment2 . sequence = seq + 2 ;
attachment2 . name = "signature.asc" ;
attachment2 . type = "application/octet-stream" ;
attachment2 . encryption = EntityAttachment . PGP_SIGNATURE ;
attachment2 . id = db . attachment ( ) . insertAttachment ( attachment2 ) ;
File file2 = attachment2 . getFile ( context ) ;
byte [ ] bytes 2 = key . getByteArrayExtra ( OpenPgpApi . RESULT_DETACHED_SIGNATURE ) ;
byte [ ] bytes = ( sign
try ( OutputStream os2 = new BufferedOutputStream ( new FileOutputStream ( file2 ) ) ) {
? result . getByteArrayExtra ( OpenPgpApi . RESULT_DETACHED_SIGNATURE )
os2 . write ( bytes2 ) ;
: encrypted . toByteArray ( ) ) ;
db . attachment ( ) . setDownloaded ( attachment2 . id , ( long ) bytes2 . length ) ;
File file = attachment . getFile ( context ) ;
Log . i ( "Writing " + file + " size=" + bytes . length ) ;
try ( OutputStream out = new BufferedOutputStream ( new FileOutputStream ( file ) ) ) {
out . write ( bytes ) ;
db . attachment ( ) . setDownloaded ( attachment . id , ( long ) bytes . length ) ;
}
}
db . setTransactionSuccessful ( ) ;
db . setTransactionSuccessful ( ) ;
@ -1231,7 +1216,15 @@ public class FragmentCompose extends FragmentBase {
db . endTransaction ( ) ;
db . endTransaction ( ) ;
}
}
break ;
if ( sign )
return null ; // send message
else {
// Sign message
Intent signRequest = new Intent ( ) ;
signRequest . setAction ( OpenPgpApi . ACTION_DETACHED_SIGN ) ;
signRequest . putExtra ( OpenPgpApi . EXTRA_SIGN_KEY_ID , data . getLongExtra ( OpenPgpApi . EXTRA_SIGN_KEY_ID , - 1 ) ) ;
return signRequest ;
}
case OpenPgpApi . RESULT_CODE_USER_INTERACTION_REQUIRED :
case OpenPgpApi . RESULT_CODE_USER_INTERACTION_REQUIRED :
return result . getParcelableExtra ( OpenPgpApi . RESULT_INTENT ) ;
return result . getParcelableExtra ( OpenPgpApi . RESULT_INTENT ) ;
@ -1239,20 +1232,26 @@ public class FragmentCompose extends FragmentBase {
case OpenPgpApi . RESULT_CODE_ERROR :
case OpenPgpApi . RESULT_CODE_ERROR :
OpenPgpError error = result . getParcelableExtra ( OpenPgpApi . RESULT_ERROR ) ;
OpenPgpError error = result . getParcelableExtra ( OpenPgpApi . RESULT_ERROR ) ;
throw new IllegalArgumentException ( error . getMessage ( ) ) ;
throw new IllegalArgumentException ( error . getMessage ( ) ) ;
}
return null ;
default :
throw new IllegalArgumentException ( "Unknown result code=" + resultCode ) ;
}
}
}
@Override
@Override
protected void onExecuted ( Bundle args , PendingIntent pi ) {
protected void onExecuted ( Bundle args , Object result ) {
if ( pi = = null )
if ( result = = null )
onAction ( R . id . action_send ) ;
onAction ( R . id . action_send ) ;
else
else if ( result instanceof Intent ) {
Intent data = ( Intent ) result ;
encrypt ( data , true ) ;
} else if ( result instanceof PendingIntent )
try {
try {
boolean sign = args . getBoolean ( "sign" ) ;
PendingIntent pi = ( PendingIntent ) result ;
startIntentSenderForResult (
startIntentSenderForResult (
pi . getIntentSender ( ) ,
pi . getIntentSender ( ) ,
ActivityCompose. REQUEST_ENCRYPT ,
sign ? ActivityCompose . REQUEST_SIGN : ActivityCompose. REQUEST_ENCRYPT ,
null , 0 , 0 , 0 , null ) ;
null , 0 , 0 , 0 , null ) ;
} catch ( IntentSender . SendIntentException ex ) {
} catch ( IntentSender . SendIntentException ex ) {
Log . e ( ex ) ;
Log . e ( ex ) ;
@ -1298,7 +1297,12 @@ public class FragmentCompose extends FragmentBase {
} else if ( requestCode = = ActivityCompose . REQUEST_ENCRYPT ) {
} else if ( requestCode = = ActivityCompose . REQUEST_ENCRYPT ) {
if ( data ! = null ) {
if ( data ! = null ) {
data . setAction ( OpenPgpApi . ACTION_SIGN_AND_ENCRYPT ) ;
data . setAction ( OpenPgpApi . ACTION_SIGN_AND_ENCRYPT ) ;
encrypt ( data ) ;
encrypt ( data , false ) ;
}
} else if ( requestCode = = ActivityCompose . REQUEST_SIGN ) {
if ( data ! = null ) {
data . setAction ( OpenPgpApi . ACTION_DETACHED_SIGN ) ;
encrypt ( data , true ) ;
}
}
} else {
} else {
if ( data ! = null )
if ( data ! = null )