From aac74ae4515aa1d746f46287029441f5a945c98e Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 23 Oct 2021 17:02:50 +0530 Subject: Switch new PGP backend to use PGPainless (#1522) * crypto-pgpainless: init * crypto-pgpainless: add an opinionated CryptoHandler impl * app: migrate to crypto-pgpainless * crypto-pgp: remove * github: remove now unused instrumentation tests job * crypto-common: fixup package names * wip(crypto-pgpainless): add `PGPKeyPair` and `PGPKeyManager` Signed-off-by: Aditya Wasan (cherry picked from commit 02d07e9e797a8600cc8c534a731dfffcc44cfdde) * crypto-pgpainless: use hex-encoded key IDs * crypto-pgpainless: replace legacy Gopenpgp-generated key file * crypto-pgpainless: fix CryptoConstants source set * crypto-pgpainless: fix tests * crypto-pgpainless: reinstate PGPKeyManager tests Co-authored-by: Aditya Wasan --- .../msfjarvis/aps/injection/crypto/CryptoHandlerModule.kt | 6 +++--- .../msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt | 10 +++++++--- .../java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt | 10 +++++++--- .../msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt | 13 +++++++++++-- 4 files changed, 28 insertions(+), 11 deletions(-) (limited to 'app/src/main/java/dev/msfjarvis') diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/crypto/CryptoHandlerModule.kt b/app/src/main/java/dev/msfjarvis/aps/injection/crypto/CryptoHandlerModule.kt index fdd37bf3..63a860d1 100644 --- a/app/src/main/java/dev/msfjarvis/aps/injection/crypto/CryptoHandlerModule.kt +++ b/app/src/main/java/dev/msfjarvis/aps/injection/crypto/CryptoHandlerModule.kt @@ -10,8 +10,8 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import dagger.multibindings.IntoSet -import dev.msfjarvis.aps.data.crypto.CryptoHandler -import dev.msfjarvis.aps.data.crypto.GopenpgpCryptoHandler +import dev.msfjarvis.aps.crypto.CryptoHandler +import dev.msfjarvis.aps.crypto.PGPainlessCryptoHandler /** * This module adds all [CryptoHandler] implementations into a Set which makes it easier to build @@ -23,7 +23,7 @@ object CryptoHandlerModule { @Provides @IntoSet fun providePgpCryptoHandler(): CryptoHandler { - return GopenpgpCryptoHandler() + return PGPainlessCryptoHandler() } } diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt b/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt index bb2e6492..5351c5d2 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt @@ -28,6 +28,7 @@ import dev.msfjarvis.aps.util.autofill.AutofillPreferences import dev.msfjarvis.aps.util.autofill.AutofillResponseBuilder import dev.msfjarvis.aps.util.autofill.DirectoryStructure import dev.msfjarvis.aps.util.extensions.asLog +import java.io.ByteArrayOutputStream import java.io.File import javax.inject.Inject import kotlinx.coroutines.Dispatchers @@ -130,11 +131,14 @@ class AutofillDecryptActivityV2 : AppCompatActivity() { runCatching { val crypto = cryptos.first { it.canHandle(file.absolutePath) } withContext(Dispatchers.IO) { + val outputStream = ByteArrayOutputStream() crypto.decrypt( DecryptActivityV2.PRIV_KEY, - DecryptActivityV2.PASS.toByteArray(charset = Charsets.UTF_8), - encryptedInput.readBytes() + DecryptActivityV2.PASS, + encryptedInput, + outputStream, ) + outputStream } } .onFailure { e -> @@ -143,7 +147,7 @@ class AutofillDecryptActivityV2 : AppCompatActivity() { } .onSuccess { result -> return runCatching { - val entry = passwordEntryFactory.create(lifecycleScope, result) + val entry = passwordEntryFactory.create(lifecycleScope, result.toByteArray()) AutofillPreferences.credentialsFromStoreEntry(this, file, entry, directoryStructure) } .getOrElse { e -> diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt index b000d21d..403b8191 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt @@ -20,6 +20,7 @@ import dev.msfjarvis.aps.injection.password.PasswordEntryFactory import dev.msfjarvis.aps.ui.adapters.FieldItemAdapter import dev.msfjarvis.aps.util.extensions.unsafeLazy import dev.msfjarvis.aps.util.extensions.viewBinding +import java.io.ByteArrayOutputStream import java.io.File import javax.inject.Inject import kotlin.time.Duration @@ -126,19 +127,22 @@ class DecryptActivityV2 : BasePgpActivity() { private fun decrypt() { lifecycleScope.launch { // TODO(msfjarvis): native methods are fallible, add error handling once out of testing - val message = withContext(Dispatchers.IO) { File(fullPath).readBytes() } + val message = withContext(Dispatchers.IO) { File(fullPath).inputStream() } val result = withContext(Dispatchers.IO) { val crypto = cryptos.first { it.canHandle(fullPath) } + val outputStream = ByteArrayOutputStream() crypto.decrypt( PRIV_KEY, - PASS.toByteArray(charset = Charsets.UTF_8), + PASS, message, + outputStream, ) + outputStream } startAutoDismissTimer() - val entry = passwordEntryFactory.create(lifecycleScope, result) + val entry = passwordEntryFactory.create(lifecycleScope, result.toByteArray()) passwordEntry = entry invalidateOptionsMenu() diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt index c2cc5b10..095ce53e 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt @@ -43,6 +43,7 @@ import dev.msfjarvis.aps.util.extensions.snackbar import dev.msfjarvis.aps.util.extensions.unsafeLazy import dev.msfjarvis.aps.util.extensions.viewBinding import dev.msfjarvis.aps.util.settings.PreferenceKeys +import java.io.ByteArrayOutputStream import java.io.File import java.io.IOException import javax.inject.Inject @@ -319,7 +320,15 @@ class PasswordCreationActivityV2 : BasePgpActivity() { runCatching { val crypto = cryptos.first { it.canHandle(path) } val result = - withContext(Dispatchers.IO) { crypto.encrypt(PUB_KEY, content.encodeToByteArray()) } + withContext(Dispatchers.IO) { + val outputStream = ByteArrayOutputStream() + crypto.encrypt( + listOf(PUB_KEY), + content.byteInputStream(), + outputStream, + ) + outputStream + } val file = File(path) // If we're not editing, this file should not already exist! // Additionally, if we were editing and the incoming and outgoing @@ -336,7 +345,7 @@ class PasswordCreationActivityV2 : BasePgpActivity() { return@runCatching } - withContext(Dispatchers.IO) { file.outputStream().use { it.write(result) } } + withContext(Dispatchers.IO) { file.writeBytes(result.toByteArray()) } // associate the new password name with the last name's timestamp in // history -- cgit v1.2.3