@@ -33,18 +33,15 @@ import qualified Data.Conduit as C
33import qualified Data.Conduit.List as CL
33import qualified Data.Conduit.List as CL
34import Control.Exception (bracket, finally, handle)
34import Control.Exception (bracket, finally, handle)
35import qualified Network.TLS.Extra as TLSExtra
35import qualified Network.TLS.Extra as TLSExtra
36−import qualified Data.Certificate.X509 as X509
37import qualified Data.ByteString as B
36import qualified Data.ByteString as B
38−import qualified Data.Certificate.KeyRSA as KeyRSA
39import Data.Conduit.Network (bindPort)
37import Data.Conduit.Network (bindPort)
40−import Data.Either (rights)
41import Control.Applicative ((<$>))
38import Control.Applicative ((<$>))
42−import qualified Data.PEM as PEM
43import Data.Conduit.Network (sourceSocket, sinkSocket, acceptSafe)
39import Data.Conduit.Network (sourceSocket, sinkSocket, acceptSafe)
44import Data.Maybe (fromMaybe)
40import Data.Maybe (fromMaybe)
45import qualified Data.IORef as I
41import qualified Data.IORef as I
46import Control.Exception (Exception, throwIO)
42import Control.Exception (Exception, throwIO)
47import Data.Typeable (Typeable)
43import Data.Typeable (Typeable)
44+import Data.Default.Class
48import qualified Data.Conduit.Binary as CB
45import qualified Data.Conduit.Binary as CB
49#if MIN_VERSION_tls(1, 1, 3)
46#if MIN_VERSION_tls(1, 1, 3)
50import qualified Crypto.Random.AESCtr
47import qualified Crypto.Random.AESCtr
@@ -100,7 +97,7 @@ defaultTlsSettings = TLSSettings
100 { certFile = "certificate.pem"
97 { certFile = "certificate.pem"
101 , keyFile = "key.pem"
98 , keyFile = "key.pem"
102 , onInsecure = DenyInsecure "This server only accepts secure HTTPS connections."
99 , onInsecure = DenyInsecure "This server only accepts secure HTTPS connections."
103− , tlsLogging = TLS.defaultLogging
100+ , tlsLogging = def
104 , tlsAllowedVersions = [TLS.SSL3,TLS.TLS10,TLS.TLS11,TLS.TLS12]
101 , tlsAllowedVersions = [TLS.SSL3,TLS.TLS10,TLS.TLS11,TLS.TLS12]
105 , tlsCiphers = ciphers
102 , tlsCiphers = ciphers
@@ -109,16 +106,16 @@ defaultTlsSettings = TLSSettings
109-- specified 'Socket'.
106-- specified 'Socket'.
110runTLSSocket :: TLSSettings -> Settings -> Socket -> Application -> IO ()
107runTLSSocket :: TLSSettings -> Settings -> Socket -> Application -> IO ()
111runTLSSocket TLSSettings {..} set sock app = do
108runTLSSocket TLSSettings {..} set sock app = do
112− certs <- readCertificates certFile
109+ credential <- either error id <$> TLS.credentialLoadX509 certFile keyFile
113− pk <- readPrivateKey keyFile
114− let params =
110+ let params = def
115− TLS.updateServerParams
116− (\sp -> sp { TLS.serverWantClientCert = False }) $
111+ { TLS.serverWantClientCert = False
117− TLS.defaultParamsServer
112+ , TLS.serverSupported = def
118− { TLS.pAllowedVersions = tlsAllowedVersions
113+ { TLS.supportedVersions = tlsAllowedVersions
119− , TLS.pCiphers = tlsCiphers
114+ , TLS.supportedCiphers = tlsCiphers
120− , TLS.pCertificates = zip certs $ (Just pk):repeat Nothing
115+ }
121− , TLS.pLogging = tlsLogging
116+ , TLS.serverShared = def
117+ { TLS.sharedCredentials = TLS.Credentials [credential]
123 runSettingsConnectionMaker set (getter params) app
120 runSettingsConnectionMaker set (getter params) app
@@ -149,6 +146,7 @@ runTLSSocket TLSSettings {..} set sock app = do
149+ TLS.contextHookSetLogging ctx tlsLogging
152 TLS.handshake ctx
150 TLS.handshake ctx
153 buf <- allocateBuffer bufferSize
151 buf <- allocateBuffer bufferSize
154 let conn = Connection
152 let conn = Connection
@@ -210,24 +208,3 @@ ciphers =
210 , TLSExtra.cipher_RC4_128_MD5
208 , TLSExtra.cipher_RC4_128_MD5
211 , TLSExtra.cipher_RC4_128_SHA1
209 , TLSExtra.cipher_RC4_128_SHA1
214−readCertificates :: FilePath -> IO [X509.X509]
215−readCertificates filepath = do
216− certs <- rights . parseCerts . PEM.pemParseBS <$> B.readFile filepath
218− []-> error "no valid certificate found"
220− where parseCerts (Right pems) = map (X509.decodeCertificate . L.fromChunks . (:[]) . PEM.pemContent)
221− $ filter (flip elem ["CERTIFICATE", "TRUSTED CERTIFICATE"] . PEM.pemName) pems
222− parseCerts (Left err) = error $ "cannot parse PEM file: " ++ err
224−readPrivateKey :: FilePath -> IO TLS.PrivateKey
225−readPrivateKey filepath = do
226− pk <- rights . parseKey . PEM.pemParseBS <$> B.readFile filepath
228− [] -> error "no valid RSA key found"
231− where parseKey (Right pems) = map (fmap (TLS.PrivRSA . snd) . KeyRSA.decodePrivate . L.fromChunks . (:[]) . PEM.pemContent)
232− $ filter ((== "RSA PRIVATE KEY") . PEM.pemName) pems
233− parseKey (Left err) = error $ "Cannot parse PEM file: " ++ err