o
    _1                     @   s  d Z ddlZddlmZ ddlZddlmZ ddlmZ ejj	Z
ejjZejjZG dd dejZe d dkr<d	Zne d d
krGdZnede d  G dd dejZG dd dejZG dd dejZG dd dejZejZejZejZdZejZdZdZ dZ!dZ"dZ#dZ$dZ%de
j&_'e(ege
j&_)ej*e
j+_'ee(ege
j+_)ej*e
j,_'ee(ege
j,_)ej*e
j-_'ege
j-_)ej*e
j._'eejej/ge
j._)ee
j0_'ee(ege
j0_)e(eej1ej2ej3gej4_)eej4_'ej5ej6_'eeje(eej3e(egej6_)ej5ej7_'ee(ee(eej3e(ej3ejgej7_)eej8_'ej1ej3ej3ejej3ej3egej8_)ej5ej9_'egej9_)ej5ej:_'eejej3e(ej3ejgej:_)ej5ej;_'eejej3e(ej3ejgej;_)dd Z<dd Z=d%d!d"Z>G d#d$ d$ej?Z@dS )&z3Implements raw HID device communication on Windows.    N)wintypes)errors)basec                   @   s4   e Zd Zdejfdejfdejfdejd fgZdS )GUIDZData1ZData2ZData3ZData4   N)__name__
__module____qualname__ctypesc_ulongc_ushortZc_ubyte_fields_ r   r   1lib/python3.10/site-packages/pyu2f/hid/windows.pyr   !   s    r   Z64bitr   Z32bit   zUnknown architecture: %sc                   @   s8   e Zd ZdejfdefdejfdeejfgZ	e
ZdS )DeviceInterfaceDatacbSizeZInterfaceClassGuidZFlagsReservedN)r   r   r	   r   DWORDr   r
   POINTERr   r   SETUPAPI_PACK_pack_r   r   r   r   r   4   s    r   c                   @   s(   e Zd Zdejfdejd fgZeZ	dS )DeviceInterfaceDetailDatar   
DevicePathr   N)
r   r   r	   r   r   r
   Zc_byter   r   r   r   r   r   r   r   <   s
    r   c                   @   s0   e Zd ZdejfdejfdejfdejfgZdS )HidAttributesZSizeVendorID	ProductIDZVersionNumberN)r   r   r	   r
   r   r   r   r   r   r   r   r   B   s    r   c                	   @   sP   e Zd Zdejfdejfdejfdejfdejfdejd fdejd	 fgZd
S )HidCapabilitiesUsage	UsagePageInputReportByteLengthOutputReportByteLengthZFeatureReportByteLengthr      ZNotUsed
   N)r   r   r	   r
   r   r   r   r   r   r   r   I   s    r   l    i            i   @l        c                 C   sh   t  }t| t|}|st td}t| |d}|s$t |j|_	|j
|_t||_dS )a^  Fill out the attributes of the device.

  Fills the devices HidAttributes and product string
  into the descriptor.

  Args:
    device: A handle to the open device
    descriptor: The DeviceDescriptor to populate with the
      attributes.

  Returns:
    None

  Raises:
    WindowsError when unable to obtain attributes or product
      string.
  i   N)r   hidHidD_GetAttributesr
   byrefWinErrorcreate_string_bufferHidD_GetProductStringr   Z	vendor_idr   Z
product_idZ
wstring_atZproduct_string)device
descriptorZ
attributesresultbufr   r   r   FillDeviceAttributes   s   
r1   c              	   C   s   t d}t| t|}|st z,t }t|t|}|tkr(t |j	|_
|j|_|j|_|j|_W t| dS t| w )a-  Fill out device capabilities.

  Fills the HidCapabilitites of the device into descriptor.

  Args:
    device: A handle to the open device
    descriptor: DeviceDescriptor to populate with the
      capabilities

  Returns:
    none

  Raises:
    WindowsError when unable to obtain capabilitites.
  r   N)PHIDP_PREPARSED_DATAr'   HidD_GetPreparsedDatar
   r)   r*   r   HidP_GetCapsHIDP_STATUS_SUCCESSr   usager   Z
usage_pager    internal_max_in_report_lenr!   internal_max_out_report_lenHidD_FreePreparsedData)r-   r.   Zpreparsed_dataretZcapsr   r   r   FillDeviceCapabilities   s   
r;   Fc              	   C   sB   t tB }ttB }|rd}t| ||dtdd}|tkrt	 |S )z*Open the device and return a handle to it.r   N)
GENERIC_WRITEGENERIC_READFILE_SHARE_READFILE_SHARE_WRITEkernel32CreateFileAOPEN_EXISTINGINVALID_HANDLE_VALUEr
   r*   )pathenumZdesired_accessZ
share_modehr   r   r   
OpenDevice   s   rG   c                   @   sL   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd ZdS )WindowsHidDevicez/Implementation of raw HID interface on Windows.c               
   C   s  t  } tt|  tt| ddd}d}t }tt|_	g }	 t
|dt| |t|}|d7 }|s<	 |S t }t|t|ddt|d}|j}|dkrXq#t|}t|}tt|_	t|t|t||dd}|s~t t }	|ttj }
tt|j|
|	_d}zt|	jd}W n ty } z|jtkrW Y d}~q#|d}~ww zt||	 t||	 ||	   W t!"| nt!"| w q$)See base class.N   r   Tr   )#r   r'   HidD_GetHidGuidr
   r)   setupapiSetupDiGetClassDevsAr   Zsizeofr   SetupDiEnumDeviceInterfacesr   r    SetupDiGetDeviceInterfaceDetailAvaluer+   r   Zfrom_bufferr*   r   DeviceDescriptorZ	string_atZ	addressofr   rD   rG   ZWindowsErrorwinerrorERROR_ACCESS_DENIEDr1   r;   appendZToPublicDictr@   CloseHandle)Zhid_guidZdevicesindexZinterface_infooutr/   Z
detail_lenr0   Zinterface_detailr.   Zpath_lenr-   er   r   r   	Enumerate   sn   6






zWindowsHidDevice.Enumeratec                 C   s4   t j| | t|| _t  | _t| j| j dS )rI   N)r   	HidDevice__init__rG   devrQ   descr;   )selfrD   r   r   r   r[   ?  s   

zWindowsHidDevice.__init__c                 C      | j jd S rI   r   )r]   r7   r^   r   r   r   GetInReportDataLengthF     z&WindowsHidDevice.GetInReportDataLengthc                 C   r_   r`   )r]   r8   ra   r   r   r   GetOutReportDataLengthJ  rc   z'WindowsHidDevice.GetOutReportDataLengthc                 C   s   t ||  krtddg| }tt|}t }t	| j
|t |t|d}|jt |kr@tddt ||jf  |sFt dS )rI   z,Packet length must match report data length.r   Nz"Failed to write complete packet.  zExpected %d, but got %d)lenrd   r   HidErrorbytes	bytearrayr   r   r@   	WriteFiler\   r
   r)   rP   r*   )r^   ZpacketZpacket_datarW   Znum_writtenr:   r   r   r   WriteN  s&   


zWindowsHidDevice.Writec                 C   sl   t | jj}t }t| j|t	|t 
|d}|j| jjkr&td|s,t  tt|dd S )rI   Nz.Failed to read full length report from device.r   )r
   r+   r]   r7   r   r   r@   ReadFiler\   re   r)   rP   r   rf   r*   listrh   )r^   r0   Znum_readr:   r   r   r   Reada  s   
zWindowsHidDevice.Readc                 C   s   t | drt| j dS dS )z,Closes the file handle when object is GC-ed.r\   N)hasattrr@   rU   r\   ra   r   r   r   __del__r  s   
zWindowsHidDevice.__del__N)r   r   r	   __doc__staticmethodrY   r[   rb   rd   rj   rm   ro   r   r   r   r   rH      s    
JrH   )F)Arp   r
   r   platformZpyu2fr   Z	pyu2f.hidr   ZwindllZHidr'   ZSetupAPIrL   ZKernel32r@   Z	Structurer   Zarchitecturer   rf   r   r   r   r   Zc_void_pZHDEVINFOZHANDLEr2   rC   Zc_longZNTSTATUSr5   r>   r?   rB   rS   r<   r=   rK   Zrestyper   argtypesZBOOLEANr(   r3   r9   r,   r   r4   Zc_char_pZHWNDr   rM   ZBOOLrN   rO   rA   rU   rk   ri   r1   r;   rG   rZ   rH   r   r   r   r   <module>   s   















"
,