{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
module Crypto.WebAuthn.Cose.Internal.Verify
(
fromX509,
Cose.Message (..),
Cose.Signature (..),
verify,
SomeHashAlgorithm (..),
toCryptHashECDSA,
SomeHashAlgorithmASN1 (..),
toCryptHashRSA,
)
where
import Crypto.Error (CryptoFailable (CryptoFailed, CryptoPassed))
import qualified Crypto.Hash as Hash
import Crypto.Number.Serialize (i2osp)
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.RSA as RSA
import qualified Crypto.PubKey.RSA.PKCS15 as RSA
import qualified Crypto.WebAuthn.Cose.PublicKey as Cose
import qualified Crypto.WebAuthn.Cose.PublicKeyWithSignAlg as Cose
import qualified Crypto.WebAuthn.Cose.SignAlg as Cose
import qualified Data.ASN1.BinaryEncoding as ASN1
import qualified Data.ASN1.Encoding as ASN1
import qualified Data.ASN1.Types as ASN1
import Data.ByteArray (convert)
import qualified Data.ByteString as BS
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.X509 as X509
import qualified Data.X509.EC as X509
fromX509 :: X509.PubKey -> Either Text Cose.PublicKey
fromX509 :: PubKey -> Either Text PublicKey
fromX509 (X509.PubKeyEd25519 PublicKey
key) =
UncheckedPublicKey -> Either Text PublicKey
Cose.checkPublicKey
Cose.PublicKeyEdDSA
{ eddsaCurve :: CoseCurveEdDSA
eddsaCurve = CoseCurveEdDSA
Cose.CoseCurveEd25519,
eddsaX :: EdDSAKeyBytes
eddsaX = ByteString -> EdDSAKeyBytes
Cose.EdDSAKeyBytes forall a b. (a -> b) -> a -> b
$ forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert PublicKey
key
}
fromX509 (X509.PubKeyEC X509.PubKeyEC_Named {CurveName
SerializedPoint
pubkeyEC_pub :: PubKeyEC -> SerializedPoint
pubkeyEC_name :: PubKeyEC -> CurveName
pubkeyEC_pub :: SerializedPoint
pubkeyEC_name :: CurveName
..}) = do
let curve :: Curve
curve = CurveName -> Curve
ECC.getCurveByName CurveName
pubkeyEC_name
CoseCurveECDSA
ecdsaCurve <- CurveName -> Either Text CoseCurveECDSA
Cose.fromCryptCurveECDSA CurveName
pubkeyEC_name
Point
point <- case Curve -> SerializedPoint -> Maybe Point
X509.unserializePoint Curve
curve SerializedPoint
pubkeyEC_pub of
Maybe Point
Nothing -> forall a b. a -> Either a b
Left Text
"Failed to unserialize ECDSA point in X509 certificate"
Just Point
res -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Point
res
UncheckedPublicKey
unchecked <- case Point
point of
ECC.Point Integer
ecdsaX Integer
ecdsaY -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Cose.PublicKeyECDSA {Integer
CoseCurveECDSA
ecdsaY :: Integer
ecdsaX :: Integer
ecdsaCurve :: CoseCurveECDSA
ecdsaY :: Integer
ecdsaX :: Integer
ecdsaCurve :: CoseCurveECDSA
..}
Point
ECC.PointO -> forall a b. a -> Either a b
Left Text
"The infinity point is not supported"
UncheckedPublicKey -> Either Text PublicKey
Cose.checkPublicKey UncheckedPublicKey
unchecked
fromX509 (X509.PubKeyRSA RSA.PublicKey {Int
Integer
public_size :: PublicKey -> Int
public_n :: PublicKey -> Integer
public_e :: PublicKey -> Integer
public_e :: Integer
public_n :: Integer
public_size :: Int
..}) =
UncheckedPublicKey -> Either Text PublicKey
Cose.checkPublicKey
Cose.PublicKeyRSA
{ rsaN :: Integer
rsaN = Integer
public_n,
rsaE :: Integer
rsaE = Integer
public_e
}
fromX509 PubKey
key = forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"X509 public key algorithm is not supported: " forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (forall a. Show a => a -> [Char]
show (PubKey -> PubKeyALG
X509.pubkeyToAlg PubKey
key))
verify :: Cose.PublicKeyWithSignAlg -> Cose.Message -> Cose.Signature -> Either Text ()
verify :: PublicKeyWithSignAlg -> Message -> Signature -> Either Text ()
verify
Cose.PublicKeyWithSignAlg
{ publicKey :: PublicKeyWithSignAlg -> PublicKey
publicKey = Cose.PublicKey Cose.PublicKeyEdDSA {eddsaCurve :: UncheckedPublicKey -> CoseCurveEdDSA
eddsaCurve = CoseCurveEdDSA
Cose.CoseCurveEd25519, EdDSAKeyBytes
eddsaX :: EdDSAKeyBytes
eddsaX :: UncheckedPublicKey -> EdDSAKeyBytes
..},
signAlg :: PublicKeyWithSignAlg -> CoseSignAlg
signAlg = CoseSignAlg
Cose.CoseSignAlgEdDSA
}
Message
msg
Signature
sig = do
PublicKey
key <- case forall ba. ByteArrayAccess ba => ba -> CryptoFailable PublicKey
Ed25519.publicKey forall a b. (a -> b) -> a -> b
$ EdDSAKeyBytes -> ByteString
Cose.unEdDSAKeyBytes EdDSAKeyBytes
eddsaX of
CryptoFailed CryptoError
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Failed to create Ed25519 public key: " forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (forall a. Show a => a -> [Char]
show CryptoError
err)
CryptoPassed PublicKey
res -> forall (f :: * -> *) a. Applicative f => a -> f a
pure PublicKey
res
Signature
sig <- case forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature (Signature -> ByteString
Cose.unSignature Signature
sig) of
CryptoFailed CryptoError
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Failed to create Ed25519 signature: " forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (forall a. Show a => a -> [Char]
show CryptoError
err)
CryptoPassed Signature
res -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Signature
res
if forall ba.
ByteArrayAccess ba =>
PublicKey -> ba -> Signature -> Bool
Ed25519.verify PublicKey
key (Message -> ByteString
Cose.unMessage Message
msg) Signature
sig
then forall a b. b -> Either a b
Right ()
else forall a b. a -> Either a b
Left Text
"EdDSA Signature invalid"
verify
Cose.PublicKeyWithSignAlg
{ publicKey :: PublicKeyWithSignAlg -> PublicKey
publicKey = Cose.PublicKey Cose.PublicKeyECDSA {Integer
CoseCurveECDSA
ecdsaY :: Integer
ecdsaX :: Integer
ecdsaCurve :: CoseCurveECDSA
ecdsaY :: UncheckedPublicKey -> Integer
ecdsaX :: UncheckedPublicKey -> Integer
ecdsaCurve :: UncheckedPublicKey -> CoseCurveECDSA
..},
signAlg :: PublicKeyWithSignAlg -> CoseSignAlg
signAlg = Cose.CoseSignAlgECDSA (CoseHashAlgECDSA -> SomeHashAlgorithm
toCryptHashECDSA -> SomeHashAlgorithm a
hash)
}
Message
msg
Signature
sig = do
let curveName :: CurveName
curveName = CoseCurveECDSA -> CurveName
Cose.toCryptCurveECDSA CoseCurveECDSA
ecdsaCurve
public_curve :: Curve
public_curve = CurveName -> Curve
ECC.getCurveByName CurveName
curveName
public_q :: Point
public_q = Integer -> Integer -> Point
ECC.Point Integer
ecdsaX Integer
ecdsaY
let key :: PublicKey
key = ECDSA.PublicKey {Curve
Point
public_curve :: Curve
public_q :: Point
public_q :: Point
public_curve :: Curve
..}
Signature
sig <- case forall a.
ASN1Decoding a =>
a -> ByteString -> Either ASN1Error [ASN1]
ASN1.decodeASN1' DER
ASN1.DER (Signature -> ByteString
Cose.unSignature Signature
sig) of
Left ASN1Error
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Failed to decode ECDSA DER value: " forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (forall a. Show a => a -> [Char]
show ASN1Error
err)
Right [ASN1.Start ASN1ConstructionType
ASN1.Sequence, ASN1.IntVal Integer
r, ASN1.IntVal Integer
s, ASN1.End ASN1ConstructionType
ASN1.Sequence] ->
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Signature
ECDSA.Signature Integer
r Integer
s
Right [ASN1]
asns -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Unexpected ECDSA ASN.1 structure: " forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (forall a. Show a => a -> [Char]
show [ASN1]
asns)
if forall msg hash.
(ByteArrayAccess msg, HashAlgorithm hash) =>
hash -> PublicKey -> Signature -> msg -> Bool
ECDSA.verify a
hash PublicKey
key Signature
sig (Message -> ByteString
Cose.unMessage Message
msg)
then forall a b. b -> Either a b
Right ()
else forall a b. a -> Either a b
Left Text
"ECDSA Signature invalid"
verify
Cose.PublicKeyWithSignAlg
{ publicKey :: PublicKeyWithSignAlg -> PublicKey
publicKey = Cose.PublicKey Cose.PublicKeyRSA {Integer
rsaE :: Integer
rsaN :: Integer
rsaE :: UncheckedPublicKey -> Integer
rsaN :: UncheckedPublicKey -> Integer
..},
signAlg :: PublicKeyWithSignAlg -> CoseSignAlg
signAlg = Cose.CoseSignAlgRSA (CoseHashAlgRSA -> SomeHashAlgorithmASN1
toCryptHashRSA -> SomeHashAlgorithmASN1 a
hash)
}
Message
msg
Signature
sig = do
let key :: PublicKey
key =
RSA.PublicKey
{
public_size :: Int
public_size = ByteString -> Int
BS.length (forall ba. ByteArray ba => Integer -> ba
i2osp Integer
rsaN),
public_n :: Integer
public_n = Integer
rsaN,
public_e :: Integer
public_e = Integer
rsaE
}
if forall hashAlg.
HashAlgorithmASN1 hashAlg =>
Maybe hashAlg -> PublicKey -> ByteString -> ByteString -> Bool
RSA.verify (forall a. a -> Maybe a
Just a
hash) PublicKey
key (Message -> ByteString
Cose.unMessage Message
msg) (Signature -> ByteString
Cose.unSignature Signature
sig)
then forall a b. b -> Either a b
Right ()
else forall a b. a -> Either a b
Left Text
"RSA Signature invalid"
verify PublicKeyWithSignAlg
key Message
_ Signature
_ = forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"PublicKeyWithSignAlg invariant violated for public key " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show PublicKeyWithSignAlg
key forall a. Semigroup a => a -> a -> a
<> [Char]
". This should not occur unless the PublicKeyWithSignAlg module has a bug"
data SomeHashAlgorithm = forall a. Hash.HashAlgorithm a => SomeHashAlgorithm a
toCryptHashECDSA :: Cose.CoseHashAlgECDSA -> SomeHashAlgorithm
toCryptHashECDSA :: CoseHashAlgECDSA -> SomeHashAlgorithm
toCryptHashECDSA CoseHashAlgECDSA
Cose.CoseHashAlgECDSASHA256 = forall a. HashAlgorithm a => a -> SomeHashAlgorithm
SomeHashAlgorithm SHA256
Hash.SHA256
toCryptHashECDSA CoseHashAlgECDSA
Cose.CoseHashAlgECDSASHA384 = forall a. HashAlgorithm a => a -> SomeHashAlgorithm
SomeHashAlgorithm SHA384
Hash.SHA384
toCryptHashECDSA CoseHashAlgECDSA
Cose.CoseHashAlgECDSASHA512 = forall a. HashAlgorithm a => a -> SomeHashAlgorithm
SomeHashAlgorithm SHA512
Hash.SHA512
data SomeHashAlgorithmASN1 = forall a. RSA.HashAlgorithmASN1 a => SomeHashAlgorithmASN1 a
toCryptHashRSA :: Cose.CoseHashAlgRSA -> SomeHashAlgorithmASN1
toCryptHashRSA :: CoseHashAlgRSA -> SomeHashAlgorithmASN1
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA1 = forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA1
Hash.SHA1
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA256 = forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA256
Hash.SHA256
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA384 = forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA384
Hash.SHA384
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA512 = forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA512
Hash.SHA512