o
    shO"                     @  s  d dl mZ ddlmZmZ d dlmZ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 d d	lmZ d d
lmZ d dlmZmZmZmZmZmZ d dlZG dd deZ G dd de Z!G dd de ZG dd de Z"G dd de Z#eG dd deZ$eG dd dZ%dd Z&e&d/d!d"Z'G d#d$ d$ej(Z)G d%d& d&e)Z*G d'd( d(e)Z+d)d* Z,d+d, Z-G d-d. d.ej(Z.dS )0    )annotations   )AuthenticatorDataAttestationObject)IntEnumunique)x509)default_backend)paddingecrsa)InvalidSignature)	dataclasswraps)ListTypeMappingSequenceOptionalAnyNc                   @     e Zd ZdZdS )InvalidAttestationz.Base exception for attestation-related errors.N__name__
__module____qualname____doc__ r   r   Q/var/www/html/env_mimamsha/lib/python3.10/site-packages/fido2/attestation/base.pyr   +       r   c                   @  r   )InvalidDataz"Attestation contains invalid data.Nr   r   r   r   r   r!   /   r    r!   c                   @  r   )r   z7The signature of the attestation could not be verified.Nr   r   r   r   r   r   3   r    r   c                   @  r   )UntrustedAttestationz)The CA of the attestation is not trusted.Nr   r   r   r   r   r"   7   r    r"   c                      s"   e Zd ZdZd fdd	Z  ZS )UnsupportedTypez(The attestation format is not supported.Nc                   s,   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__r   r   r%   >   s   
zUnsupportedType.__init__N)r   r   r   r   r%   __classcell__r   r   r)   r   r#   ;   s    r#   c                   @  s$   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   r   r-   H   s    r-   c                   @  s"   e Zd ZU dZded< ded< dS )AttestationResultz'The result of verifying an attestation.r-   attestation_typeList[bytes]
trust_pathN)r   r   r   r   __annotations__r   r   r   r   r6   S   s   
 r6   c                   s   t   fdd}|S )zDUtility decoractor to wrap common exceptions related to InvalidData.c               
     s8   z | i |W S  t ttfy } zt|d }~ww r+   )
ValueErrorKeyError
IndexErrorr!   )argskwargsefr   r   inner^   s   zcatch_builtins.<locals>.innerr   )rB   rC   r   rA   r   catch_builtins[   s   rD   chainr8   returnNonec                 C  s   dd | D }| d}|rd|}| d}| }z<t|tjr5|jdus'J ||j|jt	
 |j nt|tjrP|jdusBJ ||j|jt|j ntdW n
 ty_   t w |s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                 S  s   g | ]	}t |t qS r   )r   load_der_x509_certificater	   ).0derr   r   r   
<listcomp>o   s    z%verify_x509_chain.<locals>.<listcomp>r   NzUnsupported signature key type)pop
public_key
isinstancer   RSAPublicKeysignature_hash_algorithmverify	signaturetbs_certificate_bytesr
   PKCS1v15r   EllipticCurvePublicKeyECDSAr;   _InvalidSignaturer   )rE   certscertchildpubr   r   r   verify_x509_chainh   s8   


r\   c                   @  s.   e Zd ZdZejdd
dZedddZdS )Attestationz7Implements verification of a specific attestation type.	statementMapping[str, Any]r&   r   client_data_hashbytesrF   r6   c                 C  s   dS )z^Verifies attestation statement.

        :return: An AttestationResult if successful.
        Nr   r(   r^   r&   r`   r   r   r   rQ      s    zAttestation.verifyr'   strType[Attestation]c                   s>   t  D ]}t|dd kr|  S qG  fdddt}|S )z6Get an Attestation subclass type for the given format.FORMATNc                      s   e Zd Z fddZ  ZS )z9Attestation.for_type.<locals>.TypedUnsupportedAttestationc                   s   t   d S r+   )r$   r%   )r(   )r*   r'   r   r   r%         zBAttestation.for_type.<locals>.TypedUnsupportedAttestation.__init__)r   r   r   r%   r,   r   r'   r)   r   TypedUnsupportedAttestation   s    rh   )r]   __subclasses__getattrUnsupportedAttestation)r'   clsrh   r   rg   r   for_type   s   zAttestation.for_typeN)r^   r_   r&   r   r`   ra   rF   r6   )r'   rc   rF   rd   )	r   r   r   r   abcabstractmethodrQ   staticmethodrm   r   r   r   r   r]      s    r]   c                   @  s   e Zd ZdddZdd ZdS )rk   Nc                 C  s
   || _ d S r+   rg   )r(   r'   r   r   r   r%      s   
zUnsupportedAttestation.__init__c                 C  s   t || jr+   )r#   r'   rb   r   r   r   rQ      s   zUnsupportedAttestation.verifyr+   )r   r   r   r%   rQ   r   r   r   r   rk      s    
rk   c                   @  s   e Zd ZdZdd ZdS )NoneAttestationnonec                 C  s   |i krt dttjg S )Nz*None Attestation requires empty statement.)r!   r6   r-   r5   rb   r   r   r   rQ      s   zNoneAttestation.verifyN)r   r   r   re   rQ   r   r   r   r   rq      s    rq   c                 C  sT   | j tjjkrtdz| jtj}|jj	rtdW d S  tj
y)   td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)rY   bcr   r   r   _validate_cert_common   s   r}   c                   C  s   dd t  D S )Nc                 S  s"   g | ]}t |d ddkr| qS )re   rr   )rj   )rI   rl   r   r   r   rK      s
    z)_default_attestations.<locals>.<listcomp>)r]   ri   r   r   r   r   _default_attestations   s   r~   c                   @  s>   e Zd ZdZddddZejdddZdddZd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Optional[Sequence[Attestation]]c                 C  s   |pt  | _d S r+   )r~   _attestation_types)r(   r   r   r   r   r%      rf   zAttestationVerifier.__init__attestation_resultr6   r&   r   rF   Optional[bytes]c                 C  s   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&   r   r   r   	ca_lookup   s   	zAttestationVerifier.ca_lookupattestation_objectr   r`   ra   rG   c              
   C  s   t |j}| jD ]}t|dd|jkr|} nq||j|j|}| ||j}|s.tdzt	|j
|g  W dS  tyI } zt|d}~ww )zVerify attestation.

        :param attestation_object: dict containing attestation data.
        :param client_data_hash: SHA256 hash of the ClientData bytes.
        re   NzNo root found for Authenticator)rk   r'   r   rj   rQ   att_stmtr&   r   r"   r\   r9   r   )r(   r   r`   att_verifieratresultrz   r@   r   r   r   verify_attestation   s(   

z&AttestationVerifier.verify_attestationc                 G  s   | j |  dS )z?Allows passing an instance to Fido2Server as verify_attestationN)r   )r(   r>   r   r   r   __call__  s   zAttestationVerifier.__call__r+   )r   r   )r   r6   r&   r   rF   r   )r   r   r`   ra   rF   rG   )
r   r   r   r   r%   rn   ro   r   r   r   r   r   r   r   r      s    

!r   )rE   r8   rF   rG   )/
__future__r   webauthnr   r   enumr   r   cryptographyr   cryptography.hazmat.backendsr	   )cryptography.hazmat.primitives.asymmetricr
   r   r   cryptography.exceptionsr   rW   dataclassesr   	functoolsr   typingr   r   r   r   r   r   rn   	Exceptionr   r!   r"   r#   r-   r6   rD   r\   ABCr]   rk   rq   r}   r~   r   r   r   r   r   <module>   s:    
"	