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 --- .../dev/msfjarvis/aps/crypto/CryptoException.kt | 19 +++++++++++ .../dev/msfjarvis/aps/crypto/CryptoHandler.kt | 37 ++++++++++++++++++++++ .../kotlin/dev/msfjarvis/aps/crypto/KeyManager.kt | 19 +++++++++++ .../kotlin/dev/msfjarvis/aps/crypto/KeyPair.kt | 14 ++++++++ .../msfjarvis/aps/data/crypto/CryptoException.kt | 19 ----------- .../dev/msfjarvis/aps/data/crypto/CryptoHandler.kt | 25 --------------- .../dev/msfjarvis/aps/data/crypto/KeyManager.kt | 19 ----------- .../dev/msfjarvis/aps/data/crypto/KeyPair.kt | 14 -------- 8 files changed, 89 insertions(+), 77 deletions(-) create mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoException.kt create mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt create mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyManager.kt create mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyPair.kt delete mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoException.kt delete mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoHandler.kt delete mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyManager.kt delete mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyPair.kt (limited to 'crypto-common/src/main') diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoException.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoException.kt new file mode 100644 index 00000000..34e64d5f --- /dev/null +++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoException.kt @@ -0,0 +1,19 @@ +package dev.msfjarvis.aps.crypto + +public sealed class CryptoException(message: String? = null) : Exception(message) + +public sealed class KeyPairException(message: String? = null) : CryptoException(message) { + public object PrivateKeyUnavailableException : + KeyPairException("Key object does not have a private sub-key") +} + +public sealed class KeyManagerException(message: String? = null) : CryptoException(message) { + public object NoKeysAvailableException : KeyManagerException("No keys were found") + public object KeyDirectoryUnavailableException : + KeyManagerException("Key directory does not exist") + public object KeyDeletionFailedException : KeyManagerException("Couldn't delete the key file") + public class KeyNotFoundException(keyId: String) : + KeyManagerException("No key found with id: $keyId") + public class KeyAlreadyExistsException(keyId: String) : + KeyManagerException("Pre-existing key was found for $keyId but 'replace' is set to false") +} diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt new file mode 100644 index 00000000..c64e9c9b --- /dev/null +++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/CryptoHandler.kt @@ -0,0 +1,37 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.crypto + +import java.io.InputStream +import java.io.OutputStream + +/** Generic interface to implement cryptographic operations on top of. */ +public interface CryptoHandler { + + /** + * Decrypt the given [ciphertextStream] using a [privateKey] and [password], and writes the + * resultant plaintext to [outputStream]. + */ + public fun decrypt( + privateKey: String, + password: String, + ciphertextStream: InputStream, + outputStream: OutputStream, + ) + + /** + * Encrypt the given [plaintextStream] to the provided [pubKeys], and writes the encrypted + * ciphertext to [outputStream]. + */ + public fun encrypt( + pubKeys: List, + plaintextStream: InputStream, + outputStream: OutputStream, + ) + + /** Given a [fileName], return whether this instance can handle it. */ + public fun canHandle(fileName: String): Boolean +} diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyManager.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyManager.kt new file mode 100644 index 00000000..2f901354 --- /dev/null +++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyManager.kt @@ -0,0 +1,19 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.crypto + +import com.github.michaelbull.result.Result + +public interface KeyManager { + + public suspend fun addKey(key: T, replace: Boolean = false): Result + public suspend fun removeKey(key: T): Result + public suspend fun getKeyById(id: String): Result + public suspend fun getAllKeys(): Result, Throwable> + + /** Given a [fileName], return whether this instance can handle it. */ + public fun canHandle(fileName: String): Boolean +} diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyPair.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyPair.kt new file mode 100644 index 00000000..b8dec216 --- /dev/null +++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/crypto/KeyPair.kt @@ -0,0 +1,14 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.crypto + +/** Defines expectations for a keypair used in public key cryptography. */ +public interface KeyPair { + + public fun getPrivateKey(): ByteArray + public fun getPublicKey(): ByteArray + public fun getKeyId(): String +} diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoException.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoException.kt deleted file mode 100644 index 6a73d381..00000000 --- a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoException.kt +++ /dev/null @@ -1,19 +0,0 @@ -package dev.msfjarvis.aps.data.crypto - -public sealed class CryptoException(message: String? = null) : Exception(message) - -public sealed class KeyPairException(message: String? = null) : CryptoException(message) { - public object PrivateKeyUnavailableException : - KeyPairException("Key object does not have a private sub-key") -} - -public sealed class KeyManagerException(message: String? = null) : CryptoException(message) { - public object NoKeysAvailableException : KeyManagerException("No keys were found") - public object KeyDirectoryUnavailableException : - KeyManagerException("Key directory does not exist") - public object KeyDeletionFailedException : KeyManagerException("Couldn't delete the key file") - public class KeyNotFoundException(keyId: String) : - KeyManagerException("No key found with id: $keyId") - public class KeyAlreadyExistsException(keyId: String) : - KeyManagerException("Pre-existing key was found for $keyId but 'replace' is set to false") -} diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoHandler.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoHandler.kt deleted file mode 100644 index 453613a4..00000000 --- a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoHandler.kt +++ /dev/null @@ -1,25 +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 - -/** Generic interface to implement cryptographic operations on top of. */ -public interface CryptoHandler { - - /** - * Decrypt the given [ciphertext] using a [privateKey] and [passphrase], returning a [ByteArray] - * corresponding to the decrypted plaintext. - */ - public fun decrypt(privateKey: String, passphrase: ByteArray, ciphertext: ByteArray): ByteArray - - /** - * Encrypt the given [plaintext] to the provided [publicKey], returning the encrypted ciphertext - * as a [ByteArray] - */ - public fun encrypt(publicKey: String, plaintext: ByteArray): ByteArray - - /** Given a [fileName], return whether this instance can handle it. */ - public fun canHandle(fileName: String): Boolean -} diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyManager.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyManager.kt deleted file mode 100644 index b5ba881e..00000000 --- a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyManager.kt +++ /dev/null @@ -1,19 +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.github.michaelbull.result.Result - -public interface KeyManager { - - public suspend fun addKey(key: T, replace: Boolean = false): Result - public suspend fun removeKey(key: T): Result - public suspend fun getKeyById(id: String): Result - public suspend fun getAllKeys(): Result, Throwable> - - /** Given a [fileName], return whether this instance can handle it. */ - public fun canHandle(fileName: String): Boolean -} diff --git a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyPair.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyPair.kt deleted file mode 100644 index e2362612..00000000 --- a/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyPair.kt +++ /dev/null @@ -1,14 +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 - -/** Defines expectations for a keypair used in public key cryptography. */ -public interface KeyPair { - - public fun getPrivateKey(): ByteArray - public fun getPublicKey(): ByteArray - public fun getKeyId(): String -} -- cgit v1.2.3