module Network.TLS.Handshake.Control (
    ClientState (..),
    ServerState (..),
    EarlySecretInfo (..),
    HandshakeSecretInfo (..),
    ApplicationSecretInfo (..),
    NegotiatedProtocol,
) where

import Crypto.Debug (DebugShow (..))
import Network.TLS.Cipher
import Network.TLS.Imports
import Network.TLS.Struct
import Network.TLS.Types

----------------------------------------------------------------

-- | ID of the application-level protocol negotiated between client and server.
-- See values listed in the <https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids IANA registry>.
type NegotiatedProtocol = ByteString

-- | Handshake information generated for traffic at 0-RTT level.
--
-- 'Show' renders the cipher and @\<secret\>@ for the key material; a trace
-- of what 'Network.TLS.QUIC.quicInstallKeys' is handed does not write the
-- traffic secrets to a log.  'Crypto.Debug.debugShow' renders them.
data EarlySecretInfo = EarlySecretInfo Cipher (ClientTrafficSecret EarlySecret)
    deriving (Int -> EarlySecretInfo -> ShowS
[EarlySecretInfo] -> ShowS
EarlySecretInfo -> String
(Int -> EarlySecretInfo -> ShowS)
-> (EarlySecretInfo -> String)
-> ([EarlySecretInfo] -> ShowS)
-> Show EarlySecretInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EarlySecretInfo -> ShowS
showsPrec :: Int -> EarlySecretInfo -> ShowS
$cshow :: EarlySecretInfo -> String
show :: EarlySecretInfo -> String
$cshowList :: [EarlySecretInfo] -> ShowS
showList :: [EarlySecretInfo] -> ShowS
Show)

instance DebugShow EarlySecretInfo where
    debugShow :: EarlySecretInfo -> String
debugShow (EarlySecretInfo Cipher
c ClientTrafficSecret EarlySecret
s) =
        String
"EarlySecretInfo " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Cipher -> String
forall a. Show a => a -> String
show Cipher
c String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ClientTrafficSecret EarlySecret -> String
forall a. DebugShow a => a -> String
debugShow ClientTrafficSecret EarlySecret
s

-- | Handshake information generated for traffic at handshake level.
--
-- The secrets are not shown; see 'EarlySecretInfo'.
data HandshakeSecretInfo
    = HandshakeSecretInfo Cipher (TrafficSecrets HandshakeSecret)
    deriving (Int -> HandshakeSecretInfo -> ShowS
[HandshakeSecretInfo] -> ShowS
HandshakeSecretInfo -> String
(Int -> HandshakeSecretInfo -> ShowS)
-> (HandshakeSecretInfo -> String)
-> ([HandshakeSecretInfo] -> ShowS)
-> Show HandshakeSecretInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HandshakeSecretInfo -> ShowS
showsPrec :: Int -> HandshakeSecretInfo -> ShowS
$cshow :: HandshakeSecretInfo -> String
show :: HandshakeSecretInfo -> String
$cshowList :: [HandshakeSecretInfo] -> ShowS
showList :: [HandshakeSecretInfo] -> ShowS
Show)

instance DebugShow HandshakeSecretInfo where
    debugShow :: HandshakeSecretInfo -> String
debugShow (HandshakeSecretInfo Cipher
c (ClientTrafficSecret HandshakeSecret,
 ServerTrafficSecret HandshakeSecret)
ts) =
        String
"HandshakeSecretInfo " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Cipher -> String
forall a. Show a => a -> String
show Cipher
c String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ (ClientTrafficSecret HandshakeSecret,
 ServerTrafficSecret HandshakeSecret)
-> String
forall a. TrafficSecrets a -> String
debugShowPair (ClientTrafficSecret HandshakeSecret,
 ServerTrafficSecret HandshakeSecret)
ts

-- | Handshake information generated for traffic at application level.
--
-- The secrets are not shown; see 'EarlySecretInfo'.
newtype ApplicationSecretInfo = ApplicationSecretInfo (TrafficSecrets ApplicationSecret)
    deriving (Int -> ApplicationSecretInfo -> ShowS
[ApplicationSecretInfo] -> ShowS
ApplicationSecretInfo -> String
(Int -> ApplicationSecretInfo -> ShowS)
-> (ApplicationSecretInfo -> String)
-> ([ApplicationSecretInfo] -> ShowS)
-> Show ApplicationSecretInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ApplicationSecretInfo -> ShowS
showsPrec :: Int -> ApplicationSecretInfo -> ShowS
$cshow :: ApplicationSecretInfo -> String
show :: ApplicationSecretInfo -> String
$cshowList :: [ApplicationSecretInfo] -> ShowS
showList :: [ApplicationSecretInfo] -> ShowS
Show)

instance DebugShow ApplicationSecretInfo where
    debugShow :: ApplicationSecretInfo -> String
debugShow (ApplicationSecretInfo (ClientTrafficSecret ApplicationSecret,
 ServerTrafficSecret ApplicationSecret)
ts) =
        String
"ApplicationSecretInfo " String -> ShowS
forall a. [a] -> [a] -> [a]
++ (ClientTrafficSecret ApplicationSecret,
 ServerTrafficSecret ApplicationSecret)
-> String
forall a. TrafficSecrets a -> String
debugShowPair (ClientTrafficSecret ApplicationSecret,
 ServerTrafficSecret ApplicationSecret)
ts

debugShowPair :: TrafficSecrets a -> String
debugShowPair :: forall a. TrafficSecrets a -> String
debugShowPair (ClientTrafficSecret a
c, ServerTrafficSecret a
s) = String
"(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ ClientTrafficSecret a -> String
forall a. DebugShow a => a -> String
debugShow ClientTrafficSecret a
c String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"," String -> ShowS
forall a. [a] -> [a] -> [a]
++ ServerTrafficSecret a -> String
forall a. DebugShow a => a -> String
debugShow ServerTrafficSecret a
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"

----------------------------------------------------------------

data ClientState
    = SendClientHello (Maybe EarlySecretInfo)
    | RecvServerHello HandshakeSecretInfo
    | SendClientFinished [ExtensionRaw] ApplicationSecretInfo

data ServerState
    = SendServerHello [ExtensionRaw] (Maybe EarlySecretInfo) HandshakeSecretInfo
    | SendServerFinished ApplicationSecretInfo