Skip to content

Commit 90fa389

Browse files
authored
Merge pull request #1 from ivanovs-4/master
Use `x509-system` instead of `certificate`. Add `initTLS'`
2 parents 03bdbdc + 791b3fb commit 90fa389

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/Transient/TLS.hs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
{-# LANGUAGE CPP, OverloadedStrings, BangPatterns, ScopedTypeVariables #-}
1515

16-
module Transient.TLS(initTLS) where
16+
module Transient.TLS(initTLS, initTLS') where
1717
#ifndef ghcjs_HOST_OS
1818
import Transient.Internals
1919
import Transient.Move.Internals
@@ -32,7 +32,6 @@ import qualified Data.ByteString.Lazy as BL
3232
import qualified Data.ByteString.Lazy.Char8 as BL8
3333
import qualified Data.ByteString.Char8 as B
3434
import qualified Data.ByteString as BE
35-
import qualified Data.Certificate.X509 as X
3635

3736
import qualified Data.X509.CertificateStore as C
3837
import Data.Default
@@ -49,18 +48,25 @@ import Debug.Trace
4948

5049

5150
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"
5752

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+
)
5861

59-
maybeTLSServerHandshake sock input= do
62+
63+
maybeTLSServerHandshake
64+
:: FilePath -> FilePath -> Socket -> BL8.ByteString -> TransIO ()
65+
maybeTLSServerHandshake certpath keypath sock input= do
6066
if ((not $ BL.null input) && BL.head input == 0x16)
6167
then do
6268
mctx <- liftIO $( do
63-
ctx <- makeServerContext ssettings sock input
69+
ctx <- makeServerContext (ssettings certpath keypath) sock input
6470
TLS.handshake ctx
6571
return $Just ctx )
6672
`catch` \(e:: SomeException) -> do
@@ -77,13 +83,13 @@ maybeTLSServerHandshake sock input= do
7783
onException $ \(e:: SomeException) -> liftIO $ TLS.contextClose ctx
7884
else return ()
7985

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
8489
return $ makeServerSettings cred
8590

8691

92+
maybeClientTLSHandshake :: String -> Socket -> BL8.ByteString -> TransIO ()
8793
maybeClientTLSHandshake hostname sock input = do
8894
mctx <- liftIO $ (do
8995
global <- getSystemCertificateStore

transient-universe-tls.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ source-repository head
2020

2121
library
2222
build-depends:
23-
base >=4.8 && <5.9, tls, cprng-aes, certificate, transient, transient-universe >= 0.4.1,
24-
bytestring, data-default, network, x509-store
23+
base >=4.8 && <5.9, tls, cprng-aes, transient, transient-universe >= 0.4.1,
24+
bytestring, data-default, network, x509-store, x509-system
2525
default-language: Haskell2010
2626
hs-source-dirs: src
2727
exposed-modules:

0 commit comments

Comments
 (0)