on master help sign in
New version of certificate
commit5a66b93fbdb0abd90066dccd60550c032ce3c588 authorMichael Snoyman <michael@snoyman.com> dateTue 17 Apr 2012, 13:55 (14 years ago) parentb57a898
2 files changed+21−19
warp-tls/Network/Wai/Handler/WarpTLS.hs+18−17 warp-tls/warp-tls.cabal+3−2
warp-tls/Network/Wai/Handler/WarpTLS.hs+18−17
@@ -17,10 +17,12 @@ import qualified Data.Conduit.List as CL
1717import Control.Exception (bracket, handle, SomeException)
1818import qualified Network.TLS.Extra as TLSExtra
1919import qualified Data.Certificate.X509 as X509
20−import qualified Data.Certificate.PEM as PEM
2120import qualified Data.ByteString as B
2221import qualified Data.Certificate.KeyRSA as KeyRSA
2322import Data.Conduit.Network (bindPort)
23+import Data.Either (rights)
24+import Control.Applicative ((<$>))
25+import qualified Data.PEM as PEM
2426
2527data TLSSettings = TLSSettings
2628 { certFile :: FilePath
@@ -75,22 +77,21 @@ ciphers =
7577
7678readCertificate :: FilePath -> IO X509.X509
7779readCertificate filepath = do
78− content <- B.readFile filepath
79− certdata <-
80− case PEM.parsePEMCert content of
81− Nothing -> error "no valid certificate section"
82− Just x -> return x
83− case X509.decodeCertificate $ L.fromChunks [certdata] of
84− Left err -> error ("cannot decode certificate: " ++ err)
85− Right x -> return x
80+ certs <- rights . parseCerts . PEM.pemParseBS <$> B.readFile filepath
81+ case certs of
82+ [] -> error "no valid certificate found"
83+ (x:_) -> return x
84+ where parseCerts (Right pems) = map (X509.decodeCertificate . L.fromChunks . (:[]) . PEM.pemContent)
85+ $ filter (flip elem ["CERTIFICATE", "TRUSTED CERTIFICATE"] . PEM.pemName) pems
86+ parseCerts (Left err) = error $ "cannot parse PEM file: " ++ err
8687
8788readPrivateKey :: FilePath -> IO TLS.PrivateKey
8889readPrivateKey filepath = do
89− content <- B.readFile filepath
90− pkdata <-
91− case PEM.parsePEMKeyRSA content of
92− Nothing -> error "no valid RSA key section"
93− Just x -> return (L.fromChunks [x])
94− case KeyRSA.decodePrivate pkdata of
95− Left err -> error ("cannot decode key: " ++ err)
96− Right (_pub, x) -> return $ TLS.PrivRSA x
90+ pk <- rights . parseKey . PEM.pemParseBS <$> B.readFile filepath
91+ case pk of
92+ [] -> error "no valid RSA key found"
93+ (x:_) -> return x
94+
95+ where parseKey (Right pems) = map (fmap (TLS.PrivRSA . snd) . KeyRSA.decodePrivate . L.fromChunks . (:[]) . PEM.pemContent)
96+ $ filter ((== "RSA PRIVATE KEY") . PEM.pemName) pems
97+ parseKey (Left err) = error $ "Cannot parse PEM file: " ++ err
warp-tls/warp-tls.cabal+3−2
@@ -1,5 +1,5 @@
11Name: warp-tls
2−Version: 1.2.0.1
2+Version: 1.2.0.2
33Synopsis: SSL support for Warp via the TLS package
44License: MIT
55License-file: LICENSE
@@ -20,7 +20,8 @@ Library
2020 , transformers >= 0.2 && < 0.4
2121 , conduit >= 0.4 && < 0.5
2222 , network-conduit >= 0.4 && < 0.5
23− , certificate >= 1.1 && < 1.2
23+ , certificate >= 1.2 && < 1.3
24+ , pem >= 0.1 && < 0.2
2425 , cryptocipher >= 0.3 && < 0.4
2526 , tls-extra >= 0.4.3 && < 0.5
2627 , tls >= 0.9 && < 0.10