
    1                     
   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ej                        Z ej"                         d   dk(  rd	ZnA ej"                         d   d
k(  rdZn' ej&                  d ej"                         d   z         G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Zej0                  Zej0                  Zej0                  ZdZej:                  ZdZdZ dZ!dZ"dZ#dZ$dZ%de
jL                  _'         ejP                  e      ge
jL                  _)        ejT                  e
jV                  _'        e ejP                  e      ge
jV                  _)        ejT                  e
jX                  _'        e ejP                  e      ge
jX                  _)        ejT                  e
jZ                  _'        ege
jZ                  _)        ejT                  e
j\                  _'        eej0                  ej^                  ge
j\                  _)        ee
j`                  _'        e ejP                  e      ge
j`                  _)         ejP                  e      ejb                  ejd                  ejf                  gejh                  _)        eejh                  _'        ejj                  ejl                  _'        eej0                   ejP                  e      ejf                   ejP                  e      gejl                  _)        ejj                  ejn                  _'        e ejP                  e       ejP                  e      ejf                   ejP                  ejf                        ej0                  gejn                  _)        eejp                  _'        ejb                  ejf                  ejf                  ej0                  ejf                  ejf                  egejp                  _)        ejj                  ejr                  _'        egejr                  _)        ejj                  ejt                  _'        eej0                  ejf                   ejP                  ejf                        ej0                  gejt                  _)        ejj                  ejv                  _'        eej0                  ejf                   ejP                  ejf                        ej0                  gejv                  _)        d Z<d Z=d!dZ> G d d ej~                        Z@y)"z3Implements raw HID device communication on Windows.    N)wintypes)errors)basec                   ~    e Zd Zdej                  fdej
                  fdej
                  fdej                  dz  fgZy)GUIDData1Data2Data3Data4   N)__name__
__module____qualname__ctypesc_ulongc_ushortc_ubyte_fields_     $lib/third_party/pyu2f/hid/windows.pyr   r   !   s>    '((*+-(r   r   64bitr   32bit   zUnknown architecture: %sc                       e Zd Zdej                  fdefdej                  fd ej                  ej                        fgZ	e
Zy)DeviceInterfaceDatacbSizeInterfaceClassGuidFlagsReservedN)r   r   r   r   DWORDr   r   POINTERr   r   SETUPAPI_PACK_pack_r   r   r   r   r   4   sG    (#T*'>6>>&..9:<( &r   r   c                   N    e Zd Zdej                  fdej                  dz  fgZeZ	y)DeviceInterfaceDetailDatar   
DevicePathr   N)
r   r   r   r   r!   r   c_byter   r#   r$   r   r   r   r&   r&   <   s+    (V]]Q./1(&r   r&   c                   x    e Zd Zdej                  fdej
                  fdej
                  fdej
                  fgZy)HidAttributesSizeVendorID	ProductIDVersionNumberN)r   r   r   r   r   r   r   r   r   r   r*   r*   B   s9    v~~&6??+FOO,02(r   r*   c            	           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z  fdej                  d	z  fgZy
)HidCapabilitiesUsage	UsagePageInputReportByteLengthOutputReportByteLengthFeatureReportByteLengthr       NotUsed
   N)r   r   r   r   r   r   r   r   r   r0   r0   I   sg    (FOO,&8'9(&//:6??R/0&//B./1(r   r0   l    i            i   @l        c                    t               }t        j                  | t        j                  |            }|st        j
                         t        j                  d      }t        j                  | |d      }|st        j
                         |j                  |_	        |j                  |_        t        j                  |      |_        y)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,   	vendor_idr-   
product_id
wstring_atproduct_string)device
descriptor
attributesresultbufs        r   FillDeviceAttributesrL      s    $ *!!&&,,z*BC&	
//
##D)#$$VS$7&	
//
#,,*$..*$//4*r   c                     t        d      }t        j                  | t        j                  |            }|st        j
                         	 t               }t        j                  |t        j                  |            }|t        k7  rt        j
                         |j                  |_
        |j                  |_        |j                  |_        |j                  |_        t        j#                  |       y# t        j#                  |       w xY 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@   r0   HidP_GetCapsHIDP_STATUS_SUCCESSr1   usager2   
usage_pager3   internal_max_in_report_lenr4   internal_max_out_report_lenHidD_FreePreparsedData)rG   rH   preparsed_dataretcapss        r   FillDeviceCapabilitiesrZ      s      (*.!!&&,,~*FG#	
//
/D


>6<<+=
>C
!!OOzzJ NNJ,0,F,FJ)-1-H-HJ* ~.C~.s   BC6 6Dc           	          t         t        z  }t        t        z  }|rd}t        j                  | ||dt        dd      }|t        k(  rt        j                         |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enumdesired_access
share_modehs        r   
OpenDeviceri      s]     </.!11*	N4)%49! 

//
	
(r   c                   D    e Zd ZdZed        Zd Zd Zd Zd Z	d Z
d Zy	)
WindowsHidDevicez/Implementation of raw HID interface on Windows.c            	      J   t               } t        j                  t        j                  |              t
        j                  t        j                  |       ddd      }d}t               }t        j                  t              |_	        g }	 t
        j                  |dt        j                  |       |t        j                  |            }|dz  }|s	 |S t        j                         }t
        j                  |t        j                  |      ddt        j                  |      d      }|j                  }|dk(  rt        j                  |      }t         j#                  |      }t        j                  t               |_	        t
        j                  |t        j                  |      t        j                  |      |dd      }|st        j$                         t'        j(                         }	|t        j                  t        j                        z
  }
t        j*                  t        j,                  |j.                        |
      |	_        d}	 t3        |	j0                  d      }	 t;        ||	       t=        ||	       |j?                  |	jA                                	 tB        jE                  |       (# t4        $ r }|j6                  t8        k(  rY d}~K|d}~ww xY w# t4        $ r }Y d}~tB        jE                  |       yd}~ww xY w# tB        jE                  |       w xY w)See base class.N   r   Tr   )#r   r=   HidD_GetHidGuidr   r?   setupapiSetupDiGetClassDevsAr   sizeofr   SetupDiEnumDeviceInterfacesr   r!    SetupDiGetDeviceInterfaceDetailAvaluerA   r&   from_bufferr@   r   DeviceDescriptor	string_at	addressofr'   rd   ri   WindowsErrorwinerrorERROR_ACCESS_DENIEDrL   rZ   appendToPublicDictr`   CloseHandle)hid_guiddevicesindexinterface_infooutrJ   
detail_lenrK   interface_detailrH   path_lenrG   es                r   	EnumeratezWindowsHidDevice.Enumerate   s    vHX./++XdD2GE(*N"MM*=>N
C
33
1fll8,e
,,~
&(f qjep Jm >>#j88
6<</q
,,z
"D*f ##j	q''
3c2>>sC &.G H88
6<</
,,'
(*dDBf oo((*j fmmHNN;;h((


+66
7Cjo fJOOT2%VZ0vz2

:**,- 	V$y ^  ::,,
'	  V$ 	V$sB   J3 $7K 3	K<KKK	L(L LL L"c                     t         j                  j                  | |       t        |      | _        t        j
                         | _        t        | j                  | j                         y)rm   N)r   	HidDevice__init__ri   devrw   descrZ   )selfrd   s     r   r   zWindowsHidDevice.__init__A  sD    NND$'$DH%%'DI488TYY/r   c                 4    | j                   j                  dz
  S rm   r   )r   rT   r   s    r   GetInReportDataLengthz&WindowsHidDevice.GetInReportDataLengthH  s    99//!33r   c                 4    | j                   j                  dz
  S r   )r   rU   r   s    r   GetOutReportDataLengthz'WindowsHidDevice.GetOutReportDataLengthL  s    9900144r   c                    t        |      | j                         k7  rt        j                  d      dg|z   }t	        t        |            }t        j                         }t        j                  | j                  |t        |      t        j                  |      d      }|j                  t        |      k7  r0t        j                  ddt        |      |j                  fz  z         |st        j                         y)rm   z,Packet length must match report data length.r   Nz"Failed to write complete packet.  zExpected %d, but got %d)lenr   r   HidErrorbytes	bytearrayr   r!   r`   	WriteFiler   r   r?   ru   r@   )r   packetpacket_datar   num_writtenrX   s         r   WritezWindowsHidDevice.WriteP  s    
6{d1133OOJKK#,K
	+&
'C.."KHHc3s8LL%t	-  CH$OO
.1Js8[&&
'2( () ) OO r   c                    t        j                  | j                  j                        }t	        j
                         }t        j                  | j                  |t        |      t        j                  |      d      }|j                  | j                  j                  k7  rt        j                  d      |st        j                         t        t!        |dd             S )rm   Nz.Failed to read full length report from device.r   )r   rA   r   rT   r   r!   r`   ReadFiler   r   r?   ru   r   r   r@   listr   )r   rK   num_readrX   s       r   ReadzWindowsHidDevice.Readc  s    

%
%dii&J&J
KC~~H


#s3xh!7?C ~~===OOLMMOO 	#ab'"##r   c                 \    t        | d      r t        j                  | j                         yy)z,Closes the file handle when object is GC-ed.r   N)hasattrr`   r   r   r   s    r   __del__zWindowsHidDevice.__del__t  s#    tU488$ r   N)r   r   r   __doc__staticmethodr   r   r   r   r   r   r   r   r   r   rk   rk      s8    7J JX045&$"%r   rk   )F)Ar   r   r   platformpyu2fr   	pyu2f.hidr   windllHidr=   SetupAPIrp   Kernel32r`   	Structurer   architecturer#   r   r   r&   r*   r0   c_void_pHDEVINFOHANDLErN   rc   c_longNTSTATUSrQ   r^   r_   rb   r|   r\   r]   ro   restyper"   argtypesBOOLEANr>   rO   rV   rB   r   rP   c_char_pHWNDr!   rq   BOOLrs   rt   ra   r   r   r   rL   rZ   ri   r   rk   r   r   r   <module>r      s   :      mm==!!==!!-6 - 81(-XQ7*-25JX5J5J5LQ5OOPP&**  0 0 2F$$ 21f&& 1 ??	  "  ==      #    .t 45   !)!1!1   #)>6>>-+H"I   $,$4$4   !&,&4fnn5I&J&L   "%-%5%5   "';&<   #$,$4$4   !&,foov~~%N   "#   1+V^^O<>    +9&..*>*2--*I   &(0   %/7}} $ $ ,foo~v~~d3X^^FNN&'1) $ $ - 5=MM ) ) 1nfnn01FNN,-x~~FNN8>>"FOO65 ) ) 2
  &   
OOX^^X^^V__NNHNNF!,     (}}   !'   $MM   
FOOX^^FNN8>>"FOO5    &]]   
FOOX^^FNN8>>"FOO5   
5D"/X F%t~~ F%r   