{-# LANGUAGE OverloadedStrings #-}

module Network.TLS.Handshake.Signature (
    createCertificateVerify,
    checkCertificateVerify,
    digitallySignDHParams,
    digitallySignECDHParams,
    digitallySignDHParamsVerify,
    digitallySignECDHParamsVerify,
    checkSupportedHashSignature,
    certificateCompatible,
    signatureCompatible,
    signatureCompatible13,
    hashSigToCertType,
    signatureParams,
    decryptError,
) where

import Control.Monad.State.Strict

import Network.TLS.Context.Internal
import Network.TLS.Crypto
import Network.TLS.Handshake.Key
import Network.TLS.Handshake.State
import Network.TLS.Imports
import Network.TLS.Packet (
    encodeSignedDHParams,
    encodeSignedECDHParams,
 )
import Network.TLS.Parameters
import Network.TLS.State
import Network.TLS.Struct
import Network.TLS.Types (invertRole)
import Network.TLS.X509

decryptError :: MonadIO m => String -> m a
decryptError :: forall (m :: * -> *) a. MonadIO m => [Char] -> m a
decryptError [Char]
msg = TLSError -> m a
forall (m :: * -> *) a. MonadIO m => TLSError -> m a
throwCore (TLSError -> m a) -> TLSError -> m a
forall a b. (a -> b) -> a -> b
$ [Char] -> AlertDescription -> TLSError
Error_Protocol [Char]
msg AlertDescription
DecryptError

-- | Check that the key is compatible with a list of 'CertificateType' values.
-- Ed25519 and Ed448 have no assigned code point and are checked with extension
-- "signature_algorithms" only.
certificateCompatible :: PubKey -> [CertificateType] -> Bool
certificateCompatible :: PubKey -> [CertificateType] -> Bool
certificateCompatible (PubKeyRSA PublicKey
_) [CertificateType]
cTypes = CertificateType
CertificateType_RSA_Sign CertificateType -> [CertificateType] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CertificateType]
cTypes
certificateCompatible (PubKeyDSA PublicKey
_) [CertificateType]
cTypes = CertificateType
CertificateType_DSA_Sign CertificateType -> [CertificateType] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CertificateType]
cTypes
certificateCompatible (PubKeyEC PubKeyEC
_) [CertificateType]
cTypes = CertificateType
CertificateType_ECDSA_Sign CertificateType -> [CertificateType] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CertificateType]
cTypes
certificateCompatible (PubKeyEd25519 PublicKey
_) [CertificateType]
_ = Bool
True
certificateCompatible (PubKeyEd448 PublicKey
_) [CertificateType]
_ = Bool
True
-- ML-DSA has no CertificateType of its own, as EdDSA does not either; what
-- a client may send is decided by signature_algorithms.
certificateCompatible (PubKeyMLDSA44 VerificationKey MLDSA44
_) [CertificateType]
_ = Bool
True
certificateCompatible (PubKeyMLDSA65 VerificationKey MLDSA65
_) [CertificateType]
_ = Bool
True
certificateCompatible (PubKeyMLDSA87 VerificationKey MLDSA87
_) [CertificateType]
_ = Bool
True
certificateCompatible PubKey
_ [CertificateType]
_ = Bool
False

signatureCompatible :: PubKey -> SignatureScheme -> Bool
-- ML-DSA first, and with a refusal for every other kind of key.  The RSA
-- equations below ignore the first byte, and ML-DSA's second byte is one
-- RSASSA-PSS also uses, so an ML-DSA scheme reaching them would be answered
-- for as though it were RSASSA-PSS.
signatureCompatible :: PubKey -> SignatureScheme -> Bool
signatureCompatible (PubKeyMLDSA44 VerificationKey MLDSA44
_) SignatureScheme
MLDSA44 = Bool
True
signatureCompatible (PubKeyMLDSA65 VerificationKey MLDSA65
_) SignatureScheme
MLDSA65 = Bool
True
signatureCompatible (PubKeyMLDSA87 VerificationKey MLDSA87
_) SignatureScheme
MLDSA87 = Bool
True
signatureCompatible PubKey
_ SignatureScheme
MLDSA44 = Bool
False
signatureCompatible PubKey
_ SignatureScheme
MLDSA65 = Bool
False
signatureCompatible PubKey
_ SignatureScheme
MLDSA87 = Bool
False
signatureCompatible (PubKeyRSA PublicKey
pk) SignatureScheme
RSA_PKCS1_SHA1 = PublicKey -> Hash -> Bool
kxCanUseRSApkcs1 PublicKey
pk Hash
SHA1
signatureCompatible (PubKeyRSA PublicKey
pk) SignatureScheme
RSA_PKCS1_SHA256 = PublicKey -> Hash -> Bool
kxCanUseRSApkcs1 PublicKey
pk Hash
SHA256
signatureCompatible (PubKeyRSA PublicKey
pk) SignatureScheme
RSA_PKCS1_SHA384 = PublicKey -> Hash -> Bool
kxCanUseRSApkcs1 PublicKey
pk Hash
SHA384
signatureCompatible (PubKeyRSA PublicKey
pk) SignatureScheme
RSA_PKCS1_SHA512 = PublicKey -> Hash -> Bool
kxCanUseRSApkcs1 PublicKey
pk Hash
SHA512
-- Only what signatureParams implements: a scheme it does not, such as
-- ecdsa_sha224, must not be chosen and then fail to sign.
signatureCompatible (PubKeyRSA PublicKey
pk) SignatureScheme
RSA_PSS_RSAE_SHA256 = PublicKey -> Hash -> Bool
kxCanUseRSApss PublicKey
pk Hash
SHA256
signatureCompatible (PubKeyRSA PublicKey
pk) SignatureScheme
RSA_PSS_RSAE_SHA384 = PublicKey -> Hash -> Bool
kxCanUseRSApss PublicKey
pk Hash
SHA384
signatureCompatible (PubKeyRSA PublicKey
pk) SignatureScheme
RSA_PSS_RSAE_SHA512 = PublicKey -> Hash -> Bool
kxCanUseRSApss PublicKey
pk Hash
SHA512
signatureCompatible (PubKeyDSA PublicKey
_) SignatureScheme
DSA_SHA1 = Bool
True
signatureCompatible (PubKeyEC PubKeyEC
_) SignatureScheme
sc =
    SignatureScheme
sc
        SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ SignatureScheme
ECDSA_SHA1
               , SignatureScheme
ECDSA_SECP256R1_SHA256
               , SignatureScheme
ECDSA_SECP384R1_SHA384
               , SignatureScheme
ECDSA_SECP521R1_SHA512
               ]
signatureCompatible (PubKeyEd25519 PublicKey
_) SignatureScheme
ED25519 = Bool
True
signatureCompatible (PubKeyEd448 PublicKey
_) SignatureScheme
ED448 = Bool
True
signatureCompatible PubKey
_ SignatureScheme
_ = Bool
False

-- Whether the signature algorithm is for the type of the key, whatever
-- its other parameters.
--
-- This takes the whole scheme and not its second byte alone, because
-- ML-DSA's second byte is one RSASSA-PSS also uses: only the pair says
-- which of the two a scheme is.  The ML-DSA equations come first for the
-- same reason -- an ML-DSA scheme fits no other kind of key, and one
-- ML-DSA size does not fit another's key.
keyTypeFits :: PubKey -> SignatureScheme -> Bool
keyTypeFits :: PubKey -> SignatureScheme -> Bool
keyTypeFits (PubKeyMLDSA44 VerificationKey MLDSA44
_) SignatureScheme
hs = SignatureScheme
hs SignatureScheme -> SignatureScheme -> Bool
forall a. Eq a => a -> a -> Bool
== SignatureScheme
MLDSA44
keyTypeFits (PubKeyMLDSA65 VerificationKey MLDSA65
_) SignatureScheme
hs = SignatureScheme
hs SignatureScheme -> SignatureScheme -> Bool
forall a. Eq a => a -> a -> Bool
== SignatureScheme
MLDSA65
keyTypeFits (PubKeyMLDSA87 VerificationKey MLDSA87
_) SignatureScheme
hs = SignatureScheme
hs SignatureScheme -> SignatureScheme -> Bool
forall a. Eq a => a -> a -> Bool
== SignatureScheme
MLDSA87
keyTypeFits PubKey
_ SignatureScheme
MLDSA44 = Bool
False
keyTypeFits PubKey
_ SignatureScheme
MLDSA65 = Bool
False
keyTypeFits PubKey
_ SignatureScheme
MLDSA87 = Bool
False
keyTypeFits (PubKeyRSA PublicKey
_) SignatureScheme
sc = SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ([SignatureScheme]
rsaPkcs1Schemes [SignatureScheme] -> [SignatureScheme] -> [SignatureScheme]
forall a. [a] -> [a] -> [a]
++ [SignatureScheme]
rsaPssSchemes)
keyTypeFits (PubKeyDSA PublicKey
_) SignatureScheme
sc = SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
dsaSchemes
keyTypeFits (PubKeyEC PubKeyEC
_) SignatureScheme
sc = SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
ecdsaSchemes
keyTypeFits (PubKeyEd25519 PublicKey
_) SignatureScheme
sc = SignatureScheme
sc SignatureScheme -> SignatureScheme -> Bool
forall a. Eq a => a -> a -> Bool
== SignatureScheme
ED25519
keyTypeFits (PubKeyEd448 PublicKey
_) SignatureScheme
sc = SignatureScheme
sc SignatureScheme -> SignatureScheme -> Bool
forall a. Eq a => a -> a -> Bool
== SignatureScheme
ED448
keyTypeFits PubKey
_ SignatureScheme
_ = Bool
False

checkKeyTypeFits :: PubKey -> SignatureScheme -> IO ()
checkKeyTypeFits :: PubKey -> SignatureScheme -> IO ()
checkKeyTypeFits PubKey
pubKey SignatureScheme
hs =
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PubKey
pubKey PubKey -> SignatureScheme -> Bool
`keyTypeFits` SignatureScheme
hs) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
        TLSError -> IO ()
forall (m :: * -> *) a. MonadIO m => TLSError -> m a
throwCore (TLSError -> IO ()) -> TLSError -> IO ()
forall a b. (a -> b) -> a -> b
$
            [Char] -> AlertDescription -> TLSError
Error_Protocol
                ([Char]
"signature algorithm " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
hs [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is for another type of key")
                AlertDescription
IllegalParameter

-- Same as 'signatureCompatible' but for TLS13: for ECDSA this also checks the
-- relation between hash in the SignatureScheme and elliptic curve
signatureCompatible13 :: PubKey -> SignatureScheme -> Bool
signatureCompatible13 :: PubKey -> SignatureScheme -> Bool
signatureCompatible13 (PubKeyEC PubKeyEC
ecPub) SignatureScheme
sc
    | SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
ecdsaSchemes =
        Bool -> (Group -> Bool) -> Maybe Group -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (\Group
g -> PubKeyEC -> Maybe Group
findEllipticCurveGroup PubKeyEC
ecPub Maybe Group -> Maybe Group -> Bool
forall a. Eq a => a -> a -> Bool
== Group -> Maybe Group
forall a. a -> Maybe a
Just Group
g) (SignatureScheme -> Maybe Group
schemeCurve SignatureScheme
sc)
  where
    -- TLS 1.3 names the curve in the scheme; TLS 1.2 did not, and the three
    -- schemes that do not name one cannot be used there.
    schemeCurve :: SignatureScheme -> Maybe Group
schemeCurve SignatureScheme
ECDSA_SECP256R1_SHA256 = Group -> Maybe Group
forall a. a -> Maybe a
Just Group
P256
    schemeCurve SignatureScheme
ECDSA_SECP384R1_SHA384 = Group -> Maybe Group
forall a. a -> Maybe a
Just Group
P384
    schemeCurve SignatureScheme
ECDSA_SECP521R1_SHA512 = Group -> Maybe Group
forall a. a -> Maybe a
Just Group
P521
    schemeCurve SignatureScheme
_ = Maybe Group
forall a. Maybe a
Nothing
signatureCompatible13 PubKey
pub SignatureScheme
hs = PubKey -> SignatureScheme -> Bool
signatureCompatible PubKey
pub SignatureScheme
hs

-- | Translate a 'SignatureScheme' to an acceptable 'CertificateType'.
-- Perhaps this needs to take supported groups into account, so that, for
-- example, if we don't support any shared ECDSA groups with the server, we
-- return 'Nothing' rather than 'CertificateType_ECDSA_Sign'.
--
-- Therefore, this interface is preliminary.  It gets us moving in the right
-- direction.  The interplay between all the various TLS extensions and
-- certificate selection is rather complex.
--
-- The goal is to ensure that the client certificate request callback only sees
-- 'CertificateType' values that are supported by the library and also
-- compatible with the server signature algorithms extension.
--
-- Since we don't yet support ECDSA private keys, the caller will use
-- 'lastSupportedCertificateType' to filter those out for now, leaving just
-- @RSA@ as the only supported client certificate algorithm for TLS 1.3.
--
-- FIXME: Add RSA_PSS_PSS signatures when supported.
hashSigToCertType :: SignatureScheme -> Maybe CertificateType
--
hashSigToCertType :: SignatureScheme -> Maybe CertificateType
hashSigToCertType SignatureScheme
sc
    | SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
rsaPkcs1Schemes = CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_RSA_Sign
    | SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
dsaSchemes = CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_DSA_Sign
    | SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
ecdsaSchemes = CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_ECDSA_Sign
--
hashSigToCertType SignatureScheme
RSA_PSS_RSAE_SHA256 =
    CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_RSA_Sign
hashSigToCertType SignatureScheme
RSA_PSS_RSAE_SHA384 =
    CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_RSA_Sign
hashSigToCertType SignatureScheme
RSA_PSS_RSAE_SHA512 =
    CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_RSA_Sign
hashSigToCertType SignatureScheme
ED25519 =
    CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_Ed25519_Sign
hashSigToCertType SignatureScheme
ED448 =
    CertificateType -> Maybe CertificateType
forall a. a -> Maybe a
Just CertificateType
CertificateType_Ed448_Sign
--
hashSigToCertType SignatureScheme
_ = Maybe CertificateType
forall a. Maybe a
Nothing

checkCertificateVerify
    :: Context
    -> Version
    -> PubKey
    -> ByteString
    -> DigitallySigned
    -> IO Bool
-- An algorithm not offered in CertificateRequest (RFC 5246 Section
-- 7.4.8), or one for another type of key, is a field that is incorrect,
-- an illegal_parameter.  One for the right type of key that still does
-- not fit it, an RSASSA-PSS one for an rsaEncryption key say, is a
-- signature that does not verify, a decrypt_error, which False leads to.
checkCertificateVerify :: Context
-> Version -> PubKey -> ByteString -> DigitallySigned -> IO Bool
checkCertificateVerify Context
ctx Version
usedVersion PubKey
pubKey ByteString
msgs digSig :: DigitallySigned
digSig@(DigitallySigned SignatureScheme
hashSigAlg ByteString
_) = do
    -- ML-DSA is defined for TLS 1.3 only, so naming one here is a field
    -- that is incorrect whatever key the peer holds.
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Version
usedVersion Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
< Version
TLS13 Bool -> Bool -> Bool
&& SignatureScheme -> Bool
isMLDSA SignatureScheme
hashSigAlg) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
        TLSError -> IO ()
forall (m :: * -> *) a. MonadIO m => TLSError -> m a
throwCore (TLSError -> IO ()) -> TLSError -> IO ()
forall a b. (a -> b) -> a -> b
$
            [Char] -> AlertDescription -> TLSError
Error_Protocol
                ([Char]
"signature algorithm " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
hashSigAlg [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is not for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Version -> [Char]
forall a. Show a => a -> [Char]
show Version
usedVersion)
                AlertDescription
IllegalParameter
    Context -> SignatureScheme -> IO ()
checkSupportedHashSignature Context
ctx SignatureScheme
hashSigAlg
    PubKey -> SignatureScheme -> IO ()
checkKeyTypeFits PubKey
pubKey SignatureScheme
hashSigAlg
    if PubKey
pubKey PubKey -> SignatureScheme -> Bool
`signatureCompatible` SignatureScheme
hashSigAlg
        then IO Bool
doVerify
        else Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
  where
    doVerify :: IO Bool
doVerify =
        Context
-> Version
-> PubKey
-> SignatureScheme
-> ByteString
-> IO CertVerifyData
prepareCertificateVerifySignatureData Context
ctx Version
usedVersion PubKey
pubKey SignatureScheme
hashSigAlg ByteString
msgs
            IO CertVerifyData -> (CertVerifyData -> IO Bool) -> IO Bool
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Context -> DigitallySigned -> CertVerifyData -> IO Bool
signatureVerifyWithCertVerifyData Context
ctx DigitallySigned
digSig

createCertificateVerify
    :: Context
    -> Version
    -> PubKey
    -> SignatureScheme -- TLS12 only
    -> ByteString
    -> IO DigitallySigned
createCertificateVerify :: Context
-> Version
-> PubKey
-> SignatureScheme
-> ByteString
-> IO DigitallySigned
createCertificateVerify Context
ctx Version
usedVersion PubKey
pubKey SignatureScheme
hashSigAlg ByteString
msgs =
    Context
-> Version
-> PubKey
-> SignatureScheme
-> ByteString
-> IO CertVerifyData
prepareCertificateVerifySignatureData Context
ctx Version
usedVersion PubKey
pubKey SignatureScheme
hashSigAlg ByteString
msgs
        IO CertVerifyData
-> (CertVerifyData -> IO DigitallySigned) -> IO DigitallySigned
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Context -> SignatureScheme -> CertVerifyData -> IO DigitallySigned
signatureCreateWithCertVerifyData Context
ctx SignatureScheme
hashSigAlg

type CertVerifyData = (SignatureParams, ByteString)

-- in the case of TLS < 1.2, RSA signing, then the data need to be hashed first, as
-- the SHA1_MD5 algorithm expect an already digested data
buildVerifyData :: SignatureParams -> ByteString -> CertVerifyData
buildVerifyData :: SignatureParams -> ByteString -> CertVerifyData
buildVerifyData (RSAParams Hash
SHA1_MD5 RSAEncoding
enc) ByteString
bs = (Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA1_MD5 RSAEncoding
enc, HashCtx -> ByteString
hashFinal (HashCtx -> ByteString) -> HashCtx -> ByteString
forall a b. (a -> b) -> a -> b
$ HashCtx -> ByteString -> HashCtx
hashUpdate (Hash -> HashCtx
hashInit Hash
SHA1_MD5) ByteString
bs)
buildVerifyData SignatureParams
sigParam ByteString
bs = (SignatureParams
sigParam, ByteString
bs)

prepareCertificateVerifySignatureData
    :: Context
    -> Version
    -> PubKey
    -> SignatureScheme -- TLS12 only
    -> ByteString
    -> IO CertVerifyData
prepareCertificateVerifySignatureData :: Context
-> Version
-> PubKey
-> SignatureScheme
-> ByteString
-> IO CertVerifyData
prepareCertificateVerifySignatureData Context
_ctx Version
_usedVersion PubKey
pubKey SignatureScheme
hashSigAlg ByteString
msgs =
    CertVerifyData -> IO CertVerifyData
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (PubKey -> SignatureScheme -> SignatureParams
signatureParams PubKey
pubKey SignatureScheme
hashSigAlg, ByteString
msgs)

signatureParams :: PubKey -> SignatureScheme -> SignatureParams
-- Which of these is used is decided by the key, and signatureCompatible has
-- already refused a scheme that does not fit the key, so an ML-DSA scheme
-- cannot arrive at a clause for another kind of key.
signatureParams :: PubKey -> SignatureScheme -> SignatureParams
signatureParams (PubKeyRSA PublicKey
_) SignatureScheme
hashSigAlg =
    case SignatureScheme
hashSigAlg of
        SignatureScheme
RSA_PKCS1_SHA512 -> Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA512 RSAEncoding
RSApkcs1
        SignatureScheme
RSA_PKCS1_SHA384 -> Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA384 RSAEncoding
RSApkcs1
        SignatureScheme
RSA_PKCS1_SHA256 -> Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA256 RSAEncoding
RSApkcs1
        SignatureScheme
RSA_PKCS1_SHA1 -> Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA1 RSAEncoding
RSApkcs1
        SignatureScheme
RSA_PSS_RSAE_SHA512 -> Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA512 RSAEncoding
RSApss
        SignatureScheme
RSA_PSS_RSAE_SHA384 -> Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA384 RSAEncoding
RSApss
        SignatureScheme
RSA_PSS_RSAE_SHA256 -> Hash -> RSAEncoding -> SignatureParams
RSAParams Hash
SHA256 RSAEncoding
RSApss
        SignatureScheme
sc
            | SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
rsaPkcs1Schemes Bool -> Bool -> Bool
|| SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
rsaPssSchemes ->
                [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"unimplemented RSA signature scheme: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
sc)
            | Bool
otherwise ->
                [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"signature scheme is incompatible with RSA: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
sc)
signatureParams (PubKeyDSA PublicKey
_) SignatureScheme
hashSigAlg =
    case SignatureScheme
hashSigAlg of
        SignatureScheme
DSA_SHA1 -> SignatureParams
DSAParams
        SignatureScheme
sc
            | SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
dsaSchemes -> [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error [Char]
"invalid DSA hash choice, only SHA1 allowed"
            | Bool
otherwise ->
                [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"signature scheme is incompatible with DSA: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
sc)
signatureParams (PubKeyEC PubKeyEC
_) SignatureScheme
hashSigAlg =
    case SignatureScheme
hashSigAlg of
        SignatureScheme
ECDSA_SECP521R1_SHA512 -> Hash -> SignatureParams
ECDSAParams Hash
SHA512
        SignatureScheme
ECDSA_SECP384R1_SHA384 -> Hash -> SignatureParams
ECDSAParams Hash
SHA384
        SignatureScheme
ECDSA_SECP256R1_SHA256 -> Hash -> SignatureParams
ECDSAParams Hash
SHA256
        SignatureScheme
ECDSA_SHA1 -> Hash -> SignatureParams
ECDSAParams Hash
SHA1
        SignatureScheme
sc
            | SignatureScheme
sc SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SignatureScheme]
ecdsaSchemes ->
                [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"unimplemented ECDSA signature scheme: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
sc)
            | Bool
otherwise ->
                [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"signature scheme is incompatible with ECDSA: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
sc)
signatureParams (PubKeyEd25519 PublicKey
_) SignatureScheme
hashSigAlg =
    case SignatureScheme
hashSigAlg of
        SignatureScheme
ED25519 -> SignatureParams
Ed25519Params
        SignatureScheme
sc -> [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"signature scheme is incompatible with Ed25519: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
sc)
signatureParams (PubKeyEd448 PublicKey
_) SignatureScheme
hashSigAlg =
    case SignatureScheme
hashSigAlg of
        SignatureScheme
ED448 -> SignatureParams
Ed448Params
        SignatureScheme
sc -> [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"signature scheme is incompatible with Ed448: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
sc)
signatureParams (PubKeyMLDSA44 VerificationKey MLDSA44
_) SignatureScheme
MLDSA44 = SignatureParams
MLDSA44Params
signatureParams (PubKeyMLDSA65 VerificationKey MLDSA65
_) SignatureScheme
MLDSA65 = SignatureParams
MLDSA65Params
signatureParams (PubKeyMLDSA87 VerificationKey MLDSA87
_) SignatureScheme
MLDSA87 = SignatureParams
MLDSA87Params
signatureParams PubKey
pk SignatureScheme
_ = [Char] -> SignatureParams
forall a. HasCallStack => [Char] -> a
error ([Char]
"signatureParams: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ PubKey -> [Char]
pubkeyType PubKey
pk [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" is not supported")

signatureCreateWithCertVerifyData
    :: Context
    -> SignatureScheme
    -> CertVerifyData
    -> IO DigitallySigned
signatureCreateWithCertVerifyData :: Context -> SignatureScheme -> CertVerifyData -> IO DigitallySigned
signatureCreateWithCertVerifyData Context
ctx SignatureScheme
malg (SignatureParams
sigParam, ByteString
toSign) = do
    role <- Context -> TLSSt Role -> IO Role
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt Role
getRole
    usingHState ctx $ setSignatureScheme role malg
    DigitallySigned malg <$> signPrivate ctx role sigParam toSign

signatureVerify :: Context -> DigitallySigned -> PubKey -> ByteString -> IO Bool
-- Checked as checkCertificateVerify does: the peer chose the algorithm, so
-- one that does not fit the key is its error, not ours.
signatureVerify :: Context -> DigitallySigned -> PubKey -> ByteString -> IO Bool
signatureVerify Context
ctx digSig :: DigitallySigned
digSig@(DigitallySigned SignatureScheme
hashSigAlg ByteString
_) PubKey
pubKey ByteString
toVerifyData = do
    usedVersion <- Context -> TLSSt Version -> IO Version
forall a. Context -> TLSSt a -> IO a
usingState_ Context
ctx TLSSt Version
getVersion
    unless (usedVersion == TLS12) $
        throwCore $
            Error_Protocol
                ("no signature algorithm in a " ++ show usedVersion ++ " digitally-signed")
                InternalError
    checkSupportedHashSignature ctx hashSigAlg
    checkKeyTypeFits pubKey hashSigAlg
    if pubKey `signatureCompatible` hashSigAlg
        then
            signatureVerifyWithCertVerifyData
                ctx
                digSig
                (signatureParams pubKey hashSigAlg, toVerifyData)
        else return False

signatureVerifyWithCertVerifyData
    :: Context
    -> DigitallySigned
    -> CertVerifyData
    -> IO Bool
signatureVerifyWithCertVerifyData :: Context -> DigitallySigned -> CertVerifyData -> IO Bool
signatureVerifyWithCertVerifyData Context
ctx (DigitallySigned SignatureScheme
hs ByteString
bs) (SignatureParams
sigParam, ByteString
toVerify) = do
    Context -> SignatureScheme -> IO ()
checkSupportedHashSignature Context
ctx SignatureScheme
hs
    ok <- Context -> SignatureParams -> ByteString -> ByteString -> IO Bool
verifyPublic Context
ctx SignatureParams
sigParam ByteString
toVerify ByteString
bs
    when ok $ do
        role <- usingState_ ctx getRole
        usingHState ctx $ setSignatureScheme (invertRole role) hs
    return ok

digitallySignParams
    :: Context
    -> ByteString
    -> PubKey
    -> SignatureScheme
    -> IO DigitallySigned
digitallySignParams :: Context
-> ByteString -> PubKey -> SignatureScheme -> IO DigitallySigned
digitallySignParams Context
ctx ByteString
signatureData PubKey
pubKey SignatureScheme
hashSigAlg =
    let sigParam :: SignatureParams
sigParam = PubKey -> SignatureScheme -> SignatureParams
signatureParams PubKey
pubKey SignatureScheme
hashSigAlg
     in Context -> SignatureScheme -> CertVerifyData -> IO DigitallySigned
signatureCreateWithCertVerifyData
            Context
ctx
            SignatureScheme
hashSigAlg
            (SignatureParams -> ByteString -> CertVerifyData
buildVerifyData SignatureParams
sigParam ByteString
signatureData)

digitallySignDHParams
    :: Context
    -> ServerDHParams
    -> PubKey
    -> SignatureScheme -- TLS12 only
    -> IO DigitallySigned
digitallySignDHParams :: Context
-> ServerDHParams
-> PubKey
-> SignatureScheme
-> IO DigitallySigned
digitallySignDHParams Context
ctx ServerDHParams
serverParams PubKey
pubKey SignatureScheme
mhash = do
    dhParamsData <-
        Context
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall b. Context -> (ClientRandom -> ServerRandom -> b) -> IO b
withClientAndServerRandom Context
ctx ((ClientRandom -> ServerRandom -> ByteString) -> IO ByteString)
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ ServerDHParams -> ClientRandom -> ServerRandom -> ByteString
encodeSignedDHParams ServerDHParams
serverParams
    digitallySignParams ctx dhParamsData pubKey mhash

digitallySignECDHParams
    :: Context
    -> ServerECDHParams
    -> PubKey
    -> SignatureScheme -- TLS12 only
    -> IO DigitallySigned
digitallySignECDHParams :: Context
-> ServerECDHParams
-> PubKey
-> SignatureScheme
-> IO DigitallySigned
digitallySignECDHParams Context
ctx ServerECDHParams
serverParams PubKey
pubKey SignatureScheme
mhash = do
    ecdhParamsData <-
        Context
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall b. Context -> (ClientRandom -> ServerRandom -> b) -> IO b
withClientAndServerRandom Context
ctx ((ClientRandom -> ServerRandom -> ByteString) -> IO ByteString)
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ ServerECDHParams -> ClientRandom -> ServerRandom -> ByteString
encodeSignedECDHParams ServerECDHParams
serverParams
    digitallySignParams ctx ecdhParamsData pubKey mhash

digitallySignDHParamsVerify
    :: Context
    -> ServerDHParams
    -> PubKey
    -> DigitallySigned
    -> IO Bool
digitallySignDHParamsVerify :: Context -> ServerDHParams -> PubKey -> DigitallySigned -> IO Bool
digitallySignDHParamsVerify Context
ctx ServerDHParams
dhparams PubKey
pubKey DigitallySigned
signature = do
    expectedData <- Context
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall b. Context -> (ClientRandom -> ServerRandom -> b) -> IO b
withClientAndServerRandom Context
ctx ((ClientRandom -> ServerRandom -> ByteString) -> IO ByteString)
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ ServerDHParams -> ClientRandom -> ServerRandom -> ByteString
encodeSignedDHParams ServerDHParams
dhparams
    signatureVerify ctx signature pubKey expectedData

digitallySignECDHParamsVerify
    :: Context
    -> ServerECDHParams
    -> PubKey
    -> DigitallySigned
    -> IO Bool
digitallySignECDHParamsVerify :: Context -> ServerECDHParams -> PubKey -> DigitallySigned -> IO Bool
digitallySignECDHParamsVerify Context
ctx ServerECDHParams
dhparams PubKey
pubKey DigitallySigned
signature = do
    expectedData <- Context
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall b. Context -> (ClientRandom -> ServerRandom -> b) -> IO b
withClientAndServerRandom Context
ctx ((ClientRandom -> ServerRandom -> ByteString) -> IO ByteString)
-> (ClientRandom -> ServerRandom -> ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ ServerECDHParams -> ClientRandom -> ServerRandom -> ByteString
encodeSignedECDHParams ServerECDHParams
dhparams
    signatureVerify ctx signature pubKey expectedData

withClientAndServerRandom
    :: Context -> (ClientRandom -> ServerRandom -> b) -> IO b
withClientAndServerRandom :: forall b. Context -> (ClientRandom -> ServerRandom -> b) -> IO b
withClientAndServerRandom Context
ctx ClientRandom -> ServerRandom -> b
f = do
    (cran, sran) <-
        Context
-> HandshakeM (ClientRandom, ServerRandom)
-> IO (ClientRandom, ServerRandom)
forall (m :: * -> *) a. MonadIO m => Context -> HandshakeM a -> m a
usingHState Context
ctx (HandshakeM (ClientRandom, ServerRandom)
 -> IO (ClientRandom, ServerRandom))
-> HandshakeM (ClientRandom, ServerRandom)
-> IO (ClientRandom, ServerRandom)
forall a b. (a -> b) -> a -> b
$
            (,)
                (ClientRandom -> ServerRandom -> (ClientRandom, ServerRandom))
-> HandshakeM ClientRandom
-> HandshakeM (ServerRandom -> (ClientRandom, ServerRandom))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (HandshakeState -> ClientRandom) -> HandshakeM ClientRandom
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets HandshakeState -> ClientRandom
hstClientRandom
                HandshakeM (ServerRandom -> (ClientRandom, ServerRandom))
-> HandshakeM ServerRandom
-> HandshakeM (ClientRandom, ServerRandom)
forall a b. HandshakeM (a -> b) -> HandshakeM a -> HandshakeM b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Maybe ServerRandom -> ServerRandom
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe ServerRandom -> ServerRandom)
-> HandshakeM (Maybe ServerRandom) -> HandshakeM ServerRandom
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (HandshakeState -> Maybe ServerRandom)
-> HandshakeM (Maybe ServerRandom)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets HandshakeState -> Maybe ServerRandom
hstServerRandom)
    return $ f cran sran

-- verify that the hash and signature selected by the peer is supported in
-- the local configuration
checkSupportedHashSignature
    :: Context -> SignatureScheme -> IO ()
checkSupportedHashSignature :: Context -> SignatureScheme -> IO ()
checkSupportedHashSignature Context
ctx SignatureScheme
hs =
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (SignatureScheme
hs SignatureScheme -> [SignatureScheme] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Supported -> [SignatureScheme]
supportedHashSignatures (Context -> Supported
ctxSupported Context
ctx)) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
        let msg :: [Char]
msg = [Char]
"unsupported hash and signature algorithm: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SignatureScheme -> [Char]
forall a. Show a => a -> [Char]
show SignatureScheme
hs
         in TLSError -> IO ()
forall (m :: * -> *) a. MonadIO m => TLSError -> m a
throwCore (TLSError -> IO ()) -> TLSError -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> AlertDescription -> TLSError
Error_Protocol [Char]
msg AlertDescription
IllegalParameter