HEX
Server: LiteSpeed
System: Linux shams.tasjeel.ae 5.14.0-611.5.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 08:09:09 EST 2025 x86_64
User: infowars (1469)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: //lib/python3.9/site-packages/yubico/__pycache__/yubikey_usb_hid.cpython-39.pyc
a

O�b�X�@sLdZgd�ZddlmZddlmZddlmZddlmZddlmZddlm	Z	dd	lm
Z
dd
l	mZmZm
Z
ddl
mZdd
lZdd
lZdd
lZdd
lZdZdZdZdZdZdZdZdZdZejejd�ejejd�d�Z Gdd�dej!�Z"Gdd�de
j#�Z$Gdd�de%�Z&Gdd�de�Z'Gdd�de%�Z(Gd d!�d!ej)�Z*d
S)"z(
module for accessing a USB HID YubiKey
)�
YubiKeyUSBHID�YubiKeyUSBHIDError�YubiKeyUSBHIDStatus�)�__version__)�yubico_util)�yubico_exception)�
yubikey_frame)�yubikey_config)�yubikey_defs)�yubikey_base)�SLOT�
YUBICO_VID�PID)�YubiKey�N� ��	i����r���HMAC�OTPc@seZdZdZdS)rz= Exception raised for errors with the USB HID communication. N)�__name__�
__module__�__qualname__�__doc__�rr�:/usr/lib/python3.9/site-packages/yubico/yubikey_usb_hid.pyr4srcspeZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Z�ZS)�YubiKeyUSBHIDCapabilitiesz�
    Capture the capabilities of the various versions of YubiKeys.

    Overrides just the functions from YubiKeyCapabilities() that are available
    in one or more versions, leaving the other ones at False through default_answer.
    cstt|�j|||d�dS)N��model�version�default_answer)�superr!�__init__)�selfr#r$r%��	__class__rr r'?s

�z"YubiKeyUSBHIDCapabilities.__init__cCsdS)zG Yubico OTP support has always been available in the standard YubiKey. Tr�r(rrr �have_yubico_OTPEsz)YubiKeyUSBHIDCapabilities.have_yubico_OTPcCs|dvrdS|jdkS)z* OATH HOTP was introduced in YubiKey 2.2. )ZHOTPF�rrr�r$�r(�moderrr �	have_OATHIsz#YubiKeyUSBHIDCapabilities.have_OATHcCs|dvrdS|jdkS)z3 Challenge-response was introduced in YubiKey 2.2. rF�rrrr.r/rrr �have_challenge_responseOsz1YubiKeyUSBHIDCapabilities.have_challenge_responsecCs
|jdkS)zW Reading serial number was introduced in YubiKey 2.2, but depends on extflags set too. r2r.r+rrr �have_serial_numberUsz,YubiKeyUSBHIDCapabilities.have_serial_numbercCs|j|j|jd�S�N�r#r$�Z
is_compatibler#r$�r(�flagrrr �have_ticket_flagYsz*YubiKeyUSBHIDCapabilities.have_ticket_flagcCs|j|j|jd�Sr5r7r8rrr �have_config_flag\sz*YubiKeyUSBHIDCapabilities.have_config_flagcCs|j|j|jd�Sr5r7r8rrr �have_extended_flag_sz,YubiKeyUSBHIDCapabilities.have_extended_flagcCs
|jdkS�N)rrrr.r+rrr �have_extended_scan_code_modebsz6YubiKeyUSBHIDCapabilities.have_extended_scan_code_modecCs
|jdkSr=r.r+rrr �have_shifted_1_modeesz-YubiKeyUSBHIDCapabilities.have_shifted_1_modecCs|dvS)Nrr)r(�slotrrr �have_configuration_slothsz1YubiKeyUSBHIDCapabilities.have_configuration_slot)rrrrr'r,r1r3r4r:r;r<r>r?rA�
__classcell__rrr)r r!8sr!c@s�eZdZdZd'dd�Zdd�Zdd	�Zd
d�Zd(dd
�Zdd�Z	dd�Z
dd�Zd)dd�Zd*dd�Z
d+dd�Zd,dd�Zd-dd�Zd d!�Zd.d"d#�Zd/d%d&�ZdS)0�YubiKeyHIDDevicezP
    High-level wrapper for low-level HID commands for a HID based YubiKey.
    FrcCs*||_d|_|�|�std��|��dS)��
        Find and connect to a YubiKey (USB HID).

        Attributes :
            skip  -- number of YubiKeys to skip
            debug -- True or False
        Nz%YubiKey USB HID initialization failed)�debug�_usb_handle�_openr�status)r(rE�skiprrr r'qs

zYubiKeyHIDDevice.__init__cCs|��}t|�|_|jS�z*
        Poll YubiKey for status.
        )�_readr�_status�r(�datarrr rHs
zYubiKeyHIDDevice.statusc	Cs.z|jr|��Wnttfy(Yn0dS�N)rF�_close�IOError�AttributeErrorr+rrr �__del__�s
zYubiKeyHIDDevice.__del__cCs�|jj}|j|d�}|�dt�|j�|f�|�|�|�t	j
�|��|�d|||jjf�|j��}|s�|jjdkr�dS|jj|dkr�dSt
d||jjf��dS)z! Write configuration to YubiKey. )r@zWriting %s frame :
%s
z&Programmed slot %i, sequence %i -> %i
rNrz6YubiKey programming failed (seq %i not increased (%i)))rL�pgm_seqZto_frame�_debugr	Zcommand2str�command�_write�_waitfor_clearr
�SLOT_WRITE_FLAGrH�
valid_configsr)r(�cfgr@Zold_pgm_seq�frameZcfgsrrr �
_write_config�s"�


�zYubiKeyHIDDevice._write_configcCsn|�tj|�dd�}|��}t�|d�}|tj@rb|d@}|rL|dkrLqb||dd�7}qqbq|��|S)z7 Wait for a response to become available, and read it. N��r)�_waitfor_setr
ZRESP_PENDING_FLAGrKr�ord_byte�_write_reset)r(�	may_block�res�this�flags�seqrrr �_read_response�s
zYubiKeyHIDDevice._read_responsecCs�ttBtB}td>}|jj|tt|td�}t	|�tkrP|�
dt|f�td��d�dd�|D��}|�
dt
j|d	d
��|S)z1 Read a USB HID feature report from the YubiKey. r��value�timeoutz7Failed reading %i bytes (got %i) from USB HID YubiKey.
z#Failed reading from USB HID YubiKey�css|]}t�|�VqdSrO)rZchr_byte)�.0�crrr �	<genexpr>�rlz)YubiKeyHIDDevice._read.<locals>.<genexpr>z
READ  : %sT�Zcolorize)�_USB_TYPE_CLASS�_USB_RECIP_INTERFACE�_USB_ENDPOINT_IN�_REPORT_TYPE_FEATURErF�
controlMsg�_HID_GET_REPORT�_FEATURE_RPT_SIZE�_USB_TIMEOUT_MS�lenrUr�joinr�hexdump)r(�request_typerjZrecvrNrrr rK�s ��zYubiKeyHIDDevice._readcCsB|j|jd�D].}d}|jr$|\}}|�tj�|�||�qdS)zy
        Write a YubiKeyFrame to the USB HID.

        Includes polling for YubiKey readiness before each write.
        )rENT)Zto_feature_reportsrErXr
rY�
_raw_write)r(r\rN�	debug_strrrr rW�szYubiKeyHIDDevice._writecCsd}|�|�|�tj�dS)z;
        Reset read mode by issuing a dummy write.
        s�T)r}rXr
rYrMrrr rb�s
zYubiKeyHIDDevice._write_resetNcCs�|jr6|sd}tj|dd�dd�}|�d||f�ttBtB}td>}|jj	|t
||td�}|tkr�|�d	t|f�t
d
��|S)z(
        Write data to YubiKey.
        �TrpN���zWRITE : %s %s
rriz7Failed writing %i bytes (wrote %i) to USB HID YubiKey.
z!Failed talking to USB HID YubiKey)rErr{rUrqrr�_USB_ENDPOINT_OUTrtrFru�_HID_SET_REPORTrxrwr)r(rNr~r{r|rj�sentrrr r}�s&��zYubiKeyHIDDevice._raw_writecCs|�d||�S)z�
        Wait for the YubiKey to turn OFF the bits in 'mask' in status responses.

        Returns the 8 bytes last read.
        �nand��_waitfor�r(�maskrcrrr rX�szYubiKeyHIDDevice._waitfor_clearcCs|�d||�S)z�
        Wait for the YubiKey to turn ON the bits in 'mask' in status responses.

        Returns the 8 bytes last read.
        �andr�r�rrr r`�szYubiKeyHIDDevice._waitfor_setrc
Cs`d}d}|ddd}d}|�s\t�|�|��}	t�|	d�}
|
tj@r�|s�d}|
tj@}|�d|�|r�t	d	|�}|ddd}|d
kr�|
|@|ks�d}n|�dt
|
�|
t
|�|f�nD|dkr�|
|@|kr�d}n|�d
t
|
�|
t
|�|f�n
d�sJ�|�sV|d8}|dk�rF|d
k�r4d|}nd|}t�|��t	||d�}q|	SqdS)z�
        Wait for the YubiKey to either turn ON or OFF certain bits in the status byte.

        mode is either 'and' or 'nand'
        timeout is a number of seconds (precision about ~0.5 seconds)
        Fg{�G�z�?rr�r^Tz0Device indicates RESP_TIMEOUT (%i seconds left)
�r�z0Status %s (0x%x) has not cleared bits %s (0x%x)
r�z,Status %s (0x%x) has not set bits %s (0x%x)
rrz2Timed out waiting for YubiKey to clear status 0x%xz0Timed out waiting for YubiKey to set status 0x%xg�?N)
�time�sleeprKrrar
ZRESP_TIMEOUT_WAIT_FLAGZRESP_TIMEOUT_WAIT_MASKrU�min�binrZYubiKeyTimeout)
r(r0r�rcrk�finishedr�Zwait_numZresp_timeoutrerfZseconds_left�reasonrrr r�sP


�
��




zYubiKeyHIDDevice._waitforc
Cs�|�|�}|r*|jd}|jdd|_ntd��z|��|_|j�d�WnFty�}z.dt	|�vrt|�
d�n
|�
d�WYd}~n
d}~00z|j�d�Wntj
y�|�
d�Yn0|j�|j�d	S)
z Perform HID initialization rzNo USB YubiKey foundz-could not detach kernel driver from interfacez3The in-kernel-HID driver has already been detached
z"detachKernelDriver not supported!
Nrz)Unable to set configuration, ignoring...
T)�_get_usb_deviceZconfigurationsZ
interfaces�_usb_intr�openrFZdetachKernelDriver�	Exception�strrUZsetConfiguration�usbZUSBErrorZclaimInterface)r(rIZ
usb_deviceZusb_conf�errorrrr rG;s$


 zYubiKeyHIDDevice._opencCs:|j��z|jj�d�WnYn0d|_d|_dS)z2
        Release the USB interface again.
        rNT)rFZreleaseInterface�devZattach_kernel_driverr�r+rrr rPUs
zYubiKeyHIDDevice._closecs�z2ddl�ddl��fdd��jjdtd�D�}Wn,ty^ddl�dd����D�}Yn0|D]8}|jtkrd|j	t
jdd�vrd|dkr�|S|d	8}qddS)
z�
        Get YubiKey USB device.

        Optionally allows you to skip n devices, to support multiple attached YubiKeys.
        rNcsg|]}�j�|��qSr)�legacyZDevice)rm�d�r�rr �
<listcomp>nrlz4YubiKeyHIDDevice._get_usb_device.<locals>.<listcomp>T)Zfind_all�idVendorcSsg|]}|jD]}|�qqSr)�devices)rmZbusr�rrr r�srl)Zotpr)Zusb.coreZ
usb.legacyZcore�findr
�ImportErrorr�Zbussesr�Z	idProductr�all)r(rIr�Zdevicerr�r r�cs �

z YubiKeyHIDDevice._get_usb_deviceTcCsF|jrB|r6|jj}t|d�r&t|d�}tj�d|�tj�|�dS)z/ Print out to stderr, if debugging is enabled. Zdebug_prefixz%s: N)rEr*r�hasattr�getattr�sys�stderr�write)r(�outZprint_prefixZprerrr rU|s

zYubiKeyHIDDevice._debug)Fr)F)N)F)F)r)r)r)T)rrrrr'rHrSr]rhrKrWrbr}rXr`r�rGrPr�rUrrrr rCls"

	



7

rCcs�eZdZdZdZdZeZd"�fdd�	Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
d#dd�Zd$dd�Zdd�Zd%dd�Zdd�Zd d!�Z�ZS)&ra�
    Class for accessing a YubiKey over USB HID.

    This class is for communicating specifically with standard YubiKeys
    (USB vendor id = 0x1050, product id = 0x10) using USB HID.

    There is another class for the YubiKey NEO BETA, even though that
    product also goes by product id 0x10 for the BETA versions. The
    expectation is that the final YubiKey NEO will have it's own product id.

    Tested with YubiKey versions 1.3 and 2.2.
    rzYubiKey (or YubiKey NANO)FrNcsHtt|��|�|dur&t||�|_n||_|j|j|��dd�|_dS)rDNFr")	r&rr'rC�_device�_capabilities_clsr#�version_num�capabilities)r(rErIZ
hid_devicer)rr r'�s��zYubiKeyUSBHID.__init__cCsd|jjtt|��|��fS)Nz'<%s instance at %s: YubiKey version %s>)r*r�hex�idr$r+rrr �__repr__�s

�zYubiKeyUSBHID.__repr__cCsd|j|��fS)Nz%s (%s)r6r+rrr �__str__�szYubiKeyUSBHID.__str__cCs
|j��SrJ)r�rHr+rrr rH�szYubiKeyUSBHID.statuscCs|jj��S)z; Get the YubiKey version as a tuple (major, minor, build). )r�rL�ykverr+rrr r��szYubiKeyUSBHID.version_numcCs|jj��S)z Get the YubiKey version. )r�rLr$r+rrr r$�szYubiKeyUSBHID.versionTcCs&|j��st�d|����|�|�S)z7 Get the YubiKey serial number (requires YubiKey 2.2). z'Serial number unsupported in YubiKey %s)r�r4r�YubiKeyVersionErrorr$�_read_serial)r(rcrrr �serial�s
zYubiKeyUSBHID.serialrrcCs4|j�|�s"t�d||��f��|�|||||�S)zR Issue a challenge to the YubiKey and return the response (requires YubiKey 2.2). z/%s challenge-response unsupported in YubiKey %s)r�r3rr�r$�_challenge_response)r(�	challenger0r@�variablercrrr �challenge_response�sz YubiKeyUSBHID.challenge_responsecKstf|��|jd�|��S)z6 Get a configuration object for this type of YubiKey. �r�r�)�YubiKeyConfigUSBHIDr�r�)r(�kwrrr �init_config�s

��zYubiKeyUSBHID.init_configcCsZ|��}||��kr4t�d|d|d|��f��|j�|�sLtd|��|j�	||�S)z' Write a configuration to the YubiKey. z9Configuration requires YubiKey version %i.%i (this is %s)rrz$Can't write configuration to slot %i)
Zversion_requiredr�rr�r$r�rArr�r])r(r[r@Zcfg_req_verrrr �write_config�s�zYubiKeyUSBHID.write_configcCsVtjtjd�}|j�|�|jj|d�}t�|dd��sBt	d��t
�d|�}|dS)z. Read the serial number from a YubiKey > 2.2. )rV�rcNr��!Read from device failed CRC checkz>lxxxr)r�YubiKeyFramerZ
DEVICE_SERIALr�rWrhr�validate_crc16r�struct�unpack)r(rcr\�responser�rrr r��szYubiKeyUSBHID._read_serialcCs>|dkrnt|�tjkr.t�dtjt|�f��t|�tjkrfd}|rX|dd�|krXd}|�tj|�}tj}nP|dkr�t|�tjkr�t�dtjt|�f��|�tjd�}d	}nt�d
|��zt||}Wnt�d|��Yn0t	j
||d�}	|j�|	�|jj
|d
�}
t�|
d|d���s2td��|
d|�S)z- Do challenge-response with a YubiKey > 2.0. rz#Mode HMAC challenge too big (%i/%i)�r�N��rz,Mode OTP challenge must be %i bytes (got %i)�z9Invalid mode supplied (%s, valid values are HMAC and OTP)zInvalid slot specified (%s))rVZpayloadr�rr�)ryr
ZSHA1_MAX_BLOCK_SIZErZ
InputError�ljustZSHA1_DIGEST_SIZEZUID_SIZE�_CMD_CHALLENGErr�r�rWrhrr�r)r(r�r0r@r�rcZpad_withZresponse_lenrVr\r�rrr r��s>���z!YubiKeyUSBHID._challenge_response)FrN)T)rrTT)r)rrrrr#�descriptionr!r�r'r�r�rHr�r$r�r�r�r�r�r�rBrrr)r r�s 




rc@s@eZdZdZdZdZdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dS)rzD Class to represent the status information we get from the YubiKey. rrcCs,d}t�||�\|_|_|_|_|_|_dS)Nz<x BBB B H B)r�r��
version_major�
version_minor�
version_buildrT�touch_levelrf)r(rNZfmtrrr r's

��zYubiKeyUSBHIDStatus.__init__cCsZd}d}|��dkr d|��}|jr0d|j}d|jjtt|��|��|j|j	||fS)Nrr-z
, valid=%sz
 (flags 0x%x)zG<%s instance at %s: YubiKey version %s, pgm_seq=%i, touch_level=%i%s%s>)
r�rZrfr*rr�r�r$rTr�)r(Z	valid_strZ	flags_strrrr r�'s

�zYubiKeyUSBHIDStatus.__repr__cCs|j|j|jfS)zQ Returns a tuple with the (major, minor, build) version of the YubiKey firmware. )r�r�r�r+rrr r�9szYubiKeyUSBHIDStatus.ykvercCsd|��}|S)z2 Return the YubiKey firmware version as a string. z%d.%d.%d)r�)r(r$rrr r$=szYubiKeyUSBHIDStatus.versioncCs\|��dkrtd|����g}|j|j@|jkr<|�d�|j|j@|jkrX|�d�|S)zL Return a list of slots having a valid configurtion. Requires firmware 2.1. r-z(Valid configs unsupported in firmware %srr)r�rr$r��
CONFIG1_VALID�append�
CONFIG2_VALID)r(rdrrr rZBs

z!YubiKeyUSBHIDStatus.valid_configsN)rrrrr�r�r'r�r�r$rZrrrr rsrcs"eZdZdZd�fdd�	Z�ZS)r�z3
    Configuration class for USB HID YubiKeys.
    Ncs tt|�jf||d�|��dS)Nr�)r&r�r')r(r�r�r�r)rr r'RszYubiKeyConfigUSBHID.__init__)N)rrrrr'rBrrr)r r�Nsr�)+r�__all__Zyubico_versionrrrrrr	r
rrr
rrr�r�r�r�rqrrrsr�rvr�rxrwrtZ
CHAL_HMAC1Z
CHAL_HMAC2Z	CHAL_OTP1Z	CHAL_OTP2r�ZYubicoErrorrZYubiKeyCapabilitiesr!�objectrCrrZ
YubiKeyConfigr�rrrr �<module>sF	�4	?