|
11 | 11 |
|
12 | 12 | import sys |
13 | 13 |
|
14 | | -## Windows |
15 | | -if sys.platform.startswith("win32"): |
16 | | - import fido2._pyu2f.windows |
17 | | - |
18 | | - oldDevAttrFunc = fido2._pyu2f.windows.FillDeviceAttributes |
19 | | - from ctypes import wintypes |
20 | | - import ctypes |
21 | | - |
22 | | - fido2._pyu2f.windows.hid.HidD_GetSerialNumberString.restype = wintypes.BOOLEAN |
23 | | - fido2._pyu2f.windows.hid.HidD_GetSerialNumberString.argtypes = [ |
24 | | - ctypes.c_void_p, |
25 | | - ctypes.c_void_p, |
26 | | - ctypes.c_ulong, |
27 | | - ] |
28 | | - |
29 | | - def newDevAttrFunc(device, descriptor): |
30 | | - oldDevAttrFunc(device, descriptor) |
31 | | - buf_ser = ctypes.create_string_buffer(1024) |
32 | | - result = fido2._pyu2f.windows.hid.HidD_GetSerialNumberString( |
33 | | - device, buf_ser, 1024 |
34 | | - ) |
35 | | - if result: |
36 | | - descriptor.serial_number = ctypes.wstring_at(buf_ser) |
37 | | - |
38 | | - fido2._pyu2f.windows.FillDeviceAttributes = newDevAttrFunc |
39 | | - |
40 | | - |
41 | | -## macOS |
42 | | -if sys.platform.startswith("darwin"): |
43 | | - import fido2._pyu2f.macos |
44 | | - from fido2._pyu2f import base |
45 | | - from fido2._pyu2f.macos import ( |
46 | | - iokit, |
47 | | - IO_HID_DEVICE_REF, |
48 | | - GetDeviceIntProperty, |
49 | | - GetDevicePath, |
50 | | - GetDeviceStringProperty, |
51 | | - HID_DEVICE_PROPERTY_VENDOR_ID, |
52 | | - HID_DEVICE_PROPERTY_PRODUCT_ID, |
53 | | - HID_DEVICE_PROPERTY_PRODUCT, |
54 | | - HID_DEVICE_PROPERTY_PRIMARY_USAGE, |
55 | | - HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE, |
56 | | - HID_DEVICE_PROPERTY_REPORT_ID, |
57 | | - cf, |
58 | | - ) |
59 | | - |
60 | | - HID_DEVICE_PROPERTY_SERIAL_NUMBER = b"SerialNumber" |
61 | | - |
62 | | - def newEnumerate(): |
63 | | - """See base class.""" |
64 | | - # Init a HID manager |
65 | | - hid_mgr = iokit.IOHIDManagerCreate(None, None) |
66 | | - if not hid_mgr: |
67 | | - raise OSError("Unable to obtain HID manager reference") |
68 | | - iokit.IOHIDManagerSetDeviceMatching(hid_mgr, None) |
69 | | - |
70 | | - # Get devices from HID manager |
71 | | - device_set_ref = iokit.IOHIDManagerCopyDevices(hid_mgr) |
72 | | - if not device_set_ref: |
73 | | - raise OSError("Failed to obtain devices from HID manager") |
74 | | - |
75 | | - num = iokit.CFSetGetCount(device_set_ref) |
76 | | - devices = (IO_HID_DEVICE_REF * num)() |
77 | | - iokit.CFSetGetValues(device_set_ref, devices) |
78 | | - |
79 | | - # Retrieve and build descriptor dictionaries for each device |
80 | | - descriptors = [] |
81 | | - for dev in devices: |
82 | | - d = base.DeviceDescriptor() |
83 | | - d.vendor_id = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_VENDOR_ID) |
84 | | - d.product_id = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_PRODUCT_ID) |
85 | | - d.product_string = GetDeviceStringProperty(dev, HID_DEVICE_PROPERTY_PRODUCT) |
86 | | - d.serial_number = GetDeviceStringProperty( |
87 | | - dev, HID_DEVICE_PROPERTY_SERIAL_NUMBER |
88 | | - ) |
89 | | - d.usage = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_PRIMARY_USAGE) |
90 | | - d.usage_page = GetDeviceIntProperty( |
91 | | - dev, HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE |
92 | | - ) |
93 | | - d.report_id = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_REPORT_ID) |
94 | | - d.path = GetDevicePath(dev) |
95 | | - descriptors.append(d.ToPublicDict()) |
96 | | - |
97 | | - # Clean up CF objects |
98 | | - cf.CFRelease(device_set_ref) |
99 | | - cf.CFRelease(hid_mgr) |
100 | | - |
101 | | - return descriptors |
102 | | - |
103 | | - fido2._pyu2f.macos.MacOsHidDevice.Enumerate = newEnumerate |
104 | | - |
105 | | - |
106 | | -## Linux |
107 | | -if sys.platform.startswith("linux"): |
108 | | - import fido2._pyu2f.linux |
109 | | - |
110 | | - oldnewParseUevent = fido2._pyu2f.linux.ParseUevent |
111 | | - |
112 | | - def newParseUevent(uevent, desc): |
113 | | - oldnewParseUevent(uevent, desc) |
114 | | - lines = uevent.split(b"\n") |
115 | | - for line in lines: |
116 | | - line = line.strip() |
117 | | - if not line: |
118 | | - continue |
119 | | - k, v = line.split(b"=") |
120 | | - if k == b"HID_UNIQ": |
121 | | - desc.serial_number = v.decode("utf8") |
122 | | - |
123 | | - fido2._pyu2f.linux.ParseUevent = newParseUevent |
| 14 | +######################################################## |
| 15 | +# removed as fido._pyu2f is not part of fido2 anymore... |
| 16 | +#################################################### |
| 17 | + |
| 18 | +# ## Windows |
| 19 | +# if sys.platform.startswith("win32"): |
| 20 | +# import fido2._pyu2f.windows |
| 21 | + |
| 22 | +# oldDevAttrFunc = fido2._pyu2f.windows.FillDeviceAttributes |
| 23 | +# from ctypes import wintypes |
| 24 | +# import ctypes |
| 25 | + |
| 26 | +# fido2._pyu2f.windows.hid.HidD_GetSerialNumberString.restype = wintypes.BOOLEAN |
| 27 | +# fido2._pyu2f.windows.hid.HidD_GetSerialNumberString.argtypes = [ |
| 28 | +# ctypes.c_void_p, |
| 29 | +# ctypes.c_void_p, |
| 30 | +# ctypes.c_ulong, |
| 31 | +# ] |
| 32 | + |
| 33 | +# def newDevAttrFunc(device, descriptor): |
| 34 | +# oldDevAttrFunc(device, descriptor) |
| 35 | +# buf_ser = ctypes.create_string_buffer(1024) |
| 36 | +# result = fido2._pyu2f.windows.hid.HidD_GetSerialNumberString( |
| 37 | +# device, buf_ser, 1024 |
| 38 | +# ) |
| 39 | +# if result: |
| 40 | +# descriptor.serial_number = ctypes.wstring_at(buf_ser) |
| 41 | + |
| 42 | +# fido2._pyu2f.windows.FillDeviceAttributes = newDevAttrFunc |
| 43 | + |
| 44 | + |
| 45 | +# ## macOS |
| 46 | +# if sys.platform.startswith("darwin"): |
| 47 | +# import fido2._pyu2f.macos |
| 48 | +# from fido2._pyu2f import base |
| 49 | +# from fido2._pyu2f.macos import ( |
| 50 | +# iokit, |
| 51 | +# IO_HID_DEVICE_REF, |
| 52 | +# GetDeviceIntProperty, |
| 53 | +# GetDevicePath, |
| 54 | +# GetDeviceStringProperty, |
| 55 | +# HID_DEVICE_PROPERTY_VENDOR_ID, |
| 56 | +# HID_DEVICE_PROPERTY_PRODUCT_ID, |
| 57 | +# HID_DEVICE_PROPERTY_PRODUCT, |
| 58 | +# HID_DEVICE_PROPERTY_PRIMARY_USAGE, |
| 59 | +# HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE, |
| 60 | +# HID_DEVICE_PROPERTY_REPORT_ID, |
| 61 | +# cf, |
| 62 | +# ) |
| 63 | + |
| 64 | +# HID_DEVICE_PROPERTY_SERIAL_NUMBER = b"SerialNumber" |
| 65 | + |
| 66 | +# def newEnumerate(): |
| 67 | +# """See base class.""" |
| 68 | +# # Init a HID manager |
| 69 | +# hid_mgr = iokit.IOHIDManagerCreate(None, None) |
| 70 | +# if not hid_mgr: |
| 71 | +# raise OSError("Unable to obtain HID manager reference") |
| 72 | +# iokit.IOHIDManagerSetDeviceMatching(hid_mgr, None) |
| 73 | + |
| 74 | +# # Get devices from HID manager |
| 75 | +# device_set_ref = iokit.IOHIDManagerCopyDevices(hid_mgr) |
| 76 | +# if not device_set_ref: |
| 77 | +# raise OSError("Failed to obtain devices from HID manager") |
| 78 | + |
| 79 | +# num = iokit.CFSetGetCount(device_set_ref) |
| 80 | +# devices = (IO_HID_DEVICE_REF * num)() |
| 81 | +# iokit.CFSetGetValues(device_set_ref, devices) |
| 82 | + |
| 83 | +# # Retrieve and build descriptor dictionaries for each device |
| 84 | +# descriptors = [] |
| 85 | +# for dev in devices: |
| 86 | +# d = base.DeviceDescriptor() |
| 87 | +# d.vendor_id = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_VENDOR_ID) |
| 88 | +# d.product_id = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_PRODUCT_ID) |
| 89 | +# d.product_string = GetDeviceStringProperty(dev, HID_DEVICE_PROPERTY_PRODUCT) |
| 90 | +# d.serial_number = GetDeviceStringProperty( |
| 91 | +# dev, HID_DEVICE_PROPERTY_SERIAL_NUMBER |
| 92 | +# ) |
| 93 | +# d.usage = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_PRIMARY_USAGE) |
| 94 | +# d.usage_page = GetDeviceIntProperty( |
| 95 | +# dev, HID_DEVICE_PROPERTY_PRIMARY_USAGE_PAGE |
| 96 | +# ) |
| 97 | +# d.report_id = GetDeviceIntProperty(dev, HID_DEVICE_PROPERTY_REPORT_ID) |
| 98 | +# d.path = GetDevicePath(dev) |
| 99 | +# descriptors.append(d.ToPublicDict()) |
| 100 | + |
| 101 | +# # Clean up CF objects |
| 102 | +# cf.CFRelease(device_set_ref) |
| 103 | +# cf.CFRelease(hid_mgr) |
| 104 | + |
| 105 | +# return descriptors |
| 106 | + |
| 107 | +# fido2._pyu2f.macos.MacOsHidDevice.Enumerate = newEnumerate |
| 108 | + |
| 109 | + |
| 110 | +# ## Linux |
| 111 | +# if sys.platform.startswith("linux"): |
| 112 | +# import fido2._pyu2f.linux |
| 113 | + |
| 114 | +# oldnewParseUevent = fido2._pyu2f.linux.ParseUevent |
| 115 | + |
| 116 | +# def newParseUevent(uevent, desc): |
| 117 | +# oldnewParseUevent(uevent, desc) |
| 118 | +# lines = uevent.split(b"\n") |
| 119 | +# for line in lines: |
| 120 | +# line = line.strip() |
| 121 | +# if not line: |
| 122 | +# continue |
| 123 | +# k, v = line.split(b"=") |
| 124 | +# if k == b"HID_UNIQ": |
| 125 | +# desc.serial_number = v.decode("utf8") |
| 126 | + |
| 127 | +# fido2._pyu2f.linux.ParseUevent = newParseUevent |
0 commit comments