{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-}
module Crypto.WebAuthn.Metadata.Statement.Decode
( decodeMetadataStatement,
decodeAAGUID,
decodeSubjectKeyIdentifier,
decodeCertificate,
)
where
import Control.Monad (unless)
import Crypto.Hash (SHA1, digestFromByteString)
import qualified Crypto.WebAuthn.Metadata.FidoRegistry as Registry
import Crypto.WebAuthn.Metadata.Statement.Types (WebauthnAttestationType (WebauthnAttestationAttCA, WebauthnAttestationBasic))
import qualified Crypto.WebAuthn.Metadata.Statement.Types as StatementTypes
import qualified Crypto.WebAuthn.Metadata.Statement.WebIDL as StatementIDL
import qualified Crypto.WebAuthn.Metadata.WebIDL as IDL
import qualified Crypto.WebAuthn.Model as M
import Crypto.WebAuthn.Model.Identifier (AAGUID (AAGUID), AuthenticatorIdentifier (AuthenticatorIdentifierFido2, AuthenticatorIdentifierFidoU2F), SubjectKeyIdentifier (SubjectKeyIdentifier))
import Data.Bifunctor (first)
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Base64 as Base64
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Data.Maybe (mapMaybe)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Text.Encoding (encodeUtf8)
import qualified Data.UUID as UUID
import qualified Data.X509 as X509
decodeAAGUID :: StatementIDL.AAGUID -> Either Text (AuthenticatorIdentifier 'M.Fido2)
decodeAAGUID :: AAGUID -> Either Text (AuthenticatorIdentifier 'Fido2)
decodeAAGUID (StatementIDL.AAGUID Text
aaguidText) = case Text -> Maybe UUID
UUID.fromText Text
aaguidText of
Maybe UUID
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Could not decode metadata aaguid: " forall a. Semigroup a => a -> a -> a
<> Text
aaguidText
Just UUID
aaguid -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ AAGUID -> AuthenticatorIdentifier 'Fido2
AuthenticatorIdentifierFido2 forall a b. (a -> b) -> a -> b
$ UUID -> AAGUID
AAGUID UUID
aaguid
decodeSubjectKeyIdentifier :: IDL.DOMString -> Either Text (AuthenticatorIdentifier 'M.FidoU2F)
decodeSubjectKeyIdentifier :: Text -> Either Text (AuthenticatorIdentifier 'FidoU2F)
decodeSubjectKeyIdentifier Text
subjectKeyIdentifierText = case ByteString -> Either String ByteString
Base16.decode (Text -> ByteString
encodeUtf8 Text
subjectKeyIdentifierText) of
Left String
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"A attestationCertificateKeyIdentifier failed to parse because it's not a valid base-16 encoding: " forall a. Semigroup a => a -> a -> a
<> Text
subjectKeyIdentifierText forall a. Semigroup a => a -> a -> a
<> Text
", error: " forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
err
Right ByteString
bytes -> case forall a ba.
(HashAlgorithm a, ByteArrayAccess ba) =>
ba -> Maybe (Digest a)
digestFromByteString @SHA1 ByteString
bytes of
Maybe (Digest SHA1)
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"A attestationCertificateKeyIdentifier failed to parse because it has the wrong length for a SHA1 hash: " forall a. Semigroup a => a -> a -> a
<> Text
subjectKeyIdentifierText
Just Digest SHA1
hash -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ SubjectKeyIdentifier -> AuthenticatorIdentifier 'FidoU2F
AuthenticatorIdentifierFidoU2F forall a b. (a -> b) -> a -> b
$ Digest SHA1 -> SubjectKeyIdentifier
SubjectKeyIdentifier Digest SHA1
hash
decodeCertificate :: IDL.DOMString -> Either Text X509.SignedCertificate
decodeCertificate :: Text -> Either Text SignedCertificate
decodeCertificate Text
text =
let bytes :: ByteString
bytes = ByteString -> ByteString
Base64.decodeLenient (Text -> ByteString
encodeUtf8 forall a b. (a -> b) -> a -> b
$ Text -> Text
Text.strip Text
text)
in case ByteString -> Either String SignedCertificate
X509.decodeSignedCertificate ByteString
bytes of
Left String
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"A certificate failed to parse because it's not a valid encoding: " forall a. Semigroup a => a -> a -> a
<> Text
text forall a. Semigroup a => a -> a -> a
<> Text
", error: " forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
err
Right SignedCertificate
certificate -> forall a b. b -> Either a b
Right SignedCertificate
certificate
decodeMetadataStatement ::
StatementIDL.MetadataStatement ->
Either (Maybe Text) StatementTypes.MetadataStatement
decodeMetadataStatement :: MetadataStatement -> Either (Maybe Text) MetadataStatement
decodeMetadataStatement StatementIDL.MetadataStatement {[Text]
[TransactionConfirmationDisplayType]
Maybe Boolean
Maybe UnsignedShort
Maybe (NonEmpty KeyIdentifier)
Maybe (NonEmpty ExtensionDescriptor)
Maybe (NonEmpty EcdaaTrustAnchor)
Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
Maybe Text
Maybe AAID
Maybe AuthenticatorGetInfo
Maybe AlternativeDescriptions
Maybe AAGUID
UnsignedShort
UnsignedLong
NonEmpty Version
NonEmpty AuthenticatorAttestationType
NonEmpty PublicKeyRepresentationFormat
NonEmpty AuthenticationAlgorithm
NonEmpty AuthenticatorAttachmentHint
NonEmpty MatcherProtectionType
NonEmpty KeyProtectionType
NonEmpty VerificationMethodANDCombinations
Text
ProtocolFamily
$sel:authenticatorGetInfo:MetadataStatement :: MetadataStatement -> Maybe AuthenticatorGetInfo
$sel:supportedExtensions:MetadataStatement :: MetadataStatement -> Maybe (NonEmpty ExtensionDescriptor)
$sel:icon:MetadataStatement :: MetadataStatement -> Maybe Text
$sel:ecdaaTrustAnchors:MetadataStatement :: MetadataStatement -> Maybe (NonEmpty EcdaaTrustAnchor)
$sel:attestationRootCertificates:MetadataStatement :: MetadataStatement -> [Text]
$sel:tcDisplayPNGCharacteristics:MetadataStatement :: MetadataStatement
-> Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
$sel:tcDisplayContentType:MetadataStatement :: MetadataStatement -> Maybe Text
$sel:tcDisplay:MetadataStatement :: MetadataStatement -> [TransactionConfirmationDisplayType]
$sel:attachmentHint:MetadataStatement :: MetadataStatement -> NonEmpty AuthenticatorAttachmentHint
$sel:cryptoStrength:MetadataStatement :: MetadataStatement -> Maybe UnsignedShort
$sel:matcherProtection:MetadataStatement :: MetadataStatement -> NonEmpty MatcherProtectionType
$sel:isFreshUserVerificationRequired:MetadataStatement :: MetadataStatement -> Maybe Boolean
$sel:isKeyRestricted:MetadataStatement :: MetadataStatement -> Maybe Boolean
$sel:keyProtection:MetadataStatement :: MetadataStatement -> NonEmpty KeyProtectionType
$sel:userVerificationDetails:MetadataStatement :: MetadataStatement -> NonEmpty VerificationMethodANDCombinations
$sel:attestationTypes:MetadataStatement :: MetadataStatement -> NonEmpty AuthenticatorAttestationType
$sel:publicKeyAlgAndEncodings:MetadataStatement :: MetadataStatement -> NonEmpty PublicKeyRepresentationFormat
$sel:authenticationAlgorithms:MetadataStatement :: MetadataStatement -> NonEmpty AuthenticationAlgorithm
$sel:upv:MetadataStatement :: MetadataStatement -> NonEmpty Version
$sel:schema:MetadataStatement :: MetadataStatement -> UnsignedShort
$sel:protocolFamily:MetadataStatement :: MetadataStatement -> ProtocolFamily
$sel:authenticatorVersion:MetadataStatement :: MetadataStatement -> UnsignedLong
$sel:alternativeDescriptions:MetadataStatement :: MetadataStatement -> Maybe AlternativeDescriptions
$sel:description:MetadataStatement :: MetadataStatement -> Text
$sel:attestationCertificateKeyIdentifiers:MetadataStatement :: MetadataStatement -> Maybe (NonEmpty KeyIdentifier)
$sel:aaguid:MetadataStatement :: MetadataStatement -> Maybe AAGUID
$sel:aaid:MetadataStatement :: MetadataStatement -> Maybe AAID
$sel:legalHeader:MetadataStatement :: MetadataStatement -> Text
authenticatorGetInfo :: Maybe AuthenticatorGetInfo
supportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
icon :: Maybe Text
ecdaaTrustAnchors :: Maybe (NonEmpty EcdaaTrustAnchor)
attestationRootCertificates :: [Text]
tcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
tcDisplayContentType :: Maybe Text
tcDisplay :: [TransactionConfirmationDisplayType]
attachmentHint :: NonEmpty AuthenticatorAttachmentHint
cryptoStrength :: Maybe UnsignedShort
matcherProtection :: NonEmpty MatcherProtectionType
isFreshUserVerificationRequired :: Maybe Boolean
isKeyRestricted :: Maybe Boolean
keyProtection :: NonEmpty KeyProtectionType
userVerificationDetails :: NonEmpty VerificationMethodANDCombinations
attestationTypes :: NonEmpty AuthenticatorAttestationType
publicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
authenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
upv :: NonEmpty Version
schema :: UnsignedShort
protocolFamily :: ProtocolFamily
authenticatorVersion :: UnsignedLong
alternativeDescriptions :: Maybe AlternativeDescriptions
description :: Text
attestationCertificateKeyIdentifiers :: Maybe (NonEmpty KeyIdentifier)
aaguid :: Maybe AAGUID
aaid :: Maybe AAID
legalHeader :: Text
..} = do
let msLegalHeader :: Text
msLegalHeader = Text
legalHeader
msDescription :: Text
msDescription = Text
description
msAlternativeDescriptions :: Maybe AlternativeDescriptions
msAlternativeDescriptions = Maybe AlternativeDescriptions
alternativeDescriptions
msAuthenticatorVersion :: UnsignedLong
msAuthenticatorVersion = UnsignedLong
authenticatorVersion
forall (f :: * -> *). Applicative f => Boolean -> f () -> f ()
unless (UnsignedShort
schema forall a. Eq a => a -> a -> Boolean
== UnsignedShort
3) forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text
"Schema version is not 3 but " forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (forall a. Show a => a -> String
show UnsignedShort
schema)
let msUpv :: NonEmpty Version
msUpv = NonEmpty Version
upv
msAuthenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
msAuthenticationAlgorithms = NonEmpty AuthenticationAlgorithm
authenticationAlgorithms
msPublicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
msPublicKeyAlgAndEncodings = NonEmpty PublicKeyRepresentationFormat
publicKeyAlgAndEncodings
NonEmpty WebauthnAttestationType
msAttestationTypes <- NonEmpty AuthenticatorAttestationType
-> Either (Maybe Text) (NonEmpty WebauthnAttestationType)
decodeAttestationTypes NonEmpty AuthenticatorAttestationType
attestationTypes
let msUserVerificationDetails :: NonEmpty VerificationMethodANDCombinations
msUserVerificationDetails = NonEmpty VerificationMethodANDCombinations
userVerificationDetails
msKeyProtection :: NonEmpty KeyProtectionType
msKeyProtection = NonEmpty KeyProtectionType
keyProtection
msIsKeyRestricted :: Maybe Boolean
msIsKeyRestricted = Maybe Boolean
isKeyRestricted
msIsFreshUserVerificationRequired :: Maybe Boolean
msIsFreshUserVerificationRequired = Maybe Boolean
isFreshUserVerificationRequired
msMatcherProtection :: NonEmpty MatcherProtectionType
msMatcherProtection = NonEmpty MatcherProtectionType
matcherProtection
msCryptoStrength :: Maybe UnsignedShort
msCryptoStrength = Maybe UnsignedShort
cryptoStrength
msAttachmentHint :: NonEmpty AuthenticatorAttachmentHint
msAttachmentHint = NonEmpty AuthenticatorAttachmentHint
attachmentHint
msTcDisplay :: [TransactionConfirmationDisplayType]
msTcDisplay = [TransactionConfirmationDisplayType]
tcDisplay
msTcDisplayContentType :: Maybe Text
msTcDisplayContentType = Maybe Text
tcDisplayContentType
msTcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
msTcDisplayPNGCharacteristics = Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
tcDisplayPNGCharacteristics
NonEmpty SignedCertificate
msAttestationRootCertificates <- case forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty [Text]
attestationRootCertificates of
Maybe (NonEmpty Text)
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Text
"attestationRootCertificates should not be empty"
Just NonEmpty Text
certs -> forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Text -> Either Text SignedCertificate
decodeCertificate NonEmpty Text
certs
Maybe PNGBytes
msIcon <- forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Text -> Either Text PNGBytes
decodeIcon Maybe Text
icon
let msSupportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
msSupportedExtensions = Maybe (NonEmpty ExtensionDescriptor)
supportedExtensions
msAuthenticatorGetInfo :: Maybe AuthenticatorGetInfo
msAuthenticatorGetInfo = Maybe AuthenticatorGetInfo
authenticatorGetInfo
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ StatementTypes.MetadataStatement {[TransactionConfirmationDisplayType]
Maybe Boolean
Maybe UnsignedShort
Maybe (NonEmpty ExtensionDescriptor)
Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
Maybe Text
Maybe AuthenticatorGetInfo
Maybe AlternativeDescriptions
Maybe PNGBytes
UnsignedLong
NonEmpty SignedCertificate
NonEmpty Version
NonEmpty PublicKeyRepresentationFormat
NonEmpty AuthenticationAlgorithm
NonEmpty AuthenticatorAttachmentHint
NonEmpty MatcherProtectionType
NonEmpty KeyProtectionType
NonEmpty VerificationMethodANDCombinations
NonEmpty WebauthnAttestationType
Text
msAuthenticatorGetInfo :: Maybe AuthenticatorGetInfo
msSupportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
msIcon :: Maybe PNGBytes
msAttestationRootCertificates :: NonEmpty SignedCertificate
msTcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
msTcDisplayContentType :: Maybe Text
msTcDisplay :: [TransactionConfirmationDisplayType]
msAttachmentHint :: NonEmpty AuthenticatorAttachmentHint
msCryptoStrength :: Maybe UnsignedShort
msMatcherProtection :: NonEmpty MatcherProtectionType
msIsFreshUserVerificationRequired :: Maybe Boolean
msIsKeyRestricted :: Maybe Boolean
msKeyProtection :: NonEmpty KeyProtectionType
msUserVerificationDetails :: NonEmpty VerificationMethodANDCombinations
msAttestationTypes :: NonEmpty WebauthnAttestationType
msPublicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
msAuthenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
msUpv :: NonEmpty Version
msAuthenticatorVersion :: UnsignedLong
msAlternativeDescriptions :: Maybe AlternativeDescriptions
msDescription :: Text
msLegalHeader :: Text
msAuthenticatorGetInfo :: Maybe AuthenticatorGetInfo
msSupportedExtensions :: Maybe (NonEmpty ExtensionDescriptor)
msIcon :: Maybe PNGBytes
msAttestationRootCertificates :: NonEmpty SignedCertificate
msTcDisplayPNGCharacteristics :: Maybe (NonEmpty DisplayPNGCharacteristicsDescriptor)
msTcDisplayContentType :: Maybe Text
msTcDisplay :: [TransactionConfirmationDisplayType]
msAttachmentHint :: NonEmpty AuthenticatorAttachmentHint
msCryptoStrength :: Maybe UnsignedShort
msMatcherProtection :: NonEmpty MatcherProtectionType
msIsFreshUserVerificationRequired :: Maybe Boolean
msIsKeyRestricted :: Maybe Boolean
msKeyProtection :: NonEmpty KeyProtectionType
msUserVerificationDetails :: NonEmpty VerificationMethodANDCombinations
msAttestationTypes :: NonEmpty WebauthnAttestationType
msPublicKeyAlgAndEncodings :: NonEmpty PublicKeyRepresentationFormat
msAuthenticationAlgorithms :: NonEmpty AuthenticationAlgorithm
msUpv :: NonEmpty Version
msAuthenticatorVersion :: UnsignedLong
msAlternativeDescriptions :: Maybe AlternativeDescriptions
msDescription :: Text
msLegalHeader :: Text
..}
where
decodeAttestationTypes ::
NonEmpty Registry.AuthenticatorAttestationType ->
Either (Maybe Text) (NonEmpty WebauthnAttestationType)
decodeAttestationTypes :: NonEmpty AuthenticatorAttestationType
-> Either (Maybe Text) (NonEmpty WebauthnAttestationType)
decodeAttestationTypes NonEmpty AuthenticatorAttestationType
types = case forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe AuthenticatorAttestationType -> Maybe WebauthnAttestationType
transform forall a b. (a -> b) -> a -> b
$ forall a. NonEmpty a -> [a]
NE.toList NonEmpty AuthenticatorAttestationType
types of
Maybe (NonEmpty WebauthnAttestationType)
Nothing -> forall a b. a -> Either a b
Left forall a. Maybe a
Nothing
Just NonEmpty WebauthnAttestationType
result -> forall a b. b -> Either a b
Right NonEmpty WebauthnAttestationType
result
where
transform :: Registry.AuthenticatorAttestationType -> Maybe WebauthnAttestationType
transform :: AuthenticatorAttestationType -> Maybe WebauthnAttestationType
transform AuthenticatorAttestationType
Registry.ATTESTATION_BASIC_FULL = forall a. a -> Maybe a
Just WebauthnAttestationType
WebauthnAttestationBasic
transform AuthenticatorAttestationType
Registry.ATTESTATION_ATTCA = forall a. a -> Maybe a
Just WebauthnAttestationType
WebauthnAttestationAttCA
transform AuthenticatorAttestationType
_ = forall a. Maybe a
Nothing
decodeIcon :: IDL.DOMString -> Either Text StatementTypes.PNGBytes
decodeIcon :: Text -> Either Text PNGBytes
decodeIcon Text
dataUrl = case Text -> Text -> Maybe Text
Text.stripPrefix Text
"data:image/png;base64," Text
dataUrl of
Maybe Text
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Icon decoding failed because there is no \"data:image/png;base64,\" prefix: " forall a. Semigroup a => a -> a -> a
<> Text
dataUrl
Just Text
suffix ->
forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ ByteString -> PNGBytes
StatementTypes.PNGBytes forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
Base64.decodeLenient (Text -> ByteString
encodeUtf8 Text
suffix)