
    <[j#                    `   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 ddlmZ dd	lmZ dd
lmZmZmZmZ e G d de                      Z G d de          Z ed           G d de                      Z ed           G d de                      Z G d d          ZdS )    )annotationsN)	dataclass)IntEnumunique   )FidoU2FAttestation)ES256)
CtapDevice)CTAPHID)
ByteBuffer	bytes2intwebsafe_decodewebsafe_encodec                      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     S/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fido2/ctap1.pyr   r   )   s$        	BJJJr   r   c                  "    e Zd ZdZd
ddZ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.

    r   codeintdatabytesc                "    || _         || _        d S N)r   r    )selfr   r    s      r   __init__zApduError.__init__;   s    				r   c                F    d| j         ddt          | j                  ddS )NzAPDU error: 0x04X dz bytes of data)r   lenr    r$   s    r   __repr__zApduError.__repr__?   s+    P	PPPDIPPPPPr   N)r   )r   r   r    r!   )r   r   r   r   r%   r,   r   r   r   r   r   2   sK             Q Q Q Q Qr   r   F)initc                       e Zd ZU dZded<   ded<   ded<   ded<   d fdZedd            ZddZe	dd            Z
 x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                R   t                                                       t          |           }|                    d          dk    rt	          d          |                    d          | _        |                    |                    d                    | _        |                    d          }|d         }|dk    r.|dz
  }|                    |          }||z  }t          |          }||                    |          z   | _	        |                                | _
        d S )NB   zReserved byte != 0x05A      r      )superr%   r   unpack
ValueErrorreadr0   r1   r   r2   r3   )r$   r4   readercert_bufcert_lenn_bytes	len_bytes	__class__s          r   r%   zRegistrationData.__init__T   s    D!!==%%4555 ++b// ++fmmC&8&899;;q>>A;d??oGG,,I	!H ++H#fkk(&;&;;r   returnstrc                     t          |           S )z6Websafe base64 encoded string of the RegistrationData.r   r+   s    r   b64zRegistrationData.b64h        d###r   	app_paramclient_paramNonec                `    t          j        ||| 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_signaturer1   r0   r2   r3   )r$   rK   rL   s      r   verifyzRegistrationData.verifym   s>     	+OON	
 	
 	
 	
 	
r   r    c                2     | 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    s     r   from_b64zRegistrationData.from_b64}        s>$''(((r   r4   r!   rE   rF   )rK   r!   rL   r!   rE   rM   )r    rF   rE   r/   r   r   r   r   __annotations__r%   propertyrI   rP   classmethodrU   __classcell__rD   s   @r   r/   r/   C   s           ' ' ' ' ' '( $ $ $ X$
 
 
 
  ) ) ) [) ) ) ) )r   r/   c                  |     e Zd ZU dZded<   ded<   ded<   d fdZedd            ZddZe	dd            Z
 x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!   r3   r4   c                    t                                                       t          |           }|                    d          | _        |                    d          | _        |                                | _        d S )Nr6   z>I)r;   r%   r   r<   ra   rb   r>   r3   )r$   r4   r?   rD   s      r   r%   zSignatureData.__init__   s`    D!!#]]3//}}T**r   rE   rF   c                     t          |           S )z8str: Websafe base64 encoded string of the SignatureData.rH   r+   s    r   rI   zSignatureData.b64   rJ   r   rK   rL   r0   rM   c                    || dd         z   |z   }t          j        |                              || 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.
        Nr7   )r	   
from_ctap1rP   r3   )r$   rK   rL   r0   ms        r   rP   zSignatureData.verify   sD     RaR </$$++At~>>>>>r   r    c                2     | t          |                    S )zParse a SignatureData from a websafe base64 encoded string.

        :param data: Websafe base64 encoded string.
        :return: The decoded and parsed SignatureData.
        rR   rS   s     r   rU   zSignatureData.from_b64   rV   r   rW   rX   )rK   r!   rL   r!   r0   r!   rE   rM   )r    rF   rE   r`   rY   r^   s   @r   r`   r`      s           LLL' ' ' ' ' ' $ $ $ X$	? 	? 	? 	? ) ) ) [) ) ) ) )r   r`   c                  n    e Zd ZdZe G d de                      ZddZ	 d d!dZd"dZ	d#dZ
	 d$d%dZdS )&Ctap1zlImplementation of the CTAP1 specification.

    :param device: A CtapHidDevice handle supporting CTAP1.
    c                      e Zd ZdZdZdZdS )	Ctap1.INSr   r9      N)r   r   r   REGISTERAUTHENTICATEVERSIONr   r   r   INSrl      s        r   rq   devicer
   c                    || _         d S r#   )rr   )r$   rr   s     r   r%   zCtap1.__init__   s    r   r   r   clar   insp1p2r    r!   rE   c                D   t          j        d||||dt          |                    |z   dz   }| j                            t
          j        |          }t          j        d|dd                   d         }|dd         }|t          j	        k    rt          ||          |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*   rr   callr   MSGr<   r   r   r   )	r$   rt   ru   rv   rw   r    apduresponsestatuss	            r   	send_apduzCtap1.send_apdu   s      {9c3B3t99EELwV;##GK66tXbcc]33A6}TWFD)))r   rF   c                p    |                      t          j        j                                                  S )zGet the U2F version implemented by the authenticator.
        The only version specified is "U2F_V2".

        :return: A U2F version string.
        )ru   )r   rj   rq   rp   decoder+   s    r   get_versionzCtap1.get_version   s)     ~~%)"3~44;;===r   rL   rK   r/   c                v    ||z   }|                      t          j        j        |          }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.
        )ru   r    )r   rj   rq   rn   r/   )r$   rL   rK   r    r   s        r   registerzCtap1.register   s6     i'>>ei&8t>DD)))r   Fr1   
check_onlyboolr`   c                    ||z   t          j        dt          |                    z   |z   }|rdnd}|                     t          j        j        ||          }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   rm   )ru   rv   r    )rz   r{   r*   r   rj   rq   ro   r`   )r$   rL   rK   r1   r   r    rv   r   s           r   authenticatezCtap1.authenticate   sg    " 9$v{4Z'I'IIJV 	  )TTT>>ei&<$>OOX&&&r   N)rr   r
   )r   r   r   r   r   )rt   r   ru   r   rv   r   rw   r   r    r!   rE   r!   rX   )rL   r!   rK   r!   rE   r/   )F)
rL   r!   rK   r!   r1   r!   r   r   rE   r`   )r   r   r   r   r   r   rq   r%   r   r   r   r   r   r   r   rj   rj      s         
     g   V
    SV    2> > > >	* 	* 	* 	*  !' ' ' ' ' ' 'r   rj   )
__future__r   rz   dataclassesr   enumr   r   attestationr   coser	   ctapr
   hidr   utilsr   r   r   r   r   	Exceptionr   r!   r/   r`   rj   r   r   r   <module>r      s  8 # " " " " "  ! ! ! ! ! !                 + + + + + +                   H H H H H H H H H H H H     7   Q Q Q Q Q	 Q Q Q" @) @) @) @) @)u @) @) @)F ,) ,) ,) ,) ,)E ,) ,) ,)^P' P' P' P' P' P' P' P' P' P'r   