{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_HADDOCK hide #-}
module Network.TLS.Core (
sendPacket12,
recvPacket12,
bye,
handshake,
getNegotiatedProtocol,
getClientSNI,
sendData,
recvData,
recvData',
closeNotifyReceived,
updateKey,
KeyUpdateRequest (..),
requestCertificate,
requestCertificateAsync,
PHAResult (..),
) where
import Control.Concurrent (forkIO)
import Control.Concurrent.STM
import qualified Control.Exception as E
import Control.Monad.State.Strict
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Lazy as L
import Data.IORef
import GHC.IO.Exception (IOErrorType (..), IOException (..))
import Network.TLS.Backend (Backend (..), RecvWith (..))
import Network.TLS.Context
import Network.TLS.Context.Internal (
PHAResult (..),
incrementTLS13KeyUpdateCount,
resetTLS13KeyUpdateCount,
)
import Network.TLS.Extension
import Network.TLS.Handshake
import Network.TLS.Handshake.Common
import Network.TLS.Handshake.Common13
import Network.TLS.Handshake.Server
import Network.TLS.Handshake.State
import Network.TLS.Handshake.State13
import Network.TLS.Handshake.TranscriptHash
import Network.TLS.IO
import Network.TLS.Imports
import Network.TLS.Parameters
import Network.TLS.PostHandshake
import Network.TLS.Session
import Network.TLS.State (getRole, getSession)
import qualified Network.TLS.State as S
import Network.TLS.Struct
import Network.TLS.Struct13
import Network.TLS.Types (
Bulk (..),
Cipher (..),
HostName,
Role (..),
)
import Network.TLS.Util (catchException, mapChunks_)
handshake :: MonadIO m => Context -> m ()
handshake :: forall (m :: * -> *). MonadIO m => Context -> m ()
handshake Context
ctx = do
Context -> m ()
forall (m :: * -> *). MonadIO m => Context -> m ()
handshake_ Context
ctx
IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
role <- Context -> TLSSt Role -> IO Role
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt Role
getRole
tls13 <- tls13orLater ctx
sentClientCert <- tls13stSentClientCert <$> getTLS13State ctx
when (role == ClientRole && tls13 && sentClientCert) $ do
rtt <- getRTT ctx
done <- newEmptyTMVarIO
void $ forkIO $ withReadLock ctx $ do
r <- E.try $ recvData13 ctx
modifyTLS13State ctx $ \TLS13State
st ->
TLS13State
st
{ tls13stPendingRecv = case r of
Right ByteString
dat -> ByteString -> PendingRecv
PendingRecvData ByteString
dat
Left SomeException
err -> SomeException -> PendingRecv
PendingRecvError SomeException
err
}
atomically $ putTMVar done ()
late <- registerDelay rtt
arrived <-
atomically $
(Just <$> takeTMVar done) `orElse` (Nothing <$ (readTVar late >>= check))
when (isJust arrived) $ do
pending <- tls13stPendingRecv <$> getTLS13State ctx
case pending of
PendingRecvError SomeException
err -> do
Context -> (TLS13State -> TLS13State) -> IO ()
modifyTLS13State Context
ctx ((TLS13State -> TLS13State) -> IO ())
-> (TLS13State -> TLS13State) -> IO ()
forall a b. (a -> b) -> a -> b
$ \TLS13State
st -> TLS13State
st{tls13stPendingRecv = NoPendingRecv}
SomeException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO SomeException
err
PendingRecv
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
rttFactor :: Int
rttFactor :: Int
rttFactor = Int
3
getRTT :: Context -> IO Int
getRTT :: Context -> IO Int
getRTT Context
ctx = do
rtt <- TLS13State -> Word64
tls13stRTT (TLS13State -> Word64) -> IO TLS13State -> IO Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> IO TLS13State
getTLS13State Context
ctx
let rtt' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
rtt) Int
10
return (rtt' * rttFactor * 1000)
bye :: MonadIO m => Context -> m ()
bye :: forall (m :: * -> *). MonadIO m => Context -> m ()
bye Context
ctx = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
eof <- Context -> IO Bool
ctxEOF Context
ctx
tls13 <- tls13orLater ctx
when (tls13 && not eof) $ do
role <- usingState_ ctx getRole
if role == ClientRole
then do
withWriteLock ctx $ sendCFifNecessary ctx
let chk = TLS13State -> Bool
tls13stRecvNST (TLS13State -> Bool) -> IO TLS13State -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> IO TLS13State
getTLS13State Context
ctx
recvNST <- chk
unless recvNST $ do
rtt <- getRTT ctx
tryRecvHS13 rtt chk
else do
let chk = TLS13State -> Bool
tls13stRecvCF (TLS13State -> Bool) -> IO TLS13State -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> IO TLS13State
getTLS13State Context
ctx
recvCF <- chk
unless recvCF $ do
let rtt = Int
1000000
tryRecvHS13 rtt chk
bye_ ctx
where
tryRecvHS13 :: Int -> IO Bool -> IO ()
tryRecvHS13 :: Int -> IO Bool -> IO ()
tryRecvHS13 Int
rtt IO Bool
chk = case Context -> Maybe RecvWith
ctxBackendRecvWith Context
ctx of
Maybe RecvWith
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just (RecvWith forall g. Int -> STM g -> IO (Either g ByteString)
recvWith) -> do
late <- Int -> IO (TVar Bool)
registerDelay Int
rtt
let giveUp = TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar TVar Bool
late STM Bool -> (Bool -> STM ()) -> STM ()
forall a b. STM a -> (a -> STM b) -> STM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> STM ()
check
recv Int
n = Int -> STM () -> IO (Either () ByteString)
forall g. Int -> STM g -> IO (Either g ByteString)
recvWith Int
n STM ()
giveUp IO (Either () ByteString)
-> (Either () ByteString -> IO ByteString) -> IO ByteString
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (() -> IO ByteString)
-> (ByteString -> IO ByteString)
-> Either () ByteString
-> IO ByteString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\() -> GaveUpRecv -> IO ByteString
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO GaveUpRecv
GaveUpRecv) ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
ctx' = Context
ctx{ctxBackend = (ctxBackend ctx){backendRecv = recv}}
void $
tryWithReadLock ctx $
recvHS13 ctx' chk `catchException` \SomeException
e ->
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe GaveUpRecv -> Bool
forall a. Maybe a -> Bool
isNothing (forall e. Exception e => SomeException -> Maybe e
E.fromException @GaveUpRecv SomeException
e)) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Context -> IO ()
setEOF Context
ctx
data GaveUpRecv = GaveUpRecv deriving (Int -> GaveUpRecv -> ShowS
[GaveUpRecv] -> ShowS
GaveUpRecv -> String
(Int -> GaveUpRecv -> ShowS)
-> (GaveUpRecv -> String)
-> ([GaveUpRecv] -> ShowS)
-> Show GaveUpRecv
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GaveUpRecv -> ShowS
showsPrec :: Int -> GaveUpRecv -> ShowS
$cshow :: GaveUpRecv -> String
show :: GaveUpRecv -> String
$cshowList :: [GaveUpRecv] -> ShowS
showList :: [GaveUpRecv] -> ShowS
Show)
instance E.Exception GaveUpRecv
bye_ :: MonadIO m => Context -> m ()
bye_ :: forall (m :: * -> *). MonadIO m => Context -> m ()
bye_ Context
ctx = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
eof <- Context -> IO Bool
ctxEOF Context
ctx
tls13 <- tls13orLater ctx
unless eof $
E.handleJust vanished return $
withWriteLock ctx $
if tls13
then sendPacket13 ctx $ Alert13 [(AlertLevel_Warning, CloseNotify)]
else sendPacket12 ctx $ Alert [(AlertLevel_Warning, CloseNotify)]
where
vanished :: IOException -> Maybe ()
vanished IOException
e
| IOException -> IOErrorType
ioe_type IOException
e IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOErrorType
ResourceVanished = () -> Maybe ()
forall a. a -> Maybe a
Just ()
| Bool
otherwise = Maybe ()
forall a. Maybe a
Nothing
getNegotiatedProtocol :: MonadIO m => Context -> m (Maybe ByteString)
getNegotiatedProtocol :: forall (m :: * -> *). MonadIO m => Context -> m (Maybe ByteString)
getNegotiatedProtocol Context
ctx = IO (Maybe ByteString) -> m (Maybe ByteString)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe ByteString) -> m (Maybe ByteString))
-> IO (Maybe ByteString) -> m (Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ Context -> TLSSt (Maybe ByteString) -> IO (Maybe ByteString)
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt (Maybe ByteString)
S.getNegotiatedProtocol
getClientSNI :: MonadIO m => Context -> m (Maybe HostName)
getClientSNI :: forall (m :: * -> *). MonadIO m => Context -> m (Maybe String)
getClientSNI Context
ctx = IO (Maybe String) -> m (Maybe String)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe String) -> m (Maybe String))
-> IO (Maybe String) -> m (Maybe String)
forall a b. (a -> b) -> a -> b
$ Context -> TLSSt (Maybe String) -> IO (Maybe String)
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt (Maybe String)
S.getClientSNI
sendCFifNecessary :: Context -> IO ()
sendCFifNecessary :: Context -> IO ()
sendCFifNecessary Context
ctx = do
st <- Context -> IO TLS13State
getTLS13State Context
ctx
let recvSF = TLS13State -> Bool
tls13stRecvSF TLS13State
st
sentCF = TLS13State -> Bool
tls13stSentCF TLS13State
st
when (recvSF && not sentCF) $ do
msend <- readIORef (ctxPendingSendAction ctx)
case msend of
Maybe (Context -> IO ())
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Context -> IO ()
sendAction -> do
Context -> IO ()
sendAction Context
ctx
IORef (Maybe (Context -> IO ()))
-> Maybe (Context -> IO ()) -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Context -> IORef (Maybe (Context -> IO ()))
ctxPendingSendAction Context
ctx) Maybe (Context -> IO ())
forall a. Maybe a
Nothing
sendData :: MonadIO m => Context -> L.ByteString -> m ()
sendData :: forall (m :: * -> *). MonadIO m => Context -> ByteString -> m ()
sendData Context
_ ByteString
"" = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
sendData Context
ctx ByteString
dataToSend = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
tls13 <- Context -> IO Bool
forall (m :: * -> *). MonadIO m => Context -> m Bool
tls13orLater Context
ctx
let sendP ByteString
bs
| Bool
tls13 = do
Context -> IO ()
updateKeyBeforeLimit Context
ctx
Context -> Packet13 -> IO ()
sendPacket13 Context
ctx (Packet13 -> IO ()) -> Packet13 -> IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Packet13
AppData13 ByteString
bs
Context -> (TLS13State -> TLS13State) -> IO ()
modifyTLS13State Context
ctx ((TLS13State -> TLS13State) -> IO ())
-> (TLS13State -> TLS13State) -> IO ()
forall a b. (a -> b) -> a -> b
$ \TLS13State
st -> TLS13State
st{tls13stKeyUpdateResponded = False}
role <- Context -> TLSSt Role -> IO Role
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt Role
getRole
sentCF <- tls13stSentCF <$> getTLS13State ctx
rtt0 <- tls13st0RTT <$> getTLS13State ctx
when (role == ClientRole && rtt0 && not sentCF) $
modifyTLS13State ctx $
\TLS13State
st -> TLS13State
st{tls13stPendingSentData = tls13stPendingSentData st . (bs :)}
| Bool
otherwise = Context -> Packet -> IO ()
sendPacket12 Context
ctx (Packet -> IO ()) -> Packet -> IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Packet
AppData ByteString
bs
when tls13 $ withWriteLock ctx $ sendCFifNecessary ctx
withWriteLock ctx $ do
checkValid ctx
mlen <- getPeerRecordLimit ctx
mapM_ (mapChunks_ mlen sendP) (L.toChunks dataToSend)
updateKeyBeforeLimit :: Context -> IO ()
updateKeyBeforeLimit :: Context -> IO ()
updateKeyBeforeLimit Context
ctx = do
(mcipher, level, sqn) <- Context -> IO (Maybe Cipher, CryptLevel, Word64)
getTxSequence Context
ctx
case mcipher of
Just Cipher
cipher | Word64
sqn Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Cipher -> Word64
recordLimit Cipher
cipher -> do
updated <-
if CryptLevel
level CryptLevel -> CryptLevel -> Bool
forall a. Eq a => a -> a -> Bool
== CryptLevel
CryptApplicationSecret
then Context -> KeyUpdate -> IO Bool
sendKeyUpdate Context
ctx KeyUpdate
UpdateNotRequested
else Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
unless updated $
E.throwIO $
PostHandshake $
Error_Misc "the key usage limit is reached"
Maybe Cipher
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
recordLimit :: Cipher -> Word64
recordLimit Cipher
cipher = Word64 -> Maybe Word64 -> Word64
forall a. a -> Maybe a -> a
fromMaybe Word64
forCipher (Maybe Word64 -> Word64) -> Maybe Word64 -> Word64
forall a b. (a -> b) -> a -> b
$ Limit -> Maybe Word64
limitRecordsPerKey (Limit -> Maybe Word64) -> Limit -> Maybe Word64
forall a b. (a -> b) -> a -> b
$ Shared -> Limit
sharedLimit (Shared -> Limit) -> Shared -> Limit
forall a b. (a -> b) -> a -> b
$ Context -> Shared
ctxShared Context
ctx
where
forCipher :: Word64
forCipher
| Bulk -> String
bulkName (Cipher -> Bulk
cipherBulk Cipher
cipher) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"CHACHA20POLY1305" = Word64
2 Word64 -> Int -> Word64
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
62 :: Int)
| Bool
otherwise = Word64
2 Word64 -> Int -> Word64
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
24 :: Int)
recvData :: MonadIO m => Context -> m ByteString
recvData :: forall (m :: * -> *). MonadIO m => Context -> m ByteString
recvData Context
ctx = IO ByteString -> m ByteString
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> m ByteString) -> IO ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ do
tls13 <- Context -> IO Bool
forall (m :: * -> *). MonadIO m => Context -> m Bool
tls13orLater Context
ctx
withReadLock ctx $ do
checkValid ctx
if tls13 then recvData13 ctx else recvData12 ctx
recvData12 :: Context -> IO ByteString
recvData12 :: Context -> IO ByteString
recvData12 Context
ctx = do
pending <- IORef [ByteString]
-> ([ByteString] -> ([ByteString], [ByteString]))
-> IO [ByteString]
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' (Context -> IORef [ByteString]
ctxPendingAppData Context
ctx) (([ByteString] -> ([ByteString], [ByteString])) -> IO [ByteString])
-> ([ByteString] -> ([ByteString], [ByteString]))
-> IO [ByteString]
forall a b. (a -> b) -> a -> b
$ \[ByteString]
xs -> ([], [ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse [ByteString]
xs)
if null pending
then recvPacket12 ctx >>= either (onError terminate12) process
else return $ B.concat pending
where
process :: Packet -> IO ByteString
process (Handshake [ch :: Handshake
ch@ClientHello{}] [[ByteString]
b]) =
Context -> HandshakeR -> IO ()
forall (m :: * -> *). MonadIO m => Context -> HandshakeR -> m ()
handshakeWith Context
ctx (Handshake
ch, [ByteString]
b) IO () -> IO ByteString -> IO ByteString
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ByteString
recvData12 Context
ctx
process (Handshake [hr :: Handshake
hr@Handshake
HelloRequest] [[ByteString]
b]) =
Context -> HandshakeR -> IO ()
forall (m :: * -> *). MonadIO m => Context -> HandshakeR -> m ()
handshakeWith Context
ctx (Handshake
hr, [ByteString]
b) IO () -> IO ByteString -> IO ByteString
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ByteString
recvData12 Context
ctx
process (Alert [(AlertLevel
AlertLevel_Warning, AlertDescription
UserCanceled)]) = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
B.empty
process (Alert [(AlertLevel
AlertLevel_Warning, AlertDescription
CloseNotify)]) = Context -> IO ()
setCloseNotify Context
ctx IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ()
tryBye Context
ctx IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ()
setEOF Context
ctx IO () -> IO ByteString -> IO ByteString
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
B.empty
process (Alert [(AlertLevel
AlertLevel_Fatal, AlertDescription
desc)]) = do
Context -> IO ()
setEOF Context
ctx
TLSException -> IO ByteString
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO
( Bool -> String -> TLSError -> TLSException
Terminated
Bool
True
(String
"received fatal error: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ AlertDescription -> String
forall a. Show a => a -> String
show AlertDescription
desc)
(String -> AlertDescription -> TLSError
Error_Protocol String
"remote side fatal error" AlertDescription
desc)
)
process (AppData ByteString
"") = Context -> IO ByteString
recvData12 Context
ctx
process (AppData ByteString
x) = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
x
process Packet
p = do
let reason :: String
reason = String
"unexpected message " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Packet -> String
forall a. Show a => a -> String
show Packet
p
TLSError
-> AlertLevel -> AlertDescription -> String -> IO ByteString
forall {a}.
TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate12 (String -> TLSError
Error_Misc String
reason) AlertLevel
AlertLevel_Fatal AlertDescription
UnexpectedMessage String
reason
terminate12 :: TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate12 = Context
-> ([(AlertLevel, AlertDescription)] -> IO ())
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO a
forall a.
Context
-> ([(AlertLevel, AlertDescription)] -> IO ())
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO a
terminateWithWriteLock Context
ctx (Context -> Packet -> IO ()
sendPacket12 Context
ctx (Packet -> IO ())
-> ([(AlertLevel, AlertDescription)] -> Packet)
-> [(AlertLevel, AlertDescription)]
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(AlertLevel, AlertDescription)] -> Packet
Alert)
recvData13 :: Context -> IO ByteString
recvData13 :: Context -> IO ByteString
recvData13 Context
ctx = do
pending <- TLS13State -> PendingRecv
tls13stPendingRecv (TLS13State -> PendingRecv) -> IO TLS13State -> IO PendingRecv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> IO TLS13State
getTLS13State Context
ctx
case pending of
PendingRecv
NoPendingRecv -> do
pkt <- Context -> IO (Either TLSError Packet13)
recvPacket13 Context
ctx
either (onError (terminate13 ctx)) process pkt
PendingRecvData ByteString
dat -> do
IO ()
clearPending
ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
dat
PendingRecvError SomeException
err -> do
IO ()
clearPending
SomeException -> IO ByteString
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO SomeException
err
where
clearPending :: IO ()
clearPending = Context -> (TLS13State -> TLS13State) -> IO ()
modifyTLS13State Context
ctx ((TLS13State -> TLS13State) -> IO ())
-> (TLS13State -> TLS13State) -> IO ()
forall a b. (a -> b) -> a -> b
$ \TLS13State
st -> TLS13State
st{tls13stPendingRecv = NoPendingRecv}
checkPHAResponse :: IO ()
checkPHAResponse = do
responding <- TLS13State -> Bool
tls13stPHAResponding (TLS13State -> Bool) -> IO TLS13State -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> IO TLS13State
getTLS13State Context
ctx
when responding $ do
let reason = String
"message inside a post-handshake authentication response"
err = String -> AlertDescription -> TLSError
Error_Protocol String
reason AlertDescription
UnexpectedMessage
modifyTLS13State ctx $ \TLS13State
st -> TLS13State
st{tls13stPHA = fmap (\(MVar PHAResult
mv, TLSError
_) -> (MVar PHAResult
mv, TLSError
err)) (tls13stPHA st)}
terminate13 ctx err AlertLevel_Fatal UnexpectedMessage reason
process :: Packet13 -> IO ByteString
process (Alert13 [(AlertLevel
_, AlertDescription
UserCanceled)]) = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
B.empty
process (Alert13 [(AlertLevel
_, AlertDescription
CloseNotify)]) = Context -> IO ()
setCloseNotify Context
ctx IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ()
tryBye Context
ctx IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ()
setEOF Context
ctx IO () -> IO ByteString -> IO ByteString
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
B.empty
process (Alert13 [(AlertLevel
_, AlertDescription
desc)]) = do
Context -> IO ()
setEOF Context
ctx
TLSException -> IO ByteString
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO
( Bool -> String -> TLSError -> TLSException
Terminated
Bool
True
(String
"received fatal error: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ AlertDescription -> String
forall a. Show a => a -> String
show AlertDescription
desc)
(String -> AlertDescription -> TLSError
Error_Protocol String
"remote side fatal error" AlertDescription
desc)
)
process (Handshake13 [Handshake13]
hs [[ByteString]]
bs) = do
[(Handshake13, [ByteString])] -> IO ()
loopHandshake13 ([(Handshake13, [ByteString])] -> IO ())
-> [(Handshake13, [ByteString])] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Handshake13] -> [[ByteString]] -> [(Handshake13, [ByteString])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Handshake13]
hs [[ByteString]]
bs
Context -> IO ByteString
recvData13 Context
ctx
process (AppData13 ByteString
"") = IO ()
checkPHAResponse IO () -> IO ByteString -> IO ByteString
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ByteString
recvData13 Context
ctx
process (AppData13 ByteString
x) = do
IO ()
checkPHAResponse
let chunkLen :: Word32
chunkLen = Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word32) -> Int -> Word32
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
C8.length ByteString
x
established <- Context -> IO Established
ctxEstablished Context
ctx
case established of
EarlyDataAllowed Word32
maxSize
| Word32
chunkLen Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
maxSize -> do
Context -> Established -> IO ()
setEstablished Context
ctx (Established -> IO ()) -> Established -> IO ()
forall a b. (a -> b) -> a -> b
$ Word32 -> Established
EarlyDataAllowed (Word32
maxSize Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
chunkLen)
ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
x
| Bool
otherwise ->
let reason :: String
reason = String
"early data overflow"
in Context
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO ByteString
forall a.
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate13 Context
ctx (String -> TLSError
Error_Misc String
reason) AlertLevel
AlertLevel_Fatal AlertDescription
UnexpectedMessage String
reason
Established
Established -> Context -> IO ()
resetTLS13KeyUpdateCount Context
ctx IO () -> IO ByteString -> IO ByteString
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
x
Established
_ -> TLSError -> IO ByteString
forall (m :: * -> *) a. MonadIO m => TLSError -> m a
throwCore (TLSError -> IO ByteString) -> TLSError -> IO ByteString
forall a b. (a -> b) -> a -> b
$ String -> AlertDescription -> TLSError
Error_Protocol String
"data at not-established" AlertDescription
UnexpectedMessage
process Packet13
ChangeCipherSpec13 = do
established <- Context -> IO Established
ctxEstablished Context
ctx
if established /= Established
then recvData13 ctx
else do
let reason = String
"CSS after Finished"
terminate13 ctx (Error_Misc reason) AlertLevel_Fatal UnexpectedMessage reason
process Packet13
p = do
let reason :: String
reason = String
"unexpected message " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Packet13 -> String
forall a. Show a => a -> String
show Packet13
p
Context
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO ByteString
forall a.
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate13 Context
ctx (String -> TLSError
Error_Misc String
reason) AlertLevel
AlertLevel_Fatal AlertDescription
UnexpectedMessage String
reason
loopHandshake13 :: [(Handshake13, [ByteString])] -> IO ()
loopHandshake13 [] = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
loopHandshake13 ((NewSessionTicket13 Word32
life Word32
add TicketNonce
nonce (SessionIDorTicket_ ByteString
ticket) [ExtensionRaw]
exts, [ByteString]
_b) : [(Handshake13, [ByteString])]
hbs) = do
role <- Context -> TLSSt Role -> IO Role
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt Role
S.getRole
unless (role == ClientRole) $ do
let reason = String
"Session ticket is allowed for client only"
terminate13 ctx (Error_Misc reason) AlertLevel_Fatal UnexpectedMessage reason
withWriteLock ctx $ do
Just resumptionSecret <- usingHState ctx getTLS13ResumptionSecret
(_, usedCipher, _, _) <- getTxRecordState ctx
let extract (EarlyDataIndication Maybe Word32
mMaxSize) =
Word32 -> Maybe Word32 -> Word32
forall a. a -> Maybe a -> a
fromMaybe Word32
0 Maybe Word32
mMaxSize
let choice = Version -> Cipher -> CipherChoice
makeCipherChoice Version
TLS13 Cipher
usedCipher
psk = CipherChoice
-> BaseSecret ResumptionSecret -> TicketNonce -> ByteString
derivePSK CipherChoice
choice BaseSecret ResumptionSecret
resumptionSecret TicketNonce
nonce
maxSize =
ExtensionID
-> MessageType
-> [ExtensionRaw]
-> Word32
-> (EarlyDataIndication -> Word32)
-> Word32
forall e a.
Extension e =>
ExtensionID -> MessageType -> [ExtensionRaw] -> a -> (e -> a) -> a
lookupAndDecode
ExtensionID
EID_EarlyData
MessageType
MsgTNewSessionTicket
[ExtensionRaw]
exts
Word32
0
EarlyDataIndication -> Word32
extract
life7d = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
min Word32
life Word32
604800
tinfo <- createTLS13TicketInfo life7d (Right add) Nothing
sdata <- getSessionData13 ctx usedCipher tinfo maxSize psk
let ticket' = ByteString -> ByteString
B.copy ByteString
ticket
void $ sessionEstablish (sharedSessionManager $ ctxShared ctx) ticket' sdata
modifyTLS13State ctx $ \TLS13State
st -> TLS13State
st{tls13stRecvNST = True}
loopHandshake13 hbs
loopHandshake13 ((KeyUpdate13 KeyUpdate
mode, [ByteString]
_b) : [(Handshake13, [ByteString])]
hbs) = do
IO ()
checkPHAResponse
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Context -> Bool
ctxQUICMode Context
ctx) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
let reason :: String
reason = String
"KeyUpdate is not allowed for QUIC"
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO ()
forall a.
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate13 Context
ctx (String -> TLSError
Error_Misc String
reason) AlertLevel
AlertLevel_Fatal AlertDescription
UnexpectedMessage String
reason
aligned <- Context -> [(Handshake13, [ByteString])] -> IO Bool
isAligned Context
ctx [(Handshake13, [ByteString])]
hbs
unless aligned $
terminate13
ctx
(Error_Misc notAligned)
AlertLevel_Fatal
UnexpectedMessage
notAligned
established <- ctxEstablished ctx
if established == Established
then do
case limitKeyUpdate $ sharedLimit $ ctxShared ctx of
Just Int
limit | Int
limit Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 -> do
count <- Context -> IO Int
incrementTLS13KeyUpdateCount Context
ctx
when (count > limit) $ do
let reason = String
"too many consecutive KeyUpdate messages"
terminate13 ctx (Error_Misc reason) AlertLevel_Fatal UnexpectedMessage reason
Maybe Int
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
keyUpdate ctx getRxRecordState setRxRecordState
when (mode == UpdateRequested) $
withWriteLock ctx $
respondToKeyUpdate ctx
loopHandshake13 hbs
else do
let reason = String
"received key update before established"
terminate13 ctx (Error_Misc reason) AlertLevel_Fatal UnexpectedMessage reason
loopHandshake13 (hb :: (Handshake13, [ByteString])
hb@(CertRequest13{}, [ByteString]
_) : [(Handshake13, [ByteString])]
hbs) = do
established <- Context -> IO Established
ctxEstablished Context
ctx
if established == Established
then postHandshakeAuthWith ctx hb >> loopHandshake13 hbs
else loopPending hb hbs
loopHandshake13 ((Handshake13, [ByteString])
hb : [(Handshake13, [ByteString])]
hbs) = (Handshake13, [ByteString])
-> [(Handshake13, [ByteString])] -> IO ()
loopPending (Handshake13, [ByteString])
hb [(Handshake13, [ByteString])]
hbs
loopPending :: (Handshake13, [ByteString])
-> [(Handshake13, [ByteString])] -> IO ()
loopPending hb :: (Handshake13, [ByteString])
hb@(Handshake13
h, [ByteString]
_) [(Handshake13, [ByteString])]
hbs = do
rtt0 <- TLS13State -> Bool
tls13st0RTT (TLS13State -> Bool) -> IO TLS13State -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> IO TLS13State
getTLS13State Context
ctx
when rtt0 $ case h of
ServerHello13 SH{[ExtensionRaw]
CompressionID
Version
CipherId
ServerRandom
Session
shVersion :: Version
shRandom :: ServerRandom
shSession :: Session
shCipher :: CipherId
shComp :: CompressionID
shExtensions :: [ExtensionRaw]
shCipher :: ServerHello -> CipherId
shComp :: ServerHello -> CompressionID
shExtensions :: ServerHello -> [ExtensionRaw]
shRandom :: ServerHello -> ServerRandom
shSession :: ServerHello -> Session
shVersion :: ServerHello -> Version
..} ->
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ServerRandom -> Bool
isHelloRetryRequest ServerRandom
shRandom) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Context -> IO ()
clearTxRecordState Context
ctx
let reason :: String
reason = String
"HRR is not allowed for 0-RTT"
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO ()
forall a.
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate13 Context
ctx (String -> TLSError
Error_Misc String
reason) AlertLevel
AlertLevel_Fatal AlertDescription
UnexpectedMessage String
reason
Handshake13
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
popAction ctx hb hbs
loopHandshake13 hbs
recvHS13 :: Context -> IO Bool -> IO ()
recvHS13 :: Context -> IO Bool -> IO ()
recvHS13 Context
ctx IO Bool
breakLoop = do
pkt <- Context -> IO (Either TLSError Packet13)
recvPacket13 Context
ctx
either (\TLSError
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()) process pkt
where
process :: Packet13 -> IO ()
process (Alert13 [(AlertLevel
_, AlertDescription
UserCanceled)]) = Context -> IO Bool -> IO ()
recvHS13 Context
ctx IO Bool
breakLoop
process (Alert13 [(AlertLevel
_, AlertDescription
CloseNotify)]) = Context -> IO ()
setCloseNotify Context
ctx IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ()
tryBye Context
ctx IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Context -> IO ()
setEOF Context
ctx
process (Alert13 [(AlertLevel
_, AlertDescription
_desc)]) = Context -> IO ()
setEOF Context
ctx
process (Handshake13 [Handshake13]
hs [[ByteString]]
bs) = do
[(Handshake13, [ByteString])] -> IO ()
loopHandshake13 ([(Handshake13, [ByteString])] -> IO ())
-> [(Handshake13, [ByteString])] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Handshake13] -> [[ByteString]] -> [(Handshake13, [ByteString])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Handshake13]
hs [[ByteString]]
bs
stop <- IO Bool
breakLoop
unless stop $ recvHS13 ctx breakLoop
process Packet13
_ = Context -> IO Bool -> IO ()
recvHS13 Context
ctx IO Bool
breakLoop
loopHandshake13 :: [(Handshake13, [ByteString])] -> IO ()
loopHandshake13 [] = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
loopHandshake13 ((NewSessionTicket13 Word32
life Word32
add TicketNonce
nonce (SessionIDorTicket_ ByteString
ticket) [ExtensionRaw]
exts, [ByteString]
_b) : [(Handshake13, [ByteString])]
hbs) = do
role <- Context -> TLSSt Role -> IO Role
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt Role
S.getRole
unless (role == ClientRole) $ do
let reason = String
"Session ticket is allowed for client only"
terminate13 ctx (Error_Misc reason) AlertLevel_Fatal UnexpectedMessage reason
withWriteLock ctx $ do
Just resumptionSecret <- usingHState ctx getTLS13ResumptionSecret
(_, usedCipher, _, _) <- getTxRecordState ctx
let choice = Version -> Cipher -> CipherChoice
makeCipherChoice Version
TLS13 Cipher
usedCipher
psk = CipherChoice
-> BaseSecret ResumptionSecret -> TicketNonce -> ByteString
derivePSK CipherChoice
choice BaseSecret ResumptionSecret
resumptionSecret TicketNonce
nonce
maxSize =
ExtensionID
-> MessageType
-> [ExtensionRaw]
-> Word32
-> (EarlyDataIndication -> Word32)
-> Word32
forall e a.
Extension e =>
ExtensionID -> MessageType -> [ExtensionRaw] -> a -> (e -> a) -> a
lookupAndDecode
ExtensionID
EID_EarlyData
MessageType
MsgTNewSessionTicket
[ExtensionRaw]
exts
Word32
0
(\(EarlyDataIndication Maybe Word32
mms) -> Maybe Word32 -> Word32
forall a. HasCallStack => Maybe a -> a
fromJust Maybe Word32
mms)
life7d = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
min Word32
life Word32
604800
tinfo <- createTLS13TicketInfo life7d (Right add) Nothing
sdata <- getSessionData13 ctx usedCipher tinfo maxSize psk
let ticket' = ByteString -> ByteString
B.copy ByteString
ticket
void $ sessionEstablish (sharedSessionManager $ ctxShared ctx) ticket' sdata
modifyTLS13State ctx $ \TLS13State
st -> TLS13State
st{tls13stRecvNST = True}
loopHandshake13 hbs
loopHandshake13 ((Handshake13, [ByteString])
hb : [(Handshake13, [ByteString])]
hbs) = do
Context
-> (Handshake13, [ByteString])
-> [(Handshake13, [ByteString])]
-> IO ()
popAction Context
ctx (Handshake13, [ByteString])
hb [(Handshake13, [ByteString])]
hbs
[(Handshake13, [ByteString])] -> IO ()
loopHandshake13 [(Handshake13, [ByteString])]
hbs
terminate13
:: Context -> TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate13 :: forall a.
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate13 Context
ctx = Context
-> ([(AlertLevel, AlertDescription)] -> IO ())
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO a
forall a.
Context
-> ([(AlertLevel, AlertDescription)] -> IO ())
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO a
terminateWithWriteLock Context
ctx (Context -> Packet13 -> IO ()
sendPacket13 Context
ctx (Packet13 -> IO ())
-> ([(AlertLevel, AlertDescription)] -> Packet13)
-> [(AlertLevel, AlertDescription)]
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(AlertLevel, AlertDescription)] -> Packet13
Alert13)
popAction :: Context -> Handshake13R -> [Handshake13R] -> IO ()
popAction :: Context
-> (Handshake13, [ByteString])
-> [(Handshake13, [ByteString])]
-> IO ()
popAction Context
ctx hb :: (Handshake13, [ByteString])
hb@(Handshake13
h, [ByteString]
_b) [(Handshake13, [ByteString])]
hbs = do
mPendingRecvAction <- Context -> IO (Maybe PendingRecvAction)
popPendingRecvAction Context
ctx
case mPendingRecvAction of
Maybe PendingRecvAction
Nothing -> do
let reason :: String
reason = String
"unexpected handshake message " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Handshake13 -> String
forall a. Show a => a -> String
show Handshake13
h
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO ()
forall a.
Context
-> TLSError -> AlertLevel -> AlertDescription -> String -> IO a
terminate13 Context
ctx (String -> TLSError
Error_Misc String
reason) AlertLevel
AlertLevel_Fatal AlertDescription
UnexpectedMessage String
reason
Just PendingRecvAction
action -> do
(IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO a
`E.onException` Context -> IO ()
setEOF Context
ctx) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Context -> IO () -> IO ()
forall a. Context -> IO a -> IO a
withWriteLock Context
ctx (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Context -> IO () -> IO ()
handleException Context
ctx (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
case PendingRecvAction
action of
PendingRecvAction Bool
needAligned Handshake13 -> IO ()
pa -> do
Bool -> IO ()
checkAligned Bool
needAligned
Context -> (Handshake13, [ByteString]) -> IO ()
updateTranscriptHash13 Context
ctx (Handshake13, [ByteString])
hb
Handshake13 -> IO ()
pa Handshake13
h
PendingRecvActionSelfUpdate Bool
needAligned (Handshake13, [ByteString]) -> IO ()
pa -> do
Bool -> IO ()
checkAligned Bool
needAligned
(Handshake13, [ByteString]) -> IO ()
pa (Handshake13, [ByteString])
hb
PendingRecvActionHash Bool
needAligned TranscriptHash -> Handshake13 -> IO ()
pa -> do
Bool -> IO ()
checkAligned Bool
needAligned
d <- Context -> String -> IO TranscriptHash
forall (m :: * -> *).
MonadIO m =>
Context -> String -> m TranscriptHash
transcriptHash Context
ctx String
"Pending action"
updateTranscriptHash13 ctx hb
pa d h
Context -> IO ()
sendCFifNecessary Context
ctx
where
checkAligned :: Bool -> IO ()
checkAligned Bool
needAligned = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
needAligned (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
aligned <- Context -> [(Handshake13, [ByteString])] -> IO Bool
isAligned Context
ctx [(Handshake13, [ByteString])]
hbs
unless aligned $ throwCore $ Error_Protocol notAligned UnexpectedMessage
isAligned :: Context -> [Handshake13R] -> IO Bool
isAligned :: Context -> [(Handshake13, [ByteString])] -> IO Bool
isAligned Context
ctx [(Handshake13, [ByteString])]
hbs = do
complete <- Context -> IO Bool
isRecvComplete Context
ctx
return $ complete && null hbs
notAligned :: String
notAligned :: String
notAligned = String
"received message not aligned with record boundary"
tryBye :: Context -> IO ()
tryBye :: Context -> IO ()
tryBye Context
ctx = IO () -> (SomeException -> IO ()) -> IO ()
forall a. IO a -> (SomeException -> IO a) -> IO a
catchException (Context -> IO ()
forall (m :: * -> *). MonadIO m => Context -> m ()
bye_ Context
ctx) (\SomeException
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
onError
:: Monad m
=> (TLSError -> AlertLevel -> AlertDescription -> String -> m ByteString)
-> TLSError
-> m ByteString
onError :: forall (m :: * -> *).
Monad m =>
(TLSError
-> AlertLevel -> AlertDescription -> String -> m ByteString)
-> TLSError -> m ByteString
onError TLSError
-> AlertLevel -> AlertDescription -> String -> m ByteString
_ TLSError
Error_EOF =
ByteString -> m ByteString
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
B.empty
onError TLSError
-> AlertLevel -> AlertDescription -> String -> m ByteString
terminate TLSError
err = TLSError
-> AlertLevel -> AlertDescription -> String -> m ByteString
terminate TLSError
err AlertLevel
lvl AlertDescription
ad String
reason
where
(AlertLevel
lvl, AlertDescription
ad) = TLSError -> (AlertLevel, AlertDescription)
errorToAlert TLSError
err
reason :: String
reason = TLSError -> String
errorToAlertMessage TLSError
err
terminateWithWriteLock
:: Context
-> ([(AlertLevel, AlertDescription)] -> IO ())
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO a
terminateWithWriteLock :: forall a.
Context
-> ([(AlertLevel, AlertDescription)] -> IO ())
-> TLSError
-> AlertLevel
-> AlertDescription
-> String
-> IO a
terminateWithWriteLock Context
ctx [(AlertLevel, AlertDescription)] -> IO ()
send TLSError
err AlertLevel
level AlertDescription
desc String
reason = Context -> IO a -> IO a
forall a. Context -> IO a -> IO a
withWriteLock Context
ctx (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
tls13 <- Context -> IO Bool
forall (m :: * -> *). MonadIO m => Context -> m Bool
tls13orLater Context
ctx
unless tls13 $ do
session <- usingState_ ctx getSession
case session of
Session Maybe ByteString
Nothing -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Session (Just ByteString
sid) ->
SessionManager -> ByteString -> IO ()
sessionInvalidate (Shared -> SessionManager
sharedSessionManager (Shared -> SessionManager) -> Shared -> SessionManager
forall a b. (a -> b) -> a -> b
$ Context -> Shared
ctxShared Context
ctx) ByteString
sid
catchException (send [(level, desc)]) (\SomeException
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
setEOF ctx
debugError (ctxDebug ctx) reason
E.throwIO (Terminated False reason err)
closeNotifyReceived :: MonadIO m => Context -> m Bool
closeNotifyReceived :: forall (m :: * -> *). MonadIO m => Context -> m Bool
closeNotifyReceived = IO Bool -> m Bool
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> (Context -> IO Bool) -> Context -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context -> IO Bool
ctxCloseNotify
{-# DEPRECATED recvData' "use recvData that returns strict bytestring" #-}
recvData' :: MonadIO m => Context -> m L.ByteString
recvData' :: forall (m :: * -> *). MonadIO m => Context -> m ByteString
recvData' Context
ctx = [ByteString] -> ByteString
L.fromChunks ([ByteString] -> ByteString)
-> (ByteString -> [ByteString]) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: []) (ByteString -> ByteString) -> m ByteString -> m ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> m ByteString
forall (m :: * -> *). MonadIO m => Context -> m ByteString
recvData Context
ctx