From b7abd561f561af451ec717746e198a8686d10868 Mon Sep 17 00:00:00 2001 From: Aditya Wasan Date: Tue, 17 Aug 2021 04:14:43 +0530 Subject: Add `KeyPair` and `KeyManager` to manage keys in the app (#1487) Co-authored-by: Harsh Shandilya --- .../dev/msfjarvis/aps/data/crypto/CryptoException.kt | 19 +++++++++++++++++++ .../dev/msfjarvis/aps/data/crypto/KeyManager.kt | 19 +++++++++++++++++++ .../kotlin/dev/msfjarvis/aps/data/crypto/KeyPair.kt | 14 ++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoException.kt create mode 100644 crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyManager.kt create 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/data/crypto/CryptoException.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoException.kt new file mode 100644 index 00000000..6a73d381 --- /dev/null +++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/CryptoException.kt @@ -0,0 +1,19 @@ +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/KeyManager.kt b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/crypto/KeyManager.kt new file mode 100644 index 00000000..b5ba881e --- /dev/null +++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/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.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 new file mode 100644 index 00000000..e2362612 --- /dev/null +++ b/crypto-common/src/main/kotlin/dev/msfjarvis/aps/data/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.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