| 1 |
-- | |
| 2 |
-- Module : Data.X509.Validation.Cache |
| 3 |
-- License : BSD-style |
| 4 |
-- Maintainer : Vincent Hanquez <[email protected]> |
| 5 |
-- Stability : experimental |
| 6 |
-- Portability : unknown |
| 7 |
-- |
| 8 |
-- X.509 Validation cache |
| 9 |
-- |
| 10 |
-- Define all the types necessary for the validation cache, |
| 11 |
-- and some simples instances of cache mechanism |
| 12 |
module Data.X509.Validation.Cache ( |
| 13 |
-- * Cache for validation |
| 14 |
ValidationCacheResult (..), |
| 15 |
ValidationCacheQueryCallback, |
| 16 |
ValidationCacheAddCallback, |
| 17 |
ValidationCache (..), |
| 18 |
defaultValidationCache, |
| 19 |
|
| 20 |
-- * Simple instances of cache mechanism |
| 21 |
exceptionValidationCache, |
| 22 |
tofuValidationCache, |
| 23 |
) where |
| 24 |
|
| 25 |
import Control.Concurrent |
| 26 |
import Data.Default |
| 27 |
import Data.X509 |
| 28 |
import Data.X509.Validation.Fingerprint |
| 29 |
import Data.X509.Validation.Types |
| 30 |
|
| 31 |
-- | The result of a cache query |
| 32 |
data ValidationCacheResult |
| 33 |
= -- | cache allow this fingerprint to go through |
| 34 |
ValidationCachePass |
| 35 |
| -- | cache denied this fingerprint for further validation |
| 36 |
ValidationCacheDenied String |
| 37 |
| -- | unknown fingerprint in cache |
| 38 |
ValidationCacheUnknown |
| 39 |
deriving (Show, Eq) |
| 40 |
|
| 41 |
-- | Validation cache query callback type |
| 42 |
type ValidationCacheQueryCallback = |
| 43 |
ServiceID |
| 44 |
-- ^ connection's identification |
| 45 |
-> Fingerprint |
| 46 |
-- ^ fingerprint of the leaf certificate |
| 47 |
-> Certificate |
| 48 |
-- ^ leaf certificate |
| 49 |
-> IO ValidationCacheResult |
| 50 |
-- ^ return if the operation is succesful or not |
| 51 |
|
| 52 |
-- | Validation cache callback type |
| 53 |
type ValidationCacheAddCallback = |
| 54 |
ServiceID |
| 55 |
-- ^ connection's identification |
| 56 |
-> Fingerprint |
| 57 |
-- ^ fingerprint of the leaf certificate |
| 58 |
-> Certificate |
| 59 |
-- ^ leaf certificate |
| 60 |
-> IO () |
| 61 |
|
| 62 |
-- | All the callbacks needed for querying and adding to the cache. |
| 63 |
data ValidationCache = ValidationCache |
| 64 |
{ cacheQuery :: ValidationCacheQueryCallback |
| 65 |
-- ^ cache querying callback |
| 66 |
, cacheAdd :: ValidationCacheAddCallback |
| 67 |
-- ^ cache adding callback |
| 68 |
} |
| 69 |
|
| 70 |
defaultValidationCache :: ValidationCache |
| 71 |
defaultValidationCache = exceptionValidationCache [] |
| 72 |
|
| 73 |
instance Default ValidationCache where |
| 74 |
def = defaultValidationCache |
| 75 |
|
| 76 |
-- | create a simple constant cache that list exceptions to the certification |
| 77 |
-- validation. Typically this is use to allow self-signed certificates for |
| 78 |
-- specific use, with out-of-bounds user checks. |
| 79 |
-- |
| 80 |
-- No fingerprints will be added after the instance is created. |
| 81 |
-- |
| 82 |
-- The underlying structure for the check is kept as a list, as |
| 83 |
-- usually the exception list will be short, but when the list go above |
| 84 |
-- a dozen exceptions it's recommended to use another cache mechanism with |
| 85 |
-- a faster lookup mechanism (hashtable, map, etc). |
| 86 |
-- |
| 87 |
-- Note that only one fingerprint is allowed per ServiceID, for other use, |
| 88 |
-- another cache mechanism need to be use. |
| 89 |
exceptionValidationCache :: [(ServiceID, Fingerprint)] -> ValidationCache |
| 90 |
exceptionValidationCache fingerprints = |
| 91 |
ValidationCache |
| 92 |
(queryListCallback fingerprints) |
| 93 |
(\_ _ _ -> return ()) |
| 94 |
|
| 95 |
-- | Trust on first use (TOFU) cache with an optional list of exceptions |
| 96 |
-- |
| 97 |
-- this is similar to the exceptionCache, except that after |
| 98 |
-- each succesfull validation it does add the fingerprint |
| 99 |
-- to the database. This prevent any further modification of the |
| 100 |
-- fingerprint for the remaining |
| 101 |
tofuValidationCache |
| 102 |
:: [(ServiceID, Fingerprint)] |
| 103 |
-- ^ a list of exceptions |
| 104 |
-> IO ValidationCache |
| 105 |
tofuValidationCache fingerprints = do |
| 106 |
l <- newMVar fingerprints |
| 107 |
return $ |
| 108 |
ValidationCache |
| 109 |
(\s f c -> readMVar l >>= \list -> (queryListCallback list) s f c) |
| 110 |
(\s f _ -> modifyMVar_ l (\list -> return ((s, f) : list))) |
| 111 |
|
| 112 |
-- | a cache query function working on list. |
| 113 |
-- don't use when the list grows a lot. |
| 114 |
queryListCallback :: [(ServiceID, Fingerprint)] -> ValidationCacheQueryCallback |
| 115 |
queryListCallback list = query |
| 116 |
where |
| 117 |
query serviceID fingerprint _ = return $ |
| 118 |
case lookup serviceID list of |
| 119 |
Nothing -> ValidationCacheUnknown |
| 120 |
Just f |
| 121 |
| fingerprint == f -> ValidationCachePass |
| 122 |
| otherwise -> |
| 123 |
ValidationCacheDenied |
| 124 |
(show serviceID ++ " expected " ++ show f ++ " but got: " ++ show fingerprint) |