-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed lookup public keys and re-verify signature if missing pubkey.| #74
- Loading branch information
Showing
10 changed files
with
317 additions
and
177 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...n/java/com/flowcrypt/email/extensions/com/flowcrypt/email/util/DiskLruCacheSnapshotExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected] | ||
* Contributors: denbond7 | ||
*/ | ||
|
||
package com.flowcrypt.email.extensions.com.flowcrypt.email.util | ||
|
||
import android.content.Context | ||
import com.flowcrypt.email.api.retrofit.response.base.Result | ||
import com.flowcrypt.email.api.retrofit.response.model.MsgBlock | ||
import com.flowcrypt.email.database.entity.AccountEntity | ||
import com.flowcrypt.email.extensions.kotlin.processing | ||
import com.flowcrypt.email.security.pgp.PgpDecryptAndOrVerify | ||
import com.flowcrypt.email.security.pgp.PgpMsg | ||
import com.flowcrypt.email.util.cache.DiskLruCache | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.pgpainless.PGPainless | ||
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector | ||
import org.pgpainless.util.Passphrase | ||
|
||
/** | ||
* @author Denys Bondarenko | ||
*/ | ||
suspend fun DiskLruCache.Snapshot.processing( | ||
context: Context, | ||
accountEntity: AccountEntity, | ||
skipAttachmentsRawData: Boolean = false, | ||
preResultAction: suspend (blocks: List<MsgBlock>) -> Unit = {} | ||
): Result<PgpMsg.ProcessedMimeMessageResult?> = withContext(Dispatchers.IO) { | ||
val uri = getUri(0) | ||
if (uri != null) { | ||
try { | ||
val inputStream = | ||
context.contentResolver.openInputStream(uri) ?: throw java.lang.IllegalStateException() | ||
|
||
val keys = PGPainless.readKeyRing() | ||
.secretKeyRingCollection(accountEntity.servicePgpPrivateKey) | ||
|
||
val decryptionStream = PgpDecryptAndOrVerify.genDecryptionStream( | ||
srcInputStream = inputStream, | ||
secretKeys = keys, | ||
protector = PasswordBasedSecretKeyRingProtector.forKey( | ||
keys.first(), | ||
Passphrase.fromPassword(accountEntity.servicePgpPassphrase) | ||
) | ||
) | ||
|
||
val processedMimeMessage = PgpMsg.processMimeMessage( | ||
context = context, | ||
inputStream = decryptionStream, | ||
skipAttachmentsRawData = skipAttachmentsRawData | ||
) | ||
|
||
preResultAction.invoke(processedMimeMessage.blocks) | ||
return@withContext Result.success(processedMimeMessage) | ||
} catch (e: Exception) { | ||
return@withContext Result.exception(e) | ||
} | ||
} else { | ||
return@withContext getByteArray(0).processing( | ||
context = context, | ||
skipAttachmentsRawData = skipAttachmentsRawData | ||
) { blocks -> | ||
preResultAction.invoke(blocks) | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
FlowCrypt/src/main/java/com/flowcrypt/email/extensions/kotlin/ByteArrayExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected] | ||
* Contributors: denbond7 | ||
*/ | ||
|
||
package com.flowcrypt.email.extensions.kotlin | ||
|
||
import android.content.Context | ||
import com.flowcrypt.email.api.retrofit.response.base.Result | ||
import com.flowcrypt.email.api.retrofit.response.model.MsgBlock | ||
import com.flowcrypt.email.security.pgp.PgpMsg | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* @author Denys Bondarenko | ||
*/ | ||
suspend fun ByteArray.processing( | ||
context: Context, | ||
skipAttachmentsRawData: Boolean = false, | ||
preResultAction: suspend (blocks: List<MsgBlock>) -> Unit = {} | ||
): Result<PgpMsg.ProcessedMimeMessageResult?> = withContext(Dispatchers.IO) { | ||
return@withContext if (isEmpty()) { | ||
Result.exception(throwable = IllegalArgumentException("empty byte array")) | ||
} else { | ||
try { | ||
val processedMimeMessageResult = PgpMsg.processMimeMessage( | ||
context = context, | ||
inputStream = inputStream(), | ||
skipAttachmentsRawData = skipAttachmentsRawData | ||
) | ||
preResultAction.invoke(processedMimeMessageResult.blocks) | ||
return@withContext Result.success(processedMimeMessageResult) | ||
} catch (e: Exception) { | ||
return@withContext Result.exception(throwable = e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.