{-# LANGUAGE RankNTypes #-}
module Data.X509.Validation.Signature (
verifySignedSignature,
verifySignature,
SignatureVerification (..),
SignatureFailure (..),
) where
import Crypto.Error (CryptoFailable (..))
import Crypto.Hash
import Crypto.Number.Basic (numBits)
import qualified Crypto.PubKey.DSA as DSA
import qualified Crypto.PubKey.ECC.ECDSA as ECDSA
import qualified Crypto.PubKey.ECC.Types as ECC
import qualified Crypto.PubKey.Ed25519 as Ed25519
import qualified Crypto.PubKey.Ed448 as Ed448
import qualified Crypto.PubKey.MLDSA as MLDSA
import Crypto.PubKey.MaskGenFunction (mgf1)
import qualified Crypto.PubKey.RSA as RSA
import qualified Crypto.PubKey.RSA.PKCS15 as RSA
import qualified Crypto.PubKey.RSA.PSS as PSS
import Data.ASN1.BinaryEncoding
import Data.ASN1.Encoding
import Data.ASN1.Types
import Data.ByteString (ByteString)
import Data.X509
import Data.X509.EC
import Data.X509.Validation.Internal (tryPure)
data SignatureVerification
=
SignaturePass
|
SignatureFailed SignatureFailure
deriving (Int -> SignatureVerification -> ShowS
[SignatureVerification] -> ShowS
SignatureVerification -> String
(Int -> SignatureVerification -> ShowS)
-> (SignatureVerification -> String)
-> ([SignatureVerification] -> ShowS)
-> Show SignatureVerification
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SignatureVerification -> ShowS
showsPrec :: Int -> SignatureVerification -> ShowS
$cshow :: SignatureVerification -> String
show :: SignatureVerification -> String
$cshowList :: [SignatureVerification] -> ShowS
showList :: [SignatureVerification] -> ShowS
Show, SignatureVerification -> SignatureVerification -> Bool
(SignatureVerification -> SignatureVerification -> Bool)
-> (SignatureVerification -> SignatureVerification -> Bool)
-> Eq SignatureVerification
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SignatureVerification -> SignatureVerification -> Bool
== :: SignatureVerification -> SignatureVerification -> Bool
$c/= :: SignatureVerification -> SignatureVerification -> Bool
/= :: SignatureVerification -> SignatureVerification -> Bool
Eq)
data SignatureFailure
=
SignatureInvalid
|
SignaturePubkeyMismatch
|
SignatureUnimplemented
deriving (Int -> SignatureFailure -> ShowS
[SignatureFailure] -> ShowS
SignatureFailure -> String
(Int -> SignatureFailure -> ShowS)
-> (SignatureFailure -> String)
-> ([SignatureFailure] -> ShowS)
-> Show SignatureFailure
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SignatureFailure -> ShowS
showsPrec :: Int -> SignatureFailure -> ShowS
$cshow :: SignatureFailure -> String
show :: SignatureFailure -> String
$cshowList :: [SignatureFailure] -> ShowS
showList :: [SignatureFailure] -> ShowS
Show, SignatureFailure -> SignatureFailure -> Bool
(SignatureFailure -> SignatureFailure -> Bool)
-> (SignatureFailure -> SignatureFailure -> Bool)
-> Eq SignatureFailure
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SignatureFailure -> SignatureFailure -> Bool
== :: SignatureFailure -> SignatureFailure -> Bool
$c/= :: SignatureFailure -> SignatureFailure -> Bool
/= :: SignatureFailure -> SignatureFailure -> Bool
Eq)
verifySignedSignature
:: (Show a, Eq a, ASN1Object a)
=> SignedExact a
-> PubKey
-> SignatureVerification
verifySignedSignature :: forall a.
(Show a, Eq a, ASN1Object a) =>
SignedExact a -> PubKey -> SignatureVerification
verifySignedSignature SignedExact a
signedObj PubKey
pubKey =
SignatureALG
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifySignature
(Signed a -> SignatureALG
forall a. (Show a, Eq a, ASN1Object a) => Signed a -> SignatureALG
signedAlg Signed a
signed)
PubKey
pubKey
(SignedExact a -> ByteString
forall a.
(Show a, Eq a, ASN1Object a) =>
SignedExact a -> ByteString
getSignedData SignedExact a
signedObj)
(Signed a -> ByteString
forall a. (Show a, Eq a, ASN1Object a) => Signed a -> ByteString
signedSignature Signed a
signed)
where
signed :: Signed a
signed = SignedExact a -> Signed a
forall a. (Show a, Eq a, ASN1Object a) => SignedExact a -> Signed a
getSigned SignedExact a
signedObj
verifySignature
:: SignatureALG
-> PubKey
-> ByteString
-> ByteString
-> SignatureVerification
verifySignature :: SignatureALG
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifySignature SignatureALG
alg PubKey
pubkey ByteString
cdata ByteString
signature
| Bool -> Bool
not (PubKey -> Bool
acceptablePubKey PubKey
pubkey) = SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid
| SignatureALG -> Bool
weakHash SignatureALG
alg = SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureUnimplemented
| Bool
otherwise =
(String -> SignatureVerification)
-> (SignatureVerification -> SignatureVerification)
-> Either String SignatureVerification
-> SignatureVerification
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (SignatureVerification -> String -> SignatureVerification
forall a b. a -> b -> a
const (SignatureVerification -> String -> SignatureVerification)
-> SignatureVerification -> String -> SignatureVerification
forall a b. (a -> b) -> a -> b
$ SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid) SignatureVerification -> SignatureVerification
forall a. a -> a
id (Either String SignatureVerification -> SignatureVerification)
-> Either String SignatureVerification -> SignatureVerification
forall a b. (a -> b) -> a -> b
$
(SignatureVerification -> ())
-> SignatureVerification -> Either String SignatureVerification
forall a. (a -> ()) -> a -> Either String a
tryPure (SignatureVerification -> () -> ()
forall a b. a -> b -> b
`seq` ()) (SignatureALG
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifySignature' SignatureALG
alg PubKey
pubkey ByteString
cdata ByteString
signature)
weakHash :: SignatureALG -> Bool
weakHash :: SignatureALG -> Bool
weakHash (SignatureALG HashALG
h PubKeyALG
_) = HashALG
h HashALG -> [HashALG] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [HashALG
HashMD2, HashALG
HashMD5]
weakHash SignatureALG
_ = Bool
False
acceptablePubKey :: PubKey -> Bool
acceptablePubKey :: PubKey -> Bool
acceptablePubKey (PubKeyRSA PublicKey
key) = PublicKey -> Bool
acceptableRSA PublicKey
key
acceptablePubKey (PubKeyRSAPSS PublicKey
key Maybe PSSParams
_) = PublicKey -> Bool
acceptableRSA PublicKey
key
acceptablePubKey (PubKeyDSA (DSA.PublicKey (DSA.Params Integer
p Integer
g Integer
q) Integer
y)) =
Integer
p Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
2
Bool -> Bool -> Bool
&& Integer -> Int
numBits Integer
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
8192
Bool -> Bool -> Bool
&& Integer
q Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
1
Bool -> Bool -> Bool
&& Integer
q Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
p
Bool -> Bool -> Bool
&& Integer -> Int
numBits Integer
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
512
Bool -> Bool -> Bool
&& Integer
g Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
1
Bool -> Bool -> Bool
&& Integer
g Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
p
Bool -> Bool -> Bool
&& Integer
y Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
1
Bool -> Bool -> Bool
&& Integer
y Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
p
acceptablePubKey PubKey
_ = Bool
True
acceptableRSA :: RSA.PublicKey -> Bool
acceptableRSA :: PublicKey -> Bool
acceptableRSA (RSA.PublicKey Int
_ Integer
n Integer
e) =
Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
1 Bool -> Bool -> Bool
&& Integer -> Int
numBits Integer
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
16384 Bool -> Bool -> Bool
&& Integer
e Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
1 Bool -> Bool -> Bool
&& Integer -> Int
numBits Integer
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
64
verifySignature'
:: SignatureALG -> PubKey -> ByteString -> ByteString -> SignatureVerification
verifySignature' :: SignatureALG
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifySignature' (SignatureALG_Unknown OID
_) PubKey
_ ByteString
_ ByteString
_ = SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureUnimplemented
verifySignature' (SignatureALG HashALG
hashALG PubKeyALG
PubKeyALG_RSAPSS) PubKey
pubkey ByteString
cdata ByteString
signature =
PSSParams
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifyPSS (HashALG -> PSSParams
defaultPSSParams HashALG
hashALG) PubKey
pubkey ByteString
cdata ByteString
signature
verifySignature' (SignatureALG_RSAPSS PSSParams
params) PubKey
pubkey ByteString
cdata ByteString
signature =
PSSParams
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifyPSS PSSParams
params PubKey
pubkey ByteString
cdata ByteString
signature
verifySignature' (SignatureALG HashALG
hashALG PubKeyALG
pubkeyALG) PubKey
pubkey ByteString
cdata ByteString
signature
| PubKey -> PubKeyALG
pubkeyToAlg PubKey
pubkey PubKeyALG -> PubKeyALG -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG
pubkeyALG = case PubKey -> Maybe (ByteString -> ByteString -> Bool)
verifyF PubKey
pubkey of
Maybe (ByteString -> ByteString -> Bool)
Nothing -> SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureUnimplemented
Just ByteString -> ByteString -> Bool
f ->
if ByteString -> ByteString -> Bool
f ByteString
cdata ByteString
signature
then SignatureVerification
SignaturePass
else SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid
| Bool
otherwise = SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignaturePubkeyMismatch
where
verifyF :: PubKey -> Maybe (ByteString -> ByteString -> Bool)
verifyF (PubKeyRSA PublicKey
key) = (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a. a -> Maybe a
Just ((ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool))
-> (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a b. (a -> b) -> a -> b
$ HashALG -> PublicKey -> ByteString -> ByteString -> Bool
forall {ba}.
ByteArrayAccess ba =>
HashALG -> PublicKey -> ba -> ByteString -> Bool
rsaVerify HashALG
hashALG PublicKey
key
verifyF (PubKeyDSA PublicKey
key)
| HashALG
hashALG HashALG -> HashALG -> Bool
forall a. Eq a => a -> a -> Bool
== HashALG
HashSHA1 = (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a. a -> Maybe a
Just ((ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool))
-> (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a b. (a -> b) -> a -> b
$ SHA1 -> PublicKey -> ByteString -> ByteString -> Bool
forall {msg} {hash}.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> msg -> ByteString -> Bool
dsaVerify SHA1
SHA1 PublicKey
key
| HashALG
hashALG HashALG -> HashALG -> Bool
forall a. Eq a => a -> a -> Bool
== HashALG
HashSHA224 = (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a. a -> Maybe a
Just ((ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool))
-> (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a b. (a -> b) -> a -> b
$ SHA224 -> PublicKey -> ByteString -> ByteString -> Bool
forall {msg} {hash}.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> msg -> ByteString -> Bool
dsaVerify SHA224
SHA224 PublicKey
key
| HashALG
hashALG HashALG -> HashALG -> Bool
forall a. Eq a => a -> a -> Bool
== HashALG
HashSHA256 = (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a. a -> Maybe a
Just ((ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool))
-> (ByteString -> ByteString -> Bool)
-> Maybe (ByteString -> ByteString -> Bool)
forall a b. (a -> b) -> a -> b
$ SHA256 -> PublicKey -> ByteString -> ByteString -> Bool
forall {msg} {hash}.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> msg -> ByteString -> Bool
dsaVerify SHA256
SHA256 PublicKey
key
| Bool
otherwise = Maybe (ByteString -> ByteString -> Bool)
forall a. Maybe a
Nothing
verifyF (PubKeyEC PubKeyEC
key) = HashALG -> PubKeyEC -> Maybe (ByteString -> ByteString -> Bool)
verifyECDSA HashALG
hashALG PubKeyEC
key
verifyF PubKey
_ = Maybe (ByteString -> ByteString -> Bool)
forall a. Maybe a
Nothing
dsaToSignature :: ByteString -> Maybe DSA.Signature
dsaToSignature :: ByteString -> Maybe Signature
dsaToSignature ByteString
b =
case DER -> ByteString -> Either ASN1Error [ASN1]
forall a.
ASN1Decoding a =>
a -> ByteString -> Either ASN1Error [ASN1]
decodeASN1' DER
DER ByteString
b of
Right [Start ASN1ConstructionType
Sequence, IntVal Integer
r, IntVal Integer
s, End ASN1ConstructionType
Sequence] ->
Signature -> Maybe Signature
forall a. a -> Maybe a
Just (Signature -> Maybe Signature) -> Signature -> Maybe Signature
forall a b. (a -> b) -> a -> b
$ DSA.Signature{sign_r :: Integer
DSA.sign_r = Integer
r, sign_s :: Integer
DSA.sign_s = Integer
s}
Either ASN1Error [ASN1]
_ -> Maybe Signature
forall a. Maybe a
Nothing
dsaVerify :: hash -> PublicKey -> msg -> ByteString -> Bool
dsaVerify hash
hsh PublicKey
key msg
b ByteString
a =
case ByteString -> Maybe Signature
dsaToSignature ByteString
a of
Maybe Signature
Nothing -> Bool
False
Just Signature
dsaSig -> hash -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
DSA.verify hash
hsh PublicKey
key Signature
dsaSig msg
b
rsaVerify :: HashALG -> PublicKey -> ba -> ByteString -> Bool
rsaVerify HashALG
HashMD2 = MD2 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith MD2
MD2
rsaVerify HashALG
HashMD5 = MD5 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith MD5
MD5
rsaVerify HashALG
HashSHA1 = SHA1 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA1
SHA1
rsaVerify HashALG
HashSHA224 = SHA224 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA224
SHA224
rsaVerify HashALG
HashSHA256 = SHA256 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA256
SHA256
rsaVerify HashALG
HashSHA384 = SHA384 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA384
SHA384
rsaVerify HashALG
HashSHA512 = SHA512 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA512
SHA512
rsaVerify HashALG
HashSHA3_224 = SHA3_224 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA3_224
SHA3_224
rsaVerify HashALG
HashSHA3_256 = SHA3_256 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA3_256
SHA3_256
rsaVerify HashALG
HashSHA3_384 = SHA3_384 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA3_384
SHA3_384
rsaVerify HashALG
HashSHA3_512 = SHA3_512 -> PublicKey -> ba -> ByteString -> Bool
forall {hashAlg} {ba}.
(HashAlgorithmASN1 hashAlg, ByteArrayAccess ba) =>
hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith SHA3_512
SHA3_512
rsaVerifyWith :: hashAlg -> PublicKey -> ba -> ByteString -> Bool
rsaVerifyWith hashAlg
h PublicKey
key ba
msg = PublicKey -> Digest hashAlg -> ByteString -> Bool
forall hashAlg.
HashAlgorithmASN1 hashAlg =>
PublicKey -> Digest hashAlg -> ByteString -> Bool
RSA.verifyDigest PublicKey
key (hashAlg -> ba -> Digest hashAlg
forall ba alg.
(ByteArrayAccess ba, HashAlgorithm alg) =>
alg -> ba -> Digest alg
hashWith hashAlg
h ba
msg)
verifySignature' (SignatureALG_IntrinsicHash PubKeyALG
pubkeyALG) PubKey
pubkey ByteString
cdata ByteString
signature
| PubKey -> PubKeyALG
pubkeyToAlg PubKey
pubkey PubKeyALG -> PubKeyALG -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG
pubkeyALG = PubKey -> SignatureVerification
doVerify PubKey
pubkey
| Bool
otherwise = SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignaturePubkeyMismatch
where
doVerify :: PubKey -> SignatureVerification
doVerify (PubKeyEd25519 PublicKey
key) = (PublicKey -> ByteString -> Signature -> Bool)
-> (ByteString -> CryptoFailable Signature)
-> PublicKey
-> SignatureVerification
forall {t} {t}.
(t -> ByteString -> t -> Bool)
-> (ByteString -> CryptoFailable t) -> t -> SignatureVerification
eddsa PublicKey -> ByteString -> Signature -> Bool
forall ba.
ByteArrayAccess ba =>
PublicKey -> ba -> Signature -> Bool
Ed25519.verify ByteString -> CryptoFailable Signature
forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature PublicKey
key
doVerify (PubKeyEd448 PublicKey
key) = (PublicKey -> ByteString -> Signature -> Bool)
-> (ByteString -> CryptoFailable Signature)
-> PublicKey
-> SignatureVerification
forall {t} {t}.
(t -> ByteString -> t -> Bool)
-> (ByteString -> CryptoFailable t) -> t -> SignatureVerification
eddsa PublicKey -> ByteString -> Signature -> Bool
forall ba.
ByteArrayAccess ba =>
PublicKey -> ba -> Signature -> Bool
Ed448.verify ByteString -> CryptoFailable Signature
forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed448.signature PublicKey
key
doVerify (PubKeyMLDSA44 VerificationKey MLDSA44
key) = VerificationKey MLDSA44 -> SignatureVerification
forall p. MLDSA p => VerificationKey p -> SignatureVerification
mldsa VerificationKey MLDSA44
key
doVerify (PubKeyMLDSA65 VerificationKey MLDSA65
key) = VerificationKey MLDSA65 -> SignatureVerification
forall p. MLDSA p => VerificationKey p -> SignatureVerification
mldsa VerificationKey MLDSA65
key
doVerify (PubKeyMLDSA87 VerificationKey MLDSA87
key) = VerificationKey MLDSA87 -> SignatureVerification
forall p. MLDSA p => VerificationKey p -> SignatureVerification
mldsa VerificationKey MLDSA87
key
doVerify PubKey
_ = SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureUnimplemented
eddsa :: (t -> ByteString -> t -> Bool)
-> (ByteString -> CryptoFailable t) -> t -> SignatureVerification
eddsa t -> ByteString -> t -> Bool
verify ByteString -> CryptoFailable t
toSig t
key =
case ByteString -> CryptoFailable t
toSig ByteString
signature of
CryptoPassed t
sig
| t -> ByteString -> t -> Bool
verify t
key ByteString
cdata t
sig -> SignatureVerification
SignaturePass
| Bool
otherwise -> SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid
CryptoFailed CryptoError
_ -> SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid
mldsa :: MLDSA.MLDSA p => MLDSA.VerificationKey p -> SignatureVerification
mldsa :: forall p. MLDSA p => VerificationKey p -> SignatureVerification
mldsa VerificationKey p
key =
case ByteString -> CryptoFailable (Signature p)
forall p ba.
(MLDSA p, ByteArrayAccess ba) =>
ba -> CryptoFailable (Signature p)
MLDSA.signature ByteString
signature of
CryptoPassed Signature p
sig
| VerificationKey p -> Context -> ByteString -> Signature p -> Bool
forall p msg.
(MLDSA p, ByteArrayAccess msg) =>
VerificationKey p -> Context -> msg -> Signature p -> Bool
MLDSA.verify VerificationKey p
key Context
MLDSA.emptyContext ByteString
cdata Signature p
sig -> SignatureVerification
SignaturePass
| Bool
otherwise -> SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid
CryptoFailed CryptoError
_ -> SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid
verifyPSS
:: PSSParams -> PubKey -> ByteString -> ByteString -> SignatureVerification
verifyPSS :: PSSParams
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifyPSS PSSParams
params (PubKeyRSAPSS PublicKey
key Maybe PSSParams
mkp) ByteString
cdata ByteString
signature
| Just PSSParams
kp <- Maybe PSSParams
mkp
, PSSParams -> HashALG
pssHash PSSParams
params HashALG -> HashALG -> Bool
forall a. Eq a => a -> a -> Bool
/= PSSParams -> HashALG
pssHash PSSParams
kp
Bool -> Bool -> Bool
|| PSSParams -> HashALG
pssMGFHash PSSParams
params HashALG -> HashALG -> Bool
forall a. Eq a => a -> a -> Bool
/= PSSParams -> HashALG
pssMGFHash PSSParams
kp
Bool -> Bool -> Bool
|| PSSParams -> Int
pssSaltLength PSSParams
params Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< PSSParams -> Int
pssSaltLength PSSParams
kp =
SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignaturePubkeyMismatch
| Bool
otherwise = PSSParams
-> PubKey -> ByteString -> ByteString -> SignatureVerification
verifyPSS PSSParams
params (PublicKey -> PubKey
PubKeyRSA PublicKey
key) ByteString
cdata ByteString
signature
verifyPSS PSSParams
params (PubKeyRSA PublicKey
key) ByteString
cdata ByteString
signature =
case HashALG
-> (forall a. HashAlgorithm a => a -> Maybe Bool) -> Maybe Bool
forall r.
HashALG -> (forall a. HashAlgorithm a => a -> Maybe r) -> Maybe r
withPSSHash (PSSParams -> HashALG
pssHash PSSParams
params) ((forall a. HashAlgorithm a => a -> Maybe Bool) -> Maybe Bool)
-> (forall a. HashAlgorithm a => a -> Maybe Bool) -> Maybe Bool
forall a b. (a -> b) -> a -> b
$ \a
h -> HashALG
-> (forall a. HashAlgorithm a => a -> Maybe Bool) -> Maybe Bool
forall r.
HashALG -> (forall a. HashAlgorithm a => a -> Maybe r) -> Maybe r
withPSSHash (PSSParams -> HashALG
pssMGFHash PSSParams
params) ((forall a. HashAlgorithm a => a -> Maybe Bool) -> Maybe Bool)
-> (forall a. HashAlgorithm a => a -> Maybe Bool) -> Maybe Bool
forall a b. (a -> b) -> a -> b
$ \a
m ->
Bool -> Maybe Bool
forall a. a -> Maybe a
Just (PSSParams a ByteString ByteString
-> PublicKey -> ByteString -> ByteString -> Bool
forall hash.
HashAlgorithm hash =>
PSSParams hash ByteString ByteString
-> PublicKey -> ByteString -> ByteString -> Bool
PSS.verify (a -> a -> PSSParams a ByteString ByteString
forall {output} {hashAlg} {seed} {hash}.
(ByteArray output, HashAlgorithm hashAlg, ByteArrayAccess seed) =>
hash -> hashAlg -> PSSParams hash seed output
pssParams a
h a
m) PublicKey
key ByteString
cdata ByteString
signature) of
Maybe Bool
Nothing -> SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureUnimplemented
Just Bool
True -> SignatureVerification
SignaturePass
Just Bool
False -> SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignatureInvalid
where
pssParams :: hash -> hashAlg -> PSSParams hash seed output
pssParams hash
h hashAlg
m =
PSS.PSSParams
{ pssHash :: hash
PSS.pssHash = hash
h
, pssMaskGenAlg :: MaskGenAlgorithm seed output
PSS.pssMaskGenAlg = hashAlg -> MaskGenAlgorithm seed output
forall seed output hashAlg.
(ByteArrayAccess seed, ByteArray output, HashAlgorithm hashAlg) =>
hashAlg -> seed -> Int -> output
mgf1 hashAlg
m
, pssSaltLength :: Int
PSS.pssSaltLength = PSSParams -> Int
pssSaltLength PSSParams
params
, pssTrailerField :: Word8
PSS.pssTrailerField = Word8
0xbc
}
verifyPSS PSSParams
_ PubKey
_ ByteString
_ ByteString
_ = SignatureFailure -> SignatureVerification
SignatureFailed SignatureFailure
SignaturePubkeyMismatch
withPSSHash
:: HashALG -> (forall a. HashAlgorithm a => a -> Maybe r) -> Maybe r
withPSSHash :: forall r.
HashALG -> (forall a. HashAlgorithm a => a -> Maybe r) -> Maybe r
withPSSHash HashALG
HashSHA1 forall a. HashAlgorithm a => a -> Maybe r
f = SHA1 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA1
SHA1
withPSSHash HashALG
HashSHA224 forall a. HashAlgorithm a => a -> Maybe r
f = SHA224 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA224
SHA224
withPSSHash HashALG
HashSHA256 forall a. HashAlgorithm a => a -> Maybe r
f = SHA256 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA256
SHA256
withPSSHash HashALG
HashSHA384 forall a. HashAlgorithm a => a -> Maybe r
f = SHA384 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA384
SHA384
withPSSHash HashALG
HashSHA512 forall a. HashAlgorithm a => a -> Maybe r
f = SHA512 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA512
SHA512
withPSSHash HashALG
HashSHA3_224 forall a. HashAlgorithm a => a -> Maybe r
f = SHA3_224 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA3_224
SHA3_224
withPSSHash HashALG
HashSHA3_256 forall a. HashAlgorithm a => a -> Maybe r
f = SHA3_256 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA3_256
SHA3_256
withPSSHash HashALG
HashSHA3_384 forall a. HashAlgorithm a => a -> Maybe r
f = SHA3_384 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA3_384
SHA3_384
withPSSHash HashALG
HashSHA3_512 forall a. HashAlgorithm a => a -> Maybe r
f = SHA3_512 -> Maybe r
forall a. HashAlgorithm a => a -> Maybe r
f SHA3_512
SHA3_512
withPSSHash HashALG
_ forall a. HashAlgorithm a => a -> Maybe r
_ = Maybe r
forall a. Maybe a
Nothing
verifyECDSA :: HashALG -> PubKeyEC -> Maybe (ByteString -> ByteString -> Bool)
verifyECDSA :: HashALG -> PubKeyEC -> Maybe (ByteString -> ByteString -> Bool)
verifyECDSA HashALG
hashALG PubKeyEC
key =
PubKeyEC -> Maybe CurveName
ecPubKeyCurveName PubKeyEC
key Maybe CurveName
-> (CurveName -> Maybe (ByteString -> ByteString -> Bool))
-> Maybe (ByteString -> ByteString -> Bool)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SerializedPoint
-> CurveName -> Maybe (ByteString -> ByteString -> Bool)
forall {msg}.
ByteArrayAccess msg =>
SerializedPoint -> CurveName -> Maybe (msg -> ByteString -> Bool)
verifyCurve (PubKeyEC -> SerializedPoint
pubkeyEC_pub PubKeyEC
key)
where
verifyCurve :: SerializedPoint -> CurveName -> Maybe (msg -> ByteString -> Bool)
verifyCurve SerializedPoint
pub CurveName
curveName = (msg -> ByteString -> Bool) -> Maybe (msg -> ByteString -> Bool)
forall a. a -> Maybe a
Just ((msg -> ByteString -> Bool) -> Maybe (msg -> ByteString -> Bool))
-> (msg -> ByteString -> Bool) -> Maybe (msg -> ByteString -> Bool)
forall a b. (a -> b) -> a -> b
$ \msg
msg ByteString
sigBS ->
case DER -> ByteString -> Either ASN1Error [ASN1]
forall a.
ASN1Decoding a =>
a -> ByteString -> Either ASN1Error [ASN1]
decodeASN1' DER
DER ByteString
sigBS of
Left ASN1Error
_ -> Bool
False
Right [Start ASN1ConstructionType
Sequence, IntVal Integer
r, IntVal Integer
s, End ASN1ConstructionType
Sequence] ->
let curve :: Curve
curve = CurveName -> Curve
ECC.getCurveByName CurveName
curveName
in case Curve -> SerializedPoint -> Maybe Point
unserializePoint Curve
curve SerializedPoint
pub of
Maybe Point
Nothing -> Bool
False
Just Point
p ->
let pubkey :: PublicKey
pubkey = Curve -> Point -> PublicKey
ECDSA.PublicKey Curve
curve Point
p
in (HashALG -> PublicKey -> Signature -> msg -> Bool
forall {msg}.
ByteArrayAccess msg =>
HashALG -> PublicKey -> Signature -> msg -> Bool
ecdsaVerify HashALG
hashALG) PublicKey
pubkey (Integer -> Integer -> Signature
ECDSA.Signature Integer
r Integer
s) msg
msg
Right [ASN1]
_ -> Bool
False
ecdsaVerify :: HashALG -> PublicKey -> Signature -> msg -> Bool
ecdsaVerify HashALG
HashMD2 = MD2 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify MD2
MD2
ecdsaVerify HashALG
HashMD5 = MD5 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify MD5
MD5
ecdsaVerify HashALG
HashSHA1 = SHA1 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA1
SHA1
ecdsaVerify HashALG
HashSHA224 = SHA224 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA224
SHA224
ecdsaVerify HashALG
HashSHA256 = SHA256 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA256
SHA256
ecdsaVerify HashALG
HashSHA384 = SHA384 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA384
SHA384
ecdsaVerify HashALG
HashSHA512 = SHA512 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA512
SHA512
ecdsaVerify HashALG
HashSHA3_224 = SHA3_224 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA3_224
SHA3_224
ecdsaVerify HashALG
HashSHA3_256 = SHA3_256 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA3_256
SHA3_256
ecdsaVerify HashALG
HashSHA3_384 = SHA3_384 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA3_384
SHA3_384
ecdsaVerify HashALG
HashSHA3_512 = SHA3_512 -> PublicKey -> Signature -> msg -> Bool
forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify SHA3_512
SHA3_512