{-# LANGUAGE RecordWildCards #-}

module Network.QUIC.Types.Resumption where

import Network.TLS
import Network.TLS.QUIC

import Network.QUIC.Imports
import Network.QUIC.Types.Frame

type SessionEstablish = SessionID -> SessionData -> IO ()

-- | Information about resumption
data ResumptionInfo = ResumptionInfo {
    ResumptionInfo -> Maybe (SessionID, SessionData)
resumptionSession :: Maybe (SessionID, SessionData)
  , ResumptionInfo -> SessionID
resumptionToken   :: Token
  , ResumptionInfo -> Bool
resumptionRetry   :: Bool
  } deriving (ResumptionInfo -> ResumptionInfo -> Bool
(ResumptionInfo -> ResumptionInfo -> Bool)
-> (ResumptionInfo -> ResumptionInfo -> Bool) -> Eq ResumptionInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ResumptionInfo -> ResumptionInfo -> Bool
$c/= :: ResumptionInfo -> ResumptionInfo -> Bool
== :: ResumptionInfo -> ResumptionInfo -> Bool
$c== :: ResumptionInfo -> ResumptionInfo -> Bool
Eq, Int -> ResumptionInfo -> ShowS
[ResumptionInfo] -> ShowS
ResumptionInfo -> String
(Int -> ResumptionInfo -> ShowS)
-> (ResumptionInfo -> String)
-> ([ResumptionInfo] -> ShowS)
-> Show ResumptionInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ResumptionInfo] -> ShowS
$cshowList :: [ResumptionInfo] -> ShowS
show :: ResumptionInfo -> String
$cshow :: ResumptionInfo -> String
showsPrec :: Int -> ResumptionInfo -> ShowS
$cshowsPrec :: Int -> ResumptionInfo -> ShowS
Show)

defaultResumptionInfo :: ResumptionInfo
defaultResumptionInfo :: ResumptionInfo
defaultResumptionInfo = ResumptionInfo :: Maybe (SessionID, SessionData)
-> SessionID -> Bool -> ResumptionInfo
ResumptionInfo {
    resumptionSession :: Maybe (SessionID, SessionData)
resumptionSession = Maybe (SessionID, SessionData)
forall a. Maybe a
Nothing
  , resumptionToken :: SessionID
resumptionToken   = SessionID
emptyToken
  , resumptionRetry :: Bool
resumptionRetry   = Bool
False
  }

-- | Is 0RTT possible?
is0RTTPossible :: ResumptionInfo -> Bool
is0RTTPossible :: ResumptionInfo -> Bool
is0RTTPossible ResumptionInfo{Bool
Maybe (SessionID, SessionData)
SessionID
resumptionRetry :: Bool
resumptionToken :: SessionID
resumptionSession :: Maybe (SessionID, SessionData)
resumptionRetry :: ResumptionInfo -> Bool
resumptionToken :: ResumptionInfo -> SessionID
resumptionSession :: ResumptionInfo -> Maybe (SessionID, SessionData)
..} =
    Bool
rtt0OK Bool -> Bool -> Bool
&& (Bool -> Bool
not Bool
resumptionRetry Bool -> Bool -> Bool
|| SessionID
resumptionToken SessionID -> SessionID -> Bool
forall a. Eq a => a -> a -> Bool
/= SessionID
emptyToken)
  where
    rtt0OK :: Bool
rtt0OK = case Maybe (SessionID, SessionData)
resumptionSession of
      Maybe (SessionID, SessionData)
Nothing      -> Bool
False
      Just (SessionID
_, SessionData
sd) -> SessionData -> Int
sessionMaxEarlyDataSize SessionData
sd Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
quicMaxEarlyDataSize

-- | Is resumption possible?
isResumptionPossible :: ResumptionInfo -> Bool
isResumptionPossible :: ResumptionInfo -> Bool
isResumptionPossible ResumptionInfo{Bool
Maybe (SessionID, SessionData)
SessionID
resumptionRetry :: Bool
resumptionToken :: SessionID
resumptionSession :: Maybe (SessionID, SessionData)
resumptionRetry :: ResumptionInfo -> Bool
resumptionToken :: ResumptionInfo -> SessionID
resumptionSession :: ResumptionInfo -> Maybe (SessionID, SessionData)
..} = Maybe (SessionID, SessionData) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (SessionID, SessionData)
resumptionSession

get0RTTCipher :: ResumptionInfo -> Maybe CipherID
get0RTTCipher :: ResumptionInfo -> Maybe CipherID
get0RTTCipher ResumptionInfo
ri = case ResumptionInfo -> Maybe (SessionID, SessionData)
resumptionSession ResumptionInfo
ri of
  Maybe (SessionID, SessionData)
Nothing      -> Maybe CipherID
forall a. Maybe a
Nothing
  Just (SessionID
_, SessionData
sd) -> CipherID -> Maybe CipherID
forall a. a -> Maybe a
Just (CipherID -> Maybe CipherID) -> CipherID -> Maybe CipherID
forall a b. (a -> b) -> a -> b
$ SessionData -> CipherID
sessionCipher SessionData
sd