| 1 |
-- | |
| 2 |
-- Module : Data.X509.Validation.Signature |
| 3 |
-- License : BSD-style |
| 4 |
-- Maintainer : Vincent Hanquez <[email protected]> |
| 5 |
-- Stability : experimental |
| 6 |
-- Portability : unknown |
| 7 |
-- |
| 8 |
-- X.509 Certificate and CRL signature verification |
| 9 |
module Data.X509.Validation.Signature ( |
| 10 |
verifySignedSignature, |
| 11 |
verifySignature, |
| 12 |
SignatureVerification (..), |
| 13 |
SignatureFailure (..), |
| 14 |
) where |
| 15 |
|
| 16 |
import Crypto.Error (CryptoFailable (..)) |
| 17 |
import Crypto.Hash |
| 18 |
import qualified Crypto.PubKey.DSA as DSA |
| 19 |
import qualified Crypto.PubKey.ECC.ECDSA as ECDSA |
| 20 |
import qualified Crypto.PubKey.ECC.Types as ECC |
| 21 |
import qualified Crypto.PubKey.Ed25519 as Ed25519 |
| 22 |
import qualified Crypto.PubKey.Ed448 as Ed448 |
| 23 |
import qualified Crypto.PubKey.RSA.PKCS15 as RSA |
| 24 |
import qualified Crypto.PubKey.RSA.PSS as PSS |
| 25 |
|
| 26 |
import Data.ASN1.BinaryEncoding |
| 27 |
import Data.ASN1.Encoding |
| 28 |
import Data.ASN1.Types |
| 29 |
import Data.ByteString (ByteString) |
| 30 |
import Data.X509 |
| 31 |
import Data.X509.EC |
| 32 |
|
| 33 |
-- | A set of possible return from signature verification. |
| 34 |
-- |
| 35 |
-- When SignatureFailed is return, the signature shouldn't be |
| 36 |
-- accepted. |
| 37 |
-- |
| 38 |
-- Other values are only useful to differentiate the failure |
| 39 |
-- reason, but are all equivalent to failure. |
| 40 |
data SignatureVerification |
| 41 |
= -- | verification succeeded |
| 42 |
SignaturePass |
| 43 |
| -- | verification failed |
| 44 |
SignatureFailed SignatureFailure |
| 45 |
deriving (Show, Eq) |
| 46 |
|
| 47 |
-- | Various failure possible during signature checking |
| 48 |
data SignatureFailure |
| 49 |
= -- | signature doesn't verify |
| 50 |
SignatureInvalid |
| 51 |
| -- | algorithm and public key mismatch, cannot proceed |
| 52 |
SignaturePubkeyMismatch |
| 53 |
| -- | unimplemented signature algorithm |
| 54 |
SignatureUnimplemented |
| 55 |
deriving (Show, Eq) |
| 56 |
|
| 57 |
-- | Verify a Signed object against a specified public key |
| 58 |
verifySignedSignature |
| 59 |
:: (Show a, Eq a, ASN1Object a) |
| 60 |
=> SignedExact a |
| 61 |
-> PubKey |
| 62 |
-> SignatureVerification |
| 63 |
verifySignedSignature signedObj pubKey = |
| 64 |
verifySignature |
| 65 |
(signedAlg signed) |
| 66 |
pubKey |
| 67 |
(getSignedData signedObj) |
| 68 |
(signedSignature signed) |
| 69 |
where |
| 70 |
signed = getSigned signedObj |
| 71 |
|
| 72 |
-- | verify signature using parameter |
| 73 |
verifySignature |
| 74 |
:: SignatureALG |
| 75 |
-- ^ Signature algorithm used |
| 76 |
-> PubKey |
| 77 |
-- ^ Public key to use for verify |
| 78 |
-> ByteString |
| 79 |
-- ^ Certificate data that need to be verified |
| 80 |
-> ByteString |
| 81 |
-- ^ Signature to verify |
| 82 |
-> SignatureVerification |
| 83 |
verifySignature (SignatureALG_Unknown _) _ _ _ = SignatureFailed SignatureUnimplemented |
| 84 |
verifySignature (SignatureALG hashALG PubKeyALG_RSAPSS) pubkey cdata signature = case verifyF pubkey of |
| 85 |
Nothing -> SignatureFailed SignatureUnimplemented |
| 86 |
Just f -> |
| 87 |
if f cdata signature |
| 88 |
then SignaturePass |
| 89 |
else SignatureFailed SignatureInvalid |
| 90 |
where |
| 91 |
verifyF (PubKeyRSA key) |
| 92 |
| hashALG == HashSHA256 = Just $ PSS.verify (PSS.defaultPSSParams SHA256) key |
| 93 |
| hashALG == HashSHA384 = Just $ PSS.verify (PSS.defaultPSSParams SHA384) key |
| 94 |
| hashALG == HashSHA512 = Just $ PSS.verify (PSS.defaultPSSParams SHA512) key |
| 95 |
| hashALG == HashSHA224 = Just $ PSS.verify (PSS.defaultPSSParams SHA224) key |
| 96 |
| otherwise = Nothing |
| 97 |
verifyF _ = Nothing |
| 98 |
verifySignature (SignatureALG hashALG pubkeyALG) pubkey cdata signature |
| 99 |
| pubkeyToAlg pubkey == pubkeyALG = case verifyF pubkey of |
| 100 |
Nothing -> SignatureFailed SignatureUnimplemented |
| 101 |
Just f -> |
| 102 |
if f cdata signature |
| 103 |
then SignaturePass |
| 104 |
else SignatureFailed SignatureInvalid |
| 105 |
| otherwise = SignatureFailed SignaturePubkeyMismatch |
| 106 |
where |
| 107 |
verifyF (PubKeyRSA key) = Just $ rsaVerify hashALG key |
| 108 |
verifyF (PubKeyDSA key) |
| 109 |
| hashALG == HashSHA1 = Just $ dsaVerify SHA1 key |
| 110 |
| hashALG == HashSHA224 = Just $ dsaVerify SHA224 key |
| 111 |
| hashALG == HashSHA256 = Just $ dsaVerify SHA256 key |
| 112 |
| otherwise = Nothing |
| 113 |
verifyF (PubKeyEC key) = verifyECDSA hashALG key |
| 114 |
verifyF _ = Nothing |
| 115 |
|
| 116 |
dsaToSignature :: ByteString -> Maybe DSA.Signature |
| 117 |
dsaToSignature b = |
| 118 |
case decodeASN1' BER b of |
| 119 |
Left _ -> Nothing |
| 120 |
Right asn1 -> |
| 121 |
case asn1 of |
| 122 |
Start Sequence : IntVal r : IntVal s : End Sequence : _ -> |
| 123 |
Just $ DSA.Signature{DSA.sign_r = r, DSA.sign_s = s} |
| 124 |
_ -> |
| 125 |
Nothing |
| 126 |
|
| 127 |
dsaVerify hsh key b a = |
| 128 |
case dsaToSignature a of |
| 129 |
Nothing -> False |
| 130 |
Just dsaSig -> DSA.verify hsh key dsaSig b |
| 131 |
|
| 132 |
rsaVerify HashMD2 = RSA.verify (Just MD2) |
| 133 |
rsaVerify HashMD5 = RSA.verify (Just MD5) |
| 134 |
rsaVerify HashSHA1 = RSA.verify (Just SHA1) |
| 135 |
rsaVerify HashSHA224 = RSA.verify (Just SHA224) |
| 136 |
rsaVerify HashSHA256 = RSA.verify (Just SHA256) |
| 137 |
rsaVerify HashSHA384 = RSA.verify (Just SHA384) |
| 138 |
rsaVerify HashSHA512 = RSA.verify (Just SHA512) |
| 139 |
verifySignature (SignatureALG_IntrinsicHash pubkeyALG) pubkey cdata signature |
| 140 |
| pubkeyToAlg pubkey == pubkeyALG = doVerify pubkey |
| 141 |
| otherwise = SignatureFailed SignaturePubkeyMismatch |
| 142 |
where |
| 143 |
doVerify (PubKeyEd25519 key) = eddsa Ed25519.verify Ed25519.signature key |
| 144 |
doVerify (PubKeyEd448 key) = eddsa Ed448.verify Ed448.signature key |
| 145 |
doVerify _ = SignatureFailed SignatureUnimplemented |
| 146 |
|
| 147 |
eddsa verify toSig key = |
| 148 |
case toSig signature of |
| 149 |
CryptoPassed sig |
| 150 |
| verify key cdata sig -> SignaturePass |
| 151 |
| otherwise -> SignatureFailed SignatureInvalid |
| 152 |
CryptoFailed _ -> SignatureFailed SignatureInvalid |
| 153 |
|
| 154 |
verifyECDSA :: HashALG -> PubKeyEC -> Maybe (ByteString -> ByteString -> Bool) |
| 155 |
verifyECDSA hashALG key = |
| 156 |
ecPubKeyCurveName key >>= verifyCurve (pubkeyEC_pub key) |
| 157 |
where |
| 158 |
verifyCurve pub curveName = Just $ \msg sigBS -> |
| 159 |
case decodeASN1' BER sigBS of |
| 160 |
Left _ -> False |
| 161 |
Right [Start Sequence, IntVal r, IntVal s, End Sequence] -> |
| 162 |
let curve = ECC.getCurveByName curveName |
| 163 |
in case unserializePoint curve pub of |
| 164 |
Nothing -> False |
| 165 |
Just p -> |
| 166 |
let pubkey = ECDSA.PublicKey curve p |
| 167 |
in (ecdsaVerify hashALG) pubkey (ECDSA.Signature r s) msg |
| 168 |
Right _ -> False |
| 169 |
|
| 170 |
ecdsaVerify HashMD2 = ECDSA.verify MD2 |
| 171 |
ecdsaVerify HashMD5 = ECDSA.verify MD5 |
| 172 |
ecdsaVerify HashSHA1 = ECDSA.verify SHA1 |
| 173 |
ecdsaVerify HashSHA224 = ECDSA.verify SHA224 |
| 174 |
ecdsaVerify HashSHA256 = ECDSA.verify SHA256 |
| 175 |
ecdsaVerify HashSHA384 = ECDSA.verify SHA384 |
| 176 |
ecdsaVerify HashSHA512 = ECDSA.verify SHA512 |