Search in sources :

Example 1 with OpenPgpError

use of org.openintents.openpgp.OpenPgpError in project k-9 by k9mail.

the class PgpMessageBuilderTest method buildOpportunisticEncrypt__withNoKeysAndNoSignOnly__shouldNotBeSigned.

@Test
public void buildOpportunisticEncrypt__withNoKeysAndNoSignOnly__shouldNotBeSigned() throws MessagingException {
    ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder.setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key"))).setCryptoMode(CryptoMode.OPPORTUNISTIC).build();
    pgpMessageBuilder.setCryptoStatus(cryptoStatus);
    Intent returnIntent = new Intent();
    returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
    returnIntent.putExtra(OpenPgpApi.RESULT_ERROR, new OpenPgpError(OpenPgpError.OPPORTUNISTIC_MISSING_KEYS, "Missing keys"));
    when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))).thenReturn(returnIntent);
    Callback mockCallback = mock(Callback.class);
    pgpMessageBuilder.buildAsync(mockCallback);
    ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class);
    verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false));
    verifyNoMoreInteractions(mockCallback);
    MimeMessage message = captor.getValue();
    Assert.assertEquals("text/plain", message.getMimeType());
}
Also used : Callback(com.fsck.k9.message.MessageBuilder.Callback) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ComposeCryptoStatus(com.fsck.k9.activity.compose.ComposeCryptoStatus) Recipient(com.fsck.k9.view.RecipientSelectView.Recipient) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) OpenPgpError(org.openintents.openpgp.OpenPgpError) Test(org.junit.Test)

Example 2 with OpenPgpError

use of org.openintents.openpgp.OpenPgpError in project Pix-Art-Messenger by kriztan.

the class PgpEngine method generateSignature.

public void generateSignature(Intent intent, final Account account, String status, final UiCallback<String> callback) {
    if (account.getPgpId() == 0) {
        return;
    }
    Intent params = intent == null ? new Intent() : intent;
    params.setAction(OpenPgpApi.ACTION_CLEARTEXT_SIGN);
    params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
    params.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, account.getPgpId());
    InputStream is = new ByteArrayInputStream(status.getBytes());
    final OutputStream os = new ByteArrayOutputStream();
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": signing status message \"" + status + "\"");
    api.executeApiAsync(params, is, os, result -> {
        switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
            case OpenPgpApi.RESULT_CODE_SUCCESS:
                StringBuilder signatureBuilder = new StringBuilder();
                try {
                    os.flush();
                    String[] lines = os.toString().split("\n");
                    boolean sig = false;
                    for (String line : lines) {
                        if (sig) {
                            if (line.contains("END PGP SIGNATURE")) {
                                sig = false;
                            } else {
                                if (!line.contains("Version")) {
                                    signatureBuilder.append(line.trim());
                                }
                            }
                        }
                        if (line.contains("BEGIN PGP SIGNATURE")) {
                            sig = true;
                        }
                    }
                } catch (IOException e) {
                    callback.error(R.string.openpgp_error, null);
                    return;
                }
                callback.success(signatureBuilder.toString());
                return;
            case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), status);
                return;
            case OpenPgpApi.RESULT_CODE_ERROR:
                OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
                if (error != null && "signing subkey not found!".equals(error.getMessage())) {
                    callback.error(0, null);
                } else {
                    logError(account, error);
                    callback.error(R.string.unable_to_connect_to_keychain, null);
                }
        }
    });
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OpenPgpError(org.openintents.openpgp.OpenPgpError)

Example 3 with OpenPgpError

use of org.openintents.openpgp.OpenPgpError in project andOTP by andOTP.

the class BackupActivity method handleOpenPGPResult.

public void handleOpenPGPResult(Intent result, ByteArrayOutputStream os, Uri file, int requestCode) {
    if (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR) == OpenPgpApi.RESULT_CODE_SUCCESS) {
        if (requestCode == Constants.INTENT_BACKUP_ENCRYPT_PGP) {
            if (os != null)
                doBackupEncrypted(file, outputStreamToString(os));
        } else if (requestCode == Constants.INTENT_BACKUP_DECRYPT_PGP) {
            if (os != null) {
                if (settings.getOpenPGPVerify()) {
                    OpenPgpSignatureResult sigResult = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
                    if (sigResult.getResult() == OpenPgpSignatureResult.RESULT_VALID_KEY_CONFIRMED) {
                        restoreEntries(outputStreamToString(os));
                    } else {
                        Toast.makeText(this, R.string.backup_toast_openpgp_not_verified, Toast.LENGTH_LONG).show();
                    }
                } else {
                    restoreEntries(outputStreamToString(os));
                }
            }
        }
    } else if (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR) == OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED) {
        PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
        // Small hack to keep the target file even after user interaction
        if (requestCode == Constants.INTENT_BACKUP_ENCRYPT_PGP) {
            encryptTargetFile = file;
        } else if (requestCode == Constants.INTENT_BACKUP_DECRYPT_PGP) {
            decryptSourceFile = file;
        }
        try {
            startIntentSenderForResult(pi.getIntentSender(), requestCode, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    } else if (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR) == OpenPgpApi.RESULT_CODE_ERROR) {
        OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
        Toast.makeText(this, String.format(getString(R.string.backup_toast_openpgp_error), error.getMessage()), Toast.LENGTH_LONG).show();
    }
}
Also used : PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError) OpenPgpSignatureResult(org.openintents.openpgp.OpenPgpSignatureResult)

Example 4 with OpenPgpError

use of org.openintents.openpgp.OpenPgpError in project k-9 by k9mail.

the class PgpMessageBuilder method launchOpenPgpApiIntent.

private PendingIntent launchOpenPgpApiIntent(@NonNull Intent openPgpIntent, MimeBodyPart bodyPart, boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
    OpenPgpDataSource dataSource = createOpenPgpDataSourceFromBodyPart(bodyPart, writeBodyContentOnly);
    BinaryTempFileBody pgpResultTempBody = null;
    OutputStream outputStream = null;
    if (captureOutputPart) {
        try {
            pgpResultTempBody = new BinaryTempFileBody(capturedOutputPartIs7Bit ? MimeUtil.ENC_7BIT : MimeUtil.ENC_8BIT);
            outputStream = pgpResultTempBody.getOutputStream();
            // OpenKeychain/BouncyCastle at this point use the system newline for formatting, which is LF on android.
            // we need this to be CRLF, so we convert the data after receiving.
            outputStream = new EOLConvertingOutputStream(outputStream);
        } catch (IOException e) {
            throw new MessagingException("could not allocate temp file for storage!", e);
        }
    }
    Intent result = openPgpApi.executeApi(openPgpIntent, dataSource, outputStream);
    switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
        case OpenPgpApi.RESULT_CODE_SUCCESS:
            mimeBuildMessage(result, bodyPart, pgpResultTempBody);
            return null;
        case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
            PendingIntent returnedPendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
            if (returnedPendingIntent == null) {
                throw new MessagingException("openpgp api needs user interaction, but returned no pendingintent!");
            }
            return returnedPendingIntent;
        case OpenPgpApi.RESULT_CODE_ERROR:
            OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
            if (error == null) {
                throw new MessagingException("internal openpgp api error");
            }
            /*
                boolean isOpportunisticError = error.getErrorId() == OpenPgpError.OPPORTUNISTIC_MISSING_KEYS;
                if (isOpportunisticError) {
                    if (!cryptoStatus.isEncryptionOpportunistic()) {
                        throw new IllegalStateException(
                                "Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
                    }
                    Timber.d("Skipping encryption due to opportunistic mode");
                    return null;
                }
                */
            throw new MessagingException(error.getMessage());
    }
    throw new IllegalStateException("unreachable code segment reached");
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) MessagingException(com.fsck.k9.mail.MessagingException) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError)

Example 5 with OpenPgpError

use of org.openintents.openpgp.OpenPgpError in project k-9 by k9mail.

the class OpenPgpApi method executeApi.

/**
 * InputStream and OutputStreams are always closed after operating on them!
 */
private Intent executeApi(Intent data, ParcelFileDescriptor input, int outputPipeId) {
    try {
        // always send version from client
        data.putExtra(EXTRA_API_VERSION, OpenPgpApi.API_VERSION);
        Intent result;
        // blocks until result is ready
        result = mService.execute(data, input, outputPipeId);
        // set class loader to current context to allow unparcelling
        // of OpenPgpError and OpenPgpSignatureResult
        // http://stackoverflow.com/a/3806769
        result.setExtrasClassLoader(mContext.getClassLoader());
        return result;
    } catch (Exception e) {
        Timber.e(e, "Exception in executeApi call");
        Intent result = new Intent();
        result.putExtra(RESULT_CODE, RESULT_CODE_ERROR);
        result.putExtra(RESULT_ERROR, new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage()));
        return result;
    } finally {
        // close() is required to halt the TransferThread
        closeLoudly(input);
    }
}
Also used : Intent(android.content.Intent) OpenPgpError(org.openintents.openpgp.OpenPgpError) IOException(java.io.IOException)

Aggregations

OpenPgpError (org.openintents.openpgp.OpenPgpError)16 Intent (android.content.Intent)13 IOException (java.io.IOException)11 PendingIntent (android.app.PendingIntent)10 OutputStream (java.io.OutputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 ParcelFileDescriptor (android.os.ParcelFileDescriptor)3 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)3 MessagingException (com.fsck.k9.mail.MessagingException)2 EOLConvertingOutputStream (com.fsck.k9.mail.filter.EOLConvertingOutputStream)2 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 ArrayList (java.util.ArrayList)2 DataSinkTransferThread (org.openintents.openpgp.util.ParcelFileDescriptorUtil.DataSinkTransferThread)2 DataSourceTransferThread (org.openintents.openpgp.util.ParcelFileDescriptorUtil.DataSourceTransferThread)2 View (android.view.View)1 ImageView (android.widget.ImageView)1