o
    sh#                     @  s   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
 ddlmZ ddlmZ d dlmZmZ d d	lmZ d d
lZeG dd deZG dd deZeddG dd deZeddG dd deZG dd dZd
S )    )annotations   )CTAPHID)
CtapDevice)websafe_encodewebsafe_decode	bytes2int
ByteBuffer)ES256)FidoU2FAttestation)IntEnumunique)	dataclassNc                   @  s   e Zd ZdZdZdZdZdS )APDUzAPDU response codes.i   ii  ij  N)__name__
__module____qualname____doc__OKUSE_NOT_SATISFIED
WRONG_DATA r   r   F/var/www/html/env_mimamsha/lib/python3.10/site-packages/fido2/ctap1.pyr   (   s
    r   c                   @  s$   e Zd ZdZddddZd	d
 ZdS )	ApduErrorzAn Exception thrown when a response APDU doesn't have an OK (0x9000)
    status.

    :param code: APDU response code.
    :param data: APDU response body.

        codeintdatabytesc                 C  s   || _ || _d S N)r   r   )selfr   r   r   r   r   __init__:   s   
zApduError.__init__c                 C  s   d| j ddt| jddS )NzAPDU error: 0x04X dz bytes of data)r   lenr   r    r   r   r   __repr__>   s   zApduError.__repr__N)r   )r   r   r   r   )r   r   r   r   r!   r'   r   r   r   r   r   1   s    r   F)initc                      sj   e Zd ZU dZded< ded< ded< ded< d fdd	ZedddZdddZe	dddZ
  ZS )RegistrationDataaw  Binary response data for a CTAP1 registration.

    :param _: The binary contents of the response data.
    :ivar public_key: Binary representation of the credential public key.
    :ivar key_handle: Binary key handle of the credential.
    :ivar certificate: Attestation certificate of the authenticator, DER
        encoded.
    :ivar signature: Attestation signature.
    r   
public_key
key_handlecertificate	signature_c                   s   t    t| }|ddkrtd|d| _||d| _|d}|d }|dkrA|d }||}||7 }t|}||| | _	| | _
d S )NB   zReserved byte != 0x05A      r      )superr!   r	   unpack
ValueErrorreadr*   r+   r   r,   r-   )r    r.   readercert_bufcert_lenn_bytes	len_bytes	__class__r   r   r!   S   s   


zRegistrationData.__init__returnstrc                 C     t | S )z6Websafe base64 encoded string of the RegistrationData.r   r&   r   r   r   b64g      zRegistrationData.b64	app_paramclient_paramNonec                 C  s    t ||| j| j| j| j dS )zVerify the included signature with regard to the given app and client
        params.

        :param app_param: SHA256 hash of the app ID used for the request.
        :param client_param: SHA256 hash of the ClientData used for the request.
        N)r   verify_signaturer+   r*   r,   r-   )r    rE   rF   r   r   r   verifyl   s   zRegistrationData.verifyr   c                 C     | t |S )zParse a RegistrationData from a websafe base64 encoded string.

        :param data: Websafe base64 encoded string.
        :return: The decoded and parsed RegistrationData.
        r   clsr   r   r   r   from_b64|      zRegistrationData.from_b64r.   r   r?   r@   )rE   r   rF   r   r?   rG   )r   r@   r?   r)   r   r   r   r   __annotations__r!   propertyrC   rI   classmethodrN   __classcell__r   r   r=   r   r)   B   s   
 

r)   c                      sb   e Zd ZU dZded< ded< ded< d fdd	ZedddZdddZe	dddZ
  ZS )SignatureDatazBinary response data for a CTAP1 authentication.

    :param _: The binary contents of the response data.
    :ivar user_presence: User presence byte.
    :ivar counter: Signature counter.
    :ivar signature: Cryptographic signature.
    r   user_presencecounterr   r-   r.   c                   s8   t    t| }|d| _|d| _| | _d S )Nr/   z>I)r4   r!   r	   r5   rX   rY   r7   r-   )r    r.   r8   r=   r   r   r!      s
   
zSignatureData.__init__r?   r@   c                 C  rA   )z8str: Websafe base64 encoded string of the SignatureData.rB   r&   r   r   r   rC      rD   zSignatureData.b64rE   rF   r*   rG   c                 C  s,   || dd  | }t ||| j dS )ae  Verify the included signature with regard to the given app and client
        params, using the given public key.

        :param app_param: SHA256 hash of the app ID used for the request.
        :param client_param: SHA256 hash of the ClientData used for the request.
        :param public_key: Binary representation of the credential public key.
        Nr0   )r
   
from_ctap1rI   r-   )r    rE   rF   r*   mr   r   r   rI      s   zSignatureData.verifyr   c                 C  rJ   )zParse a SignatureData from a websafe base64 encoded string.

        :param data: Websafe base64 encoded string.
        :return: The decoded and parsed SignatureData.
        rK   rL   r   r   r   rN      rO   zSignatureData.from_b64rP   rQ   )rE   r   rF   r   r*   r   r?   rG   )r   r@   r?   rW   rR   r   r   r=   r   rW      s   
 
rW   c                   @  s^   e Zd ZdZeG dd deZd$ddZ		d%d&ddZd'ddZ	d(ddZ
	d)d*d!d"Zd#S )+Ctap1zlImplementation of the CTAP1 specification.

    :param device: A CtapHidDevice handle supporting CTAP1.
    c                   @  s   e Zd ZdZdZdZdS )z	Ctap1.INSr   r2      N)r   r   r   REGISTERAUTHENTICATEVERSIONr   r   r   r   INS   s    ra   devicer   c                 C  s
   || _ d S r   )rb   )r    rb   r   r   r   r!      s   
zCtap1.__init__r   r   clar   insp1p2r   r   r?   c           	   
   C  sn   t d||||dt|| d }| jtj|}t d|dd d }|dd }|tj	kr5t
|||S )a\  Packs and sends an APDU for use in CTAP1 commands.
        This is a low-level method mainly used internally. Avoid calling it
        directly if possible, and use the get_version, register, and
        authenticate methods if possible instead.

        :param cla: The CLA parameter of the request.
        :param ins: The INS parameter of the request.
        :param p1: The P1 parameter of the request.
        :param p2: The P2 parameter of the request.
        :param data: The body of the request.
        :return: The response APDU data of a successful request.
        :raise: ApduError
        z>BBBBBHr   s     z>HN)structpackr%   rb   callr   MSGr5   r   r   r   )	r    rc   rd   re   rf   r   apduresponsestatusr   r   r   	send_apdu   s   "

zCtap1.send_apdur@   c                 C  s   | j tjjd S )zGet the U2F version implemented by the authenticator.
        The only version specified is "U2F_V2".

        :return: A U2F version string.
        )rd   )ro   r\   ra   r`   decoder&   r   r   r   get_version   s   zCtap1.get_versionrF   rE   r)   c                 C  s"   || }| j tjj|d}t|S )a  Register a new U2F credential.

        :param client_param: SHA256 hash of the ClientData used for the request.
        :param app_param: SHA256 hash of the app ID used for the request.
        :return: The registration response from the authenticator.
        )rd   r   )ro   r\   ra   r^   r)   )r    rF   rE   r   rm   r   r   r   register   s   zCtap1.registerFr+   
check_onlyboolrW   c                 C  sD   || t dt| | }|rdnd}| jtjj||d}t|S )a  Authenticate a previously registered credential.

        :param client_param: SHA256 hash of the ClientData used for the request.
        :param app_param: SHA256 hash of the app ID used for the request.
        :param key_handle: The binary key handle of the credential.
        :param check_only: True to send a "check-only" request, which is used to
            determine if a key handle is known.
        :return: The authentication response from the authenticator.
        z>B   r]   )rd   re   r   )rh   ri   r%   ro   r\   ra   r_   rW   )r    rF   rE   r+   rs   r   re   rm   r   r   r   authenticate   s
   zCtap1.authenticateN)rb   r   )r   r   r   r   r   )rc   r   rd   r   re   r   rf   r   r   r   r?   r   rQ   )rF   r   rE   r   r?   r)   )F)
rF   r   rE   r   r+   r   rs   rt   r?   rW   )r   r   r   r   r   r   ra   r!   ro   rq   rr   rv   r   r   r   r   r\      s    


r\   )
__future__r   hidr   ctapr   utilsr   r   r   r	   coser
   attestationr   enumr   r   dataclassesr   rh   r   	Exceptionr   r   r)   rW   r\   r   r   r   r   <module>   s"   C/