{-# LANGUAGE ScopedTypeVariables #-}
module OpenSSL.X509.SystemStore.Unix
( contextLoadSystemCerts
) where
import OpenSSL.Session (SSLContext, contextSetCADirectory, contextSetCAFile)
import qualified System.Posix.Files as U
import Control.Exception (try, IOException)
import System.IO.Unsafe (unsafePerformIO)
contextLoadSystemCerts :: SSLContext -> IO ()
contextLoadSystemCerts =
unsafePerformIO $ loop defaultSystemPaths
where
loop ((isDir, path) : rest) = do
mst <- try $ U.getFileStatus path
:: IO (Either IOException U.FileStatus)
case mst of
Right st | isDir, U.isDirectory st ->
return (flip contextSetCADirectory path)
Right st | not isDir, U.isRegularFile st ->
return (flip contextSetCAFile path)
_ -> loop rest
loop [] = return (const $ return ())
{-# NOINLINE contextLoadSystemCerts #-}
defaultSystemPaths :: [(Bool, FilePath)]
defaultSystemPaths =
[ (False, "/etc/pki/tls/certs/ca-bundle.crt" )
, (True , "/etc/ssl/certs" )
, (True , "/system/etc/security/cacerts" )
, (False, "/etc/ssl/cert.pem" )
, (False, "/usr/share/ssl/certs/ca-bundle.crt" )
, (False, "/usr/local/share/certs/ca-root-nss.crt")
, (True , "/usr/local/share/certs" )
]