{-# LANGUAGE TypeApplications #-}
module Network.TLS.Record.Recv (
recvRecord12,
recvRecord13,
) where
import Control.Concurrent.STM
import qualified Control.Exception as E
import qualified Data.ByteString as B
import Data.IORef
import Network.TLS.Backend
import Network.TLS.Context.Internal
import Network.TLS.Hooks
import Network.TLS.Imports
import Network.TLS.Measurement
import Network.TLS.Packet
import Network.TLS.Record
import Network.TLS.Struct
import Network.TLS.Types
getMyPlainLimit :: Context -> IO Int
getMyPlainLimit :: Context -> IO Int
getMyPlainLimit Context
ctx = do
msiz <- Context -> IO (Maybe Int)
getMyRecordLimit Context
ctx
return $ case msiz of
Maybe Int
Nothing -> Int
defaultRecordSizeLimit
Just Int
siz -> Int
siz
getRecord
:: Context
-> Header
-> ByteString
-> IO (Either TLSError (Record Plaintext))
getRecord :: Context
-> Header -> ByteString -> IO (Either TLSError (Record Plaintext))
getRecord Context
ctx Header
header ByteString
content = do
Context -> (Logging -> IO ()) -> IO ()
withLog Context
ctx ((Logging -> IO ()) -> IO ()) -> (Logging -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Logging
logging -> Logging -> Header -> ByteString -> IO ()
loggingIORecv Logging
logging Header
header ByteString
content
lim <- Context -> IO Int
getMyPlainLimit Context
ctx
runRxRecordState ctx $ do
let erecord = Header -> Fragment Ciphertext -> Record Ciphertext
forall a. Header -> Fragment a -> Record a
rawToRecord Header
header (Fragment Ciphertext -> Record Ciphertext)
-> Fragment Ciphertext -> Record Ciphertext
forall a b. (a -> b) -> a -> b
$ ByteString -> Fragment Ciphertext
fragmentCiphertext ByteString
content
decryptRecord erecord lim
exceedsTLSCiphertext :: Int -> Word16 -> Bool
exceedsTLSCiphertext :: Int -> Word16 -> Bool
exceedsTLSCiphertext Int
overhead Word16
actual =
Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
actual Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
defaultRecordSizeLimit Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
overhead
recvRecord12
:: Context
-> IO (Either TLSError (Record Plaintext))
recvRecord12 :: Context -> IO (Either TLSError (Record Plaintext))
recvRecord12 Context
ctx =
Context -> Bool -> Int -> IO (Either TLSError ByteString)
readExactBytes Context
ctx Bool
False Int
5
IO (Either TLSError ByteString)
-> (Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext)))
-> IO (Either TLSError (Record Plaintext))
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TLSError -> IO (Either TLSError (Record Plaintext)))
-> (ByteString -> IO (Either TLSError (Record Plaintext)))
-> Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> (TLSError -> Either TLSError (Record Plaintext))
-> TLSError
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left) (Either TLSError Header -> IO (Either TLSError (Record Plaintext))
recvLengthE (Either TLSError Header -> IO (Either TLSError (Record Plaintext)))
-> (ByteString -> Either TLSError Header)
-> ByteString
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Either TLSError Header
decodeHeader (ByteString -> Either TLSError Header)
-> (Header -> Either TLSError Header)
-> ByteString
-> Either TLSError Header
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Header -> Either TLSError Header
checkType))
where
recvLengthE :: Either TLSError Header -> IO (Either TLSError (Record Plaintext))
recvLengthE = (TLSError -> IO (Either TLSError (Record Plaintext)))
-> (Header -> IO (Either TLSError (Record Plaintext)))
-> Either TLSError Header
-> IO (Either TLSError (Record Plaintext))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> (TLSError -> Either TLSError (Record Plaintext))
-> TLSError
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left) Header -> IO (Either TLSError (Record Plaintext))
recvLength
recvLength :: Header -> IO (Either TLSError (Record Plaintext))
recvLength header :: Header
header@(Header ProtocolType
_ Version
_ Word16
readlen) = do
if Int -> Word16 -> Bool
exceedsTLSCiphertext Int
2048 Word16
readlen
then Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a b. (a -> b) -> a -> b
$ TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left TLSError
maximumSizeExceeded
else
Context -> Bool -> Int -> IO (Either TLSError ByteString)
readExactBytes Context
ctx Bool
True (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
readlen)
IO (Either TLSError ByteString)
-> (Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext)))
-> IO (Either TLSError (Record Plaintext))
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TLSError -> IO (Either TLSError (Record Plaintext)))
-> (ByteString -> IO (Either TLSError (Record Plaintext)))
-> Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> (TLSError -> Either TLSError (Record Plaintext))
-> TLSError
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left) (Context
-> Header -> ByteString -> IO (Either TLSError (Record Plaintext))
getRecord Context
ctx Header
header)
recvRecord13 :: Context -> IO (Either TLSError (Record Plaintext))
recvRecord13 :: Context -> IO (Either TLSError (Record Plaintext))
recvRecord13 Context
ctx =
Context -> Bool -> Int -> IO (Either TLSError ByteString)
readExactBytes Context
ctx Bool
False Int
5
IO (Either TLSError ByteString)
-> (Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext)))
-> IO (Either TLSError (Record Plaintext))
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TLSError -> IO (Either TLSError (Record Plaintext)))
-> (ByteString -> IO (Either TLSError (Record Plaintext)))
-> Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> (TLSError -> Either TLSError (Record Plaintext))
-> TLSError
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left) (Either TLSError Header -> IO (Either TLSError (Record Plaintext))
recvLengthE (Either TLSError Header -> IO (Either TLSError (Record Plaintext)))
-> (ByteString -> Either TLSError Header)
-> ByteString
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Either TLSError Header
decodeHeader (ByteString -> Either TLSError Header)
-> (Header -> Either TLSError Header)
-> ByteString
-> Either TLSError Header
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Header -> Either TLSError Header
checkType))
where
recvLengthE :: Either TLSError Header -> IO (Either TLSError (Record Plaintext))
recvLengthE = (TLSError -> IO (Either TLSError (Record Plaintext)))
-> (Header -> IO (Either TLSError (Record Plaintext)))
-> Either TLSError Header
-> IO (Either TLSError (Record Plaintext))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> (TLSError -> Either TLSError (Record Plaintext))
-> TLSError
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left) Header -> IO (Either TLSError (Record Plaintext))
recvLength
recvLength :: Header -> IO (Either TLSError (Record Plaintext))
recvLength header :: Header
header@(Header ProtocolType
_ Version
_ Word16
readlen) = do
if Int -> Word16 -> Bool
exceedsTLSCiphertext Int
256 Word16
readlen
then Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a b. (a -> b) -> a -> b
$ TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left TLSError
maximumSizeExceeded
else
Context -> Bool -> Int -> IO (Either TLSError ByteString)
readExactBytes Context
ctx Bool
True (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
readlen)
IO (Either TLSError ByteString)
-> (Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext)))
-> IO (Either TLSError (Record Plaintext))
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TLSError -> IO (Either TLSError (Record Plaintext)))
-> (ByteString -> IO (Either TLSError (Record Plaintext)))
-> Either TLSError ByteString
-> IO (Either TLSError (Record Plaintext))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext)))
-> (TLSError -> Either TLSError (Record Plaintext))
-> TLSError
-> IO (Either TLSError (Record Plaintext))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TLSError -> Either TLSError (Record Plaintext)
forall a b. a -> Either a b
Left) (Header -> ByteString -> IO (Either TLSError (Record Plaintext))
getRecord13 Header
header)
getRecord13 :: Header -> ByteString -> IO (Either TLSError (Record Plaintext))
getRecord13 header :: Header
header@(Header ProtocolType
ty Version
_ Word16
readlen) ByteString
content = do
erecord <- Context
-> Header -> ByteString -> IO (Either TLSError (Record Plaintext))
getRecord Context
ctx Header
header ByteString
content
case erecord of
Left (Error_Protocol String
_ AlertDescription
BadRecordMac) -> do
skipped <- Context -> Int -> IO Bool
skipEarlyData Context
ctx (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
readlen)
if skipped then recvRecord13 ctx else return erecord
Right Record Plaintext
_ | ProtocolType
ty ProtocolType -> ProtocolType -> Bool
forall a. Eq a => a -> a -> Bool
== ProtocolType
ProtocolType_AppData -> do
established <- Context -> IO Established
ctxEstablished Context
ctx
case established of
EarlyDataNotAllowed Word32
_ -> Context -> Established -> IO ()
setEstablished Context
ctx Established
NotEstablished
Established
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
return erecord
Either TLSError (Record Plaintext)
_ -> Either TLSError (Record Plaintext)
-> IO (Either TLSError (Record Plaintext))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Either TLSError (Record Plaintext)
erecord
maximumSizeExceeded :: TLSError
maximumSizeExceeded :: TLSError
maximumSizeExceeded = String -> AlertDescription -> TLSError
Error_Protocol String
"record exceeding maximum size" AlertDescription
RecordOverflow
checkType :: Header -> Either TLSError Header
checkType :: Header -> Either TLSError Header
checkType header :: Header
header@(Header ProtocolType
ty Version
_ Word16
_)
| ProtocolType
ty ProtocolType -> [ProtocolType] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ProtocolType]
known = Header -> Either TLSError Header
forall a b. b -> Either a b
Right Header
header
| Bool
otherwise =
TLSError -> Either TLSError Header
forall a b. a -> Either a b
Left (TLSError -> Either TLSError Header)
-> TLSError -> Either TLSError Header
forall a b. (a -> b) -> a -> b
$ String -> String -> TLSError
Error_Packet_unexpected (ProtocolType -> String
forall a. Show a => a -> String
show ProtocolType
ty) String
" expected: TLS record type"
where
known :: [ProtocolType]
known =
[ ProtocolType
ProtocolType_ChangeCipherSpec
, ProtocolType
ProtocolType_Alert
, ProtocolType
ProtocolType_Handshake
, ProtocolType
ProtocolType_AppData
]
readExactBytes :: Context -> Bool -> Int -> IO (Either TLSError ByteString)
readExactBytes :: Context -> Bool -> Int -> IO (Either TLSError ByteString)
readExactBytes Context
ctx Bool
inRecord Int
sz = do
Context -> IO (Maybe SomeException)
ctxRxBrokenBy Context
ctx IO (Maybe SomeException) -> (Maybe SomeException -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SomeException -> IO (ZonkAny 0)) -> Maybe SomeException -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String -> SomeException -> IO (ZonkAny 0)
forall a. String -> SomeException -> IO a
throwBroken String
"a previous receive failed")
mgiveUp <- IORef (Maybe GiveUp) -> IO (Maybe GiveUp)
forall a. IORef a -> IO a
readIORef (IORef (Maybe GiveUp) -> IO (Maybe GiveUp))
-> IORef (Maybe GiveUp) -> IO (Maybe GiveUp)
forall a b. (a -> b) -> a -> b
$ Context -> IORef (Maybe GiveUp)
ctxRxGiveUp Context
ctx
hdrbs <- recv mgiveUp `E.catch` broken
if B.length hdrbs == sz
then return $ Right hdrbs
else do
setEOF ctx
return . Left $
if B.null hdrbs
then Error_EOF
else
Error_Packet
( "partial packet: expecting "
++ show sz
++ " bytes, got: "
++ show (B.length hdrbs)
)
where
recv :: Maybe GiveUp -> IO ByteString
recv Maybe GiveUp
mgiveUp = case (Maybe GiveUp
mgiveUp, Context -> Maybe RecvWith
ctxBackendRecvWith Context
ctx) of
(Just GiveUp
giveUp, Just (RecvWith forall g. Int -> STM g -> IO (Either g ByteString)
recvWith)) -> do
Context -> (Measurement -> Measurement) -> IO ()
updateMeasure Context
ctx ((Measurement -> Measurement) -> IO ())
-> (Measurement -> Measurement) -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Measurement -> Measurement
addBytesReceived Int
sz
let inside :: Int -> IO ByteString
inside Int
n = do
r <- Int -> STM () -> IO (Either () ByteString)
forall g. Int -> STM g -> IO (Either g ByteString)
recvWith Int
n (STM () -> IO (Either () ByteString))
-> STM () -> IO (Either () ByteString)
forall a b. (a -> b) -> a -> b
$ GiveUp -> STM ()
giveUpInside GiveUp
giveUp
gaveUp <- fired $ giveUpInside giveUp
case r of
Right ByteString
bs | ByteString -> Int
B.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n Bool -> Bool -> Bool
|| Bool -> Bool
not Bool
gaveUp -> ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs
Either () ByteString
_ -> do
let e :: TLSException
e = TLSError -> TLSException
PostHandshake (TLSError -> TLSException) -> TLSError -> TLSException
forall a b. (a -> b) -> a -> b
$ String -> TLSError
Error_Misc String
"gave up inside a record"
Context -> SomeException -> IO ()
setRxBroken Context
ctx (SomeException -> IO ()) -> SomeException -> IO ()
forall a b. (a -> b) -> a -> b
$ TLSException -> SomeException
forall e. Exception e => e -> SomeException
E.toException TLSException
e
TLSException -> IO ByteString
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO TLSException
e
if Bool
inRecord
then Int -> IO ByteString
inside Int
sz
else do
r <- Int -> STM () -> IO (Either () ByteString)
forall g. Int -> STM g -> IO (Either g ByteString)
recvWith Int
sz (STM () -> IO (Either () ByteString))
-> STM () -> IO (Either () ByteString)
forall a b. (a -> b) -> a -> b
$ GiveUp -> STM ()
giveUpBetween GiveUp
giveUp
case r of
Left () -> GaveUp -> IO ByteString
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO GaveUp
GaveUp
Right ByteString
bs
| ByteString -> Int
B.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz Bool -> Bool -> Bool
|| ByteString -> Bool
B.null ByteString
bs -> ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs
| Bool
otherwise -> do
gaveUp <- STM () -> IO Bool
forall {b}. STM b -> IO Bool
fired (STM () -> IO Bool) -> STM () -> IO Bool
forall a b. (a -> b) -> a -> b
$ GiveUp -> STM ()
giveUpBetween GiveUp
giveUp
if gaveUp
then (bs <>) <$> inside (sz - B.length bs)
else return bs
(Maybe GiveUp, Maybe RecvWith)
_ -> Context -> Int -> IO ByteString
contextRecv Context
ctx Int
sz
fired :: STM b -> IO Bool
fired STM b
stm = STM Bool -> IO Bool
forall a. STM a -> IO a
atomically (STM Bool -> IO Bool) -> STM Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ (Bool
True Bool -> STM b -> STM Bool
forall a b. a -> STM b -> STM a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ STM b
stm) STM Bool -> STM Bool -> STM Bool
forall a. STM a -> STM a -> STM a
`orElse` Bool -> STM Bool
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
broken :: E.SomeException -> IO a
broken :: forall a. SomeException -> IO a
broken SomeException
e
| Maybe IOException -> Bool
forall a. Maybe a -> Bool
isJust (forall e. Exception e => SomeException -> Maybe e
E.fromException @E.IOException SomeException
e) = Context -> IO ()
setEOF Context
ctx IO () -> IO a -> IO a
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> SomeException -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO SomeException
e
| Maybe GaveUp -> Bool
forall a. Maybe a -> Bool
isJust (forall e. Exception e => SomeException -> Maybe e
E.fromException @GaveUp SomeException
e) = SomeException -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO SomeException
e
| Bool
inRecord = Context -> SomeException -> IO ()
setRxBroken Context
ctx SomeException
e IO () -> IO a -> IO a
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> SomeException -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO SomeException
e
| Bool
otherwise = SomeException -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO SomeException
e