def _challenge_response(self, challenge, mode, slot, variable, may_block): """ Do challenge-response with a YubiKey > 2.0. """ # Check length and pad challenge if appropriate if mode == 'HMAC': if len(challenge) > yubikey_defs.SHA1_MAX_BLOCK_SIZE: raise yubico_exception.InputError('Mode HMAC challenge too big (%i/%i)' \ % (yubikey_defs.SHA1_MAX_BLOCK_SIZE, len(challenge))) if len(challenge) < yubikey_defs.SHA1_MAX_BLOCK_SIZE: pad_with = chr(0x0) if variable and challenge[-1] == pad_with: pad_with = chr(0xff) challenge = challenge.ljust(yubikey_defs.SHA1_MAX_BLOCK_SIZE, pad_with) response_len = yubikey_defs.SHA1_DIGEST_SIZE elif mode == 'OTP': if len(challenge) != yubikey_defs.UID_SIZE: raise yubico_exception.InputError('Mode OTP challenge must be %i bytes (got %i)' \ % (yubikey_defs.UID_SIZE, len(challenge))) challenge = challenge.ljust(yubikey_defs.SHA1_MAX_BLOCK_SIZE, chr(0x0)) response_len = 16 else: raise yubico_exception.InputError('Invalid mode supplied (%s, valid values are HMAC and OTP)' \ % (mode)) try: command = _CMD_CHALLENGE[mode][slot] except: raise yubico_exception.InputError('Invalid slot specified (%s)' % (slot)) frame = yubikey_frame.YubiKeyFrame(command=command, payload=challenge) self._write(frame) response = self._read_response(may_block=may_block) if not yubico_util.validate_crc16(response[:response_len + 2]): raise YubiKeyUSBHIDError("Read from device failed CRC check") return response[:response_len]
def _read_serial(self, may_block): """ Read the serial number from a YubiKey > 2.2. """ frame = yubikey_frame.YubiKeyFrame(command = _SLOT_DEVICE_SERIAL) self._write(frame) response = self._read_response(may_block=may_block) if not yubico_util.validate_crc16(response[:6]): raise YubiKeyUSBHIDError("Read from device failed CRC check") # the serial number is big-endian, although everything else is little-endian serial = struct.unpack('>lxxx', response) return serial[0]
def _read_serial(self, may_block): """ Read the serial number from a YubiKey > 2.0. """ frame = yubikey_frame.YubiKeyFrame(command = _SLOT_DEVICE_SERIAL) self._write(frame) response = self._read_response(may_block=may_block) if not yubico_util.validate_crc16(response[:6]): raise YubiKeyUSBHIDError("Read from device failed CRC check") # the serial number is big-endian, although everything else is little-endian serial = struct.unpack('>lxxx', response) return serial[0]
def _challenge_response(self, challenge, mode, slot): """ Do challenge-response with a YubiKey > 2.0. """ try: command = _CMD_CHALLENGE[mode][slot] except: raise YubiKeyUSBHIDError('Invalid slot (%s) or mode (%s) specified' % (slot, mode)) frame = yubikey_frame.YubiKeyFrame(command=command, payload=challenge) self._write(frame) response = self._read_response(may_block=True) if not yubico_util.validate_crc16(response[:22]): raise YubiKeyUSBHIDError("Read from device failed CRC check") return response[:20]
def _challenge_response(self, challenge, mode, slot): """ Do challenge-response with a YubiKey > 2.0. """ try: command = _CMD_CHALLENGE[mode][slot] except: raise YubiKeyUSBHIDError( 'Invalid slot (%s) or mode (%s) specified' % (slot, mode)) frame = yubikey_frame.YubiKeyFrame(command=command, payload=challenge) self._write(frame) response = self._read_response(may_block=True) if not yubico_util.validate_crc16(response[:22]): raise YubiKeyUSBHIDError("Read from device failed CRC check") return response[:20]