-- |
-- Module      : Crypto.Store.Error
-- License     : BSD-style
-- Maintainer  : Olivier Chéron <olivier.cheron@gmail.com>
-- Stability   : experimental
-- Portability : unknown
--
-- Error data type.
module Crypto.Store.Error
    ( StoreError(..)
    , fromCryptoFailable
    ) where

import Crypto.Error
import Crypto.PubKey.RSA.Types as RSA

import Data.ASN1.Error

-- | Error type in cryptostore.
data StoreError =
      CryptoError CryptoError
      -- ^ Wraps a cryptonite error
    | RSAError RSA.Error
      -- ^ Wraps an RSA crypto error
    | DecodingError ASN1Error
      -- ^ Error while decoding ASN.1 content
    | ParseFailure String
      -- ^ Error while parsing an ASN.1 object
    | DecryptionFailed
      -- ^ Unable to decrypt, incorrect key or password?
    | BadContentMAC
      -- ^ MAC verification failed, incorrect key or password?
    | BadChecksum
      -- ^ Checksum verification failed, incorrect key or password?
    | DigestMismatch
      -- ^ Digest verification failed
    | SignatureNotVerified
      -- ^ Signature verification failed
    | InvalidInput String
      -- ^ Some condition is not met about input to algorithm
    | InvalidPassword String
      -- ^ Some condition is not met about input password
    | InvalidParameter String
      -- ^ Some condition is not met about algorithm parameters
    | UnexpectedPublicKeyType
      -- ^ The algorithm expects another public key type
    | UnexpectedPrivateKeyType
      -- ^ The algorithm expects another private key type
    | RecipientTypeMismatch
      -- ^ Returned when the type of recipient info does not match the consumer
      -- function
    | RecipientKeyNotFound
      -- ^ The certificate provided does not match any encrypted key found
    | NoRecipientInfoFound
      -- ^ No recipient info is available in the enveloped data
    | NoRecipientInfoMatched
      -- ^ No recipient info could be used with the consumer function
    | UnsupportedOriginatorFormat
      -- ^ Only anonymous public key is supported
    | UnsupportedEllipticCurve
      -- ^ The elliptic curve used is not supported
    | NamedCurveRequired
      -- ^ The algorithm requires a named elliptic curve
    deriving (Int -> StoreError -> ShowS
[StoreError] -> ShowS
StoreError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StoreError] -> ShowS
$cshowList :: [StoreError] -> ShowS
show :: StoreError -> String
$cshow :: StoreError -> String
showsPrec :: Int -> StoreError -> ShowS
$cshowsPrec :: Int -> StoreError -> ShowS
Show,StoreError -> StoreError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StoreError -> StoreError -> Bool
$c/= :: StoreError -> StoreError -> Bool
== :: StoreError -> StoreError -> Bool
$c== :: StoreError -> StoreError -> Bool
Eq)

-- | Turn a 'CryptoFailed' into a 'StoreError'.
fromCryptoFailable ::CryptoFailable a -> Either StoreError a
fromCryptoFailable :: forall a. CryptoFailable a -> Either StoreError a
fromCryptoFailable (CryptoPassed a
a) = forall a b. b -> Either a b
Right a
a
fromCryptoFailable (CryptoFailed CryptoError
e) = forall a b. a -> Either a b
Left (CryptoError -> StoreError
CryptoError CryptoError
e)