{-# LANGUAGE CPP #-}
{-# LANGUAGE EmptyDataDecls #-}
-- |
-- Module      : Network.TLS.Types
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
module Network.TLS.Types
    ( Version(..)
    , SessionID
    , SessionData(..)
    , SessionFlag(..)
    , CertReqContext
    , TLS13TicketInfo(..)
    , CipherID
    , CompressionID
    , Role(..)
    , invertRole
    , Direction(..)
    , HostName
    , Second
    , Millisecond
    , EarlySecret
    , HandshakeSecret
    , ApplicationSecret
    , ResumptionSecret
    , BaseSecret(..)
    , AnyTrafficSecret(..)
    , ClientTrafficSecret(..)
    , ServerTrafficSecret(..)
    , TrafficSecrets
    , SecretTriple(..)
    , SecretPair(..)
    , MasterSecret(..)
    ) where

#ifdef INCLUDE_NETWORK
import Network.Socket (HostName)
#endif

import Network.TLS.Imports
import Network.TLS.Crypto.Types (Group)

#ifndef INCLUDE_NETWORK
type HostName    = String
#endif
type Second      = Word32
type Millisecond = Word64

-- | Versions known to TLS
--
-- SSL2 is just defined, but this version is and will not be supported.
data Version = SSL2 | SSL3 | TLS10 | TLS11 | TLS12 | TLS13 deriving (Int -> Version -> ShowS
[Version] -> ShowS
Version -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Version] -> ShowS
$cshowList :: [Version] -> ShowS
show :: Version -> String
$cshow :: Version -> String
showsPrec :: Int -> Version -> ShowS
$cshowsPrec :: Int -> Version -> ShowS
Show, Version -> Version -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Version -> Version -> Bool
$c/= :: Version -> Version -> Bool
== :: Version -> Version -> Bool
$c== :: Version -> Version -> Bool
Eq, Eq Version
Version -> Version -> Bool
Version -> Version -> Ordering
Version -> Version -> Version
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Version -> Version -> Version
$cmin :: Version -> Version -> Version
max :: Version -> Version -> Version
$cmax :: Version -> Version -> Version
>= :: Version -> Version -> Bool
$c>= :: Version -> Version -> Bool
> :: Version -> Version -> Bool
$c> :: Version -> Version -> Bool
<= :: Version -> Version -> Bool
$c<= :: Version -> Version -> Bool
< :: Version -> Version -> Bool
$c< :: Version -> Version -> Bool
compare :: Version -> Version -> Ordering
$ccompare :: Version -> Version -> Ordering
Ord, Version
forall a. a -> a -> Bounded a
maxBound :: Version
$cmaxBound :: Version
minBound :: Version
$cminBound :: Version
Bounded)

-- | A session ID
type SessionID = ByteString

-- | Session data to resume
data SessionData = SessionData
    { SessionData -> Version
sessionVersion     :: Version
    , SessionData -> CipherID
sessionCipher      :: CipherID
    , SessionData -> CompressionID
sessionCompression :: CompressionID
    , SessionData -> Maybe String
sessionClientSNI   :: Maybe HostName
    , 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]
    } deriving (Int -> SessionData -> ShowS
[SessionData] -> ShowS
SessionData -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SessionData] -> ShowS
$cshowList :: [SessionData] -> ShowS
show :: SessionData -> String
$cshow :: SessionData -> String
showsPrec :: Int -> SessionData -> ShowS
$cshowsPrec :: Int -> SessionData -> ShowS
Show,SessionData -> SessionData -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SessionData -> SessionData -> Bool
$c/= :: SessionData -> SessionData -> Bool
== :: SessionData -> SessionData -> Bool
$c== :: SessionData -> SessionData -> Bool
Eq)

-- | Some session flags
data SessionFlag
    = SessionEMS        -- ^ Session created with Extended Master Secret
    deriving (Int -> SessionFlag -> ShowS
[SessionFlag] -> ShowS
SessionFlag -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SessionFlag] -> ShowS
$cshowList :: [SessionFlag] -> ShowS
show :: SessionFlag -> String
$cshow :: SessionFlag -> String
showsPrec :: Int -> SessionFlag -> ShowS
$cshowsPrec :: Int -> SessionFlag -> ShowS
Show,SessionFlag -> SessionFlag -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SessionFlag -> SessionFlag -> Bool
$c/= :: SessionFlag -> SessionFlag -> Bool
== :: SessionFlag -> SessionFlag -> Bool
$c== :: SessionFlag -> SessionFlag -> Bool
Eq,Int -> SessionFlag
SessionFlag -> Int
SessionFlag -> [SessionFlag]
SessionFlag -> SessionFlag
SessionFlag -> SessionFlag -> [SessionFlag]
SessionFlag -> SessionFlag -> SessionFlag -> [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
enumFromThenTo :: SessionFlag -> SessionFlag -> SessionFlag -> [SessionFlag]
$cenumFromThenTo :: SessionFlag -> SessionFlag -> SessionFlag -> [SessionFlag]
enumFromTo :: SessionFlag -> SessionFlag -> [SessionFlag]
$cenumFromTo :: SessionFlag -> SessionFlag -> [SessionFlag]
enumFromThen :: SessionFlag -> SessionFlag -> [SessionFlag]
$cenumFromThen :: SessionFlag -> SessionFlag -> [SessionFlag]
enumFrom :: SessionFlag -> [SessionFlag]
$cenumFrom :: SessionFlag -> [SessionFlag]
fromEnum :: SessionFlag -> Int
$cfromEnum :: SessionFlag -> Int
toEnum :: Int -> SessionFlag
$ctoEnum :: Int -> SessionFlag
pred :: SessionFlag -> SessionFlag
$cpred :: SessionFlag -> SessionFlag
succ :: SessionFlag -> SessionFlag
$csucc :: SessionFlag -> SessionFlag
Enum)

-- | Certificate request context for TLS 1.3.
type CertReqContext = ByteString

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 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TLS13TicketInfo] -> ShowS
$cshowList :: [TLS13TicketInfo] -> ShowS
show :: TLS13TicketInfo -> String
$cshow :: TLS13TicketInfo -> String
showsPrec :: Int -> TLS13TicketInfo -> ShowS
$cshowsPrec :: Int -> TLS13TicketInfo -> ShowS
Show, TLS13TicketInfo -> TLS13TicketInfo -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
$c/= :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
== :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
$c== :: TLS13TicketInfo -> TLS13TicketInfo -> Bool
Eq)

-- | Cipher identification
type CipherID = Word16

-- | Compression identification
type CompressionID = Word8

-- | Role
data Role = ClientRole | ServerRole
    deriving (Int -> Role -> ShowS
[Role] -> ShowS
Role -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Role] -> ShowS
$cshowList :: [Role] -> ShowS
show :: Role -> String
$cshow :: Role -> String
showsPrec :: Int -> Role -> ShowS
$cshowsPrec :: Int -> Role -> ShowS
Show,Role -> Role -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Role -> Role -> Bool
$c/= :: Role -> Role -> Bool
== :: Role -> Role -> Bool
$c== :: Role -> Role -> Bool
Eq)

-- | Direction
data Direction = Tx | Rx
    deriving (Int -> Direction -> ShowS
[Direction] -> ShowS
Direction -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Direction] -> ShowS
$cshowList :: [Direction] -> ShowS
show :: Direction -> String
$cshow :: Direction -> String
showsPrec :: Int -> Direction -> ShowS
$cshowsPrec :: Int -> Direction -> ShowS
Show,Direction -> Direction -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Direction -> Direction -> Bool
$c/= :: Direction -> Direction -> Bool
== :: Direction -> Direction -> Bool
$c== :: Direction -> Direction -> Bool
Eq)

invertRole :: Role -> Role
invertRole :: Role -> Role
invertRole Role
ClientRole = Role
ServerRole
invertRole Role
ServerRole = Role
ClientRole

-- | Phantom type indicating early traffic secret.
data EarlySecret

-- | Phantom type indicating handshake traffic secrets.
data HandshakeSecret

-- | Phantom type indicating application traffic secrets.
data ApplicationSecret

data ResumptionSecret

newtype BaseSecret a = BaseSecret ByteString deriving Int -> BaseSecret a -> ShowS
forall a. Int -> BaseSecret a -> ShowS
forall a. [BaseSecret a] -> ShowS
forall a. BaseSecret a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BaseSecret a] -> ShowS
$cshowList :: forall a. [BaseSecret a] -> ShowS
show :: BaseSecret a -> String
$cshow :: forall a. BaseSecret a -> String
showsPrec :: Int -> BaseSecret a -> ShowS
$cshowsPrec :: forall a. Int -> BaseSecret a -> ShowS
Show
newtype AnyTrafficSecret a = AnyTrafficSecret ByteString deriving Int -> AnyTrafficSecret a -> ShowS
forall a. Int -> AnyTrafficSecret a -> ShowS
forall a. [AnyTrafficSecret a] -> ShowS
forall a. AnyTrafficSecret a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AnyTrafficSecret a] -> ShowS
$cshowList :: forall a. [AnyTrafficSecret a] -> ShowS
show :: AnyTrafficSecret a -> String
$cshow :: forall a. AnyTrafficSecret a -> String
showsPrec :: Int -> AnyTrafficSecret a -> ShowS
$cshowsPrec :: forall a. Int -> AnyTrafficSecret a -> ShowS
Show

-- | A client traffic secret, typed with a parameter indicating a step in the
-- TLS key schedule.
newtype ClientTrafficSecret a = ClientTrafficSecret ByteString deriving Int -> ClientTrafficSecret a -> ShowS
forall a. Int -> ClientTrafficSecret a -> ShowS
forall a. [ClientTrafficSecret a] -> ShowS
forall a. ClientTrafficSecret a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ClientTrafficSecret a] -> ShowS
$cshowList :: forall a. [ClientTrafficSecret a] -> ShowS
show :: ClientTrafficSecret a -> String
$cshow :: forall a. ClientTrafficSecret a -> String
showsPrec :: Int -> ClientTrafficSecret a -> ShowS
$cshowsPrec :: forall a. Int -> ClientTrafficSecret a -> ShowS
Show

-- | A server traffic secret, typed with a parameter indicating a step in the
-- TLS key schedule.
newtype ServerTrafficSecret a = ServerTrafficSecret ByteString deriving Int -> ServerTrafficSecret a -> ShowS
forall a. Int -> ServerTrafficSecret a -> ShowS
forall a. [ServerTrafficSecret a] -> ShowS
forall a. ServerTrafficSecret a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ServerTrafficSecret a] -> ShowS
$cshowList :: forall a. [ServerTrafficSecret a] -> ShowS
show :: ServerTrafficSecret a -> String
$cshow :: forall a. ServerTrafficSecret a -> String
showsPrec :: Int -> ServerTrafficSecret a -> ShowS
$cshowsPrec :: forall a. Int -> ServerTrafficSecret a -> ShowS
Show

data SecretTriple a = SecretTriple
    { forall a. SecretTriple a -> BaseSecret a
triBase   :: BaseSecret a
    , forall a. SecretTriple a -> ClientTrafficSecret a
triClient :: ClientTrafficSecret a
    , forall a. SecretTriple a -> ServerTrafficSecret a
triServer :: ServerTrafficSecret a
    }

data SecretPair a = SecretPair
    { forall a. SecretPair a -> BaseSecret a
pairBase   :: BaseSecret a
    , forall a. SecretPair a -> ClientTrafficSecret a
pairClient :: ClientTrafficSecret a
    }

-- | Hold both client and server traffic secrets at the same step.
type TrafficSecrets a = (ClientTrafficSecret a, ServerTrafficSecret a)

-- Master secret for TLS 1.2 or earlier.
newtype MasterSecret = MasterSecret ByteString deriving Int -> MasterSecret -> ShowS
[MasterSecret] -> ShowS
MasterSecret -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MasterSecret] -> ShowS
$cshowList :: [MasterSecret] -> ShowS
show :: MasterSecret -> String
$cshow :: MasterSecret -> String
showsPrec :: Int -> MasterSecret -> ShowS
$cshowsPrec :: Int -> MasterSecret -> ShowS
Show