diff options
author | Aditya Wasan <adityawasan55@gmail.com> | 2021-08-17 04:14:43 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-17 04:14:43 +0530 |
commit | b7abd561f561af451ec717746e198a8686d10868 (patch) | |
tree | 16af9397d205d6501624ba4e98389e21764d9dd3 /crypto-common | |
parent | 9982562dc4e1ab4dbd058cf9d3c3c46fc598dec8 (diff) |
Add `KeyPair` and `KeyManager` to manage keys in the app (#1487)
Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'crypto-common')
5 files changed, 111 insertions, 0 deletions
diff --git a/crypto-common/api/crypto-common.api b/crypto-common/api/crypto-common.api index 7493379c..6468ea9b 100644 --- a/crypto-common/api/crypto-common.api +++ b/crypto-common/api/crypto-common.api @@ -1,6 +1,63 @@ +public abstract class dev/msfjarvis/aps/data/crypto/CryptoException : java/lang/Exception { + public synthetic fun <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V +} + public abstract interface class dev/msfjarvis/aps/data/crypto/CryptoHandler { public abstract fun canHandle (Ljava/lang/String;)Z public abstract fun decrypt (Ljava/lang/String;[B[B)[B public abstract fun encrypt (Ljava/lang/String;[B)[B } +public abstract interface class dev/msfjarvis/aps/data/crypto/KeyManager { + public abstract fun addKey (Ldev/msfjarvis/aps/data/crypto/KeyPair;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun canHandle (Ljava/lang/String;)Z + public abstract fun getAllKeys (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun getKeyById (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun removeKey (Ldev/msfjarvis/aps/data/crypto/KeyPair;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class dev/msfjarvis/aps/data/crypto/KeyManager$DefaultImpls { + public static synthetic fun addKey$default (Ldev/msfjarvis/aps/data/crypto/KeyManager;Ldev/msfjarvis/aps/data/crypto/KeyPair;ZLkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; +} + +public abstract class dev/msfjarvis/aps/data/crypto/KeyManagerException : dev/msfjarvis/aps/data/crypto/CryptoException { + public synthetic fun <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V +} + +public final class dev/msfjarvis/aps/data/crypto/KeyManagerException$KeyAlreadyExistsException : dev/msfjarvis/aps/data/crypto/KeyManagerException { + public fun <init> (Ljava/lang/String;)V +} + +public final class dev/msfjarvis/aps/data/crypto/KeyManagerException$KeyDeletionFailedException : dev/msfjarvis/aps/data/crypto/KeyManagerException { + public static final field INSTANCE Ldev/msfjarvis/aps/data/crypto/KeyManagerException$KeyDeletionFailedException; +} + +public final class dev/msfjarvis/aps/data/crypto/KeyManagerException$KeyDirectoryUnavailableException : dev/msfjarvis/aps/data/crypto/KeyManagerException { + public static final field INSTANCE Ldev/msfjarvis/aps/data/crypto/KeyManagerException$KeyDirectoryUnavailableException; +} + +public final class dev/msfjarvis/aps/data/crypto/KeyManagerException$KeyNotFoundException : dev/msfjarvis/aps/data/crypto/KeyManagerException { + public fun <init> (Ljava/lang/String;)V +} + +public final class dev/msfjarvis/aps/data/crypto/KeyManagerException$NoKeysAvailableException : dev/msfjarvis/aps/data/crypto/KeyManagerException { + public static final field INSTANCE Ldev/msfjarvis/aps/data/crypto/KeyManagerException$NoKeysAvailableException; +} + +public abstract interface class dev/msfjarvis/aps/data/crypto/KeyPair { + public abstract fun getKeyId ()Ljava/lang/String; + public abstract fun getPrivateKey ()[B + public abstract fun getPublicKey ()[B +} + +public abstract class dev/msfjarvis/aps/data/crypto/KeyPairException : dev/msfjarvis/aps/data/crypto/CryptoException { + public synthetic fun <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V +} + +public final class dev/msfjarvis/aps/data/crypto/KeyPairException$PrivateKeyUnavailableException : dev/msfjarvis/aps/data/crypto/KeyPairException { + public static final field INSTANCE Ldev/msfjarvis/aps/data/crypto/KeyPairException$PrivateKeyUnavailableException; +} + diff --git a/crypto-common/build.gradle.kts b/crypto-common/build.gradle.kts index c1f3eef8..44323900 100644 --- a/crypto-common/build.gradle.kts +++ b/crypto-common/build.gradle.kts @@ -6,3 +6,5 @@ plugins { kotlin("jvm") `aps-plugin` } + +dependencies { implementation(libs.thirdparty.kotlinResult) } 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<T : KeyPair> { + + public suspend fun addKey(key: T, replace: Boolean = false): Result<T, Throwable> + public suspend fun removeKey(key: T): Result<T, Throwable> + public suspend fun getKeyById(id: String): Result<T, Throwable> + public suspend fun getAllKeys(): Result<List<T>, 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 +} |