crypton-x509-store 1.10.0 → 1.10.1
raw patch · 7 files changed
+226/−47 lines, 7 filesdep ~crypton-asn1-typesdep ~crypton-x509PVP ok
version bump matches the API change (PVP)
Dependency ranges changed: crypton-asn1-types, crypton-x509
API changes (from Hackage documentation)
+ Data.X509.CertificateStore: findCertificates :: DistinguishedName -> CertificateStore -> [SignedCertificate]
Files
- ChangeLog.md +12/−0
- Data/X509/CertificateStore.hs +74/−22
- Data/X509/File.hs +5/−20
- Data/X509/Memory.hs +1/−1
- Data/X509/ReadFile.hs +29/−0
- Tests/Tests.hs +97/−0
- crypton-x509-store.cabal +8/−4
ChangeLog.md view
@@ -1,5 +1,17 @@ # ChangeLog for crypton-x509-store +## 1.10.1++`TRUSTED CERTIFICATE` PEM blocks are no longer used as trust anchors,+and several CAs with the same subject are kept.++* Fix trust store loading+ [#38](https://github.com/kazu-yamamoto/crypton-certificate/pull/38)+* Build certification paths with backtracking and implement CRL distribution points+ [#48](https://github.com/kazu-yamamoto/crypton-certificate/pull/48)+* Read store files strictly with cloexec+ [#49](https://github.com/kazu-yamamoto/crypton-certificate/pull/49)+ ## 1.10.0 * Read ML-DSA private keys
Data/X509/CertificateStore.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-}+{-# LANGUAGE OverloadedStrings #-} module Data.X509.CertificateStore ( CertificateStore,@@ -9,40 +10,46 @@ -- * Queries findCertificate,+ findCertificates, listCertificates, ) where import Data.Char (isDigit, isHexDigit) import Data.Either (rights)-import Data.List (foldl', isPrefixOf)-#if MIN_VERSION_base(4,9,0)+import qualified Data.List as L+#if !(MIN_VERSION_base(4,11,0)) import Data.Semigroup-#else-import Data.Monoid #endif import qualified Control.Exception as E-import Control.Monad (filterM, mplus)+import Control.Monad (filterM) import qualified Data.ByteString as B+import qualified Data.ByteString.Char8 as BC import qualified Data.Map as M-import Data.PEM (pemContent, pemParseBS)+import Data.PEM (PEM, pemContent, pemName, pemParseBS) import Data.X509+import Data.X509.ReadFile (readFileCloexec) import System.Directory ( doesDirectoryExist, doesFileExist, getDirectoryContents,+ pathIsSymbolicLink, )-import System.FilePath ((</>))+import System.FilePath (takeFileName, (</>)) import qualified System.IO.Error as E+#if !defined(mingw32_HOST_OS)+import System.Posix.Files (getFileStatus, isRegularFile)+#endif --- | A Collection of certificate or store of certificates.+-- | A Collection of certificate or store of certificates. Several+-- certificates may have the same subject, which is compared in its+-- canonical form. data CertificateStore- = CertificateStore (M.Map DistinguishedName SignedCertificate)+ = CertificateStore (M.Map DistinguishedName [SignedCertificate]) | CertificateStores [CertificateStore] isEmptyCertificateStore :: CertificateStore -> Bool isEmptyCertificateStore (CertificateStore m) = M.null m-isEmptyCertificateStore (CertificateStores []) = True-isEmptyCertificateStore _ = False+isEmptyCertificateStore (CertificateStores l) = all isEmptyCertificateStore l #if MIN_VERSION_base(4,9,0) instance Semigroup CertificateStore where@@ -63,21 +70,33 @@ -- | Create a certificate store out of a list of X509 certificate makeCertificateStore :: [SignedCertificate] -> CertificateStore-makeCertificateStore = CertificateStore . foldl' accumulate M.empty+makeCertificateStore = CertificateStore . L.foldl' accumulate M.empty where- accumulate m x509 = M.insert (certSubjectDN $ getCertificate x509) x509 m+ -- the latest certificate comes first+ accumulate m x509 = M.insertWith add (subjectKey x509) [x509] m+ add new old = new ++ filter (`notElem` new) old+ subjectKey = canonicalDistinguishedName . certSubjectDN . getCertificate --- | Find a certificate using the subject distinguished name+-- | Find a certificate using the subject distinguished name. If several+-- certificates have the subject, the one added last is returned. findCertificate :: DistinguishedName -> CertificateStore -> Maybe SignedCertificate-findCertificate dn store = lookupIn store+findCertificate dn store = case findCertificates dn store of+ c : _ -> Just c+ [] -> Nothing++-- | Find all certificates with the subject distinguished name.+findCertificates+ :: DistinguishedName -> CertificateStore -> [SignedCertificate]+findCertificates dn = lookupIn where- lookupIn (CertificateStore m) = M.lookup dn m- lookupIn (CertificateStores l) = foldl mplus Nothing $ map lookupIn l+ key = canonicalDistinguishedName dn+ lookupIn (CertificateStore m) = M.findWithDefault [] key m+ lookupIn (CertificateStores l) = concatMap lookupIn l -- | List all certificates in a store listCertificates :: CertificateStore -> [SignedCertificate]-listCertificates (CertificateStore store) = map snd $ M.toList store+listCertificates (CertificateStore store) = concat $ M.elems store listCertificates (CertificateStores l) = concatMap listCertificates l -- | Create certificate store by reading certificates from file or directory@@ -104,30 +123,54 @@ -- Try to read certificate from the content of a file. ----- The file may contains multiple certificates+-- The file may contains multiple certificates. Only the PEM blocks+-- labeled CERTIFICATE are used: a TRUSTED CERTIFICATE block carries+-- OpenSSL trust settings, which are not interpreted. Broken blocks+-- are skipped. readCertificates :: FilePath -> IO [SignedCertificate] readCertificates file = E.catchIOError- (either (const []) (rights . map getCert) . pemParseBS <$> B.readFile file)+ (rights . map getCert . filter isCert . pemBlocks <$> readFileCloexec file) skipIOError where+ isCert pem = pemName pem `elem` ["CERTIFICATE", "X509 CERTIFICATE"] getCert = decodeSignedCertificate . pemContent skipIOError :: E.IOException -> IO [SignedCertificate] skipIOError _ = return [] +-- | Parse PEM blocks one by one so that a broken block does not+-- discard the others.+pemBlocks :: B.ByteString -> [PEM]+pemBlocks = go . BC.lines+ where+ go ls = case dropWhile (not . B.isPrefixOf "-----BEGIN ") ls of+ [] -> []+ begin : rest -> case break (B.isPrefixOf "-----END ") rest of+ (body, end : rest') ->+ either (const []) id (pemParseBS $ BC.unlines (begin : body ++ [end]))+ ++ go rest'+ _ -> []+ -- 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+ getDirContents >>= filterM keep where+ -- hashed names are usually symbolic links to other files in the+ -- directory, but are regular files on Android, for instance+ keep p = do+ regular <- isRegular p+ if regular && isHashedFile (takeFileName p)+ then not <$> pathIsSymbolicLink p+ else return regular isHashedFile s = length s == 10 && isDigit (s !! 9) && (s !! 8) == '.' && all isHexDigit (take 8 s)- isCert x = (not $ isPrefixOf "." x) && (not $ isHashedFile x)+ isCert x = not $ L.isPrefixOf "." x getDirContents = E.catchIOError@@ -136,3 +179,12 @@ where emptyPaths :: E.IOException -> IO [FilePath] emptyPaths _ = return []++-- | Whether the path is a regular file, following symbolic links.+isRegular :: FilePath -> IO Bool+#if defined(mingw32_HOST_OS)+isRegular = doesFileExist+#else+isRegular p =+ E.catchIOError (isRegularFile <$> getFileStatus p) (\_ -> return False)+#endif
Data/X509/File.hs view
@@ -1,25 +1,16 @@-{-# LANGUAGE CPP #-}- module Data.X509.File ( readSignedObject, readKeyFile, PEMError (..), ) where -#ifndef MIN_VERSION_unix-#define MIN_VERSION_unix(x, y, z) 0-#endif- import qualified Control.Exception as E import Data.ASN1.Types-import qualified Data.ByteString.Lazy as L import Data.Maybe-import Data.PEM (PEM, pemContent, pemParseLBS)+import Data.PEM (PEM, pemContent, pemParseBS) import qualified Data.X509 as X509 import Data.X509.Memory (pemToKey)-#if defined(MIN_VERSION_unix) && MIN_VERSION_unix(2,8,0)-import System.Posix.IO-#endif+import Data.X509.ReadFile (readFileCloexec) newtype PEMError = PEMError {displayPEMError :: String} deriving (Show)@@ -29,14 +20,8 @@ readPEMs :: FilePath -> IO [PEM] readPEMs filepath = do-#if defined(MIN_VERSION_unix) && MIN_VERSION_unix(2,8,0)- fd <- openFd filepath ReadOnly defaultFileFlags{cloexec = True}- h <- fdToHandle fd- content <- L.hGetContents h-#else- content <- L.readFile filepath-#endif- either (E.throw . PEMError) pure $ pemParseLBS content+ content <- readFileCloexec filepath+ either (E.throwIO . PEMError) pure $ pemParseBS content -- | return all the signed objects in a file. --@@ -52,4 +37,4 @@ -- | return all the private keys that were successfully read from a file. readKeyFile :: FilePath -> IO [X509.PrivKey]-readKeyFile path = catMaybes . foldl pemToKey [] <$> readPEMs path+readKeyFile path = catMaybes . concatMap (pemToKey []) <$> readPEMs path
Data/X509/Memory.hs view
@@ -27,7 +27,7 @@ import Data.X509.EC as X509 readKeyFileFromMemory :: B.ByteString -> [X509.PrivKey]-readKeyFileFromMemory = either (const []) (catMaybes . foldl pemToKey []) . pemParseBS+readKeyFileFromMemory = either (const []) (catMaybes . concatMap (pemToKey [])) . pemParseBS readSignedObjectFromMemory :: (ASN1Object a, Eq a, Show a)
+ Data/X509/ReadFile.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE CPP #-}++module Data.X509.ReadFile (+ readFileCloexec,+) where++#ifndef MIN_VERSION_unix+#define MIN_VERSION_unix(x, y, z) 0+#endif++import qualified Data.ByteString as B+#if defined(MIN_VERSION_unix) && MIN_VERSION_unix(2,8,0)+import qualified Control.Exception as E+import System.IO (hClose)+import System.Posix.IO+#endif++-- | Read a whole file strictly. The file descriptor is not inherited+-- by child processes (when unix >= 2.8 is available).+readFileCloexec :: FilePath -> IO B.ByteString+#if defined(MIN_VERSION_unix) && MIN_VERSION_unix(2,8,0)+readFileCloexec path =+ E.bracket+ (openFd path ReadOnly defaultFileFlags{cloexec = True} >>= fdToHandle)+ hClose+ B.hGetContents+#else+readFileCloexec = B.readFile+#endif
Tests/Tests.hs view
@@ -1,10 +1,19 @@ -- | x509-store test suite. module Main (main) where +import Control.Exception (bracket_)+import Data.ASN1.Types (ASN1StringEncoding (..), asn1CharacterString) import qualified Data.ByteString as B+import qualified Data.ByteString.Char8 as BC+import Data.Maybe (isJust, isNothing) import Data.String (fromString) import Data.X509+import Data.X509.CertificateStore+import Data.X509.File import Data.X509.Memory+import System.Directory+import System.FilePath ((</>))+import System.Info (os) import Test.Tasty import Test.Tasty.HUnit@@ -390,6 +399,92 @@ readSignedCertificateFromMemory :: B.ByteString -> [SignedCertificate] readSignedCertificateFromMemory = readSignedObjectFromMemory +keyOrderTest :: TestTree+keyOrderTest =+ testCase "Key order" $+ map kind (readKeyFileFromMemory (rsaKey2 <> ed25519Key2 <> dsaKey2))+ @?= ["RSA", "Ed25519", "DSA"]++kind :: PrivKey -> String+kind PrivKeyRSA{} = "RSA"+kind PrivKeyEd25519{} = "Ed25519"+kind PrivKeyDSA{} = "DSA"+kind _ = "other"++-- | Runs an action in a fresh temporary directory.+withTempDir :: (FilePath -> IO a) -> IO a+withTempDir f = do+ tmp <- getTemporaryDirectory+ let dir = tmp </> "crypton-x509-store-test"+ removePathForcibly dir+ bracket_ (createDirectory dir) (removePathForcibly dir) (f dir)++storeTests :: TestTree+storeTests =+ testGroup+ "CertificateStore"+ [ testCase "TRUSTED CERTIFICATE is skipped" $ withTempDir $ \dir -> do+ let file = dir </> "bundle.crt"+ B.writeFile file $ relabel rsaCertificate <> dsaCertificate+ certs <- readCertificates file+ length certs @?= 1+ , testCase "broken PEM block is skipped" $ withTempDir $ \dir -> do+ let file = dir </> "bundle.pem"+ B.writeFile file $ rsaCertificate <> broken <> dsaCertificate+ certs <- readCertificates file+ length certs @?= 2+ , testCase "hashed regular file is read" $ withTempDir $ \dir -> do+ B.writeFile (dir </> "00673b5b.0") rsaCertificate+ store <- readCertificateStore dir+ assertBool "empty store" $ isJust store+ , testCase "hashed symbolic link is skipped" $+ if os == "mingw32"+ then return ()+ else withTempDir $ \dir -> do+ let sub = dir </> "certs"+ createDirectory sub+ B.writeFile (dir </> "target.pem") rsaCertificate+ createFileLink (dir </> "target.pem") (sub </> "00673b5b.0")+ store <- readCertificateStore sub+ assertBool "non-empty store" $ isNothing store+ , testCase "certificates with the same subject" $ do+ -- both certificates have CN=Test+ let certs = readSignedObjectFromMemory (rsaCertificate <> dsaCertificate)+ store = makeCertificateStore certs+ subject = certSubjectDN $ getCertificate $ last certs+ length (findCertificates subject store) @?= 2+ length (listCertificates store) @?= 2+ findCertificate subject store @?= Just (last certs)+ , testCase "subjects are compared in canonical form" $ do+ let store = makeCertificateStore $ readSignedObjectFromMemory rsaCertificate+ dn = DistinguishedName [([2, 5, 4, 3], asn1CharacterString Printable " TEST ")]+ assertBool "not found" $ isJust $ findCertificate dn store+ , testCase "key file order" $ withTempDir $ \dir -> do+ let file = dir </> "keys.pem"+ B.writeFile file $ rsaKey2 <> ed25519Key2 <> dsaKey2+ keys <- readKeyFile file+ map kind keys @?= ["RSA", "Ed25519", "DSA"]+ , testCase "signed object file" $ withTempDir $ \dir -> do+ let file = dir </> "cert.pem"+ B.writeFile file rsaCertificate+ certs <- readSignedObject file :: IO [SignedCertificate]+ length certs @?= 1+ , testCase "composed empty store is empty" $+ assertBool "not empty" $+ isEmptyCertificateStore (mempty <> mempty)+ ]+ where+ relabel = BC.pack . replace "CERTIFICATE-----" "TRUSTED CERTIFICATE-----" . BC.unpack+ replace old new str@(c : cs)+ | take (length old) str == old = new ++ replace old new (drop (length old) str)+ | otherwise = c : replace old new cs+ replace _ _ [] = []+ broken =+ fromString $+ "-----BEGIN CERTIFICATE-----\n"+ ++ "abc\n"+ ++ "-----END CERTIFICATE-----\n"+ -- | Runs the test suite. main :: IO () main =@@ -400,5 +495,7 @@ "Memory" [ memoryKeyTests , memoryCertificateTests+ , keyOrderTest ]+ , storeTests ]
crypton-x509-store.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: crypton-x509-store-version: 1.10.0+version: 1.10.1 license: BSD3 license-file: LICENSE copyright: Vincent Hanquez <vincent@snarc.org>@@ -19,7 +19,7 @@ source-repository head type: git location: https://github.com/kazu-yamamoto/crypton-certificate- subdir: x509-store+ subdir: crypton-x509-store library exposed-modules:@@ -27,6 +27,7 @@ Data.X509.File Data.X509.Memory + other-modules: Data.X509.ReadFile default-language: Haskell2010 ghc-options: -Wall build-depends:@@ -37,7 +38,7 @@ crypton-asn1-encoding >=0.10.0 && <0.11, crypton-asn1-types >=0.4.1 && <0.5, crypton-pem >=0.2.4 && <0.4,- crypton-x509 >=1.10 && <1.11,+ crypton-x509 >=1.10.3 && <1.11, directory, filepath, mtl@@ -57,4 +58,7 @@ tasty, tasty-hunit, crypton-x509,- crypton-x509-store+ crypton-asn1-types,+ crypton-x509-store,+ directory,+ filepath