13
13
14
14
{-# LANGUAGE CPP, OverloadedStrings, BangPatterns, ScopedTypeVariables #-}
15
15
16
- module Transient.TLS (initTLS ) where
16
+ module Transient.TLS (initTLS , initTLS' ) where
17
17
#ifndef ghcjs_HOST_OS
18
18
import Transient.Internals
19
19
import Transient.Move.Internals
@@ -32,7 +32,6 @@ import qualified Data.ByteString.Lazy as BL
32
32
import qualified Data.ByteString.Lazy.Char8 as BL8
33
33
import qualified Data.ByteString.Char8 as B
34
34
import qualified Data.ByteString as BE
35
- import qualified Data.Certificate.X509 as X
36
35
37
36
import qualified Data.X509.CertificateStore as C
38
37
import Data.Default
@@ -49,18 +48,25 @@ import Debug.Trace
49
48
50
49
51
50
initTLS :: MonadIO m => m ()
52
- initTLS= liftIO $
53
- writeIORef tlsHooks (unsafeCoerce $ (TLS. sendData :: TLS. Context -> BL8. ByteString -> IO () )
54
- ,unsafeCoerce $ (TLS. recvData :: TLS. Context -> IO BE. ByteString )
55
- ,unsafeCoerce $ Transient.TLS. maybeTLSServerHandshake
56
- ,unsafeCoerce $ Transient.TLS. maybeClientTLSHandshake )
51
+ initTLS = initTLS' " certificate.pem" " key.pem"
57
52
53
+ initTLS' :: MonadIO m => FilePath -> FilePath -> m ()
54
+ initTLS' certpath keypath = liftIO $ writeIORef
55
+ tlsHooks
56
+ ( unsafeCoerce $ (TLS. sendData :: TLS. Context -> BL8. ByteString -> IO () )
57
+ , unsafeCoerce $ (TLS. recvData :: TLS. Context -> IO BE. ByteString )
58
+ , unsafeCoerce $ Transient.TLS. maybeTLSServerHandshake certpath keypath
59
+ , unsafeCoerce $ Transient.TLS. maybeClientTLSHandshake
60
+ )
58
61
59
- maybeTLSServerHandshake sock input= do
62
+
63
+ maybeTLSServerHandshake
64
+ :: FilePath -> FilePath -> Socket -> BL8. ByteString -> TransIO ()
65
+ maybeTLSServerHandshake certpath keypath sock input= do
60
66
if ((not $ BL. null input) && BL. head input == 0x16 )
61
67
then do
62
68
mctx <- liftIO $ ( do
63
- ctx <- makeServerContext ssettings sock input
69
+ ctx <- makeServerContext ( ssettings certpath keypath) sock input
64
70
TLS. handshake ctx
65
71
return $ Just ctx )
66
72
`catch` \ (e:: SomeException ) -> do
@@ -77,13 +83,13 @@ maybeTLSServerHandshake sock input= do
77
83
onException $ \ (e:: SomeException ) -> liftIO $ TLS. contextClose ctx
78
84
else return ()
79
85
80
- ssettings = unsafePerformIO $ do
81
- cred <- either error id <$> TLS. credentialLoadX509
82
- " certificate.pem"
83
- " key.pem"
86
+ ssettings :: FilePath -> FilePath -> ServerParams
87
+ ssettings certpath keypath = unsafePerformIO $ do
88
+ cred <- either error id <$> TLS. credentialLoadX509 certpath keypath
84
89
return $ makeServerSettings cred
85
90
86
91
92
+ maybeClientTLSHandshake :: String -> Socket -> BL8. ByteString -> TransIO ()
87
93
maybeClientTLSHandshake hostname sock input = do
88
94
mctx <- liftIO $ (do
89
95
global <- getSystemCertificateStore
0 commit comments