module Data.X509.Validation.Cache
(
ValidationCacheResult(..)
, ValidationCacheQueryCallback
, ValidationCacheAddCallback
, ValidationCache(..)
, exceptionValidationCache
, tofuValidationCache
) where
import Control.Concurrent
import Data.Default.Class
import Data.X509
import Data.X509.Validation.Types
import Data.X509.Validation.Fingerprint
data ValidationCacheResult =
ValidationCachePass
| ValidationCacheDenied String
| ValidationCacheUnknown
deriving (Int -> ValidationCacheResult -> ShowS
[ValidationCacheResult] -> ShowS
ValidationCacheResult -> String
(Int -> ValidationCacheResult -> ShowS)
-> (ValidationCacheResult -> String)
-> ([ValidationCacheResult] -> ShowS)
-> Show ValidationCacheResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ValidationCacheResult -> ShowS
showsPrec :: Int -> ValidationCacheResult -> ShowS
$cshow :: ValidationCacheResult -> String
show :: ValidationCacheResult -> String
$cshowList :: [ValidationCacheResult] -> ShowS
showList :: [ValidationCacheResult] -> ShowS
Show,ValidationCacheResult -> ValidationCacheResult -> Bool
(ValidationCacheResult -> ValidationCacheResult -> Bool)
-> (ValidationCacheResult -> ValidationCacheResult -> Bool)
-> Eq ValidationCacheResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ValidationCacheResult -> ValidationCacheResult -> Bool
== :: ValidationCacheResult -> ValidationCacheResult -> Bool
$c/= :: ValidationCacheResult -> ValidationCacheResult -> Bool
/= :: ValidationCacheResult -> ValidationCacheResult -> Bool
Eq)
type ValidationCacheQueryCallback = ServiceID
-> Fingerprint
-> Certificate
-> IO ValidationCacheResult
type ValidationCacheAddCallback = ServiceID
-> Fingerprint
-> Certificate
-> IO ()
data ValidationCache = ValidationCache
{ ValidationCache -> ValidationCacheQueryCallback
cacheQuery :: ValidationCacheQueryCallback
, ValidationCache -> ValidationCacheAddCallback
cacheAdd :: ValidationCacheAddCallback
}
instance Default ValidationCache where
def :: ValidationCache
def = [(ServiceID, Fingerprint)] -> ValidationCache
exceptionValidationCache []
exceptionValidationCache :: [(ServiceID, Fingerprint)] -> ValidationCache
exceptionValidationCache :: [(ServiceID, Fingerprint)] -> ValidationCache
exceptionValidationCache [(ServiceID, Fingerprint)]
fingerprints =
ValidationCacheQueryCallback
-> ValidationCacheAddCallback -> ValidationCache
ValidationCache ([(ServiceID, Fingerprint)] -> ValidationCacheQueryCallback
queryListCallback [(ServiceID, Fingerprint)]
fingerprints)
(\ServiceID
_ Fingerprint
_ Certificate
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
tofuValidationCache :: [(ServiceID, Fingerprint)]
-> IO ValidationCache
tofuValidationCache :: [(ServiceID, Fingerprint)] -> IO ValidationCache
tofuValidationCache [(ServiceID, Fingerprint)]
fingerprints = do
MVar [(ServiceID, Fingerprint)]
l <- [(ServiceID, Fingerprint)] -> IO (MVar [(ServiceID, Fingerprint)])
forall a. a -> IO (MVar a)
newMVar [(ServiceID, Fingerprint)]
fingerprints
ValidationCache -> IO ValidationCache
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ValidationCache -> IO ValidationCache)
-> ValidationCache -> IO ValidationCache
forall a b. (a -> b) -> a -> b
$ ValidationCacheQueryCallback
-> ValidationCacheAddCallback -> ValidationCache
ValidationCache (\ServiceID
s Fingerprint
f Certificate
c -> MVar [(ServiceID, Fingerprint)] -> IO [(ServiceID, Fingerprint)]
forall a. MVar a -> IO a
readMVar MVar [(ServiceID, Fingerprint)]
l IO [(ServiceID, Fingerprint)]
-> ([(ServiceID, Fingerprint)] -> IO ValidationCacheResult)
-> IO ValidationCacheResult
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[(ServiceID, Fingerprint)]
list -> ([(ServiceID, Fingerprint)] -> ValidationCacheQueryCallback
queryListCallback [(ServiceID, Fingerprint)]
list) ServiceID
s Fingerprint
f Certificate
c)
(\ServiceID
s Fingerprint
f Certificate
_ -> MVar [(ServiceID, Fingerprint)]
-> ([(ServiceID, Fingerprint)] -> IO [(ServiceID, Fingerprint)])
-> IO ()
forall a. MVar a -> (a -> IO a) -> IO ()
modifyMVar_ MVar [(ServiceID, Fingerprint)]
l (\[(ServiceID, Fingerprint)]
list -> [(ServiceID, Fingerprint)] -> IO [(ServiceID, Fingerprint)]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ((ServiceID
s,Fingerprint
f) (ServiceID, Fingerprint)
-> [(ServiceID, Fingerprint)] -> [(ServiceID, Fingerprint)]
forall a. a -> [a] -> [a]
: [(ServiceID, Fingerprint)]
list)))
queryListCallback :: [(ServiceID, Fingerprint)] -> ValidationCacheQueryCallback
queryListCallback :: [(ServiceID, Fingerprint)] -> ValidationCacheQueryCallback
queryListCallback [(ServiceID, Fingerprint)]
list = ValidationCacheQueryCallback
forall {m :: * -> *} {p}.
Monad m =>
ServiceID -> Fingerprint -> p -> m ValidationCacheResult
query
where query :: ServiceID -> Fingerprint -> p -> m ValidationCacheResult
query ServiceID
serviceID Fingerprint
fingerprint p
_ = ValidationCacheResult -> m ValidationCacheResult
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (ValidationCacheResult -> m ValidationCacheResult)
-> ValidationCacheResult -> m ValidationCacheResult
forall a b. (a -> b) -> a -> b
$
case ServiceID -> [(ServiceID, Fingerprint)] -> Maybe Fingerprint
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup ServiceID
serviceID [(ServiceID, Fingerprint)]
list of
Maybe Fingerprint
Nothing -> ValidationCacheResult
ValidationCacheUnknown
Just Fingerprint
f | Fingerprint
fingerprint Fingerprint -> Fingerprint -> Bool
forall a. Eq a => a -> a -> Bool
== Fingerprint
f -> ValidationCacheResult
ValidationCachePass
| Bool
otherwise -> String -> ValidationCacheResult
ValidationCacheDenied (ServiceID -> String
forall a. Show a => a -> String
show ServiceID
serviceID String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" expected " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Fingerprint -> String
forall a. Show a => a -> String
show Fingerprint
f String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" but got: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Fingerprint -> String
forall a. Show a => a -> String
show Fingerprint
fingerprint)