-
Notifications
You must be signed in to change notification settings - Fork 34
/
TLS.hs
75 lines (68 loc) · 3.29 KB
/
TLS.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-|
Module : Database.MySQL.Connection
Description : TLS support for mysql-haskell via @tls@ package.
Copyright : (c) Winterland, 2016
License : BSD
Maintainer : [email protected]
Stability : experimental
Portability : PORTABLE
This module provides secure MySQL connection using 'tls' package, please make sure your certificate is v3 extension enabled.
-}
module Database.MySQL.TLS (
connect
, connectDetail
, module Data.TLSSetting
) where
import Control.Exception (bracketOnError, throwIO)
import qualified Data.Binary as Binary
import qualified Data.Binary.Put as Binary
import qualified Data.Connection as Conn
import Data.IORef (newIORef)
import Data.TLSSetting
import Database.MySQL.Connection hiding (connect, connectDetail)
import Database.MySQL.Protocol.Auth
import Database.MySQL.Protocol.Packet
import qualified Network.TLS as TLS
import qualified System.IO.Streams.TCP as TCP
import qualified Data.Connection as TCP
import qualified System.IO.Streams.TLS as TLS
--------------------------------------------------------------------------------
-- | Provide a 'TLS.ClientParams' and a subject name to establish a TLS connection.
--
connect :: ConnectInfo -> (TLS.ClientParams, String) -> IO MySQLConn
connect c cp = fmap snd (connectDetail c cp)
connectDetail :: ConnectInfo -> (TLS.ClientParams, String) -> IO (Greeting, MySQLConn)
connectDetail (ConnectInfo host port db user pass charset) (cparams, subName) =
bracketOnError (connectWithBufferSize host port bUFSIZE)
(TCP.close) $ \ c -> do
let is = TCP.source c
is' <- decodeInputStream is
p <- readPacket is'
greet <- decodeFromPacket p
if supportTLS (greetingCaps greet)
then do
let cparams' = cparams {
TLS.clientUseServerNameIndication = False
, TLS.clientServerIdentification = (subName, "")
}
let (sock, sockAddr) = Conn.connExtraInfo c
write c (encodeToPacket 1 $ sslRequest charset)
bracketOnError (TLS.contextNew sock cparams')
( \ ctx -> TLS.bye ctx >> TCP.close c ) $ \ ctx -> do
TLS.handshake ctx
tc <- TLS.tLsToConnection (ctx, sockAddr)
let tlsIs = TCP.source tc
tlsIs' <- decodeInputStream tlsIs
let auth = mkAuth db user pass charset greet
write tc (encodeToPacket 2 auth)
q <- readPacket tlsIs'
if isOK q
then do
consumed <- newIORef True
let conn = MySQLConn tlsIs' (write c) (TCP.close c) consumed
return (greet, conn)
else TCP.close c >> decodeFromPacket q >>= throwIO . ERRException
else error "Database.MySQL.TLS: server doesn't support TLS connection"
where
connectWithBufferSize h p bs = TCP.connectSocket h p >>= TCP.socketToConnection bs
write c a = TCP.send c $ Binary.runPut . Binary.put $ a