-- | The secret types of the TLS key schedule.
--
-- None of these prints its key material: 'Show' renders @\<secret\>@, since
-- these values reach a QUIC implementation through
-- "Network.TLS.QUIC" and are the kind of thing a handshake trace prints
-- without meaning to.  'Crypto.Debug.debugShow' returns the hexadecimal that
-- 'Show' used to, for a debugging session that wants it.  @SSLKEYLOGFILE@
-- does not go through either: it uses 'Network.TLS.Handshake.Key.LogLabel'.
module Network.TLS.Types.Secret where

import Crypto.Debug (DebugShow (..))
import Data.ByteArray (convert)
import Network.TLS.Imports
import Network.TLS.Types.Cipher

-- | 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 Secret

instance Show (BaseSecret a) where
    show :: BaseSecret a -> String
show BaseSecret a
_ = String
"<secret>"

instance DebugShow (BaseSecret a) where
    debugShow :: BaseSecret a -> String
debugShow (BaseSecret ScrubbedBytes
bs) = ByteString -> String
showBytesHex (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert ScrubbedBytes
bs

newtype AnyTrafficSecret a = AnyTrafficSecret Secret

instance Show (AnyTrafficSecret a) where
    show :: AnyTrafficSecret a -> String
show AnyTrafficSecret a
_ = String
"<secret>"

instance DebugShow (AnyTrafficSecret a) where
    debugShow :: AnyTrafficSecret a -> String
debugShow (AnyTrafficSecret ScrubbedBytes
bs) = ByteString -> String
showBytesHex (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert ScrubbedBytes
bs

-- | A client traffic secret, typed with a parameter indicating a step in the
-- TLS key schedule.
newtype ClientTrafficSecret a = ClientTrafficSecret Secret

instance Show (ClientTrafficSecret a) where
    show :: ClientTrafficSecret a -> String
show ClientTrafficSecret a
_ = String
"<secret>"

instance DebugShow (ClientTrafficSecret a) where
    debugShow :: ClientTrafficSecret a -> String
debugShow (ClientTrafficSecret ScrubbedBytes
bs) = ByteString -> String
showBytesHex (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert ScrubbedBytes
bs

-- | A server traffic secret, typed with a parameter indicating a step in the
-- TLS key schedule.
newtype ServerTrafficSecret a = ServerTrafficSecret Secret

instance Show (ServerTrafficSecret a) where
    show :: ServerTrafficSecret a -> String
show ServerTrafficSecret a
_ = String
"<secret>"

instance DebugShow (ServerTrafficSecret a) where
    debugShow :: ServerTrafficSecret a -> String
debugShow (ServerTrafficSecret ScrubbedBytes
bs) = ByteString -> String
showBytesHex (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert ScrubbedBytes
bs

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
    }
    deriving (Int -> SecretTriple a -> ShowS
[SecretTriple a] -> ShowS
SecretTriple a -> String
(Int -> SecretTriple a -> ShowS)
-> (SecretTriple a -> String)
-> ([SecretTriple a] -> ShowS)
-> Show (SecretTriple a)
forall a. Int -> SecretTriple a -> ShowS
forall a. [SecretTriple a] -> ShowS
forall a. SecretTriple a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> SecretTriple a -> ShowS
showsPrec :: Int -> SecretTriple a -> ShowS
$cshow :: forall a. SecretTriple a -> String
show :: SecretTriple a -> String
$cshowList :: forall a. [SecretTriple a] -> ShowS
showList :: [SecretTriple a] -> ShowS
Show)

data SecretPair a = SecretPair
    { forall a. SecretPair a -> BaseSecret a
pairBase :: BaseSecret a
    , forall a. SecretPair a -> ClientTrafficSecret a
pairClient :: ClientTrafficSecret a
    }
    deriving (Int -> SecretPair a -> ShowS
[SecretPair a] -> ShowS
SecretPair a -> String
(Int -> SecretPair a -> ShowS)
-> (SecretPair a -> String)
-> ([SecretPair a] -> ShowS)
-> Show (SecretPair a)
forall a. Int -> SecretPair a -> ShowS
forall a. [SecretPair a] -> ShowS
forall a. SecretPair a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> SecretPair a -> ShowS
showsPrec :: Int -> SecretPair a -> ShowS
$cshow :: forall a. SecretPair a -> String
show :: SecretPair a -> String
$cshowList :: forall a. [SecretPair a] -> ShowS
showList :: [SecretPair a] -> ShowS
Show)

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

-- | The exporter main secret, which RFC 8446 section 7.5 derives exported
-- keying material from, and which RFC 9266's @tls-exporter@ channel binding
-- is one use of.
newtype ExporterSecret = ExporterSecret Secret

instance Show ExporterSecret where
    show :: ExporterSecret -> String
show ExporterSecret
_ = String
"<secret>"

instance DebugShow ExporterSecret where
    debugShow :: ExporterSecret -> String
debugShow (ExporterSecret ScrubbedBytes
bs) = ByteString -> String
showBytesHex (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert ScrubbedBytes
bs

-- Main secret for TLS 1.2 or earlier.
newtype MainSecret = MainSecret Secret

instance Show MainSecret where
    show :: MainSecret -> String
show MainSecret
_ = String
"<secret>"

instance DebugShow MainSecret where
    debugShow :: MainSecret -> String
debugShow (MainSecret ScrubbedBytes
bs) = ByteString -> String
showBytesHex (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert ScrubbedBytes
bs