def open_all_devices(): max_index = c_uint() status = u2fh_devs_discover(devs, byref(max_index)) if status == 0: # We have devices! devices = [] resp = create_string_buffer(1024) for index in range(max_index.value + 1): buf_size = c_size_t(1024) if u2fh_get_device_description( devs, index, resp, byref(buf_size)) == 0: if resp.value.startswith('Yubikey NEO'): mode = MODE.mode_for_flags( 'OTP' in resp.value, 'CCID' in resp.value, 'U2F' in resp.value ) devices.append(U2FDevice(devs, index, mode)) elif resp.value.startswith('Yubikey 4'): mode = MODE.mode_for_flags( 'OTP' in resp.value, 'CCID' in resp.value, 'U2F' in resp.value ) devices.append(YK4Device(devs, index, mode)) elif resp.value.startswith('Security Key by Yubico'): devices.append(SKYDevice(devs, index)) return devices else: # No devices! # u2fh_devs_done(devs) pass return []
def _read_mode(self, dev): vid = c_int() pid = c_int() try: yk_get_key_vid_pid(dev, byref(vid), byref(pid)) mode = 0x0f & pid.value return MODE.mode_for_flags(bool(mode & 1), bool(mode & 4), bool(mode & 2)) except: # We know we at least have OTP enabled... return MODE.mode_for_flags(True, False, False)
def __init__(self, dev): self._dev = dev ser = c_uint() if yk_get_serial(dev, 0, 0, byref(ser)): self._serial = ser.value else: self._serial = None vid = c_int() pid = c_int() try: yk_get_key_vid_pid(dev, byref(vid), byref(pid)) mode = 0x0f & pid.value if mode == 1: # mode 1 has PID 0112 and mode 2 has PID 0111 self._mode = 2 elif mode == 2: self._mode = 1 else: # all others have 011x where x = mode self._mode = mode except: self._mode = MODE.mode_for_flags(True, False, False) status = ykds_alloc() try: if yk_get_status(dev, status): self._version = (ykds_version_major(status), ykds_version_minor(status), ykds_version_build(status)) else: self._version = (0, 0, 0) finally: ykds_free(status)
def __init__(self, dev): self._dev = dev ser = c_uint() if yk_get_serial(dev, 0, 0, byref(ser)): self._serial = ser.value else: self._serial = None vid = c_int() pid = c_int() try: yk_get_key_vid_pid(dev, byref(vid), byref(pid)) mode = 0x0f & pid.value if mode == 1: # mode 1 has PID 0112 and mode 2 has PID 0111 self._mode = 2 elif mode == 2: self._mode = 1 else: # all others have 011x where x = mode self._mode = mode except: self._mode = MODE.mode_for_flags(True, False, False) status = ykds_alloc() try: if yk_get_status(dev, status): self._version = ( ykds_version_major(status), ykds_version_minor(status), ykds_version_build(status) ) else: self._version = (0, 0, 0) finally: ykds_free(status)
def __init__(self, devs, index, mode=MODE.mode_for_flags(False, False, True)): self._devs = devs self._index = index self._mode = mode self._serial = None self._version = (0, 0, 0)
class YKStandardDevice(BaseDevice): device_type = 'OTP' supported = False serial = None version = (0, 0, 0) mode = MODE.mode_for_flags(True, False, False) def __init__(self, version): self._version = version @property def default_name(self): return 'YubiKey %s' % '.'.join(map(str, self._version))
def _read_mode(self, dev): vid = c_int() pid = c_int() try: yk_get_key_vid_pid(dev, byref(vid), byref(pid)) mode = 0x0f & pid.value if mode == 1: # mode 1 has PID 0112 and mode 2 has PID 0111 return 2 elif mode == 2: return 1 else: # all others have 011x where x = mode return mode except: # We know we at least have OTP enabled... return MODE.mode_for_flags(True, False, False)
class YKPlusDevice(YKStandardDevice): mode = MODE.mode_for_flags(True, False, True) default_name = 'YubiKey Plus'
def mode(self): return MODE.mode_for_flags(*self.flags)
def __init__(self, devs, index, mode=MODE.mode_for_flags(False, False, True)): self._devs = devs self._index = index self._mode = mode self._serial = None