use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.
the class PgpMessageBuilder method createOpenPgpDataSourceFromBodyPart.
@NonNull
private OpenPgpDataSource createOpenPgpDataSourceFromBodyPart(final MimeBodyPart bodyPart, final boolean writeBodyContentOnly) throws MessagingException {
return new OpenPgpDataSource() {
@Override
public void writeTo(OutputStream os) throws IOException {
try {
if (writeBodyContentOnly) {
Body body = bodyPart.getBody();
InputStream inputStream = body.getInputStream();
IOUtils.copy(inputStream, os);
} else {
bodyPart.writeTo(os);
}
} catch (MessagingException e) {
throw new IOException(e);
}
}
};
}
use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource 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");
}
use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.
the class MessageCryptoHelper method callAsyncDecrypt.
private void callAsyncDecrypt(Intent intent) throws IOException {
OpenPgpDataSource dataSource = getDataSourceForEncryptedOrInlineData();
OpenPgpDataSink<MimeBodyPart> openPgpDataSink = getDataSinkForDecryptedData();
cancelableBackgroundOperation = openPgpApi.executeApiAsync(intent, dataSource, openPgpDataSink, new IOpenPgpSinkResultCallback<MimeBodyPart>() {
@Override
public void onReturn(Intent result, MimeBodyPart decryptedPart) {
cancelableBackgroundOperation = null;
currentCryptoResult = result;
onCryptoOperationReturned(decryptedPart);
}
@Override
public void onProgress(int current, int max) {
Timber.d("received progress status: %d / %d", current, max);
callbackProgress(current, max);
}
});
}
use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.
the class MessageCryptoHelper method callAsyncInlineOperation.
private void callAsyncInlineOperation(Intent intent) throws IOException {
OpenPgpDataSource dataSource = getDataSourceForEncryptedOrInlineData();
OpenPgpDataSink<MimeBodyPart> dataSink = getDataSinkForDecryptedInlineData();
cancelableBackgroundOperation = openPgpApi.executeApiAsync(intent, dataSource, dataSink, new IOpenPgpSinkResultCallback<MimeBodyPart>() {
@Override
public void onProgress(int current, int max) {
Timber.d("received progress status: %d / %d", current, max);
callbackProgress(current, max);
}
@Override
public void onReturn(Intent result, MimeBodyPart bodyPart) {
cancelableBackgroundOperation = null;
currentCryptoResult = result;
onCryptoOperationReturned(bodyPart);
}
});
}
use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.
the class MessageCryptoHelper method callAsyncDetachedVerify.
private void callAsyncDetachedVerify(Intent intent) throws IOException, MessagingException {
OpenPgpDataSource dataSource = getDataSourceForSignedData(currentCryptoPart.part);
byte[] signatureData = MessageCryptoStructureDetector.getSignatureData(currentCryptoPart.part);
intent.putExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE, signatureData);
openPgpApi.executeApiAsync(intent, dataSource, new IOpenPgpSinkResultCallback<Void>() {
@Override
public void onReturn(Intent result, Void dummy) {
cancelableBackgroundOperation = null;
currentCryptoResult = result;
onCryptoOperationReturned(null);
}
@Override
public void onProgress(int current, int max) {
Timber.d("received progress status: %d / %d", current, max);
callbackProgress(current, max);
}
});
}
Aggregations