{-# LANGUAGE DeriveGeneric #-}

module Network.TLS.Types.Session where

import Codec.Serialise
import Crypto.Debug (DebugShow (..))
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 :: ByteString -> Bool
isTicket ByteString
x
    | ByteString -> Int
B.length ByteString
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
32 = Bool
True
    | Bool
otherwise = Bool
False

toSessionID :: Ticket -> SessionID
toSessionID :: ByteString -> ByteString
toSessionID = Hash -> ByteString -> ByteString
forall ba. (ByteArray ba, ByteArrayAccess ba) => Hash -> ba -> ba
hash Hash
SHA256

-- | Compression identification
type CompressionID = Word8

-- | Session data to resume
data SessionData = SessionData
    { SessionData -> Version
sessionVersion :: Version
    , SessionData -> Word16
sessionCipher :: CipherID
    , SessionData -> Word8
sessionCompression :: CompressionID
    , SessionData -> Maybe HostName
sessionClientSNI :: Maybe HostName
    , -- ScrubbedBytes is not an instance of Generic, sigh.
      SessionData -> ByteString
sessionSecret :: ByteString
    , SessionData -> Maybe Group
sessionGroup :: Maybe Group
    , SessionData -> Maybe TLS13TicketInfo
sessionTicketInfo :: Maybe TLS13TicketInfo
    , SessionData -> Maybe ByteString
sessionALPN :: Maybe ByteString
    , SessionData -> Int
sessionMaxEarlyDataSize :: Int
    , SessionData -> [SessionFlag]
sessionFlags :: [SessionFlag]
    , SessionData -> Maybe ByteString
sessionClientCertificate :: Maybe ByteString
    -- ^ On a server, the DER of the leaf certificate the client was
    -- authenticated with, so that a resumed connection can tell the client.
    , SessionData -> Maybe ByteString
sessionAppData :: Maybe ByteString
    -- ^ Data of the application's own, kept with the session and given
    -- back on resumption; never set by this library.  A server can put
    -- what 0-RTT depends on here and check it in 'onAccept0RTT'.
    --
    -- @since 2.5.1
    }
    -- sessionFromTicket :: Bool
    deriving (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)

-- | Everything but @sessionSecret@, which renders as @\<secret\>@: whoever
-- has it can resume the session.  'Crypto.Debug.debugShow' renders it.
instance Show SessionData where
    showsPrec :: Int -> SessionData -> ShowS
showsPrec = ShowS -> Int -> SessionData -> ShowS
showsSessionData (HostName -> ShowS
showString HostName
"<secret>")

instance DebugShow SessionData where
    debugShow :: SessionData -> HostName
debugShow SessionData
sd = ShowS -> Int -> SessionData -> ShowS
showsSessionData (ByteString -> ShowS
forall a. Show a => a -> ShowS
shows (ByteString -> ShowS) -> ByteString -> ShowS
forall a b. (a -> b) -> a -> b
$ SessionData -> ByteString
sessionSecret SessionData
sd) Int
0 SessionData
sd HostName
""

-- | What the two instances above share, so that a field added to
-- 'SessionData' cannot reach one of them and not the other.
showsSessionData :: ShowS -> Int -> SessionData -> ShowS
showsSessionData :: ShowS -> Int -> SessionData -> ShowS
showsSessionData ShowS
secret Int
d SessionData
sd =
    Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
        HostName -> ShowS
showString HostName
"SessionData {sessionVersion = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Version
sessionVersion SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionCipher = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Word16
sessionCipher SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionCompression = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Word8
sessionCompression SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionClientSNI = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe HostName -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Maybe HostName
sessionClientSNI SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionSecret = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
secret
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionGroup = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Group -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Maybe Group
sessionGroup SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionTicketInfo = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe TLS13TicketInfo -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Maybe TLS13TicketInfo
sessionTicketInfo SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionALPN = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe ByteString -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Maybe ByteString
sessionALPN SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionMaxEarlyDataSize = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Int
sessionMaxEarlyDataSize SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionFlags = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SessionFlag] -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> [SessionFlag]
sessionFlags SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionClientCertificate = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString (HostName
-> (ByteString -> HostName) -> Maybe ByteString -> HostName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe HostName
"Nothing" ByteString -> HostName
derLength (SessionData -> Maybe ByteString
sessionClientCertificate SessionData
sd))
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostName -> ShowS
showString HostName
", sessionAppData = "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe ByteString -> ShowS
forall a. Show a => a -> ShowS
shows (SessionData -> Maybe ByteString
sessionAppData SessionData
sd)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
'}'
  where
    derLength :: ByteString -> HostName
derLength ByteString
der = HostName
"Just <" HostName -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> HostName
forall a. Show a => a -> HostName
show (ByteString -> Int
B.length ByteString
der) HostName -> ShowS
forall a. [a] -> [a] -> [a]
++ HostName
" bytes>"

-- | A 'SessionData' to build one from with record update syntax, so that a
-- field added later does not break the code that builds it.
defaultSessionData :: SessionData
defaultSessionData :: SessionData
defaultSessionData =
    SessionData
        { sessionVersion :: Version
sessionVersion = Version
TLS13
        , sessionCipher :: Word16
sessionCipher = Word16
0
        , sessionCompression :: Word8
sessionCompression = Word8
0
        , sessionClientSNI :: Maybe HostName
sessionClientSNI = Maybe HostName
forall a. Maybe a
Nothing
        , sessionSecret :: ByteString
sessionSecret = ByteString
B.empty
        , sessionGroup :: Maybe Group
sessionGroup = Maybe Group
forall a. Maybe a
Nothing
        , sessionTicketInfo :: Maybe TLS13TicketInfo
sessionTicketInfo = Maybe TLS13TicketInfo
forall a. Maybe a
Nothing
        , sessionALPN :: Maybe ByteString
sessionALPN = Maybe ByteString
forall a. Maybe a
Nothing
        , sessionMaxEarlyDataSize :: Int
sessionMaxEarlyDataSize = Int
0
        , sessionFlags :: [SessionFlag]
sessionFlags = []
        , sessionClientCertificate :: Maybe ByteString
sessionClientCertificate = Maybe ByteString
forall a. Maybe a
Nothing
        , sessionAppData :: Maybe ByteString
sessionAppData = Maybe ByteString
forall a. Maybe a
Nothing
        }

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