1{-# LANGUAGE EmptyDataDecls #-}
2-- |
3-- Module      : Network.TLS.Types
4-- License     : BSD-style
5-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
6-- Stability   : experimental
7-- Portability : unknown
8--
9module Network.TLS.Types
10    ( Version(..)
11    , SessionID
12    , SessionData(..)
13    , SessionFlag(..)
14    , CertReqContext
15    , TLS13TicketInfo(..)
16    , CipherID
17    , CompressionID
18    , Role(..)
19    , invertRole
20    , Direction(..)
21    , HostName
22    , Second
23    , Millisecond
24    , EarlySecret
25    , HandshakeSecret
26    , ApplicationSecret
27    , ResumptionSecret
28    , BaseSecret(..)
29    , AnyTrafficSecret(..)
30    , ClientTrafficSecret(..)
31    , ServerTrafficSecret(..)
32    , TrafficSecrets
33    , SecretTriple(..)
34    , SecretPair(..)
35    , MasterSecret(..)
36    ) where
37
38import Network.TLS.Imports
39import Network.TLS.Crypto.Types (Group)
40
41type HostName    = String
42type Second      = Word32
43type Millisecond = Word64
44
45-- | Versions known to TLS
46--
47-- SSL2 is just defined, but this version is and will not be supported.
48data Version = SSL2 | SSL3 | TLS10 | TLS11 | TLS12 | TLS13 deriving (Show, Eq, Ord, Bounded)
49
50-- | A session ID
51type SessionID = ByteString
52
53-- | Session data to resume
54data SessionData = SessionData
55    { sessionVersion     :: Version
56    , sessionCipher      :: CipherID
57    , sessionCompression :: CompressionID
58    , sessionClientSNI   :: Maybe HostName
59    , sessionSecret      :: ByteString
60    , sessionGroup       :: Maybe Group
61    , sessionTicketInfo  :: Maybe TLS13TicketInfo
62    , sessionALPN        :: Maybe ByteString
63    , sessionMaxEarlyDataSize :: Int
64    , sessionFlags       :: [SessionFlag]
65    } deriving (Show,Eq)
66
67-- | Some session flags
68data SessionFlag
69    = SessionEMS        -- ^ Session created with Extended Master Secret
70    deriving (Show,Eq,Enum)
71
72-- | Certificate request context for TLS 1.3.
73type CertReqContext = ByteString
74
75data TLS13TicketInfo = TLS13TicketInfo
76    { lifetime :: Second      -- NewSessionTicket.ticket_lifetime in seconds
77    , ageAdd   :: Second      -- NewSessionTicket.ticket_age_add
78    , txrxTime :: Millisecond -- serverSendTime or clientReceiveTime
79    , estimatedRTT :: Maybe Millisecond
80    } deriving (Show, Eq)
81
82-- | Cipher identification
83type CipherID = Word16
84
85-- | Compression identification
86type CompressionID = Word8
87
88-- | Role
89data Role = ClientRole | ServerRole
90    deriving (Show,Eq)
91
92-- | Direction
93data Direction = Tx | Rx
94    deriving (Show,Eq)
95
96invertRole :: Role -> Role
97invertRole ClientRole = ServerRole
98invertRole ServerRole = ClientRole
99
100-- | Phantom type indicating early traffic secret.
101data EarlySecret
102
103-- | Phantom type indicating handshake traffic secrets.
104data HandshakeSecret
105
106-- | Phantom type indicating application traffic secrets.
107data ApplicationSecret
108
109data ResumptionSecret
110
111newtype BaseSecret a = BaseSecret ByteString deriving Show
112newtype AnyTrafficSecret a = AnyTrafficSecret ByteString deriving Show
113
114-- | A client traffic secret, typed with a parameter indicating a step in the
115-- TLS key schedule.
116newtype ClientTrafficSecret a = ClientTrafficSecret ByteString deriving Show
117
118-- | A server traffic secret, typed with a parameter indicating a step in the
119-- TLS key schedule.
120newtype ServerTrafficSecret a = ServerTrafficSecret ByteString deriving Show
121
122data SecretTriple a = SecretTriple
123    { triBase   :: BaseSecret a
124    , triClient :: ClientTrafficSecret a
125    , triServer :: ServerTrafficSecret a
126    }
127
128data SecretPair a = SecretPair
129    { pairBase   :: BaseSecret a
130    , pairClient :: ClientTrafficSecret a
131    }
132
133-- | Hold both client and server traffic secrets at the same step.
134type TrafficSecrets a = (ClientTrafficSecret a, ServerTrafficSecret a)
135
136-- Master secret for TLS 1.2 or earlier.
137newtype MasterSecret = MasterSecret ByteString deriving Show
138