o
    sh9                     @  sj  d dl mZ ddlmZmZmZmZ d dlZd dlZd dl	Z	d dl
mZmZ d dlZeeZdZdZdZd	Zd
ZdZdZdZdZG dd dejZG dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!e"eZ#e"eZ$e"eZ%e"e Z&ej'Z(e"e!Z)ej*Z+ej*Z,ej-Z.ej/Z0ej1Z2ej3Z4dZ5ej6Z7ej6Z8ej6Z9ej6Z:ej6Z;e:Z<e:Z=e"eZ>e"eZ?e@dejAe7ej*e8ej3e"ejBe0ZCe@dejAe7ej*ZDdZEdZFd ZGdZHd ZIdZJdZKd ZLejMNejOPd!pd"ZQejMNejOPd#pd$ZRe$SeRd%ZTdeRjU_Ve#e"ej*geRjU_We$eRjX_Vej*ejYej3geRjX_WejZeRj[_Ve%ejYe0e4geRj[_We.eRj\_Ve%geRj\_We.eRj]_Ve.eRj^_Vej_eRj`_Ve&eRja_Vg eRja_We(eRjb_Ve$e2ejZgeRjb_We7eRjc_Ve%geRjc_We:geQjd_We>eQje_Ve)e9geQje_We#eQjf_Ve>geQjf_WdeQjg_Ve>e%geQjg_We,eQjh_VejigeQjh_We7eQjj_Ve=e"ejigeQjj_We?eQjk_Ve)e<geQjk_We7eQjl_Ve?ej3geQjl_WdeQjm_Ve?e&e$geQjm_WdeQjn_Ve?e&e$geQjn_We%eQjo_Ve?e$geQjo_We7eQjp_Ve?e8e0ej*e0geQjp_We<eQjq_Ve;e+geQjq_Wd&d' ZreCerZsd(d) ZteDetZud*d+ ZvG d,d- d-eZwd.d/ Zxd0d1 Zyd2d3 Zzd4d5 Z{d6d7 Z|d8d9 Z}d:d; Z~d<d= ZdS )>    )annotations   )HidDescriptorCtapHidConnectionFIDO_USAGE_PAGE
FIDO_USAGEN)QueueEmptys   VendorIDs	   ProductIDs   Products   SerialNumbers   PrimaryUsages   PrimaryUsagePages   MaxInputReportSizes   MaxOutputReportSizes   ReportIDc                   @     e Zd ZdS )_CFTypeN__name__
__module____qualname__ r   r   J/var/www/html/env_mimamsha/lib/python3.10/site-packages/fido2/hid/macos.pyr   -       r   c                   @  r
   )	_CFStringNr   r   r   r   r   r   1   r   r   c                   @  r
   )_CFSetNr   r   r   r   r   r   5   r   r   c                   @  r
   )_IOHIDManagerNr   r   r   r   r   r   9   r   r   c                   @  r
   )_IOHIDDeviceNr   r   r   r   r   r   =   r   r   c                   @  r
   )
_CFRunLoopNr   r   r   r   r   r   A   r   r   c                   @  r
   )_CFAllocatorNr   r   r   r   r   r   E   r   r   i           IOKitz0/System/Library/Frameworks/IOKit.framework/IOKitCoreFoundationzB/System/Library/Frameworks/CoreFoundation.framework/CoreFoundationkCFRunLoopDefaultModec                 C  s   ~~~~|  t|| dS )z+Handles incoming IN report from HID device.N)putctypes	string_at)
read_queueresultsenderreport_type	report_idreportreport_lengthr   r   r   _hid_read_callback   s   r)   c                 C  s   ~~t | j d S N)cfCFRunLoopStoprun_loop_ref)
hid_devicer#   r$   r   r   r   _hid_removal_callback   s   r/   c                 C  s~   t  | _| jstd dS t| j| jt t	| jt
t|  t tdd}|tkr4td| t| j| jt dS )zBinds a device to the thread's run loop, then starts the run loop.

    Args:
    hid_device: The MacOsHidDevice object

    The HID manager requires a run loop to handle Report reads. This thread
    function serves that purpose.
    zFailed to get current run loopNr   Tz!Unexpected run loop exit code: %d)r+   CFRunLoopGetCurrentr-   loggererroriokitIOHIDDeviceScheduleWithRunLoophandleK_CF_RUNLOOP_DEFAULT_MODE"IOHIDDeviceRegisterRemovalCallbackREMOVAL_CALLBACKr    	py_objectCFRunLoopRunInMode K_CF_RUN_LOOP_RUN_HANDLED_SOURCE IOHIDDeviceUnscheduleFromRunLoop)r.   run_loop_run_resultr   r   r   _dev_read_thread   s$   



r>   c                   @  s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
MacCtapHidConnectionc              	   C  sz   || _ t|j| _t| jd}|tkrtd| t | _	d | _
tj|j  | _t| j| j| j jtt| j	 d S )Nr   z)Failed to open device for communication: )
descriptor_handle_from_pathpathr5   r3   IOHIDDeviceOpenK_IO_RETURN_SUCCESSOSErrorr   r"   r-   r    c_uint8report_size_inin_report_buffer&IOHIDDeviceRegisterInputReportCallbackREGISTERED_READ_CALLBACKr9   )selfr@   r#   r   r   r   __init__  s   
zMacCtapHidConnection.__init__c              	   C  s&   t | j| j| jjtdtd  d S )Nr   )	r3   rI   r5   rH   r@   rG   r    castIO_HID_REPORT_CALLBACK)rK   r   r   r   close)  s   
zMacCtapHidConnection.closec                 C  s2   t | jtd|t|}|tkrtd| d S )Nr   z"Failed to write report to device: )r3   IOHIDDeviceSetReportr5   K_IO_HID_REPORT_TYPE_OUTPUTlenrD   rE   )rK   packetr#   r   r   r   write_packet2  s   	z!MacCtapHidConnection.write_packetc                 C  sh   z| j dW S  ty3   tjt| fd}|  |  z	| j dW  Y S  ty2   tdw w )NF)targetargszFailed reading a response)	r"   getr	   	threadingThreadr>   startjoinrE   )rK   read_threadr   r   r   read_packet?  s   z MacCtapHidConnection.read_packetN)r   r   r   rL   rO   rT   r]   r   r   r   r   r?     s
    	r?   c                 C  s|   t d|d}t| |}t | |sdS t |t  kr)tdt | t	 }t 
|tt|}|s;dS |jS )z'Reads int property from the HID device.Nr   zExpected number type, got )r+   CFStringCreateWithCStringr3   IOHIDDeviceGetProperty	CFReleaseCFGetTypeIDCFNumberGetTypeIDrE   r    c_int32CFNumberGetValueK_CF_NUMBER_SINT32_TYPEbyrefvaluedevkeycf_keytype_refoutretr   r   r   get_int_propertyL  s   
ro   c                 C  s   t d|d}t| |}t | |sdS t |t  kr)tdt | t	d}t 
||t|t}|s=dS z	|jdpEdW S  tyP   Y dS w )z*Reads string property from the HID device.Nr   zExpected string type, got    zutf-8)r+   r^   r3   r_   r`   ra   CFStringGetTypeIDrE   r    create_string_bufferCFStringGetCStringsizeof CF_STRING_BUILTIN_ENCODINGS_UTF8rg   decodeUnicodeDecodeErrorrh   r   r   r   get_string_property_  s$   

rx   c                 C  s@   t | }t }t |t|}|tkrtd| |jS )zObtains the unique IORegistry entry ID for the device.

    Args:
    handle: reference to the device

    Returns:
    A unique ID for the device, obtained from the IO Registry
    z&Failed to obtain IORegistry entry ID: )	r3   IOHIDDeviceGetServicer    c_uint64!IORegistryEntryGetRegistryEntryIDrf   rD   rE   rg   )r5   io_service_objentry_idr#   r   r   r   get_device_idw  s   


r~   c                 C  sD   t t| }t|}tt|}|std|  dtt	|S )Nz
Device ID z, does not match any HID device on the system)
r    rz   intr3   IORegistryEntryIDMatchingIOServiceGetMatchingServiceK_IO_MASTER_PORT_DEFAULTrE   IOHIDDeviceCreateK_CF_ALLOCATOR_DEFAULT)rB   r}   matching_dictdevice_entryr   r   r   rA     s   
rA   c                 C  s   t | S r*   )r?   )r@   r   r   r   open_connection  s   r   c           
      C  s   t | t}t | t}|tkr@|tkr@t| }t | t}t | t}t| t	}t| t
}t | t}t | t}	tt|||||	||S td)NzNot a CTAP device)ro   &HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE!HID_DEVICE_PROPERTY_PRIMARY_USAGEr   r   r~   HID_DEVICE_PROPERTY_VENDOR_IDHID_DEVICE_PROPERTY_PRODUCT_IDrx   HID_DEVICE_PROPERTY_PRODUCT!HID_DEVICE_PROPERTY_SERIAL_NUMBER)HID_DEVICE_PROPERTY_MAX_INPUT_REPORT_SIZE*HID_DEVICE_PROPERTY_MAX_OUTPUT_REPORT_SIZEr   str
ValueError)
r5   
usage_pageusage	device_idvidpidproductserialsize_insize_outr   r   r   _get_descriptor_from_handle  s   







r   c                 C  s   t t| S r*   )r   rA   )rB   r   r   r   get_descriptor  s   r   c               	   C  s   t d d} | stdzQt | d  t | }|stdz9t |}t|  }t || g }|D ]}zt|}|	| W q3 t
yI   Y q3w |W t| W t|  S t| w t|  w )Nr   z&Unable to obtain HID manager referencez)Failed to obtain devices from HID manager)r3   IOHIDManagerCreaterE   IOHIDManagerSetDeviceMatchingIOHIDManagerCopyDevicesCFSetGetCountIO_HID_DEVICE_REFCFSetGetValuesr   appendr   r+   r`   )hid_mgrdevice_set_refnumdevicesdescriptorsr5   r@   r   r   r   list_descriptors  s2   


r   )
__future__r   baser   r   r   r   r    ctypes.utilrX   queuer   r	   logging	getLoggerr   r1   r   r   r   r   r   r   r   r   HID_DEVICE_PROPERTY_REPORT_ID	Structurer   r   r   r   r   r   r   POINTER
CF_SET_REFCF_STRING_REFCF_TYPE_REFCF_RUN_LOOP_REFrc   CF_RUN_LOOP_RUN_RESULTCF_ALLOCATOR_REFc_void_pCF_DICTIONARY_REFCF_MUTABLE_DICTIONARY_REFc_ulong
CF_TYPE_IDc_longCF_INDEXc_doubleCF_TIME_INTERVALc_uint32CF_STRING_ENCODINGru   c_uint	IO_RETURNIO_HID_REPORT_TYPEIO_OPTION_BITSIO_OBJECT_TMACH_PORT_TIO_SERVICE_TIO_REGISTRY_ENTRY_TIO_HID_MANAGER_REFr   	CFUNCTYPEr9   rF   rN   IO_HID_CALLBACKre   r   r   rQ   rD   K_CF_RUN_LOOP_RUN_STOPPEDK_CF_RUN_LOOP_RUN_TIMED_OUTr;   cdllLoadLibraryutilfind_libraryr3   r+   in_dllr6   r   restypeargtypesr^   c_char_pc_boolrs   ra   rb   rq   c_intrd   r0   r:   r`   IOObjectReleaser   r   r   r   rz   r{   r   IOHIDDeviceCloser4   r<   r_   rP   r   r)   rJ   r/   r8   r>   r?   ro   rx   r~   rA   r   r   r   r   r   r   r   r   <module>   s,  
















'=