
    DjE"                    2   d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZ ddlmZmZ  G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z!e G d de                      Z"e G d d                      Z#d Z$e$d+d             Z% G d! d"ej&                  Z' G d# d$e'          Z( G d% d&e'          Z)d' Z*d( Z+ G d) d*ej&                  Z,dS ),    )annotationsN)	dataclass)IntEnumuniquewraps)AnyMappingSequencecast)x509)InvalidSignature)default_backend)ecpaddingrsa   )AttestationObjectAuthenticatorDatac                      e Zd ZdZdS )InvalidAttestationz.Base exception for attestation-related errors.N__name__
__module____qualname____doc__     Q/home/thesage/.hermes/venv/lib/python3.11/site-packages/fido2/attestation/base.pyr   r   ,   s        8888r   r   c                      e Zd ZdZdS )InvalidDataz"Attestation contains invalid data.Nr   r   r   r   r!   r!   0   s        ,,,,r   r!   c                      e Zd ZdZdS )r   z7The signature of the attestation could not be verified.Nr   r   r   r   r   r   4   s        AAAAr   r   c                      e Zd ZdZdS )UntrustedAttestationz)The CA of the attestation is not trusted.Nr   r   r   r   r$   r$   8   s        3333r   r$   c                  $     e Zd ZdZd fd	Z xZS )UnsupportedTypez(The attestation format is not supported.Nc                v    t                                          |rd| dnd           || _        || _        d S )NzAttestation format "z" is not supportedz)This attestation format is not supported!)super__init__	auth_datafmt)selfr*   r+   	__class__s      r   r)   zUnsupportedType.__init__?   sO    =:3::::<	
 	
 	

 #r   N)r   r   r   r   r)   __classcell__)r-   s   @r   r&   r&   <   sC        22         r   r&   c                  &    e Zd ZdZdZdZdZdZdZdS )AttestationTypezSupported attestation types.   r         r   N)	r   r   r   r   BASICSELFATT_CAANON_CANONEr   r   r   r1   r1   I   s-        &&EDFGDDDr   r1   c                  (    e Zd ZU dZded<   ded<   dS )AttestationResultz'The result of verifying an attestation.r1   attestation_typelist[bytes]
trust_pathN)r   r   r   r   __annotations__r   r   r   r;   r;   T   s0         11%%%%r   r;   c                <     t                      fd            }|S )zDUtility decoractor to wrap common exceptions related to InvalidData.c                 r    	  | i |S # t           t          t          f$ r}t          |          d }~ww xY wr.   )
ValueErrorKeyError
IndexErrorr!   )argskwargsefs      r   innerzcatch_builtins.<locals>.inner_   sO    	!1d%f%%%Hj1 	! 	! 	!a.. 	!s    616r   )rH   rI   s   ` r   catch_builtinsrJ   \   s5     1XX! ! ! ! X! Lr   chainr=   returnNonec                x   d | D             }|                     d          }|r|}|                     d          }|                                }	 t          |t          j                  rB|j        J |                    |j        |j        t          j
                    |j                   nkt          |t          j                  rB|j        J |                    |j        |j        t          j        |j                             nt          d          n# t          $ r t!                      w xY w|dS dS )zVerifies a chain of certificates.

    Checks that the first item in the chain is signed by the next, and so on.
    The first item is the leaf, the last is the root.
    c                P    g | ]#}t          j        |t                                $S r   )r   load_der_x509_certificater   ).0ders     r   
<listcomp>z%verify_x509_chain.<locals>.<listcomp>p   s+    UUUT+C1B1BCCUUUr   r   NzUnsupported signature key type)pop
public_key
isinstancer   RSAPublicKeysignature_hash_algorithmverify	signaturetbs_certificate_bytesr   PKCS1v15r   EllipticCurvePublicKeyECDSArB   _InvalidSignaturer   )rK   certscertchildpubs        r   verify_x509_chainrd   i   sh    VUuUUUE99Q<<D
 %yy||oo	%#s/00 C5AAA

O/$&&2	    C!:;; C5AAA

O/HU;<<    !!ABBB  	% 	% 	%"$$$	%-  % % % % %s   CD D2c                  L    e Zd ZdZej        dd
            Zedd            ZdS )Attestationz7Implements verification of a specific attestation type.	statementMapping[str, Any]r*   r   client_data_hashbytesrL   r;   c                    dS )z^Verifies attestation statement.

        :return: An AttestationResult if successful.
        Nr   r,   rg   r*   ri   s       r   rY   zAttestation.verify   s      r   r+   strtype[Attestation]c                     t                                           D ]}t          |dd           k    r|c S  G  fddt                    }|S )z6Get an Attestation subclass type for the given format.FORMATNc                  "     e Zd Z fdZ xZS )9Attestation.for_type.<locals>.TypedUnsupportedAttestationc                J    t                                                     d S r.   )r(   r)   )r,   r-   r+   s    r   r)   zBAttestation.for_type.<locals>.TypedUnsupportedAttestation.__init__   s!      %%%%%r   )r   r   r   r)   r/   )r-   r+   s   @r   TypedUnsupportedAttestationrr      s>        & & & & & & & & & &r   rt   )rf   __subclasses__getattrUnsupportedAttestation)r+   clsrt   s   `  r   for_typezAttestation.for_type   s     --// 	 	CsHd++s22


 3	& 	& 	& 	& 	& 	& 	&*@ 	& 	& 	& +*r   N)rg   rh   r*   r   ri   rj   rL   r;   )r+   rm   rL   rn   )	r   r   r   r   abcabstractmethodrY   staticmethodry   r   r   r   rf   rf      s\        AA	 	 	 	 
+ 
+ 
+ \
+ 
+ 
+r   rf   c                      e Zd ZddZd ZdS )rw   Nc                    || _         d S r.   )r+   )r,   r+   s     r   r)   zUnsupportedAttestation.__init__   s    r   c                ,    t          || j                  r.   )r&   r+   rl   s       r   rY   zUnsupportedAttestation.verify   s    i222r   r.   )r   r   r   r)   rY   r   r   r   rw   rw      s7           3 3 3 3 3r   rw   c                      e Zd ZdZd ZdS )NoneAttestationnonec                `    |i k    rt          d          t          t          j        g           S )Nz*None Attestation requires empty statement.)r!   r;   r1   r9   rl   s       r   rY   zNoneAttestation.verify   s-    ??JKKK !5r:::r   N)r   r   r   rp   rY   r   r   r   r   r      s(        F; ; ; ; ;r   r   c                   | j         t          j        j        k    rt	          d          	 | j                            t          j                  }|j        j	        rt	          d          d S # t          j
        $ r t	          d          w xY w)Nz+Attestation certificate must use version 3!z+Attestation certificate must have CA=false!z4Attestation certificate must have Basic Constraints!)versionr   Versionv3r!   
extensionsget_extension_for_classBasicConstraintsvaluecaExtensionNotFound)ra   bcs     r   _validate_cert_commonr      s    |t|&&GHHHR_44T5JKK8; 	MKLLL	M 	M! R R RPQQQRs   ?A, ,Bc                 H    d t                                           D             S )Nc                    g | ]?}t          |d d          dk     t          t          t                   |                      @S )rp   r   )rv   r   typer	   )rQ   rx   s     r   rS   z)_default_attestations.<locals>.<listcomp>   sO       3&))V33 	T#Y333r   )rf   ru   r   r   r   _default_attestationsr      s/     --//   r   c                  L    e Zd ZdZdddZej        dd            ZddZd Z	dS )AttestationVerifierzBase class for verifying attestation.

    Override the ca_lookup method to provide a trusted root certificate used
    to verify the trust path from the attestation.
    Nattestation_typesSequence[Attestation] | Nonec                0    |pt                      | _        d S r.   )r   _attestation_types)r,   r   s     r   r)   zAttestationVerifier.__init__   s    "3"N7L7N7Nr   attestation_resultr;   r*   r   rL   bytes | Nonec                    t                      )zLookup a CA certificate to be used to verify a trust path.

        :param attestation_result: The result of the attestation
        :param auth_data: The AuthenticatorData from the registration
        )NotImplementedError)r,   r   r*   s      r   	ca_lookupzAttestationVerifier.ca_lookup   s     "###r   attestation_objectr   ri   rj   rM   c                   t          |j                  }| j        D ] }t          |dd          |j        k    r|} n!|                    |j        |j        |          }|                     ||j                  }|st          d          	 t          |j
        |gz              dS # t          $ r}t          |          d}~ww xY w)zVerify attestation.

        :param attestation_object: dict containing attestation data.
        :param client_data_hash: SHA256 hash of the ClientData bytes.
        rp   NzNo root found for Authenticator)rw   r+   r   rv   rY   att_stmtr*   r   r$   rd   r>   r   )r,   r   ri   att_verifieratresultr   rG   s           r   verify_attestationz&AttestationVerifier.verify_attestation   s     %;;M;Q$R$R) 	 	Br8T**.@.DDD! E $$'(
 
 ^^F$6$@AA 	J&'HIII	*f/2$677777 	* 	* 	*&q)))	*s   B% %
C/B>>Cc                     | j         |  dS )z?Allows passing an instance to Fido2Server as verify_attestationN)r   )r,   rE   s     r   __call__zAttestationVerifier.__call__  s    &&&&r   r.   )r   r   )r   r;   r*   r   rL   r   )r   r   ri   rj   rL   rM   )
r   r   r   r   r)   rz   r{   r   r   r   r   r   r   r   r      s         O O O O O 	$ $ $ $* * * *B' ' ' ' 'r   r   )rK   r=   rL   rM   )-
__future__r   rz   dataclassesr   enumr   r   	functoolsr   typingr	   r
   r   r   cryptographyr   cryptography.exceptionsr   r_   cryptography.hazmat.backendsr   )cryptography.hazmat.primitives.asymmetricr   r   r   webauthnr   r   	Exceptionr   r!   r$   r&   r1   r;   rJ   rd   ABCrf   rw   r   r   r   r   r   r   r   <module>r      s=  8 # " " " " " 



 ! ! ! ! ! !                       / / / / / / / / / / / /       I I I I I I 8 8 8 8 8 8 F F F F F F F F F F ; ; ; ; ; ; ; ;9 9 9 9 9 9 9 9- - - - -$ - - -B B B B B) B B B4 4 4 4 4- 4 4 4
 
 
 
 
( 
 
 
     g           
 
 
 % % % %D+ + + + +#' + + +:3 3 3 3 3[ 3 3 3; ; ; ; ;k ; ; ;	R 	R 	R  8' 8' 8' 8' 8'#' 8' 8' 8' 8' 8'r   