Created
April 10, 2015 16:17
-
-
Save berdario/382e8cc73821a9f4d9db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE OverloadedStrings #-} | |
| import Prelude hiding (putStrLn, writeFile) | |
| import System.Environment (getArgs) | |
| import Data.Word (Word16) | |
| import Data.Maybe (fromMaybe) | |
| import Data.Monoid ((<>), mempty) | |
| import Data.ByteString (ByteString, writeFile) | |
| import Data.ByteString.Char8 (putStrLn) | |
| import Text.Regex.TDFA (match, makeRegexOpts, blankCompOpt) | |
| import Text.Regex.TDFA.Common | |
| import Data.Conduit.Shell (run, proc, conduit, ($|), Segment) | |
| import Conduit (foldC) | |
| type Port = Word16 | |
| emptyInput = conduit mempty | |
| tlsConnect :: String -> Maybe Port -> Segment () | |
| tlsConnect hostname mPort = proc "openssl" ["s_client", "-showcerts", "-connect", host] | |
| where | |
| host = hostname <> ":" <> (show $ fromMaybe 443 mPort) | |
| dumpCerts host port = emptyInput $| tlsConnect host port $| conduit foldC | |
| extractCert :: ByteString -> ByteString | |
| extractCert dump = cert | |
| where | |
| regex = makeRegexOpts blankCompOpt (ExecOption{captureGroups=False}) ("-----BEGIN CERTIFICATE-----[^-]*-----END CERTIFICATE-----" :: ByteString) | |
| (_, cert, _) = (match regex dump :: (ByteString, ByteString, ByteString)) | |
| verifyCert root cert = proc "openssl" ["verify", "-CAfile", root, cert] | |
| main = do root:[host] <- getArgs | |
| cert <- fmap extractCert $ run $ dumpCerts host Nothing | |
| writeFile "/tmp/tempdownloadedcert" cert | |
| run $ verifyCert root "/tmp/tempdownloadedcert" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment