summaryrefslogtreecommitdiff
path: root/crypto-pgp/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-10-23 17:02:50 +0530
committerGitHub <noreply@github.com>2021-10-23 17:02:50 +0530
commitaac74ae4515aa1d746f46287029441f5a945c98e (patch)
tree9d23e06592ecd884d6b58dd089692d9e4224a3f9 /crypto-pgp/src/main
parent21c8653e6815ca34574e783a5ce7ac783b188228 (diff)
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 <adityawasan55@gmail.com> (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 <adityawasan55@gmail.com>
Diffstat (limited to 'crypto-pgp/src/main')
-rw-r--r--crypto-pgp/src/main/AndroidManifest.xml6
-rw-r--r--crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyManager.kt95
-rw-r--r--crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyPair.kt28
-rw-r--r--crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt49
4 files changed, 0 insertions, 178 deletions
diff --git a/crypto-pgp/src/main/AndroidManifest.xml b/crypto-pgp/src/main/AndroidManifest.xml
deleted file mode 100644
index f72b702d..00000000
--- a/crypto-pgp/src/main/AndroidManifest.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?><!--
- ~ Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
- ~ SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception
- -->
-
-<manifest package="dev.msfjarvis.aps.cryptopgp"></manifest>
diff --git a/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyManager.kt b/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyManager.kt
deleted file mode 100644
index 478d2700..00000000
--- a/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyManager.kt
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
- * SPDX-License-Identifier: GPL-3.0-only
- */
-
-package dev.msfjarvis.aps.data.crypto
-
-import androidx.annotation.VisibleForTesting
-import com.github.michaelbull.result.Result
-import com.github.michaelbull.result.runCatching
-import com.proton.Gopenpgp.crypto.Crypto
-import java.io.File
-import kotlinx.coroutines.CoroutineDispatcher
-import kotlinx.coroutines.withContext
-
-public class GPGKeyManager(filesDir: String, private val dispatcher: CoroutineDispatcher) :
- KeyManager<GPGKeyPair> {
-
- private val keyDir = File(filesDir, KEY_DIR_NAME)
-
- override suspend fun addKey(key: GPGKeyPair, replace: Boolean): Result<GPGKeyPair, Throwable> =
- withContext(dispatcher) {
- runCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
- val keyFile = File(keyDir, "${key.getKeyId()}.$KEY_EXTENSION")
- if (keyFile.exists()) {
- // Check for replace flag first and if it is false, throw an error
- if (!replace) throw KeyManagerException.KeyAlreadyExistsException(key.getKeyId())
- if (!keyFile.delete()) throw KeyManagerException.KeyDeletionFailedException
- }
-
- keyFile.writeBytes(key.getPrivateKey())
-
- key
- }
- }
-
- override suspend fun removeKey(key: GPGKeyPair): Result<GPGKeyPair, Throwable> =
- withContext(dispatcher) {
- runCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
- val keyFile = File(keyDir, "${key.getKeyId()}.$KEY_EXTENSION")
- if (keyFile.exists()) {
- if (!keyFile.delete()) throw KeyManagerException.KeyDeletionFailedException
- }
-
- key
- }
- }
-
- override suspend fun getKeyById(id: String): Result<GPGKeyPair, Throwable> =
- withContext(dispatcher) {
- runCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
- val keys = keyDir.listFiles()
- if (keys.isNullOrEmpty()) throw KeyManagerException.NoKeysAvailableException
-
- for (keyFile in keys) {
- val keyPair = GPGKeyPair(Crypto.newKeyFromArmored(keyFile.readText()))
- if (keyPair.getKeyId() == id) return@runCatching keyPair
- }
-
- throw KeyManagerException.KeyNotFoundException(id)
- }
- }
-
- override suspend fun getAllKeys(): Result<List<GPGKeyPair>, Throwable> =
- withContext(dispatcher) {
- runCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
- val keys = keyDir.listFiles()
- if (keys.isNullOrEmpty()) return@runCatching listOf()
-
- keys.map { GPGKeyPair(Crypto.newKeyFromArmored(it.readText())) }.toList()
- }
- }
-
- override fun canHandle(fileName: String): Boolean {
- // TODO: This is a temp hack for now and in future it should check that the GPGKeyManager can
- // decrypt the file
- return fileName.endsWith(KEY_EXTENSION)
- }
-
- private fun keyDirExists(): Boolean {
- return keyDir.exists() || keyDir.mkdirs()
- }
-
- internal companion object {
-
- @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
- internal const val KEY_DIR_NAME: String = "keys"
- @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
- internal const val KEY_EXTENSION: String = "key"
- }
-}
diff --git a/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyPair.kt b/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyPair.kt
deleted file mode 100644
index 2dbe8689..00000000
--- a/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GPGKeyPair.kt
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
- * SPDX-License-Identifier: GPL-3.0-only
- */
-
-package dev.msfjarvis.aps.data.crypto
-
-import com.proton.Gopenpgp.crypto.Key
-
-/** Wraps a Gopenpgp [Key] to implement [KeyPair]. */
-public class GPGKeyPair(private val key: Key) : KeyPair {
-
- init {
- if (!key.isPrivate) throw KeyPairException.PrivateKeyUnavailableException
- }
-
- override fun getPrivateKey(): ByteArray {
- return key.armor().encodeToByteArray()
- }
-
- override fun getPublicKey(): ByteArray {
- return key.armoredPublicKey.encodeToByteArray()
- }
-
- override fun getKeyId(): String {
- return key.hexKeyID
- }
-}
diff --git a/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt b/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt
deleted file mode 100644
index 5d14b160..00000000
--- a/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
- * SPDX-License-Identifier: GPL-3.0-only
- */
-
-package dev.msfjarvis.aps.data.crypto
-
-import com.proton.Gopenpgp.crypto.Crypto
-import com.proton.Gopenpgp.helper.Helper
-import javax.inject.Inject
-
-/** Gopenpgp backed implementation of [CryptoHandler]. */
-public class GopenpgpCryptoHandler @Inject constructor() : CryptoHandler {
-
- /**
- * Decrypt the given [ciphertext] using the given PGP [privateKey] and corresponding [passphrase].
- */
- override fun decrypt(
- privateKey: String,
- passphrase: ByteArray,
- ciphertext: ByteArray,
- ): ByteArray {
- // Decode the incoming cipher into a string and try to guess if it's armored.
- val cipherString = ciphertext.decodeToString()
- val isArmor = cipherString.startsWith("-----BEGIN PGP MESSAGE-----")
- val message =
- if (isArmor) {
- Crypto.newPGPMessageFromArmored(cipherString)
- } else {
- Crypto.newPGPMessage(ciphertext)
- }
- return Helper.decryptBinaryMessageArmored(
- privateKey,
- passphrase,
- message.armored,
- )
- }
-
- override fun encrypt(publicKey: String, plaintext: ByteArray): ByteArray {
- return Helper.encryptBinaryMessage(
- publicKey,
- plaintext,
- )
- }
-
- override fun canHandle(fileName: String): Boolean {
- return fileName.split('.').last() == "gpg"
- }
-}