2011-03-02 08:43:05 +00:00
|
|
|
{-# OPTIONS_HADDOCK hide #-}
|
2011-03-01 20:01:40 +00:00
|
|
|
-- |
|
|
|
|
-- Module : Network.TLS.Core
|
|
|
|
-- License : BSD-style
|
|
|
|
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : unknown
|
|
|
|
--
|
|
|
|
module Network.TLS.Core
|
2011-03-01 23:09:17 +00:00
|
|
|
(
|
2011-03-02 07:56:37 +00:00
|
|
|
-- * Context configuration
|
2011-03-01 23:09:17 +00:00
|
|
|
TLSParams(..)
|
2011-05-04 07:41:16 +00:00
|
|
|
, TLSLogging(..)
|
2011-05-13 06:02:44 +00:00
|
|
|
, TLSCertificateUsage(..)
|
|
|
|
, TLSCertificateRejectReason(..)
|
2011-05-04 07:41:16 +00:00
|
|
|
, defaultLogging
|
2011-03-01 20:01:40 +00:00
|
|
|
, defaultParams
|
2011-03-02 07:56:37 +00:00
|
|
|
|
|
|
|
-- * Context object
|
2011-03-01 20:01:40 +00:00
|
|
|
, TLSCtx
|
2011-03-02 07:56:37 +00:00
|
|
|
, ctxHandle
|
2011-06-13 07:19:29 +00:00
|
|
|
, ctxEOF
|
2011-03-02 07:56:37 +00:00
|
|
|
|
2011-03-19 21:45:43 +00:00
|
|
|
-- * Internal packet sending and receiving
|
|
|
|
, sendPacket
|
|
|
|
, recvPacket
|
|
|
|
|
2011-03-02 07:56:37 +00:00
|
|
|
-- * Creating a context
|
2011-03-01 20:01:40 +00:00
|
|
|
, client
|
|
|
|
, server
|
2011-03-02 07:56:37 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- * Initialisation and Termination of context
|
2011-03-01 20:01:40 +00:00
|
|
|
, bye
|
2011-03-01 23:09:17 +00:00
|
|
|
, handshake
|
2011-03-02 07:56:37 +00:00
|
|
|
|
|
|
|
-- * High level API
|
2011-03-01 20:01:40 +00:00
|
|
|
, sendData
|
2011-03-02 07:41:59 +00:00
|
|
|
, recvData
|
2011-03-01 20:01:40 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Network.TLS.Struct
|
2011-08-12 17:41:49 +00:00
|
|
|
import Network.TLS.Record
|
2011-03-01 20:01:40 +00:00
|
|
|
import Network.TLS.Cipher
|
|
|
|
import Network.TLS.Compression
|
|
|
|
import Network.TLS.Crypto
|
2011-03-01 20:01:40 +00:00
|
|
|
import Network.TLS.Packet
|
|
|
|
import Network.TLS.State
|
|
|
|
import Network.TLS.Sending
|
|
|
|
import Network.TLS.Receiving
|
2011-03-02 07:35:25 +00:00
|
|
|
import Data.Maybe
|
2011-03-01 20:01:40 +00:00
|
|
|
import Data.Certificate.X509
|
2011-03-02 07:35:25 +00:00
|
|
|
import Data.List (intersect, intercalate, find)
|
2011-03-01 20:01:40 +00:00
|
|
|
import qualified Data.ByteString as B
|
2011-03-01 20:01:40 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2011-03-01 20:01:40 +00:00
|
|
|
|
2011-04-11 18:54:21 +00:00
|
|
|
import Crypto.Random
|
2011-03-01 20:01:40 +00:00
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Control.Concurrent.MVar
|
|
|
|
import Control.Monad.State
|
2011-05-13 07:39:15 +00:00
|
|
|
import Control.Exception (throwIO, Exception(), onException, fromException, catch)
|
2011-06-13 07:19:29 +00:00
|
|
|
import Data.IORef
|
2011-03-01 23:09:17 +00:00
|
|
|
import System.IO (Handle, hSetBuffering, BufferMode(..), hFlush)
|
2011-06-13 07:19:29 +00:00
|
|
|
import System.IO.Error (mkIOError, eofErrorType)
|
2011-05-13 07:39:15 +00:00
|
|
|
import Prelude hiding (catch)
|
2011-03-01 20:01:40 +00:00
|
|
|
|
2011-05-04 07:41:16 +00:00
|
|
|
data TLSLogging = TLSLogging
|
|
|
|
{ loggingPacketSent :: String -> IO ()
|
|
|
|
, loggingPacketRecv :: String -> IO ()
|
|
|
|
, loggingIOSent :: Bytes -> IO ()
|
|
|
|
, loggingIORecv :: Header -> Bytes -> IO ()
|
|
|
|
}
|
|
|
|
|
2011-05-13 06:02:44 +00:00
|
|
|
-- | Certificate and Chain rejection reason
|
|
|
|
data TLSCertificateRejectReason =
|
|
|
|
CertificateRejectExpired
|
|
|
|
| CertificateRejectRevoked
|
|
|
|
| CertificateRejectUnknownCA
|
|
|
|
| CertificateRejectOther String
|
|
|
|
deriving (Show,Eq)
|
|
|
|
|
|
|
|
-- | Certificate Usage callback possible returns values.
|
|
|
|
data TLSCertificateUsage =
|
|
|
|
CertificateUsageAccept -- ^ usage of certificate accepted
|
|
|
|
| CertificateUsageReject TLSCertificateRejectReason -- ^ usage of certificate rejected
|
|
|
|
deriving (Show,Eq)
|
|
|
|
|
2011-03-01 20:01:40 +00:00
|
|
|
data TLSParams = TLSParams
|
|
|
|
{ pConnectVersion :: Version -- ^ version to use on client connection.
|
|
|
|
, pAllowedVersions :: [Version] -- ^ allowed versions that we can use.
|
|
|
|
, pCiphers :: [Cipher] -- ^ all ciphers supported ordered by priority.
|
|
|
|
, pCompressions :: [Compression] -- ^ all compression supported ordered by priority.
|
|
|
|
, pWantClientCert :: Bool -- ^ request a certificate from client.
|
|
|
|
-- use by server only.
|
2011-06-07 06:41:31 +00:00
|
|
|
, pUseSecureRenegotiation :: Bool -- notify that we want to use secure renegotation
|
2011-03-01 20:01:40 +00:00
|
|
|
, pCertificates :: [(X509, Maybe PrivateKey)] -- ^ the cert chain for this context with the associated keys if any.
|
2011-05-04 07:41:16 +00:00
|
|
|
, pLogging :: TLSLogging -- ^ callback for logging
|
2011-05-13 06:02:44 +00:00
|
|
|
, onCertificatesRecv :: ([X509] -> IO TLSCertificateUsage) -- ^ callback to verify received cert chain.
|
2011-03-01 20:01:40 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 07:41:16 +00:00
|
|
|
defaultLogging :: TLSLogging
|
|
|
|
defaultLogging = TLSLogging
|
|
|
|
{ loggingPacketSent = (\_ -> return ())
|
|
|
|
, loggingPacketRecv = (\_ -> return ())
|
|
|
|
, loggingIOSent = (\_ -> return ())
|
|
|
|
, loggingIORecv = (\_ _ -> return ())
|
|
|
|
}
|
|
|
|
|
2011-03-01 20:01:40 +00:00
|
|
|
defaultParams :: TLSParams
|
|
|
|
defaultParams = TLSParams
|
2011-06-07 06:41:31 +00:00
|
|
|
{ pConnectVersion = TLS10
|
|
|
|
, pAllowedVersions = [TLS10,TLS11]
|
|
|
|
, pCiphers = []
|
|
|
|
, pCompressions = [nullCompression]
|
|
|
|
, pWantClientCert = False
|
|
|
|
, pUseSecureRenegotiation = True
|
|
|
|
, pCertificates = []
|
|
|
|
, pLogging = defaultLogging
|
|
|
|
, onCertificatesRecv = (\_ -> return CertificateUsageAccept)
|
2011-03-01 20:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
instance Show TLSParams where
|
|
|
|
show p = "TLSParams { " ++ (intercalate "," $ map (\(k,v) -> k ++ "=" ++ v)
|
|
|
|
[ ("connectVersion", show $ pConnectVersion p)
|
|
|
|
, ("allowedVersions", show $ pAllowedVersions p)
|
|
|
|
, ("ciphers", show $ pCiphers p)
|
|
|
|
, ("compressions", show $ pCompressions p)
|
|
|
|
, ("want-client-cert", show $ pWantClientCert p)
|
|
|
|
, ("certificates", show $ length $ pCertificates p)
|
|
|
|
]) ++ " }"
|
2011-03-01 20:01:40 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | A TLS Context is a handle augmented by tls specific state and parameters
|
2011-03-01 20:01:40 +00:00
|
|
|
data TLSCtx = TLSCtx
|
2011-03-19 08:37:47 +00:00
|
|
|
{ ctxHandle :: Handle -- ^ return the handle associated with this context
|
|
|
|
, ctxParams :: TLSParams
|
2011-03-01 20:01:40 +00:00
|
|
|
, ctxState :: MVar TLSState
|
2011-06-13 07:19:29 +00:00
|
|
|
, ctxEOF_ :: IORef Bool -- ^ is the handle as EOFed or not.
|
2011-03-01 20:01:40 +00:00
|
|
|
}
|
|
|
|
|
2011-06-13 07:19:29 +00:00
|
|
|
ctxEOF :: MonadIO m => TLSCtx -> m Bool
|
|
|
|
ctxEOF ctx = liftIO (readIORef $ ctxEOF_ ctx)
|
|
|
|
|
2011-05-12 08:10:26 +00:00
|
|
|
throwCore :: (MonadIO m, Exception e) => e -> m a
|
|
|
|
throwCore = liftIO . throwIO
|
|
|
|
|
2011-03-01 20:01:40 +00:00
|
|
|
newCtx :: Handle -> TLSParams -> TLSState -> IO TLSCtx
|
2011-04-24 12:43:57 +00:00
|
|
|
newCtx handle params st = do
|
2011-03-01 20:01:40 +00:00
|
|
|
hSetBuffering handle NoBuffering
|
2011-04-24 12:43:57 +00:00
|
|
|
stvar <- newMVar st
|
2011-06-13 07:19:29 +00:00
|
|
|
eof <- newIORef False
|
2011-03-01 20:01:40 +00:00
|
|
|
return $ TLSCtx
|
|
|
|
{ ctxHandle = handle
|
|
|
|
, ctxParams = params
|
|
|
|
, ctxState = stvar
|
2011-06-13 07:19:29 +00:00
|
|
|
, ctxEOF_ = eof
|
2011-03-01 20:01:40 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 07:41:16 +00:00
|
|
|
ctxLogging :: TLSCtx -> TLSLogging
|
|
|
|
ctxLogging = pLogging . ctxParams
|
|
|
|
|
2011-03-01 20:01:40 +00:00
|
|
|
usingState :: MonadIO m => TLSCtx -> TLSSt a -> m (Either TLSError a)
|
2011-05-13 06:12:54 +00:00
|
|
|
usingState ctx f = liftIO (takeMVar mvar) >>= \st -> liftIO $ onException (execAndStore st) (putMVar mvar st)
|
2011-03-01 20:01:40 +00:00
|
|
|
where
|
|
|
|
mvar = ctxState ctx
|
|
|
|
execAndStore st = do
|
|
|
|
let (a, newst) = runTLSState f st
|
2011-05-13 06:12:54 +00:00
|
|
|
putMVar mvar newst
|
2011-03-01 20:01:40 +00:00
|
|
|
return a
|
|
|
|
|
|
|
|
usingState_ :: MonadIO m => TLSCtx -> TLSSt a -> m a
|
|
|
|
usingState_ ctx f = do
|
|
|
|
ret <- usingState ctx f
|
|
|
|
case ret of
|
2011-06-12 20:39:34 +00:00
|
|
|
Left err -> throwCore err
|
2011-03-01 20:01:40 +00:00
|
|
|
Right r -> return r
|
|
|
|
|
|
|
|
getStateRNG :: MonadIO m => TLSCtx -> Int -> m Bytes
|
2011-04-11 18:54:21 +00:00
|
|
|
getStateRNG ctx n = usingState_ ctx (genTLSRandom n)
|
2011-03-01 20:01:40 +00:00
|
|
|
|
|
|
|
whileStatus :: MonadIO m => TLSCtx -> (TLSStatus -> Bool) -> m a -> m ()
|
|
|
|
whileStatus ctx p a = do
|
|
|
|
b <- usingState_ ctx (p . stStatus <$> get)
|
|
|
|
when b (a >> whileStatus ctx p a)
|
|
|
|
|
2011-05-13 07:39:15 +00:00
|
|
|
errorToAlert :: TLSError -> Packet
|
2011-06-10 20:24:46 +00:00
|
|
|
errorToAlert (Error_Protocol (_, _, ad)) = Alert [(AlertLevel_Fatal, ad)]
|
|
|
|
errorToAlert _ = Alert [(AlertLevel_Fatal, InternalError)]
|
2011-05-13 07:39:15 +00:00
|
|
|
|
2011-06-13 07:19:29 +00:00
|
|
|
setEOF :: MonadIO m => TLSCtx -> m ()
|
|
|
|
setEOF ctx = liftIO $ writeIORef (ctxEOF_ ctx) True
|
|
|
|
|
|
|
|
readExact :: MonadIO m => TLSCtx -> Int -> m Bytes
|
|
|
|
readExact ctx sz = do
|
|
|
|
hdrbs <- liftIO $ B.hGet (ctxHandle ctx) sz
|
|
|
|
when (B.length hdrbs < sz) $ do
|
|
|
|
setEOF ctx
|
|
|
|
if B.null hdrbs
|
|
|
|
then throwCore Error_EOF
|
|
|
|
else throwCore (Error_Packet ("partial packet: expecting " ++ show sz ++ " bytes, got: " ++ (show $B.length hdrbs)))
|
|
|
|
return hdrbs
|
|
|
|
|
2011-06-12 20:39:17 +00:00
|
|
|
-- | receive one packet from the context that contains 1 or
|
|
|
|
-- many messages (many only in case of handshake). if will returns a
|
2011-03-02 08:43:05 +00:00
|
|
|
-- TLSError if the packet is unexpected or malformed
|
2011-06-10 20:24:46 +00:00
|
|
|
recvPacket :: MonadIO m => TLSCtx -> m (Either TLSError Packet)
|
2011-03-01 20:01:40 +00:00
|
|
|
recvPacket ctx = do
|
2011-06-13 07:19:29 +00:00
|
|
|
hdrbs <- readExact ctx 5
|
2011-06-12 20:40:02 +00:00
|
|
|
case decodeHeader hdrbs of
|
2011-03-01 20:01:40 +00:00
|
|
|
Left err -> return $ Left err
|
2011-05-12 08:11:01 +00:00
|
|
|
Right header@(Header _ _ readlen) ->
|
|
|
|
if readlen > (16384 + 2048)
|
|
|
|
then return $ Left $ Error_Protocol ("record exceeding maximum size",True, RecordOverflow)
|
|
|
|
else recvLength header readlen
|
|
|
|
where recvLength header readlen = do
|
2011-06-13 07:19:29 +00:00
|
|
|
content <- readExact ctx (fromIntegral readlen)
|
2011-05-12 08:11:01 +00:00
|
|
|
liftIO $ (loggingIORecv $ ctxLogging ctx) header content
|
2011-08-12 17:41:49 +00:00
|
|
|
pkt <- usingState ctx $ readPacket $ rawToRecord header (fragmentCiphertext content)
|
2011-05-12 08:11:01 +00:00
|
|
|
case pkt of
|
2011-06-10 20:24:46 +00:00
|
|
|
Right p -> liftIO $ (loggingPacketRecv $ ctxLogging ctx) $ show p
|
2011-05-12 08:11:01 +00:00
|
|
|
_ -> return ()
|
|
|
|
return pkt
|
2011-03-01 20:01:40 +00:00
|
|
|
|
2011-06-12 20:38:18 +00:00
|
|
|
recvPacketSuccess :: MonadIO m => TLSCtx -> m ()
|
|
|
|
recvPacketSuccess ctx = do
|
|
|
|
pkt <- recvPacket ctx
|
|
|
|
case pkt of
|
|
|
|
Left err -> throwCore err
|
|
|
|
Right _ -> return ()
|
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | Send one packet to the context
|
2011-03-01 20:01:40 +00:00
|
|
|
sendPacket :: MonadIO m => TLSCtx -> Packet -> m ()
|
|
|
|
sendPacket ctx pkt = do
|
2011-05-04 07:41:16 +00:00
|
|
|
liftIO $ (loggingPacketSent $ ctxLogging ctx) (show pkt)
|
2011-03-01 20:01:40 +00:00
|
|
|
dataToSend <- usingState_ ctx $ writePacket pkt
|
2011-05-04 07:41:16 +00:00
|
|
|
liftIO $ (loggingIOSent $ ctxLogging ctx) dataToSend
|
2011-03-01 20:01:40 +00:00
|
|
|
liftIO $ B.hPut (ctxHandle ctx) dataToSend
|
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | Create a new Client context with a configuration, a RNG, and a Handle.
|
|
|
|
-- It reconfigures the handle buffermode to noBuffering
|
2011-04-11 18:54:21 +00:00
|
|
|
client :: (MonadIO m, CryptoRandomGen g) => TLSParams -> g -> Handle -> m TLSCtx
|
2011-04-24 12:43:57 +00:00
|
|
|
client params rng handle = liftIO $ newCtx handle params st
|
|
|
|
where st = (newTLSState rng) { stClientContext = True }
|
2011-03-01 20:01:40 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | Create a new Server context with a configuration, a RNG, and a Handle.
|
|
|
|
-- It reconfigures the handle buffermode to noBuffering
|
2011-04-11 18:54:21 +00:00
|
|
|
server :: (MonadIO m, CryptoRandomGen g) => TLSParams -> g -> Handle -> m TLSCtx
|
2011-04-24 12:43:57 +00:00
|
|
|
server params rng handle = liftIO $ newCtx handle params st
|
|
|
|
where st = (newTLSState rng) { stClientContext = False }
|
2011-03-01 20:01:40 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | notify the context that this side wants to close connection.
|
|
|
|
-- this is important that it is called before closing the handle, otherwise
|
|
|
|
-- the session might not be resumable (for version < TLS1.2).
|
|
|
|
--
|
|
|
|
-- this doesn't actually close the handle
|
2011-03-01 20:01:40 +00:00
|
|
|
bye :: MonadIO m => TLSCtx -> m ()
|
2011-06-10 20:24:46 +00:00
|
|
|
bye ctx = sendPacket ctx $ Alert [(AlertLevel_Warning, CloseNotify)]
|
2011-03-01 20:01:40 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- client part of handshake. send a bunch of handshake of client
|
|
|
|
-- values intertwined with response from the server.
|
2011-03-01 23:09:17 +00:00
|
|
|
handshakeClient :: MonadIO m => TLSCtx -> m ()
|
|
|
|
handshakeClient ctx = do
|
|
|
|
-- Send ClientHello
|
|
|
|
crand <- getStateRNG ctx 32 >>= return . ClientRandom
|
2011-06-07 07:13:43 +00:00
|
|
|
extensions <- getExtensions
|
2011-06-12 20:42:55 +00:00
|
|
|
usingState_ ctx (startHandshakeClient ver crand)
|
2011-06-10 20:24:46 +00:00
|
|
|
sendPacket ctx $ Handshake
|
|
|
|
[ ClientHello ver crand (Session Nothing) (map cipherID ciphers)
|
|
|
|
(map compressionID compressions) extensions
|
|
|
|
]
|
2011-03-01 23:09:17 +00:00
|
|
|
|
|
|
|
-- Receive Server information until ServerHelloDone
|
|
|
|
whileStatus ctx (/= (StatusHandshake HsStatusServerHelloDone)) $ do
|
|
|
|
pkts <- recvPacket ctx
|
|
|
|
case pkts of
|
2011-06-12 20:39:34 +00:00
|
|
|
Left err -> throwCore err
|
2011-06-10 20:24:46 +00:00
|
|
|
Right l -> processServerInfo l
|
2011-03-01 23:09:17 +00:00
|
|
|
|
|
|
|
-- Send Certificate if requested. XXX disabled for now.
|
|
|
|
certRequested <- return False
|
2011-06-10 20:24:46 +00:00
|
|
|
when certRequested (sendPacket ctx $ Handshake [Certificates clientCerts])
|
2011-03-01 23:09:17 +00:00
|
|
|
|
2011-06-12 20:55:22 +00:00
|
|
|
sendClientKeyXchg
|
2011-03-01 23:09:17 +00:00
|
|
|
|
|
|
|
{- maybe send certificateVerify -}
|
|
|
|
{- FIXME not implemented yet -}
|
|
|
|
|
|
|
|
sendPacket ctx ChangeCipherSpec
|
2011-03-02 07:56:37 +00:00
|
|
|
liftIO $ hFlush $ ctxHandle ctx
|
2011-03-01 23:09:17 +00:00
|
|
|
|
|
|
|
-- Send Finished
|
|
|
|
cf <- usingState_ ctx $ getHandshakeDigest True
|
2011-06-10 20:24:46 +00:00
|
|
|
sendPacket ctx (Handshake [Finished cf])
|
2011-03-01 23:09:17 +00:00
|
|
|
|
|
|
|
-- receive changeCipherSpec & Finished
|
2011-06-12 20:38:18 +00:00
|
|
|
recvPacketSuccess ctx >> recvPacketSuccess ctx >> return ()
|
2011-03-01 23:09:17 +00:00
|
|
|
|
|
|
|
where
|
2011-03-02 07:56:37 +00:00
|
|
|
params = ctxParams ctx
|
2011-03-01 23:09:17 +00:00
|
|
|
ver = pConnectVersion params
|
|
|
|
allowedvers = pAllowedVersions params
|
|
|
|
ciphers = pCiphers params
|
|
|
|
compressions = pCompressions params
|
|
|
|
clientCerts = map fst $ pCertificates params
|
2011-06-07 07:13:43 +00:00
|
|
|
getExtensions =
|
|
|
|
if pUseSecureRenegotiation params
|
2011-06-19 20:23:01 +00:00
|
|
|
then usingState_ ctx (getVerifiedData True) >>= \vd -> return [ (0xff01, encodeExtSecureRenegotiation vd Nothing) ]
|
2011-06-07 07:13:43 +00:00
|
|
|
else return []
|
2011-03-01 23:09:17 +00:00
|
|
|
|
2011-06-10 20:24:46 +00:00
|
|
|
processServerInfo (Handshake hss) = mapM_ processHandshake hss
|
|
|
|
processServerInfo _ = return ()
|
|
|
|
|
|
|
|
processHandshake (ServerHello rver _ _ cipher _ _) = do
|
2011-05-12 08:12:09 +00:00
|
|
|
when (rver == SSL2) $ throwCore $ Error_Protocol ("ssl2 is not supported", True, ProtocolVersion)
|
2011-03-01 23:09:17 +00:00
|
|
|
case find ((==) rver) allowedvers of
|
2011-05-13 06:03:16 +00:00
|
|
|
Nothing -> throwCore $ Error_Protocol ("version " ++ show ver ++ "is not supported", True, ProtocolVersion)
|
2011-03-01 23:09:17 +00:00
|
|
|
Just _ -> usingState_ ctx $ setVersion ver
|
|
|
|
case find ((==) cipher . cipherID) ciphers of
|
2011-05-12 08:12:31 +00:00
|
|
|
Nothing -> throwCore $ Error_Protocol ("no cipher in common with the server", True, HandshakeFailure)
|
2011-03-01 23:09:17 +00:00
|
|
|
Just c -> usingState_ ctx $ setCipher c
|
|
|
|
|
2011-06-10 20:24:46 +00:00
|
|
|
processHandshake (Certificates certs) = do
|
2011-03-02 07:56:37 +00:00
|
|
|
let cb = onCertificatesRecv $ params
|
2011-05-13 06:02:44 +00:00
|
|
|
usage <- liftIO $ cb certs
|
|
|
|
case usage of
|
|
|
|
CertificateUsageAccept -> return ()
|
|
|
|
CertificateUsageReject reason -> certificateRejected reason
|
2011-03-01 23:09:17 +00:00
|
|
|
|
2011-06-10 20:24:46 +00:00
|
|
|
processHandshake (CertRequest _ _ _) = do
|
|
|
|
return ()
|
|
|
|
--modify (\sc -> sc { scCertRequested = True })
|
|
|
|
processHandshake _ = return ()
|
|
|
|
|
2011-06-12 20:55:22 +00:00
|
|
|
sendClientKeyXchg = do
|
|
|
|
prerand <- getStateRNG ctx 46 >>= return . ClientKeyData
|
|
|
|
sendPacket ctx $ Handshake [ClientKeyXchg ver prerand]
|
2011-03-01 23:09:17 +00:00
|
|
|
|
2011-05-13 06:02:44 +00:00
|
|
|
-- on certificate reject, throw an exception with the proper protocol alert error.
|
|
|
|
certificateRejected CertificateRejectRevoked =
|
|
|
|
throwCore $ Error_Protocol ("certificate is revoked", True, CertificateRevoked)
|
|
|
|
certificateRejected CertificateRejectExpired =
|
|
|
|
throwCore $ Error_Protocol ("certificate has expired", True, CertificateExpired)
|
|
|
|
certificateRejected CertificateRejectUnknownCA =
|
|
|
|
throwCore $ Error_Protocol ("certificate has unknown CA", True, UnknownCa)
|
|
|
|
certificateRejected (CertificateRejectOther s) =
|
|
|
|
throwCore $ Error_Protocol ("certificate rejected: " ++ s, True, CertificateUnknown)
|
|
|
|
|
2011-03-02 07:35:25 +00:00
|
|
|
handshakeServerWith :: MonadIO m => TLSCtx -> Handshake -> m ()
|
|
|
|
handshakeServerWith ctx (ClientHello ver _ _ ciphers compressions _) = do
|
|
|
|
-- Handle Client hello
|
2011-05-13 06:08:27 +00:00
|
|
|
when (ver == SSL2) $ throwCore $ Error_Protocol ("ssl2 is not supported", True, ProtocolVersion)
|
|
|
|
when (not $ elem ver (pAllowedVersions params)) $
|
|
|
|
throwCore $ Error_Protocol ("version " ++ show ver ++ "is not supported", True, ProtocolVersion)
|
|
|
|
when (commonCiphers == []) $
|
|
|
|
throwCore $ Error_Protocol ("no cipher in common with the client", True, HandshakeFailure)
|
2011-08-12 17:31:58 +00:00
|
|
|
when (null commonCompressions) $
|
2011-05-13 06:08:27 +00:00
|
|
|
throwCore $ Error_Protocol ("no compression in common with the client", True, HandshakeFailure)
|
2011-03-02 07:35:25 +00:00
|
|
|
usingState_ ctx $ modify (\st -> st
|
2011-08-12 17:31:58 +00:00
|
|
|
{ stVersion = ver
|
|
|
|
, stCipher = Just usedCipher
|
|
|
|
, stCompression = usedCompression
|
2011-03-02 07:35:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
-- send Server Data until ServerHelloDone
|
|
|
|
handshakeSendServerData
|
2011-03-02 07:56:37 +00:00
|
|
|
liftIO $ hFlush $ ctxHandle ctx
|
2011-03-02 07:35:25 +00:00
|
|
|
|
|
|
|
-- Receive client info until client Finished.
|
2011-06-12 20:38:18 +00:00
|
|
|
whileStatus ctx (/= (StatusHandshake HsStatusClientFinished)) (recvPacketSuccess ctx)
|
2011-03-02 07:35:25 +00:00
|
|
|
|
|
|
|
sendPacket ctx ChangeCipherSpec
|
|
|
|
|
|
|
|
-- Send Finish
|
|
|
|
cf <- usingState_ ctx $ getHandshakeDigest False
|
2011-06-10 20:24:46 +00:00
|
|
|
sendPacket ctx (Handshake [Finished cf])
|
2011-03-02 07:35:25 +00:00
|
|
|
|
2011-03-02 07:56:37 +00:00
|
|
|
liftIO $ hFlush $ ctxHandle ctx
|
2011-03-02 07:35:25 +00:00
|
|
|
return ()
|
|
|
|
where
|
2011-03-02 07:56:37 +00:00
|
|
|
params = ctxParams ctx
|
2011-03-02 07:35:25 +00:00
|
|
|
commonCiphers = intersect ciphers (map cipherID $ pCiphers params)
|
|
|
|
usedCipher = fromJust $ find (\c -> cipherID c == head commonCiphers) (pCiphers params)
|
2011-08-12 17:31:58 +00:00
|
|
|
commonCompressions = compressionIntersectID (pCompressions params) compressions
|
|
|
|
usedCompression = head commonCompressions
|
2011-03-02 07:35:25 +00:00
|
|
|
srvCerts = map fst $ pCertificates params
|
|
|
|
privKeys = map snd $ pCertificates params
|
|
|
|
needKeyXchg = cipherExchangeNeedMoreData $ cipherKeyExchange usedCipher
|
|
|
|
|
|
|
|
handshakeSendServerData = do
|
|
|
|
srand <- getStateRNG ctx 32 >>= return . ServerRandom
|
|
|
|
|
|
|
|
case privKeys of
|
|
|
|
(Just privkey : _) -> usingState_ ctx $ setPrivateKey privkey
|
|
|
|
_ -> return () -- return a sensible error
|
|
|
|
|
|
|
|
-- in TLS12, we need to check as well the certificates we are sending if they have in the extension
|
|
|
|
-- the necessary bits set.
|
|
|
|
|
|
|
|
-- send ServerHello & Certificate & ServerKeyXchg & CertReq
|
2011-06-07 07:28:02 +00:00
|
|
|
secReneg <- usingState_ ctx getSecureRenegotiation
|
|
|
|
extensions <- if secReneg
|
|
|
|
then do
|
|
|
|
vf <- usingState_ ctx $ do
|
|
|
|
cvf <- getVerifiedData True
|
|
|
|
svf <- getVerifiedData False
|
2011-06-13 07:33:14 +00:00
|
|
|
return $ encodeExtSecureRenegotiation cvf (Just svf)
|
2011-06-07 07:28:02 +00:00
|
|
|
return [ (0xff01, vf) ]
|
|
|
|
else return []
|
2011-06-12 20:42:55 +00:00
|
|
|
usingState_ ctx (setVersion ver >> setServerRandom srand)
|
2011-06-10 20:24:46 +00:00
|
|
|
sendPacket ctx $ Handshake
|
|
|
|
[ ServerHello ver srand (Session Nothing) (cipherID usedCipher)
|
|
|
|
(compressionID usedCompression) extensions
|
|
|
|
, Certificates srvCerts
|
|
|
|
]
|
2011-03-02 07:35:25 +00:00
|
|
|
when needKeyXchg $ do
|
|
|
|
let skg = SKX_RSA Nothing
|
2011-06-10 20:24:46 +00:00
|
|
|
sendPacket ctx (Handshake [ServerKeyXchg skg])
|
2011-03-02 07:35:25 +00:00
|
|
|
-- FIXME we don't do this on a Anonymous server
|
|
|
|
when (pWantClientCert params) $ do
|
|
|
|
let certTypes = [ CertificateType_RSA_Sign ]
|
|
|
|
let creq = CertRequest certTypes Nothing [0,0,0]
|
2011-06-10 20:24:46 +00:00
|
|
|
sendPacket ctx (Handshake [creq])
|
2011-03-02 07:35:25 +00:00
|
|
|
-- Send HelloDone
|
2011-06-10 20:24:46 +00:00
|
|
|
sendPacket ctx (Handshake [ServerHelloDone])
|
2011-03-02 07:35:25 +00:00
|
|
|
|
2011-03-02 07:56:37 +00:00
|
|
|
handshakeServerWith _ _ = fail "unexpected handshake type received. expecting client hello"
|
2011-03-02 07:35:25 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- after receiving a client hello, we need to redo a handshake -}
|
2011-03-02 07:35:25 +00:00
|
|
|
handshakeServer :: MonadIO m => TLSCtx -> m ()
|
|
|
|
handshakeServer ctx = do
|
|
|
|
pkts <- recvPacket ctx
|
|
|
|
case pkts of
|
2011-06-10 20:24:46 +00:00
|
|
|
Right (Handshake [hs]) -> handshakeServerWith ctx hs
|
|
|
|
x -> fail ("unexpected type received. expecting handshake ++ " ++ show x)
|
2011-03-02 07:35:25 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | Handshake for a new TLS connection
|
|
|
|
-- This is to be called at the beginning of a connection, and during renegociation
|
2011-05-13 07:39:15 +00:00
|
|
|
handshake :: MonadIO m => TLSCtx -> m Bool
|
2011-03-01 23:09:17 +00:00
|
|
|
handshake ctx = do
|
|
|
|
cc <- usingState_ ctx (stClientContext <$> get)
|
2011-05-13 07:39:15 +00:00
|
|
|
liftIO $ handleException $ if cc then handshakeClient ctx else handshakeServer ctx
|
|
|
|
where
|
|
|
|
handleException f = catch (f >> return True) (\e -> handler e >> return False)
|
|
|
|
handler e = case fromException e of
|
|
|
|
Just err -> sendPacket ctx (errorToAlert err)
|
|
|
|
Nothing -> sendPacket ctx (errorToAlert $ Error_Misc "")
|
2011-03-01 23:09:17 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | sendData sends a bunch of data.
|
|
|
|
-- It will automatically chunk data to acceptable packet size
|
2011-03-01 20:01:40 +00:00
|
|
|
sendData :: MonadIO m => TLSCtx -> L.ByteString -> m ()
|
2011-06-13 07:19:29 +00:00
|
|
|
sendData ctx dataToSend = do
|
|
|
|
eofed <- ctxEOF ctx
|
|
|
|
when eofed $ liftIO $ throwIO $ mkIOError eofErrorType "sendData" (Just (ctxHandle ctx)) Nothing
|
|
|
|
mapM_ sendDataChunk (L.toChunks dataToSend)
|
|
|
|
where sendDataChunk d = if B.length d > 16384
|
2011-03-01 20:01:40 +00:00
|
|
|
then do
|
|
|
|
let (sending, remain) = B.splitAt 16384 d
|
|
|
|
sendPacket ctx $ AppData sending
|
|
|
|
sendDataChunk remain
|
|
|
|
else
|
|
|
|
sendPacket ctx $ AppData d
|
2011-03-02 07:41:59 +00:00
|
|
|
|
2011-03-02 08:43:05 +00:00
|
|
|
-- | recvData get data out of Data packet, and automatically renegociate if
|
|
|
|
-- a Handshake ClientHello is received
|
2011-03-02 07:41:59 +00:00
|
|
|
recvData :: MonadIO m => TLSCtx -> m L.ByteString
|
|
|
|
recvData ctx = do
|
2011-06-13 07:19:29 +00:00
|
|
|
eofed <- ctxEOF ctx
|
|
|
|
when eofed $ liftIO $ throwIO $ mkIOError eofErrorType "recvData" (Just (ctxHandle ctx)) Nothing
|
|
|
|
pkt <- recvPacket ctx
|
2011-03-02 07:41:59 +00:00
|
|
|
case pkt of
|
|
|
|
-- on server context receiving a client hello == renegociation
|
2011-06-10 20:24:46 +00:00
|
|
|
Right (Handshake [ch@(ClientHello _ _ _ _ _ _)]) ->
|
2011-03-02 07:41:59 +00:00
|
|
|
handshakeServerWith ctx ch >> recvData ctx
|
|
|
|
-- on client context, receiving a hello request == renegociation
|
2011-06-10 20:24:46 +00:00
|
|
|
Right (Handshake [HelloRequest]) ->
|
2011-03-02 07:41:59 +00:00
|
|
|
handshakeClient ctx >> recvData ctx
|
2011-06-13 07:19:29 +00:00
|
|
|
Right (Alert [(AlertLevel_Fatal, _)]) -> do
|
|
|
|
setEOF ctx
|
2011-05-12 08:13:03 +00:00
|
|
|
return L.empty
|
2011-06-10 20:24:46 +00:00
|
|
|
Right (Alert [(AlertLevel_Warning, CloseNotify)]) -> do
|
2011-06-13 07:19:29 +00:00
|
|
|
setEOF ctx
|
2011-05-12 08:13:03 +00:00
|
|
|
return L.empty
|
2011-06-10 20:24:46 +00:00
|
|
|
Right (AppData x) -> return $ L.fromChunks [x]
|
|
|
|
Right p -> error ("error unexpected packet: p" ++ show p)
|
2011-03-02 07:41:59 +00:00
|
|
|
Left err -> error ("error received: " ++ show err)
|