HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //proc/self/root/usr/lib/python3.6/site-packages/yubico/__pycache__/yubikey_usb_hid.cpython-36.pyc
3

y@�V�X�@sNdZdddgZddlmZddlmZddlmZddlmZdd	lmZdd
lm	Z	ddlm
Z
ddl	mZmZm
Z
dd
l
mZddlZddlZddlZddlZd!ZdZdZdZdZdZdZdZdZejejd�ejejd�d�Z Gdd�dej!�Z"Gdd�de
j#�Z$Gdd�de%�Z&Gdd�de�Z'Gdd�de%�Z(Gdd �d ej)�Z*dS)"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.6/yubikey_usb_hid.pyr4scspeZdZdZ�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__rrr$?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|dkrdS|jdkS)z* OATH HOTP was introduced in YubiKey 2.2. �HOTPFrrr)r()rrr)r!)r%�moderrr�	have_OATHIsz#YubiKeyUSBHIDCapabilities.have_OATHcCs|dkrdS|jdkS)z3 Challenge-response was introduced in YubiKey 2.2. rrFrr)rr)rrr)r!)r%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. rr)rrr)r!)r%rrr�have_serial_numberUsz,YubiKeyUSBHIDCapabilities.have_serial_numbercCs|j|j|jd�S)N)r r!)�
is_compatibler r!)r%�flagrrr�have_ticket_flagYsz*YubiKeyUSBHIDCapabilities.have_ticket_flagcCs|j|j|jd�S)N)r r!)r-r r!)r%r.rrr�have_config_flag\sz*YubiKeyUSBHIDCapabilities.have_config_flagcCs|j|j|jd�S)N)r r!)r-r r!)r%r.rrr�have_extended_flag_sz,YubiKeyUSBHIDCapabilities.have_extended_flagcCs
|jdkS)Nrr)rrr)r!)r%rrr�have_extended_scan_code_modebsz6YubiKeyUSBHIDCapabilities.have_extended_scan_code_modecCs
|jdkS)Nrr)rrr)r!)r%rrr�have_shifted_1_modeesz-YubiKeyUSBHIDCapabilities.have_shifted_1_modecCs|dkS)Nrr)rrr)r%�slotrrr�have_configuration_slothsz1YubiKeyUSBHIDCapabilities.have_configuration_slot)rrrrr$r'r*r+r,r/r0r1r2r3r5�
__classcell__rr)r&rr8src@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|_|j|�std��|j�dS)z�
        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%r8�skiprrrr$qs

zYubiKeyHIDDevice.__init__cCs|j�}t|�|_|jS)z*
        Poll YubiKey for status.
        )�_readr�_status)r%�datarrrr;s
zYubiKeyHIDDevice.statuscCs,y|jr|j�Wntk
r&YnXdS)N)r9�_close�IOError)r%rrr�__del__�s
zYubiKeyHIDDevice.__del__cCs�|jj}|j|d�}|jdtj|j�|f�|j|�|jt	j
�|j�|jd|||jjf�|jj�}|r�|jjdkr�dS|jj|dkr�dSt
d||jjf��dS)z! Write configuration to YubiKey. )r4zWriting %s frame :
%s
z&Programmed slot %i, sequence %i -> %i
rNrz6YubiKey programming failed (seq %i not increased (%i)))r>�pgm_seqZto_frame�_debugr	Zcommand2str�command�_write�_waitfor_clearr
�SLOT_WRITE_FLAGr;�
valid_configsr)r%�cfgr4Zold_pgm_seq�frameZcfgsrrr�
_write_config�s

zYubiKeyHIDDevice._write_configcCsr|jtj|�dd�}xN|j�}tj|d�}|tj@r`|d@}|rN|dkrNP||dd�7}qPqW|j�|S)z7 Wait for a response to become available, and read it. N��r)�_waitfor_setr
ZRESP_PENDING_FLAGr=r�ord_byte�_write_reset)r%�	may_block�res�this�flags�seqrrr�_read_response�s
zYubiKeyHIDDevice._read_responsecCs�ttBtB}td>}|jj|tt|td�}t	|�tkrP|j
dt|f�td��djdd�|D��}|j
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|]}tj|�VqdS)N)rZchr_byte)�.0�crrr�	<genexpr>�sz)YubiKeyHIDDevice._read.<locals>.<genexpr>z
READ  : %sT)�colorize)�_USB_TYPE_CLASS�_USB_RECIP_INTERFACE�_USB_ENDPOINT_IN�_REPORT_TYPE_FEATUREr9�
controlMsg�_HID_GET_REPORT�_FEATURE_RPT_SIZE�_USB_TIMEOUT_MS�lenrDr�joinr�hexdump)r%�request_typerXZrecvr?rrrr=�szYubiKeyHIDDevice._readcCsFx@|j|jd�D].}d}|jr&|\}}|jtj�|j||�qWdS)zy
        Write a YubiKeyFrame to the USB HID.

        Includes polling for YubiKey readiness before each write.
        )r8NT)Zto_feature_reportsr8rGr
rH�
_raw_write)r%rKr?�	debug_strrrrrF�szYubiKeyHIDDevice._writecCsd}|j|�|jtj�dS)z;
        Reset read mode by issuing a dummy write.
        s�T)rkrGr
rH)r%r?rrrrQ�s
zYubiKeyHIDDevice._write_resetNcCs�|jr6|sd}tj|dd�dd�}|jd||f�ttBtB}td>}|jj	|t
||td�}|tkr�|jd	t|f�t
d
��|S)z(
        Write data to YubiKey.
        �T)r^NrzWRITE : %s %s
r)rXrYz7Failed writing %i bytes (wrote %i) to USB HID YubiKey.
z!Failed talking to USB HID YubiKey���)r8rrirDr_r`�_USB_ENDPOINT_OUTrbr9rc�_HID_SET_REPORTrfrer)r%r?rlrirjrXZsentrrrrk�s"zYubiKeyHIDDevice._raw_writecCs|jd||�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%�maskrRrrrrG�szYubiKeyHIDDevice._waitfor_clearcCs|jd||�S)z�
        Wait for the YubiKey to turn ON the bits in 'mask' in status responses.

        Returns the 8 bytes last read.
        �and)rr)r%rsrRrrrrO�szYubiKeyHIDDevice._waitfor_setrc
Cshd}d}|ddd}d}�xD|�sbtj|�|j�}	tj|	d�}
|
tj@r�|s�d}|
tj@}|jd|�|r�t	d	|�}|ddd}|d
kr�|
|@|ks�d}n|jdt
|
�|
t
|�|f�nF|dk�r|
|@|kr�d}n|jd
t
|
�|
t
|�|f�n
f�st�|�s\|d8}|dk�rL|d
k�r:d|}nd|}tj
|��t	||d�}q |	Sq WdS)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�rMTz0Device indicates RESP_TIMEOUT (%i seconds left)
�rqz0Status %s (0x%x) has not cleared bits %s (0x%x)
rtz,Status %s (0x%x) has not set bits %s (0x%x)
rz2Timed out waiting for YubiKey to clear status 0x%xz0Timed out waiting for YubiKey to set status 0x%xg�?N)�time�sleepr=rrPr
ZRESP_TIMEOUT_WAIT_FLAGZRESP_TIMEOUT_WAIT_MASKrD�min�bin�AssertionErrorrZYubiKeyTimeout)
r%r)rsrRrYZfinishedrxZwait_numZresp_timeoutrTrUZseconds_left�reasonrrrrrsJ










zYubiKeyHIDDevice._waitforcCs�|j|�}|r*|jd}|jdd|_ntd��y|j�|_|jjd�WnDtk
r�}z(dt	|�krv|j
d�n
|j
d�WYdd}~XnXy|jjd�Wn tj
k
r�|j
d�YnX|jj|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�openr9ZdetachKernelDriver�	Exception�strrDZsetConfiguration�usbZUSBErrorZclaimInterface)r%r<Z
usb_deviceZusb_conf�errorrrrr:;s$


zYubiKeyHIDDevice._openc	Cs:|jj�y|jjjd�WnYnXd|_d|_dS)z2
        Release the USB interface again.
        rNT)r9ZreleaseInterfaceZdevZattach_kernel_driverr~)r%rrrr@Us
zYubiKeyHIDDevice._closec
s�y2ddl�ddl��fdd��jjdtd�D�}Wn.tk
r`ddl�dd��j�D�}YnXx<|D]4}|jtkrh|j	t
jdd�krh|dkr�|S|d	8}qhWdS)
z�
        Get YubiKey USB device.

        Optionally allows you to skip n devices, to support multiple attached YubiKeys.
        rNcsg|]}�jj|��qSr)�legacyZDevice)r[�d)r�rr�
<listcomp>nsz4YubiKeyHIDDevice._get_usb_device.<locals>.<listcomp>T)Zfind_all�idVendorcSsg|]}|jD]}|�qqSr)�devices)r[Zbusr�rrrr�ss)Zotpr)Zusb.coreZ
usb.legacyZcore�findr
�ImportErrorr�Zbussesr�Z	idProductr�all)r%r<r�Zdevicer)r�rr}cs

z YubiKeyHIDDevice._get_usb_deviceTcCsF|jrB|r6|jj}t|d�r&t|d�}tjjd|�tjj|�dS)z/ Print out to stderr, if debugging is enabled. Zdebug_prefixz%s: N)r8r&r�hasattr�getattr�sys�stderr�write)r%�outZprint_prefixZprerrrrD|s

zYubiKeyHIDDevice._debug)Fr)F)N)F)F)r)r)r)T)rrrrr$r;rBrLrWr=rFrQrkrGrOrrr:r@r}rDrrrrr7ls"

	



7

r7cs�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|�j|�|dkr&t||�|_n||_|j|j|j�dd�|_dS)z�
        Find and connect to a YubiKey (USB HID).

        Attributes :
            skip  -- number of YubiKeys to skip
            debug -- True or False
        NF)r r!r")	r#rr$r7�_device�_capabilities_clsr �version_num�capabilities)r%r8r<Z
hid_device)r&rrr$�szYubiKeyUSBHID.__init__cCsd|jjtt|��|j�fS)Nz'<%s instance at %s: YubiKey version %s>)r&r�hex�idr!)r%rrr�__repr__�s
zYubiKeyUSBHID.__repr__cCsd|j|j�fS)Nz%s (%s))r r!)r%rrr�__str__�szYubiKeyUSBHID.__str__cCs
|jj�S)z*
        Poll YubiKey for status.
        )r�r;)r%rrrr;�szYubiKeyUSBHID.statuscCs|jjj�S)z; Get the YubiKey version as a tuple (major, minor, build). )r�r>�ykver)r%rrrr��szYubiKeyUSBHID.version_numcCs|jjj�S)z Get the YubiKey version. )r�r>r!)r%rrrr!�szYubiKeyUSBHID.versionTcCs&|jj�stjd|j���|j|�S)z7 Get the YubiKey serial number (requires YubiKey 2.2). z'Serial number unsupported in YubiKey %s)r�r,r�YubiKeyVersionErrorr!�_read_serial)r%rRrrr�serial�s
zYubiKeyUSBHID.serialrrcCs4|jj|�s"tjd||j�f��|j|||||�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�r+rr�r!�_challenge_response)r%�	challenger)r4�variablerRrrr�challenge_response�sz YubiKeyUSBHID.challenge_responsecKstf|j�|jd�|��S)z6 Get a configuration object for this type of YubiKey. )r�r�)�YubiKeyConfigUSBHIDr�r�)r%�kwrrr�init_config�s
zYubiKeyUSBHID.init_configcCsZ|j�}||j�kr4tjd|d|d|j�f��|jj|�sLtd|��|jj	||�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�r5rr�rL)r%rJr4Zcfg_req_verrrr�write_config�szYubiKeyUSBHID.write_configcCsVtjtjd�}|jj|�|jj|d�}tj|dd��sBt	d��t
jd|�}|dS)z. Read the serial number from a YubiKey > 2.2. )rE)rRNruz!Read from device failed CRC checkz>lxxxr)r�YubiKeyFramerZ
DEVICE_SERIALr�rFrWr�validate_crc16r�struct�unpack)r%rRrK�responser�rrrr��szYubiKeyUSBHID._read_serialcCs>|dkrnt|�tjkr.tjdtjt|�f��t|�tjkrfd}|rX|dd�|krXd}|jtj|�}tj}nP|dkr�t|�tjkr�tjdtjt|�f��|jtjd�}d	}ntjd
|��yt||}Wntjd|��YnXt	j
||d�}	|jj|	�|jj
|d
�}
tj|
d|d���s2td��|
d|�S)z- Do challenge-response with a YubiKey > 2.0. rz#Mode HMAC challenge too big (%i/%i)�rN��rz,Mode OTP challenge must be %i bytes (got %i)�z9Invalid mode supplied (%s, valid values are HMAC and OTP)zInvalid slot specified (%s))rEZpayload)rRrz!Read from device failed CRC checkrn)rgr
ZSHA1_MAX_BLOCK_SIZErZ
InputError�ljustZSHA1_DIGEST_SIZEZUID_SIZE�_CMD_CHALLENGErr�r�rFrWrr�r)r%r�r)r4r�rRZpad_withZresponse_lenrErKr�rrrr��s8z!YubiKeyUSBHID._challenge_response)FrN)T)rrTT)r)rrrrr �descriptionrr�r$r�r�r;r�r!r�r�r�r�r�r�r6rr)r&rr�s 



c@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}tj||�\|_|_|_|_|_|_dS)Nz<x BBB B H B)r�r��
version_major�
version_minor�
version_buildrC�touch_levelrU)r%r?Zfmtrrrr$s
zYubiKeyUSBHIDStatus.__init__cCsZd}d}|j�dkr d|j�}|jr0d|j}d|jjtt|��|j�|j|j	||fS)	Nrmrrrz
, valid=%sz
 (flags 0x%x)zG<%s instance at %s: YubiKey version %s, pgm_seq=%i, touch_level=%i%s%s>)rrr)
r�rIrUr&rr�r�r!rCr�)r%Z	valid_strZ	flags_strrrrr�'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%rrrr�9szYubiKeyUSBHIDStatus.ykvercCsd|j�}|S)z2 Return the YubiKey firmware version as a string. z%d.%d.%d)r�)r%r!rrrr!=szYubiKeyUSBHIDStatus.versioncCs\|j�dkrtd|j���g}|j|j@|jkr<|jd�|j|j@|jkrX|jd�|S)zL Return a list of slots having a valid configurtion. Requires firmware 2.1. rrrz(Valid configs unsupported in firmware %s)rrr)r�rr!r��
CONFIG1_VALID�append�
CONFIG2_VALID)r%rSrrrrIBs

z!YubiKeyUSBHIDStatus.valid_configsN)rrrrr�r�r$r�r�r!rIrrrrrscs"eZdZdZd�fdd�	Z�ZS)r�z3
    Configuration class for USB HID YubiKeys.
    Ncs tt|�jf||d�|��dS)N)r�r�)r#r�r$)r%r�r�r�)r&rrr$RszYubiKeyConfigUSBHID.__init__)N)rrrrr$r6rr)r&rr�Nsr�� )+r�__all__Zyubico_versionrrmrrrr	r
rrr
rrr�rwr�r�r_r`rarordrprfrerbZ
CHAL_HMAC1Z
CHAL_HMAC2Z	CHAL_OTP1Z	CHAL_OTP2r�ZYubicoErrorrZYubiKeyCapabilitiesr�objectr7rrZ
YubiKeyConfigr�rrrr�<module>sH	4	?