From 4b7457c7f712b92f21604d8612ec8ff19df75c81 Mon Sep 17 00:00:00 2001 From: Tad Fisher Date: Sun, 9 Oct 2022 15:10:10 -0700 Subject: Add crypto-hwsecurity library --- .../app/passwordstore/crypto/DeviceHandler.kt | 12 +++++++ .../passwordstore/crypto/errors/CryptoException.kt | 39 ++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 crypto-common/src/main/kotlin/app/passwordstore/crypto/DeviceHandler.kt (limited to 'crypto-common/src/main') diff --git a/crypto-common/src/main/kotlin/app/passwordstore/crypto/DeviceHandler.kt b/crypto-common/src/main/kotlin/app/passwordstore/crypto/DeviceHandler.kt new file mode 100644 index 00000000..74eb0cfa --- /dev/null +++ b/crypto-common/src/main/kotlin/app/passwordstore/crypto/DeviceHandler.kt @@ -0,0 +1,12 @@ +package app.passwordstore.crypto + +import app.passwordstore.crypto.errors.DeviceHandlerException +import com.github.michaelbull.result.Result + +public interface DeviceHandler { + public suspend fun pairWithPublicKey(publicKey: Key): Result + + public suspend fun decryptSessionKey( + encryptedSessionKey: EncryptedSessionKey + ): Result +} diff --git a/crypto-common/src/main/kotlin/app/passwordstore/crypto/errors/CryptoException.kt b/crypto-common/src/main/kotlin/app/passwordstore/crypto/errors/CryptoException.kt index 81bdf95f..328a7a32 100644 --- a/crypto-common/src/main/kotlin/app/passwordstore/crypto/errors/CryptoException.kt +++ b/crypto-common/src/main/kotlin/app/passwordstore/crypto/errors/CryptoException.kt @@ -6,7 +6,7 @@ public sealed class CryptoException(message: String? = null, cause: Throwable? = Exception(message, cause) /** Sealed exception types for [KeyManager]. */ -public sealed class KeyManagerException(message: String? = null) : CryptoException(message) +public sealed class KeyManagerException(message: String? = null, cause: Throwable? = null) : CryptoException(message, cause) /** Store contains no keys. */ public object NoKeysAvailableException : KeyManagerException("No keys were found") @@ -19,8 +19,8 @@ public object KeyDirectoryUnavailableException : public object KeyDeletionFailedException : KeyManagerException("Couldn't delete the key file") /** Failed to parse the key as a known type. */ -public object InvalidKeyException : - KeyManagerException("Given key cannot be parsed as a known key type") +public class InvalidKeyException(cause: Throwable? = null) : + KeyManagerException("Given key cannot be parsed as a known key type", cause) /** No key matching `keyId` could be found. */ public class KeyNotFoundException(keyId: String) : @@ -30,6 +30,9 @@ public class KeyNotFoundException(keyId: String) : public class KeyAlreadyExistsException(keyId: String) : KeyManagerException("Pre-existing key was found for $keyId") +public class NoSecretKeyException(keyId: String) : + KeyManagerException("No secret keys found for $keyId") + /** Sealed exception types for [app.passwordstore.crypto.CryptoHandler]. */ public sealed class CryptoHandlerException(message: String? = null, cause: Throwable? = null) : CryptoException(message, cause) @@ -42,3 +45,33 @@ public class NoKeysProvided(message: String?) : CryptoHandlerException(message, /** An unexpected error that cannot be mapped to a known type. */ public class UnknownError(cause: Throwable) : CryptoHandlerException(null, cause) + +public class KeySpecific(public val key: Any, cause: Throwable?) : CryptoHandlerException(key.toString(), cause) + +/** Wrapper containing possibly multiple child exceptions via [suppressedExceptions]. */ +public class MultipleKeySpecific( + message: String?, + public val errors: List +) : CryptoHandlerException(message) { + init { + for (error in errors) { + addSuppressed(error) + } + } +} + +/** Sealed exception types for [app.passwordstore.crypto.DeviceHandler]. */ +public sealed class DeviceHandlerException(message: String? = null, cause: Throwable? = null) : + CryptoHandlerException(message, cause) + +/** The device crypto operation was canceled by the user. */ +public class DeviceOperationCanceled(message: String) : DeviceHandlerException(message, null) + +/** The device crypto operation failed. */ +public class DeviceOperationFailed(message: String?, cause: Throwable? = null) : DeviceHandlerException(message, cause) + +/** The device's key fingerprint doesn't match the fingerprint we are trying to pair it to. */ +public class DeviceFingerprintMismatch( + public val publicFingerprint: String, + public val deviceFingerprint: String, +) : DeviceHandlerException() -- cgit v1.2.3