    {-# LANGUAGE DeriveGeneric #-}

    module Network.TLS.Types.Session where

    import Codec.Serialise
    import qualified Data.ByteString as B
    import GHC.Generics
    import Network.Socket (HostName)

    import Network.TLS.Crypto (Group, Hash (..), hash)
    import Network.TLS.Imports
    import Network.TLS.Types.Cipher
    import Network.TLS.Types.Version

    -- | A session ID
    type SessionID = ByteString

    -- | Identity
    type SessionIDorTicket = ByteString

    -- | Encrypted session ticket (encrypt(encode 'SessionData')).
    type Ticket = ByteString

    isTicket :: SessionIDorTicket -> Bool
    isTicket :: SessionIDorTicket -> Bool
    isTicket SessionIDorTicket
    x
        | SessionIDorTicket -> Int
    B.length SessionIDorTicket
    x Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > Int
    32 = Bool
    True
        | Bool
    otherwise = Bool
    False

    toSessionID :: Ticket -> SessionID
    toSessionID :: SessionIDorTicket -> SessionIDorTicket
    toSessionID = Hash -> SessionIDorTicket -> SessionIDorTicket
    hash Hash
    SHA256

    -- | Compression identification
    type CompressionID = Word8

    -- | Session data to resume
    data SessionData = SessionData
        { SessionData -> Version
    sessionVersion :: Version
        , SessionData -> CipherID
    sessionCipher :: CipherID
        , SessionData -> CompressionID
    sessionCompression :: CompressionID
        , SessionData -> Maybe HostName
    sessionClientSNI :: Maybe HostName
        , SessionData -> SessionIDorTicket
    sessionSecret :: ByteString
        , SessionData -> Maybe Group
    sessionGroup :: Maybe Group
        , SessionData -> Maybe TLS13TicketInfo
    sessionTicketInfo :: Maybe TLS13TicketInfo
        , SessionData -> Maybe SessionIDorTicket
    sessionALPN :: Maybe ByteString
        , SessionData -> Int
    sessionMaxEarlyDataSize :: Int
        , SessionData -> [SessionFlag]
    sessionFlags :: [SessionFlag]
        } -- sessionFromTicket :: Bool
        deriving (Int -> SessionData -> ShowS
    [SessionData] -> ShowS
    SessionData -> HostName
    (Int -> SessionData -> ShowS)
    -> (SessionData -> HostName)
    -> ([SessionData] -> ShowS)
    -> Show SessionData
    forall a.
    (Int -> a -> ShowS) -> (a -> HostName) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> SessionData -> ShowS
    showsPrec :: Int -> SessionData -> ShowS
    $cshow :: SessionData -> HostName
    show :: SessionData -> HostName
    $cshowList :: [SessionData] -> ShowS
    showList :: [SessionData] -> ShowS
    Show, SessionData -> SessionData -> Bool
    (SessionData -> SessionData -> Bool)
    -> (SessionData -> SessionData -> Bool) -> Eq SessionData
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: SessionData -> SessionData -> Bool
    == :: SessionData -> SessionData -> Bool
    $c/= :: SessionData -> SessionData -> Bool
    /= :: SessionData -> SessionData -> Bool
    Eq, (forall x. SessionData -> Rep SessionData x)
    -> (forall x. Rep SessionData x -> SessionData)
    -> Generic SessionData
    forall x. Rep SessionData x -> SessionData
    forall x. SessionData -> Rep SessionData x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. SessionData -> Rep SessionData x
    from :: forall x. SessionData -> Rep SessionData x
    $cto :: forall x. Rep SessionData x -> SessionData
    to :: forall x. Rep SessionData x -> SessionData
    Generic)

    is0RTTPossible :: SessionData -> Bool
    is0RTTPossible :: SessionData -> Bool
    is0RTTPossible SessionData
    sd = SessionData -> Int
    sessionMaxEarlyDataSize SessionData
    sd Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > Int
    0

    -- | Some session flags
    data SessionFlag
        = -- | Session created with Extended Main Secret
          SessionEMS
        deriving (Int -> SessionFlag -> ShowS
    [SessionFlag] -> ShowS
    SessionFlag -> HostName
    (Int -> SessionFlag -> ShowS)
    -> (SessionFlag -> HostName)
    -> ([SessionFlag] -> ShowS)
    -> Show SessionFlag
    forall a.
    (Int -> a -> ShowS) -> (a -> HostName) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> SessionFlag -> ShowS
    showsPrec :: Int -> SessionFlag -> ShowS
    $cshow :: SessionFlag -> HostName
    show :: SessionFlag -> HostName
    $cshowList :: [SessionFlag] -> ShowS
    showList :: [SessionFlag] -> ShowS
    Show, SessionFlag -> SessionFlag -> Bool
    (SessionFlag -> SessionFlag -> Bool)
    -> (SessionFlag -> SessionFlag -> Bool) -> Eq SessionFlag
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: SessionFlag -> SessionFlag -> Bool
    == :: SessionFlag -> SessionFlag -> Bool
    $c/= :: SessionFlag -> SessionFlag -> Bool
    /= :: SessionFlag -> SessionFlag -> Bool
    Eq, Int -> SessionFlag
    SessionFlag -> Int
    SessionFlag -> [SessionFlag]
    SessionFlag -> SessionFlag
    SessionFlag -> SessionFlag -> [SessionFlag]
    SessionFlag -> SessionFlag -> SessionFlag -> [SessionFlag]
    (SessionFlag -> SessionFlag)
    -> (SessionFlag -> SessionFlag)
    -> (Int -> SessionFlag)
    -> (SessionFlag -> Int)
    -> (SessionFlag -> [SessionFlag])
    -> (SessionFlag -> SessionFlag -> [SessionFlag])
    -> (SessionFlag -> SessionFlag -> [SessionFlag])
    -> (SessionFlag -> SessionFlag -> SessionFlag -> [SessionFlag])
    -> Enum SessionFlag
    forall a.
    (a -> a)
    -> (a -> a)
    -> (Int -> a)
    -> (a -> Int)
    -> (a -> [a])
    -> (a -> a -> [a])
    -> (a -> a -> [a])
    -> (a -> a -> a -> [a])
    -> Enum a
    $csucc :: SessionFlag -> SessionFlag
    succ :: SessionFlag -> SessionFlag
    $cpred :: SessionFlag -> SessionFlag
    pred :: SessionFlag -> SessionFlag
    $ctoEnum :: Int -> SessionFlag
    toEnum :: Int -> SessionFlag
    $cfromEnum :: SessionFlag -> Int
    fromEnum :: SessionFlag -> Int
    $cenumFrom :: SessionFlag -> [SessionFlag]
    enumFrom :: SessionFlag -> [SessionFlag]
    $cenumFromThen :: SessionFlag -> SessionFlag -> [SessionFlag]
    enumFromThen :: SessionFlag -> SessionFlag -> [SessionFlag]
    $cenumFromTo :: SessionFlag -> SessionFlag -> [SessionFlag]
    enumFromTo :: SessionFlag -> SessionFlag -> [SessionFlag]
    $cenumFromThenTo :: SessionFlag -> SessionFlag -> SessionFlag -> [SessionFlag]
    enumFromThenTo :: SessionFlag -> SessionFlag -> SessionFlag -> [SessionFlag]
    Enum, (forall x. SessionFlag -> Rep SessionFlag x)
    -> (forall x. Rep SessionFlag x -> SessionFlag)
    -> Generic SessionFlag
    forall x. Rep SessionFlag x -> SessionFlag
    forall x. SessionFlag -> Rep SessionFlag x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. SessionFlag -> Rep SessionFlag x
    from :: forall x. SessionFlag -> Rep SessionFlag x
    $cto :: forall x. Rep SessionFlag x -> SessionFlag
    to :: forall x. Rep SessionFlag x -> SessionFlag
    Generic)

    type Second = Word32
    type Millisecond = Word64

    data TLS13TicketInfo = TLS13TicketInfo
        { TLS13TicketInfo -> Second
    lifetime :: Second -- NewSessionTicket.ticket_lifetime in seconds
        , TLS13TicketInfo -> Second
    ageAdd :: Second -- NewSessionTicket.ticket_age_add
        , TLS13TicketInfo -> Millisecond
    txrxTime :: Millisecond -- serverSendTime or clientReceiveTime
        , TLS13TicketInfo -> Maybe Millisecond
    estimatedRTT :: Maybe Millisecond
        }
        deriving (Int -> TLS13TicketInfo -> ShowS
    [TLS13TicketInfo] -> ShowS
    TLS13TicketInfo -> HostName
    (Int -> TLS13TicketInfo -> ShowS)
    -> (TLS13TicketInfo -> HostName)
    -> ([TLS13TicketInfo] -> ShowS)
    -> Show TLS13TicketInfo
    forall a.
    (Int -> a -> ShowS) -> (a -> HostName) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> TLS13TicketInfo -> ShowS
    showsPrec :: Int -> TLS13TicketInfo -> ShowS
    $cshow :: TLS13TicketInfo -> HostName
    show :: TLS13TicketInfo -> HostName
    $cshowList :: [TLS13TicketInfo] -> ShowS
    showList :: [TLS13TicketInfo] -> ShowS
    Show, TLS13TicketInfo -> TLS13TicketInfo -> Bool
    (TLS13TicketInfo -> TLS13TicketInfo -> Bool)
    -> (TLS13TicketInfo -> TLS13TicketInfo -> Bool)
    -> Eq TLS13TicketInfo
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
    == :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
    $c/= :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
    /= :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
    Eq, (forall x. TLS13TicketInfo -> Rep TLS13TicketInfo x)
    -> (forall x. Rep TLS13TicketInfo x -> TLS13TicketInfo)
    -> Generic TLS13TicketInfo
    forall x. Rep TLS13TicketInfo x -> TLS13TicketInfo
    forall x. TLS13TicketInfo -> Rep TLS13TicketInfo x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. TLS13TicketInfo -> Rep TLS13TicketInfo x
    from :: forall x. TLS13TicketInfo -> Rep TLS13TicketInfo x
    $cto :: forall x. Rep TLS13TicketInfo x -> TLS13TicketInfo
    to :: forall x. Rep TLS13TicketInfo x -> TLS13TicketInfo
    Generic)

    instance Serialise TLS13TicketInfo
    instance Serialise SessionFlag
    instance Serialise SessionData