packages feed

x509-store 1.6.1 → 1.6.2

raw patch · 2 files changed

+63/−3 lines, 2 filesdep +directorydep +filepath

Dependencies added: directory, filepath

Files

Data/X509/CertificateStore.hs view
@@ -1,17 +1,27 @@ module Data.X509.CertificateStore     ( CertificateStore     , makeCertificateStore+    , readCertificateStore     -- * Queries     , findCertificate     , listCertificates     ) where -import Data.List (foldl')+import Data.Char (isDigit, isHexDigit)+import Data.Either (rights)+import Data.List (foldl', isPrefixOf) import Data.Monoid+import Data.PEM (pemParseBS, pemContent) import Data.X509 import qualified Data.Map as M-import Control.Monad (mplus)+import Control.Applicative ((<$>))+import Control.Monad (mplus, filterM)+import System.Directory (getDirectoryContents, doesFileExist, doesDirectoryExist)+import System.FilePath ((</>))+import qualified Control.Exception as E+import qualified Data.ByteString as B + -- | A Collection of certificate or store of certificates. data CertificateStore = CertificateStore (M.Map DistinguishedName SignedCertificate)                       | CertificateStores [CertificateStore]@@ -38,3 +48,51 @@ listCertificates :: CertificateStore -> [SignedCertificate] listCertificates (CertificateStore store) = map snd $ M.toList store listCertificates (CertificateStores l)    = concatMap listCertificates l++-- | Create certificate store by reading certificates from file or directory+--+-- This function can be used to read multiple certificates from either+-- single file (multiple PEM formatted certificates concanated) or+-- directory (one certificate per file, file names are hashes from+-- certificate).+readCertificateStore :: FilePath -> IO (Maybe CertificateStore)+readCertificateStore path = do+    isDir  <- doesDirectoryExist path+    isFile <- doesFileExist path+    wrapStore <$> (if isDir then makeDirStore else if isFile then makeFileStore else return [])+  where+    wrapStore :: [SignedCertificate] -> Maybe CertificateStore+    wrapStore [] = Nothing+    wrapStore l  = Just $ makeCertificateStore l++    makeFileStore = readCertificates path+    makeDirStore  = do+        certFiles <- listDirectoryCerts path+        concat <$> mapM readCertificates certFiles++-- Try to read certificate from the content of a file.+--+-- The file may contains multiple certificates+readCertificates :: FilePath -> IO [SignedCertificate]+readCertificates file = E.catch (either (const []) (rights . map getCert) . pemParseBS <$> B.readFile file) skipIOError+    where+        getCert = decodeSignedCertificate . pemContent+        skipIOError :: E.IOException -> IO [SignedCertificate]+        skipIOError _ = return []++-- List all the path susceptible to contains a certificate in a directory+--+-- if the parameter is not a directory, hilarity follows.+listDirectoryCerts :: FilePath -> IO [FilePath]+listDirectoryCerts path =+    getDirContents >>= filterM doesFileExist+  where+    isHashedFile s = length s == 10+                  && isDigit (s !! 9)+                  && (s !! 8) == '.'+                  && all isHexDigit (take 8 s)+    isCert x = (not $ isPrefixOf "." x) && (not $ isHashedFile x)++    getDirContents = E.catch (map (path </>) . filter isCert <$> getDirectoryContents path) emptyPaths+            where emptyPaths :: E.IOException -> IO [FilePath]+                  emptyPaths _ = return []
x509-store.cabal view
@@ -1,5 +1,5 @@ Name:                x509-store-Version:             1.6.1+Version:             1.6.2 Description:         X.509 collection accessing and storing methods for certificate, crl, exception list License:             BSD3 License-file:        LICENSE@@ -18,6 +18,8 @@                    , bytestring                    , mtl                    , containers+                   , directory+                   , filepath                    , pem >= 0.1 && < 0.3                    , asn1-types >= 0.3 && < 0.4                    , asn1-encoding >= 0.9 && < 0.10