def testVersionErrors(self):
        mock_transport = mock.MagicMock()
        sk = hardware.SecurityKey(mock_transport)

        mock_transport.SendMsgBytes.return_value = bytearray([0xfa, 0x05])

        self.assertRaises(errors.ApduError, sk.CmdVersion)
        self.assertEquals(mock_transport.SendMsgBytes.call_count, 1)
    def testVersion(self):
        mock_transport = mock.MagicMock()
        sk = hardware.SecurityKey(mock_transport)

        mock_transport.SendMsgBytes.return_value = bytearray(b'U2F_V2\x90\x00')

        reply = sk.CmdVersion()
        self.assertEquals(reply, bytearray(b'U2F_V2'))
        self.assertEquals(mock_transport.SendMsgBytes.call_count, 1)
        (sent_msg, ), _ = mock_transport.SendMsgBytes.call_args
        self.assertEquals(
            sent_msg, bytearray([0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00]))
    def testRegisterTUPRequired(self):
        mock_transport = mock.MagicMock()
        sk = hardware.SecurityKey(mock_transport)

        challenge_param = b'01234567890123456789012345678901'
        app_param = b'01234567890123456789012345678901'

        mock_transport.SendMsgBytes.return_value = bytearray([0x69, 0x85])

        self.assertRaises(errors.TUPRequiredError, sk.CmdRegister,
                          challenge_param, app_param)
        self.assertEquals(mock_transport.SendMsgBytes.call_count, 1)
    def testSimpleCommands(self):
        mock_transport = mock.MagicMock()
        sk = hardware.SecurityKey(mock_transport)

        sk.CmdBlink(5)
        mock_transport.SendBlink.assert_called_once_with(5)

        sk.CmdWink()
        mock_transport.SendWink.assert_called_once_with()

        sk.CmdPing(bytearray(b'foo'))
        mock_transport.SendPing.assert_called_once_with(bytearray(b'foo'))
Esempio n. 5
0
def GetLocalU2FInterface(origin=socket.gethostname()):
  """Obtains a U2FInterface for the first valid local U2FHID device found."""
  hid_transports = hidtransport.DiscoverLocalHIDU2FDevices()
  for t in hid_transports:
    try:
      return U2FInterface(security_key=hardware.SecurityKey(transport=t), origin=origin)
    except errors.UnsupportedVersionException:
      # Skip over devices that don't speak the proper version of the protocol.
      pass

  # Unable to find a device
  raise errors.NoDeviceFoundError()
    def testAuthenticateInvalidKeyHandle(self):
        mock_transport = mock.MagicMock()
        sk = hardware.SecurityKey(mock_transport)

        challenge_param = b'01234567890123456789012345678901'
        app_param = b'01234567890123456789012345678901'
        key_handle = b'\x01\x02\x03\x04'

        mock_transport.SendMsgBytes.return_value = bytearray([0x6a, 0x80])

        self.assertRaises(errors.InvalidKeyHandleError, sk.CmdAuthenticate,
                          challenge_param, app_param, key_handle)
        self.assertEquals(mock_transport.SendMsgBytes.call_count, 1)
Esempio n. 7
0
  def testRegisterSuccess(self):
    mock_transport = mock.MagicMock()
    sk = hardware.SecurityKey(mock_transport)

    challenge_param = '01234567890123456789012345678901'
    app_param = '01234567890123456789012345678901'

    mock_transport.SendMsgBytes.return_value = bytearray(
        [0x01, 0x02, 0x90, 0x00])

    reply = sk.CmdRegister(challenge_param, app_param)
    self.assertEquals(reply, bytearray([0x01, 0x02]))
    self.assertEquals(mock_transport.SendMsgBytes.call_count, 1)
    (sent_msg,), _ = mock_transport.SendMsgBytes.call_args
    self.assertEquals(sent_msg[0:4], bytearray([0x00, 0x01, 0x03, 0x00]))
    self.assertEquals(sent_msg[7:-2], bytearray(challenge_param + app_param))
Esempio n. 8
0
  def testAuthenticateSuccess(self):
    mock_transport = mock.MagicMock()
    sk = hardware.SecurityKey(mock_transport)

    challenge_param = '01234567890123456789012345678901'
    app_param = '01234567890123456789012345678901'
    key_handle = '\x01\x02\x03\x04'

    mock_transport.SendMsgBytes.return_value = bytearray(
        [0x01, 0x02, 0x90, 0x00])

    reply = sk.CmdAuthenticate(challenge_param, app_param, key_handle)
    self.assertEquals(reply, bytearray([0x01, 0x02]))
    self.assertEquals(mock_transport.SendMsgBytes.call_count, 1)
    (sent_msg,), _ = mock_transport.SendMsgBytes.call_args
    self.assertEquals(sent_msg[0:4], bytearray([0x00, 0x02, 0x03, 0x00]))
    self.assertEquals(
        sent_msg[7:-2],
        bytearray(challenge_param + app_param + bytearray([4, 1, 2, 3, 4])))
Esempio n. 9
0
  def testVersionFallback(self):
    mock_transport = mock.MagicMock()
    sk = hardware.SecurityKey(mock_transport)

    mock_transport.SendMsgBytes.side_effect = [
        bytearray([0x67, 0x00]),
        bytearray('U2F_V2\x90\x00')]

    reply = sk.CmdVersion()
    self.assertEquals(reply, bytearray('U2F_V2'))
    self.assertEquals(mock_transport.SendMsgBytes.call_count, 2)
    (sent_msg,), _ = mock_transport.SendMsgBytes.call_args_list[0]
    self.assertEquals(len(sent_msg), 7)
    self.assertEquals(sent_msg[0:4], bytearray([0x00, 0x03, 0x00, 0x00]))
    self.assertEquals(sent_msg[4:7], bytearray([0x00, 0x00, 0x00]))  # Le
    (sent_msg,), _ = mock_transport.SendMsgBytes.call_args_list[1]
    self.assertEquals(len(sent_msg), 9)
    self.assertEquals(sent_msg[0:4], bytearray([0x00, 0x03, 0x00, 0x00]))
    self.assertEquals(sent_msg[4:7], bytearray([0x00, 0x00, 0x00]))  # Lc
    self.assertEquals(sent_msg[7:9], bytearray([0x00, 0x00]))  # Le
Esempio n. 10
0
    def testRegisterInvalidParams(self):
        mock_transport = mock.MagicMock()
        sk = hardware.SecurityKey(mock_transport)

        self.assertRaises(errors.InvalidRequestError, sk.CmdRegister, '1234',
                          '1234')