network-conduit-tls-1.2.2/0000755000000000000000000000000012714534120013635 5ustar0000000000000000network-conduit-tls-1.2.2/ChangeLog.md0000644000000000000000000000106512714534120016010 0ustar0000000000000000## 1.2.2 * Make runTLS{Client,Server}StartTLS general [#264](https://github.com/snoyberg/conduit/pull/264) ## 1.2.1 * Expose ApplicationStartTLS [#262](https://github.com/snoyberg/conduit/pull/262) ## 1.2.0.1 * tls 1.3 support ## 1.2.0 * Drop system-filepath ## 1.1.2 * Added 'runGeneralTCPServerTLS' function [#208](https://github.com/snoyberg/conduit/pull/208) ## 1.1.1.1 * Fill in `appRawSocket` for streaming-commons 0.1.12 and later ## 1.1.1 * Support chain certificates in network-conduit-tls [#203](https://github.com/snoyberg/conduit/pull/203) network-conduit-tls-1.2.2/LICENSE0000644000000000000000000000207512714534120014646 0ustar0000000000000000Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. network-conduit-tls-1.2.2/network-conduit-tls.cabal0000644000000000000000000000303212714534120020553 0ustar0000000000000000name: network-conduit-tls version: 1.2.2 synopsis: Create TLS-aware network code with conduits description: Uses the tls package for a pure-Haskell implementation. homepage: https://github.com/snoyberg/conduit license: MIT license-file: LICENSE author: Michael Snoyman maintainer: michael@snoyman.com category: Network build-type: Simple cabal-version: >=1.8 extra-source-files: README.md ChangeLog.md library exposed-modules: Data.Conduit.Network.TLS Data.Conduit.Network.TLS.Internal build-depends: base >= 4 && < 5 , bytestring >= 0.9 , tls >= 1.2.15 , conduit-extra >= 1.1 , conduit >= 1.1 , network , transformers , transformers-base , cprng-aes , connection , monad-control , data-default , streaming-commons test-suite test hs-source-dirs: test main-is: main.hs type: exitcode-stdio-1.0 cpp-options: -DTEST build-depends: conduit , conduit-extra , connection , base , mtl , network-conduit-tls , bytestring , HUnit ghc-options: -Wall -threaded network-conduit-tls-1.2.2/README.md0000644000000000000000000000017512714534120015117 0ustar0000000000000000## network-conduit-tls Create TLS-aware network code with conduits. Uses the tls package for a pure-Haskell implementation. network-conduit-tls-1.2.2/Setup.hs0000644000000000000000000000005612714534120015272 0ustar0000000000000000import Distribution.Simple main = defaultMain network-conduit-tls-1.2.2/Data/0000755000000000000000000000000012714534120014506 5ustar0000000000000000network-conduit-tls-1.2.2/Data/Conduit/0000755000000000000000000000000012714534120016113 5ustar0000000000000000network-conduit-tls-1.2.2/Data/Conduit/Network/0000755000000000000000000000000012714534120017544 5ustar0000000000000000network-conduit-tls-1.2.2/Data/Conduit/Network/TLS.hs0000644000000000000000000003517012714534120020550 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE CPP #-} {-# LANGUAGE KindSignatures #-} module Data.Conduit.Network.TLS ( -- * Common ApplicationStartTLS , GeneralApplicationStartTLS -- * Server , TLSConfig , tlsConfigBS , tlsConfig , tlsConfigChainBS , tlsConfigChain , tlsHost , tlsPort -- , tlsCertificate -- , tlsKey , tlsNeedLocalAddr , tlsAppData , runTCPServerTLS , runGeneralTCPServerTLS , runTCPServerStartTLS -- * Client , TLSClientConfig , tlsClientConfig , runTLSClient , runTLSClientStartTLS , tlsClientPort , tlsClientHost , tlsClientUseTLS , tlsClientTLSSettings , tlsClientSockSettings , tlsClientConnectionContext ) where import Control.Applicative ((<$>), (<*>)) import Control.Monad (forever, void) import qualified Data.ByteString.Lazy as L import qualified Network.TLS as TLS import Data.Conduit.Network (sinkSocket, runTCPServerWithHandle, serverSettings, sourceSocket) import Data.Streaming.Network.Internal (AppData (..), HostPreference) import Data.Streaming.Network (ConnectionHandle, safeRecv) import Data.Conduit.Network.TLS.Internal import Data.Conduit (yield, awaitForever, Producer, Consumer) import qualified Data.Conduit.List as CL import Network.Socket (SockAddr (SockAddrInet), sClose) import Network.Socket.ByteString (sendAll) import Control.Exception (bracket) import Control.Monad.Trans.Class (lift) import Control.Monad.IO.Class (liftIO, MonadIO) import Control.Monad.Base (liftBase) import qualified Network.TLS.Extra as TLSExtra import Network.Socket (Socket) import qualified Data.ByteString as S import qualified Data.ByteString.Char8 as S8 import qualified Crypto.Random.AESCtr import qualified Network.Connection as NC import Control.Monad.Trans.Control import Data.Default makeCertDataPath :: FilePath -> [FilePath] -> FilePath -> TlsCertData makeCertDataPath certPath chainCertPaths keyPath = TlsCertData (S.readFile certPath) (mapM S.readFile chainCertPaths) (S.readFile keyPath) makeCertDataBS :: S.ByteString -> [S.ByteString] -> S.ByteString -> TlsCertData makeCertDataBS certBS chainCertsBS keyBS = TlsCertData (return certBS) (return chainCertsBS) (return keyBS) tlsConfig :: HostPreference -> Int -- ^ port -> FilePath -- ^ certificate -> FilePath -- ^ key -> TLSConfig tlsConfig a b c d = tlsConfigChain a b c [] d -- | allow to build a server config directly from raw bytestring data (exact same -- string as if the certificates were read from the filesystem). -- this enables to plug another backend to fetch certifcates (other than FS) tlsConfigBS :: HostPreference -> Int -- ^ port -> S.ByteString -- ^ Certificate raw data -> S.ByteString -- ^ Key file raw data -> TLSConfig tlsConfigBS a b c d = tlsConfigChainBS a b c [] d -- | Like 'tlsConfig', but also allow specifying chain certificates. -- -- Since 1.1.1 tlsConfigChain :: HostPreference -> Int -- ^ Port -> FilePath -- ^ Certificate -> [FilePath] -- ^ Chain certificates -> FilePath -- ^ Key -> TLSConfig tlsConfigChain a b c d e = TLSConfig a b (makeCertDataPath c d e) False -- | Like 'tlsConfigBS', but also allow specifying chain certificates. -- -- Since 1.1.1 tlsConfigChainBS :: HostPreference -> Int -- ^ Port -> S.ByteString -- ^ Certificate raw data -> [S.ByteString] -- ^ Chain certificate raw data -> S.ByteString -- ^ Key file raw data -> TLSConfig tlsConfigChainBS a b c d e = TLSConfig a b (makeCertDataBS c d e) False serverHandshake :: Socket -> TLS.Credentials -> IO (TLS.Context) serverHandshake socket creds = do #if !MIN_VERSION_tls(1,3,0) gen <- Crypto.Random.AESCtr.makeSystem #endif ctx <- TLS.contextNew TLS.Backend { TLS.backendFlush = return () , TLS.backendClose = return () , TLS.backendSend = sendAll socket , TLS.backendRecv = recvExact socket } params #if !MIN_VERSION_tls(1,3,0) gen #endif TLS.handshake ctx return ctx where params = def { TLS.serverWantClientCert = False , TLS.serverSupported = def { TLS.supportedCiphers = ciphers , TLS.supportedVersions = [TLS.SSL3,TLS.TLS10,TLS.TLS11,TLS.TLS12] } , TLS.serverShared = def { TLS.sharedCredentials = creds } } runTCPServerTLS :: TLSConfig -> (AppData -> IO ()) -> IO () runTCPServerTLS TLSConfig{..} app = do creds <- readCreds tlsCertData runTCPServerWithHandle settings (wrapApp creds) where -- convert tls settings to regular conduit network ones settings = serverSettings tlsPort tlsHost -- (const $ return () ) tlsNeedLocalAddr wrapApp creds = app' where app' socket addr mlocal = do ctx <- serverHandshake socket creds app (tlsAppData ctx addr mlocal) TLS.bye ctx -- | -- -- @since 1.2.2 type GeneralApplicationStartTLS m a = (AppData, (AppData -> m ()) -> m ()) -> m a type ApplicationStartTLS = GeneralApplicationStartTLS IO () -- | Like 'runTCPServerTLS', but monad can be any instance of 'MonadBaseControl' 'IO'. -- -- Note that any changes to the monadic state performed by individual -- client handlers will be discarded. If you have mutable state you want -- to share among multiple handlers, you need to use some kind of mutable -- variables. -- -- Since 1.1.2 runGeneralTCPServerTLS :: MonadBaseControl IO m => TLSConfig -> (AppData -> m ()) -> m () runGeneralTCPServerTLS config app = liftBaseWith $ \run -> runTCPServerTLS config $ void . run . app -- | run a server un-crypted but also pass a call-back to trigger a StartTLS handshake -- on the underlying connection -- -- example usage : -- @ -- runTCPServerStartTLS serverConfig $ (appData,startTLS) -> do -- abortTLS <- doSomethingInClear appData -- unless (abortTLS) $ startTls $ appDataTls -> do -- doSomethingSSL appDataTls -- @ runTCPServerStartTLS :: MonadBaseControl IO m => TLSConfig -> GeneralApplicationStartTLS m () -> m () runTCPServerStartTLS TLSConfig{..} app = do creds <- liftBase $ readCreds tlsCertData liftBaseWith $ \run -> runTCPServerWithHandle settings (wrapApp creds run) where -- convert tls settings to regular conduit network ones settings = serverSettings tlsPort tlsHost -- (const $ return () ) tlsNeedLocalAddr wrapApp creds run = clearapp where clearapp socket addr mlocal = let -- setup app data for the clear part of the connection clearData = AppData { appRead' = safeRecv socket 4096 , appWrite' = sendAll socket , appSockAddr' = addr , appLocalAddr' = mlocal #if MIN_VERSION_streaming_commons(0,1,6) , appCloseConnection' = sClose socket #endif #if MIN_VERSION_streaming_commons(0,1,12) , appRawSocket' = Just socket #endif } -- wrap up the current connection with TLS startTls = \app' -> do ctx <- liftBase $ serverHandshake socket creds app' (tlsAppData ctx addr mlocal) liftBase $ TLS.bye ctx in void $ run $ app (clearData, startTls) -- | Create an @AppData@ from an existing tls @Context@ value. This is a lower level function, allowing you to create a connection in any way you want. -- -- Sample usage: -- -- > import Network.Simple.TCP.TLS -- > -- > myapp :: Application IO -- > ... -- > main = do -- > cset <- getDefaultClientSettings -- > connect cset "host" "port" $ -- > (\(ctx, addr) -> myapp $ tlsAppData ctx addr Nothing) -- -- Since 1.0.1 tlsAppData :: TLS.Context -- ^ a TLS context -> SockAddr -- ^ remote address -> Maybe SockAddr -- ^ local address -> AppData tlsAppData ctx addr mlocal = AppData { appRead' = TLS.recvData ctx , appWrite' = TLS.sendData ctx . L.fromChunks . return , appSockAddr' = addr , appLocalAddr' = mlocal #if MIN_VERSION_streaming_commons(0,1,6) , appCloseConnection' = TLS.contextClose ctx #endif #if MIN_VERSION_streaming_commons(0,1,12) , appRawSocket' = Nothing #endif } -- taken from stunnel example in tls-extra ciphers :: [TLS.Cipher] ciphers = [ TLSExtra.cipher_AES128_SHA1 , TLSExtra.cipher_AES256_SHA1 , TLSExtra.cipher_RC4_128_MD5 , TLSExtra.cipher_RC4_128_SHA1 ] readCreds :: TlsCertData -> IO TLS.Credentials readCreds (TlsCertData iocert iochains iokey) = (TLS.credentialLoadX509ChainFromMemory <$> iocert <*> iochains <*> iokey) >>= either (error . ("Error reading TLS credentials: " ++)) (return . TLS.Credentials . return) -- | TLS requires exactly the number of bytes requested to be returned. recvExact :: Socket -> Int -> IO S.ByteString recvExact socket = loop id where loop front rest | rest < 0 = error "Data.Conduit.Network.TLS.recvExact: rest < 0" | rest == 0 = return $ S.concat $ front [] | otherwise = do next <- safeRecv socket rest if S.length next == 0 then return $ S.concat $ front [] else loop (front . (next:)) $ rest - S.length next -- | Settings type for TLS client connection. -- -- Since 1.0.2 data TLSClientConfig = TLSClientConfig { tlsClientPort :: Int -- ^ -- -- Since 1.0.2 , tlsClientHost :: S.ByteString -- ^ -- -- Since 1.0.2 , tlsClientUseTLS :: Bool -- ^ Default is True. If set to @False@, will make a non-TLS connection. -- -- Since 1.0.2 , tlsClientTLSSettings :: NC.TLSSettings -- ^ TLS settings to use. If not provided, defaults will be provided. -- -- Since 1.0.2 , tlsClientSockSettings :: Maybe NC.SockSettings -- ^ Socks configuration; default is @Nothing@. If absent, Socks will not be used. -- -- Since 1.0.2 , tlsClientConnectionContext :: Maybe NC.ConnectionContext -- ^ Connection context. Default is @Nothing@, which will generate a new -- context automatically. If you will be making many connections, it's -- recommended to call 'NC.initConnectionContext' yourself. -- -- Since 1.0.2 } -- | Smart constructor for @TLSClientConfig@. -- -- Since 1.0.2 tlsClientConfig :: Int -- ^ port -> S.ByteString -- ^ host -> TLSClientConfig tlsClientConfig port host = TLSClientConfig { tlsClientPort = port , tlsClientHost = host , tlsClientUseTLS = True , tlsClientTLSSettings = def , tlsClientSockSettings = Nothing , tlsClientConnectionContext = Nothing } -- | Run an application with the given configuration. -- -- Since 1.0.2 runTLSClient :: (MonadBaseControl IO m) => TLSClientConfig -> (AppData -> m a) -> m a runTLSClient TLSClientConfig {..} app = do context <- maybe (liftBase NC.initConnectionContext) return tlsClientConnectionContext let params = NC.ConnectionParams { NC.connectionHostname = S8.unpack tlsClientHost , NC.connectionPort = fromIntegral tlsClientPort , NC.connectionUseSecure = if tlsClientUseTLS then Just tlsClientTLSSettings else Nothing , NC.connectionUseSocks = tlsClientSockSettings } control $ \run -> bracket (NC.connectTo context params) NC.connectionClose (\conn -> run $ app AppData { appRead' = NC.connectionGetChunk conn , appWrite' = NC.connectionPut conn , appSockAddr' = SockAddrInet (fromIntegral tlsClientPort) 0 -- FIXME , appLocalAddr' = Nothing #if MIN_VERSION_streaming_commons(0,1,6) , appCloseConnection' = NC.connectionClose conn #endif #if MIN_VERSION_streaming_commons(0,1,12) , appRawSocket' = Nothing #endif }) -- | Run an application with the given configuration. starting with a clear connection -- but provide also a call back to trigger a StartTLS handshake on the connection -- -- Since 1.0.2 runTLSClientStartTLS :: (MonadBaseControl IO m) => TLSClientConfig -> GeneralApplicationStartTLS m a -> m a runTLSClientStartTLS TLSClientConfig {..} app = do context <- maybe (liftBase NC.initConnectionContext) return tlsClientConnectionContext let params = NC.ConnectionParams { NC.connectionHostname = S8.unpack tlsClientHost , NC.connectionPort = fromIntegral tlsClientPort , NC.connectionUseSecure = Nothing , NC.connectionUseSocks = tlsClientSockSettings } liftBaseOp (bracket (NC.connectTo context params) NC.connectionClose) (\conn -> app ( AppData { appRead' = NC.connectionGetChunk conn , appWrite' = NC.connectionPut conn , appSockAddr' = SockAddrInet (fromIntegral tlsClientPort) 0 -- FIXME , appLocalAddr' = Nothing #if MIN_VERSION_streaming_commons(0,1,6) , appCloseConnection' = NC.connectionClose conn #endif #if MIN_VERSION_streaming_commons(0,1,12) , appRawSocket' = Nothing #endif } , \app' -> do liftBase $ NC.connectionSetSecure context conn tlsClientTLSSettings app' AppData { appRead' = NC.connectionGetChunk conn , appWrite' = NC.connectionPut conn , appSockAddr' = SockAddrInet (fromIntegral tlsClientPort) 0 -- FIXME , appLocalAddr' = Nothing #if MIN_VERSION_streaming_commons(0,1,6) , appCloseConnection' = NC.connectionClose conn #endif #if MIN_VERSION_streaming_commons(0,1,12) , appRawSocket' = Nothing #endif } ) ) -- | Read from a 'NC.Connection'. -- -- Since 1.0.2 sourceConnection :: MonadIO m => NC.Connection -> Producer m S.ByteString sourceConnection conn = loop where loop = do bs <- liftIO $ NC.connectionGetChunk conn if S.null bs then return () else yield bs >> loop -- | Write to a 'NC.Connection'. -- -- Since 1.0.2 sinkConnection :: MonadIO m => NC.Connection -> Consumer S.ByteString m () sinkConnection conn = awaitForever (liftIO . NC.connectionPut conn) network-conduit-tls-1.2.2/Data/Conduit/Network/TLS/0000755000000000000000000000000012714534120020206 5ustar0000000000000000network-conduit-tls-1.2.2/Data/Conduit/Network/TLS/Internal.hs0000644000000000000000000000124612714534120022321 0ustar0000000000000000{-# OPTIONS_HADDOCK not-home #-} module Data.Conduit.Network.TLS.Internal ( TLSConfig (..) , TlsCertData (..) ) where import Prelude hiding (FilePath) import Data.Streaming.Network (HostPreference) import qualified Data.ByteString as S -- structure providing access to certificate and key data through call backs data TlsCertData = TlsCertData { getTLSCert :: IO S.ByteString , getTLSChainCerts :: IO [S.ByteString] , getTLSKey :: IO S.ByteString } data TLSConfig = TLSConfig { tlsHost :: HostPreference , tlsPort :: Int , tlsCertData :: TlsCertData , tlsNeedLocalAddr :: Bool } network-conduit-tls-1.2.2/test/0000755000000000000000000000000012714534120014614 5ustar0000000000000000network-conduit-tls-1.2.2/test/main.hs0000644000000000000000000001211512714534120016074 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} import Test.HUnit import Data.Conduit import Data.Conduit.Network (appSource, appSink) import Data.Conduit.Network.TLS import Control.Concurrent (forkIO, threadDelay, killThread) import qualified Network.Connection as NC import qualified Data.ByteString as BS testKeyRaw :: BS.ByteString testKeyRaw = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAwAU371YZKOVON+S/TpNERcSbe5vWk0kdodR/cC7iwQ40ukO6\nIH7H40THVAWQwlD6kasRdsxcsk+KcOpoBgivw9izJ7ggBp7reFe8mJRp2qMGyK+n89ZRHNlVWl1qSAC/o0A1ldvyfZ2X4nNYHVAFqwhPSsFTxQgxORJbL7qdKy1tirqg\nWpHQMgK6dQJjOEEhrMKmOC2q6l9vbTYuAghDsdtbbEc8FWWVeExiIj8RopPY9+if\nj3BoXxp4WhfiDWmnnBWp71oJIfB1uziLV6PJdA1nKfVbPUeAM0wCFFUCbrjaxdg3\n4RenckCZIJwDo+ff/OSpKynrwznunZW847m2lwIDAQABAoIBAEqjPKS5MLpmt0qe\njYX7VDRSQaWAY52IdA4tTQPMFbO40+H65WQwI35Bg8EzEJuXYm4wsm8c7IMay9Ms\nKhb+VWOo3ap4tWodZ6W1ZMdiGOs1JzPmoz/ydEDkcXrYiLFIKTVJhgqkHdOZ6CnL\nb9qk+i8K4ddK4kbZ8lgevHcG8ISRTV2B8dRc3iohGJ0F6VlL62GnjbExjegsUs4N\n4Ozy8xI4oxlKdZcgutBkfPqdJOWixWPnMXf0PtJVFMzKzVujZlupoonqUUGn51c6\nTVVXAh1pcF0XrmKNscuODFMwBtVfIrfNf/iL1KvIIlKFbUSb/Yu9/9KBvLmfKdxf\nyrtvNBECgYEA5rRdd8IaskROgQxRTJagZn39Sl6oBVFLQ+fy0LGXV3bDbgl7myx8\nOtkKiTMHGT8g6JWv5NMWUgGSZBkMnZSQ/QCtbCxpuDjajxY2GVKU+1EbJjPccuWH\nTnopBuss6WiDbI/Jl9JjPBmhs8EsuAgAOo9yPzgs6SLiMfUwWKkPRdUCgYEA1RMH\nhhKUULqE+/xF214aUqcIk38BCw9g9Uo0pGp4cIfA8iuRachZGsbRpDQyaGRWL+4A\n9hOLPdV2ey6TvNcP/7H6dXrvj4TXLqrxPC2ne2zawqeCkqigxq8Rk55pBF5c52Xz\nX5Rie98TC++gf+fyUTIUS4OqMLg4q1Erk23g5LsCgYEApZg3MtvXj7ep5cUyodfI\nYGj0oyoYTmDQtnhJ+PRQHk637kbOO06OCSt6/YnsAXono+q1q3i8n7ZTHphATuex\nvnh7ApdKdxoP/v7BbCGzoETSSPSWur34BiN3SWkK/qqvEwCOgfRYmG4JfF4fPCU6\nDM6kAa7PxbPtSlClGC6ZMNUCgYEAwp+tIaPa4ZpdWiXmUSe1d4Wm6cL6WvXjJGpx\nhzTRakg1z35IRo2ABltQpmIfIQd1SjZlnl/fsc1HeeDjhXwT2wTgt2phY4B9ZN0z\nmDpDXxPhBigntnpc0N6ceXAakKj4x0xybv2Er4zlQuPQgMSGq+/IZemQDQxYhvOP\nkAyvfX0CgYBEVKvhcXQ9ETmEsk0FxPvpS9CtWXaNWItVzC/z3+mrU2B5JPcBQF72\nBsuoupeq52S+SGH7el5Xp2AoLXjZYsQ9S0t76p6G3lE/cHmnc/QNt4kT6oe5mpv1\nYXIo3/044Cbw2FEkEaj0iucagYCoqhlZTFN8aR6dXFTmvU+k6VP7pg==\n-----END RSA PRIVATE KEY-----" -- self signed certificate corresponding to private key above. -- this certificate will expire circa january 2015 ... testCertificateRaw :: BS.ByteString testCertificateRaw = "-----BEGIN CERTIFICATE-----\nMIIDBjCCAe4CCQDBE77UEng3SDANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJG\nUjETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\ncyBQdHkgTHRkMB4XDTE0MDEwNjIxNTA1OVoXDTE1MDEwNjIxNTA1OVowRTELMAkG\nA1UEBhMCRlIxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\nIFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nAMAFN+9WGSjlTjfkv06TREXEm3ub1pNJHaHUf3Au4sEONLpDuiB+x+NEx1QFkMJQ\n+pGrEXbMXLJPinDqaAYIr8PYsye4IAae63hXvJiUadqjBsivp/PWURzZVVpdakgA\nv6NANZXb8n2dl+JzWB1QBasIT0rBU8UIMTkSWy+6nSstbYq6oFqR0DICunUCYzhB\nIazCpjgtqupfb202LgIIQ7HbW2xHPBVllXhMYiI/EaKT2Pfon49waF8aeFoX4g1p\np5wVqe9aCSHwdbs4i1ejyXQNZyn1Wz1HgDNMAhRVAm642sXYN+EXp3JAmSCcA6Pn\n3/zkqSsp68M57p2VvOO5tpcCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAq1Vy0VBj\nKxuXrpzU8O8bMNrH571Mtjb7tNAhpv77HeyfssW151Rltn71DDPIOqwhoA9zN47I\ns/t/aq1+BmXSdEEb9chbOkZ+KOsJlG/Y0Io4jSK4j4JHlnSBhjItTaoEkkvQtr45\nbyrLYSeixGY5JZd8hIOUcGuru+PPx+SKtuZrnxHF+oXyT9O4BLIe9BYWHvE0Qpop\nvc060w8CIDW4gfYcxxMsA45IrULv5mq2J8bLAtcI9hQY3Z8dPNejsChYTHK6JDEL\n7/G6POAMxenO5cg+Y6Y3OKp5+LrzJNIwfnAnLLFl+/Gb2kC+GcfwZDojuiCJ9iIG\njPwFEAl/7WuMlg==\n-----END CERTIFICATE-----" serverConfig :: TLSConfig serverConfig = tlsConfigBS "*4" 4242 testCertificateRaw testKeyRaw clientConfig :: TLSClientConfig clientConfig = tlsClientConfig 4242 "127.0.0.1" clientConfigNoCA :: TLSClientConfig clientConfigNoCA = clientConfig {tlsClientTLSSettings = NC.TLSSettingsSimple True False False} testSimpleServerClient :: IO () testSimpleServerClient = do -- a simple server that says hello over tls serverThreadId <- forkIO $ runTCPServerTLS serverConfig $ \ad -> yield "hello world" $$ appSink ad -- wait for server to be ready threadDelay 1000000 -- default settings checks CA, the test cert is self-signed. should runTLSClient clientConfigNoCA $ \ad -> do d <- appSource ad $$ (await >>= return) assertEqual "client receives hello world" (Just "hello world") d -- kill the server killThread serverThreadId testSimpleServerClientStartTLS :: IO () testSimpleServerClientStartTLS = do serverThreadId <- forkIO $ runTCPServerStartTLS serverConfig serve threadDelay 100000 runTLSClientStartTLS clientConfigNoCA client killThread serverThreadId where serve (ad, startTls) = do yield "proceed" $$ appSink ad startTls $ \app -> (yield "crypted") $$ appSink app client (ad, startTls) = do -- reads one message from server msg <- appSource ad $$ (await >>= return) assertEqual "server sends proceed" (Just "proceed") msg startTls $ \app -> do msgTls <- appSource app $$ (await >>= return) assertEqual "server sends crypted" (Just "crypted") msgTls main :: IO (Counts) main = runTestTT $ TestList [ TestLabel "TLS Server" $ TestCase testSimpleServerClient , TestLabel "StartTLS" $ TestCase testSimpleServerClientStartTLS ]