{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}

-- | Stability: internal
-- public keys and signature algorithms are represented with three
-- different types:
--
-- * 'Cose.CoseSignAlg', which is the signature algorithm used, equivalent to a
--   COSE Algorithm from the COSE registry
-- * 'Cose.CosePublicKey', which is a combination of a 'Cose.CoseSignAlg' along with
--   a public key that can be used with it. This is what the COSE_Key
--   CBOR structure decodes to
-- * 'Cose.PublicKey', only the public key part of 'Cose.CosePublicKey'
--
-- The following main operations are supported for these types:
--
-- * 'Cose.CosePublicKey' can be totally decomposed into a 'Cose.CoseSignAlg'
--   with 'Cose.signAlg' and a 'Cose.PublicKey' with 'Cose.publicKey'
-- * A 'Cose.PublicKey' can be created from an X.509 public key with 'fromX509'
-- * A 'Cose.CoseSignAlg' and a 'Cose.PublicKey' can be used to verify a signature
--   with 'verify'
module Crypto.WebAuthn.Cose.Internal.Verify
  ( -- * Public Key
    fromX509,

    -- * Signature verification
    Cose.Message (..),
    Cose.Signature (..),
    verify,

    -- * Hash Conversions to crypton types
    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

-- | Turns a X.509 certificates 'X509.PubKey' into a 'Cose.PublicKey'
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 (ByteString -> EdDSAKeyBytes) -> ByteString -> EdDSAKeyBytes
forall a b. (a -> b) -> a -> b
$ PublicKey -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert PublicKey
key
      }
fromX509 (X509.PubKeyEC X509.PubKeyEC_Named {CurveName
SerializedPoint
pubkeyEC_name :: CurveName
pubkeyEC_pub :: SerializedPoint
pubkeyEC_pub :: PubKeyEC -> SerializedPoint
pubkeyEC_name :: PubKeyEC -> 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 -> Text -> Either Text Point
forall a b. a -> Either a b
Left Text
"Failed to unserialize ECDSA point in X509 certificate"
    Just Point
res -> Point -> Either Text Point
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Point
res
  UncheckedPublicKey
unchecked <- case Point
point of
    ECC.Point Integer
ecdsaX Integer
ecdsaY -> UncheckedPublicKey -> Either Text UncheckedPublicKey
forall a b. b -> Either a b
Right (UncheckedPublicKey -> Either Text UncheckedPublicKey)
-> UncheckedPublicKey -> Either Text UncheckedPublicKey
forall a b. (a -> b) -> a -> b
$ Cose.PublicKeyECDSA {Integer
CoseCurveECDSA
ecdsaCurve :: CoseCurveECDSA
ecdsaX :: Integer
ecdsaY :: Integer
ecdsaCurve :: CoseCurveECDSA
ecdsaX :: Integer
ecdsaY :: Integer
..}
    Point
ECC.PointO -> Text -> Either Text UncheckedPublicKey
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 :: Int
public_n :: Integer
public_e :: Integer
public_size :: PublicKey -> Int
public_n :: PublicKey -> Integer
public_e :: PublicKey -> Integer
..}) =
  UncheckedPublicKey -> Either Text PublicKey
Cose.checkPublicKey
    Cose.PublicKeyRSA
      { rsaN :: Integer
rsaN = Integer
public_n,
        rsaE :: Integer
rsaE = Integer
public_e
      }
fromX509 PubKey
key = Text -> Either Text PublicKey
forall a b. a -> Either a b
Left (Text -> Either Text PublicKey) -> Text -> Either Text PublicKey
forall a b. (a -> b) -> a -> b
$ Text
"X509 public key algorithm is not supported: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (PubKeyALG -> [Char]
forall a. Show a => a -> [Char]
show (PubKey -> PubKeyALG
X509.pubkeyToAlg PubKey
key))

-- | Verifies an asymmetric signature for a message using a
-- 'Cose.PublicKeyWithSignAlg' Returns an error if the signature algorithm
-- doesn't match. Also returns an error if the signature wasn't valid or for
-- other errors.
-- FIXME: https://w3c.github.io/webauthn/#sctn-signature-attestation-types kind of documents this, but not for all formats. This is notably not really related to COSE, but rather webauthn's own definitions. The spec should be made less ambiguous, file upstream issues and refactor this code
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 :: UncheckedPublicKey -> EdDSAKeyBytes
eddsaX :: EdDSAKeyBytes
..},
      signAlg :: PublicKeyWithSignAlg -> CoseSignAlg
signAlg = CoseSignAlg
Cose.CoseSignAlgEdDSA
    }
  Message
msg
  Signature
sig = do
    PublicKey
key <- case ByteString -> CryptoFailable PublicKey
forall ba. ByteArrayAccess ba => ba -> CryptoFailable PublicKey
Ed25519.publicKey (ByteString -> CryptoFailable PublicKey)
-> ByteString -> CryptoFailable PublicKey
forall a b. (a -> b) -> a -> b
$ EdDSAKeyBytes -> ByteString
Cose.unEdDSAKeyBytes EdDSAKeyBytes
eddsaX of
      CryptoFailed CryptoError
err -> Text -> Either Text PublicKey
forall a b. a -> Either a b
Left (Text -> Either Text PublicKey) -> Text -> Either Text PublicKey
forall a b. (a -> b) -> a -> b
$ Text
"Failed to create Ed25519 public key: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (CryptoError -> [Char]
forall a. Show a => a -> [Char]
show CryptoError
err)
      CryptoPassed PublicKey
res -> PublicKey -> Either Text PublicKey
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PublicKey
res
    Signature
sig <- case ByteString -> CryptoFailable Signature
forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature (Signature -> ByteString
Cose.unSignature Signature
sig) of
      CryptoFailed CryptoError
err -> Text -> Either Text Signature
forall a b. a -> Either a b
Left (Text -> Either Text Signature) -> Text -> Either Text Signature
forall a b. (a -> b) -> a -> b
$ Text
"Failed to create Ed25519 signature: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (CryptoError -> [Char]
forall a. Show a => a -> [Char]
show CryptoError
err)
      CryptoPassed Signature
res -> Signature -> Either Text Signature
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Signature
res
    if PublicKey -> ByteString -> Signature -> Bool
forall ba.
ByteArrayAccess ba =>
PublicKey -> ba -> Signature -> Bool
Ed25519.verify PublicKey
key (Message -> ByteString
Cose.unMessage Message
msg) Signature
sig
      then () -> Either Text ()
forall a b. b -> Either a b
Right ()
      else Text -> Either Text ()
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
ecdsaCurve :: UncheckedPublicKey -> CoseCurveECDSA
ecdsaX :: UncheckedPublicKey -> Integer
ecdsaY :: UncheckedPublicKey -> Integer
ecdsaCurve :: CoseCurveECDSA
ecdsaX :: Integer
ecdsaY :: Integer
..},
      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

    -- This check is already done in checkPublicKey
    -- unless (ECC.isPointValid public_curve public_q) $
    --  Left $ "ECDSA point is not valid for curve " <> Text.pack (show curveName) <> ": " <> Text.pack (show public_q)
    let key :: PublicKey
key = ECDSA.PublicKey {Point
Curve
public_curve :: Curve
public_q :: Point
public_curve :: Curve
public_q :: Point
..}

    -- https://www.w3.org/TR/webauthn-2/#sctn-signature-attestation-types
    -- > For COSEAlgorithmIdentifier -7 (ES256), and other ECDSA-based algorithms,
    -- the `sig` value MUST be encoded as an ASN.1 DER Ecdsa-Sig-Value, as defined
    -- in [RFC3279](https://www.w3.org/TR/webauthn-2/#biblio-rfc3279) section 2.2.3.
    Signature
sig <- case DER -> ByteString -> Either ASN1Error [ASN1]
forall a.
ASN1Decoding a =>
a -> ByteString -> Either ASN1Error [ASN1]
ASN1.decodeASN1' DER
ASN1.DER (Signature -> ByteString
Cose.unSignature Signature
sig) of
      Left ASN1Error
err -> Text -> Either Text Signature
forall a b. a -> Either a b
Left (Text -> Either Text Signature) -> Text -> Either Text Signature
forall a b. (a -> b) -> a -> b
$ Text
"Failed to decode ECDSA DER value: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (ASN1Error -> [Char]
forall a. Show a => a -> [Char]
show ASN1Error
err)
      -- Ecdsa-Sig-Value in https://datatracker.ietf.org/doc/html/rfc3279#section-2.2.3
      Right [ASN1.Start ASN1ConstructionType
ASN1.Sequence, ASN1.IntVal Integer
r, ASN1.IntVal Integer
s, ASN1.End ASN1ConstructionType
ASN1.Sequence] ->
        Signature -> Either Text Signature
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Signature -> Either Text Signature)
-> Signature -> Either Text Signature
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Signature
ECDSA.Signature Integer
r Integer
s
      Right [ASN1]
asns -> Text -> Either Text Signature
forall a b. a -> Either a b
Left (Text -> Either Text Signature) -> Text -> Either Text Signature
forall a b. (a -> b) -> a -> b
$ Text
"Unexpected ECDSA ASN.1 structure: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack ([ASN1] -> [Char]
forall a. Show a => a -> [Char]
show [ASN1]
asns)

    if a -> PublicKey -> Signature -> ByteString -> Bool
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 () -> Either Text ()
forall a b. b -> Either a b
Right ()
      else Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"ECDSA Signature invalid"
verify
  Cose.PublicKeyWithSignAlg
    { publicKey :: PublicKeyWithSignAlg -> PublicKey
publicKey = Cose.PublicKey Cose.PublicKeyRSA {Integer
rsaN :: UncheckedPublicKey -> Integer
rsaE :: UncheckedPublicKey -> Integer
rsaN :: Integer
rsaE :: Integer
..},
      signAlg :: PublicKeyWithSignAlg -> CoseSignAlg
signAlg = Cose.CoseSignAlgRSA (CoseHashAlgRSA -> SomeHashAlgorithmASN1
toCryptHashRSA -> SomeHashAlgorithmASN1 a
hash)
    }
  Message
msg
  Signature
sig = do
    let key :: PublicKey
key =
          RSA.PublicKey
            { -- https://www.rfc-editor.org/rfc/rfc8017#section-8.2.2
              -- > k is the length in octets of the RSA modulus n
              --
              -- > Length checking: If the length of the signature S is not k
              -- > octets, output "invalid signature" and stop.
              -- This is done by the RSA.verify call
              public_size :: Int
public_size = ByteString -> Int
BS.length (Integer -> ByteString
forall ba. ByteArray ba => Integer -> ba
i2osp Integer
rsaN),
              public_n :: Integer
public_n = Integer
rsaN,
              public_e :: Integer
public_e = Integer
rsaE
            }
    if Maybe a -> PublicKey -> ByteString -> ByteString -> Bool
forall hashAlg.
HashAlgorithmASN1 hashAlg =>
Maybe hashAlg -> PublicKey -> ByteString -> ByteString -> Bool
RSA.verify (a -> Maybe a
forall a. a -> Maybe a
Just a
hash) PublicKey
key (Message -> ByteString
Cose.unMessage Message
msg) (Signature -> ByteString
Cose.unSignature Signature
sig)
      then () -> Either Text ()
forall a b. b -> Either a b
Right ()
      else Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"RSA Signature invalid"
verify PublicKeyWithSignAlg
key Message
_ Signature
_ = [Char] -> Either Text ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Either Text ()) -> [Char] -> Either Text ()
forall a b. (a -> b) -> a -> b
$ [Char]
"PublicKeyWithSignAlg invariant violated for public key " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> PublicKeyWithSignAlg -> [Char]
forall a. Show a => a -> [Char]
show PublicKeyWithSignAlg
key [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
". This should not occur unless the PublicKeyWithSignAlg module has a bug"

-- | Some crypton 'Hash.HashAlgorithm' type, used as a return value of 'toCryptHashECDSA'
data SomeHashAlgorithm = forall a. (Hash.HashAlgorithm a) => SomeHashAlgorithm a

-- | Returns the crypton 'SomeHashAlgorithm' corresponding to this hash algorithm
toCryptHashECDSA :: Cose.CoseHashAlgECDSA -> SomeHashAlgorithm
toCryptHashECDSA :: CoseHashAlgECDSA -> SomeHashAlgorithm
toCryptHashECDSA CoseHashAlgECDSA
Cose.CoseHashAlgECDSASHA256 = SHA256 -> SomeHashAlgorithm
forall a. HashAlgorithm a => a -> SomeHashAlgorithm
SomeHashAlgorithm SHA256
Hash.SHA256
toCryptHashECDSA CoseHashAlgECDSA
Cose.CoseHashAlgECDSASHA384 = SHA384 -> SomeHashAlgorithm
forall a. HashAlgorithm a => a -> SomeHashAlgorithm
SomeHashAlgorithm SHA384
Hash.SHA384
toCryptHashECDSA CoseHashAlgECDSA
Cose.CoseHashAlgECDSASHA512 = SHA512 -> SomeHashAlgorithm
forall a. HashAlgorithm a => a -> SomeHashAlgorithm
SomeHashAlgorithm SHA512
Hash.SHA512

-- | Some crypton 'RSA.HashAlgorithmASN1' type, used as a return value of 'toCryptHashRSA'
data SomeHashAlgorithmASN1 = forall a. (RSA.HashAlgorithmASN1 a) => SomeHashAlgorithmASN1 a

-- | Returns the crypton 'SomeHashAlgorithmASN1' corresponding to this hash algorithm
toCryptHashRSA :: Cose.CoseHashAlgRSA -> SomeHashAlgorithmASN1
toCryptHashRSA :: CoseHashAlgRSA -> SomeHashAlgorithmASN1
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA1 = SHA1 -> SomeHashAlgorithmASN1
forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA1
Hash.SHA1
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA256 = SHA256 -> SomeHashAlgorithmASN1
forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA256
Hash.SHA256
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA384 = SHA384 -> SomeHashAlgorithmASN1
forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA384
Hash.SHA384
toCryptHashRSA CoseHashAlgRSA
Cose.CoseHashAlgRSASHA512 = SHA512 -> SomeHashAlgorithmASN1
forall a. HashAlgorithmASN1 a => a -> SomeHashAlgorithmASN1
SomeHashAlgorithmASN1 SHA512
Hash.SHA512