Ejemplo n.º 1
0
  def testMsg(self):
    fake_hid_dev = util.FakeHidDevice(
        bytearray([0x00, 0x00, 0x00, 0x01]), bytearray([0x01, 0x90, 0x00]))
    t = hidtransport.UsbHidTransport(fake_hid_dev)

    reply = t.SendMsgBytes([0x00, 0x01, 0x00, 0x00])
    self.assertEqual(reply, bytearray([0x01, 0x90, 0x00]))
Ejemplo n.º 2
0
    def _discover_devices(communication_queue, abort_event):
        """
        Discover FIDO2 authenticators
        :param communication_queue: Queue to put newly discovered
                authenticator in
        :param abort_event: Event to abort discovery with
        :return:
        """
        discovered = []

        # look for new devices until abort
        while not abort_event.is_set():
            new_devices = []
            for d in hidtransport.hid.Enumerate():
                if hidtransport.HidUsageSelector(d) and \
                        d not in [e.descriptor for e in discovered]:
                    # new device found
                    dev = hidtransport.hid.Open(d['path'])
                    dev = CtapHidDevice(d, hidtransport.UsbHidTransport(dev))
                    new_devices.append(dev)
                    discovered.append(dev)
            # notify main thread
            if new_devices:
                communication_queue.put(
                    (CommunicationObject.DEVICE, new_devices))
            # slow down busy wait
            time.sleep(0.1)

        # on abort put an empty list in the queue to stop the main thread
        # from waiting for results
        communication_queue.put((CommunicationObject.DEVICE, []))
Ejemplo n.º 3
0
  def testFragmentedResponseMsg(self):
    body = bytearray([x % 256 for x in range(0, 1000)])
    fake_hid_dev = util.FakeHidDevice(bytearray([0x00, 0x00, 0x00, 0x01]), body)
    t = hidtransport.UsbHidTransport(fake_hid_dev)

    reply = t.SendMsgBytes([0x00, 0x01, 0x00, 0x00])
    # Confirm we properly reassemble the message
    self.assertEqual(reply, bytearray(x % 256 for x in range(0, 1000)))
Ejemplo n.º 4
0
  def testFragmentedSendApdu(self):
    body = bytearray(x % 256 for x in range(0, 1000))
    fake_hid_dev = util.FakeHidDevice(
        bytearray([0x00, 0x00, 0x00, 0x01]), [0x90, 0x00])
    t = hidtransport.UsbHidTransport(fake_hid_dev)

    reply = t.SendMsgBytes(body)
    self.assertEqual(reply, bytearray([0x90, 0x00]))
    # 1 init packet from creating transport, 18 packets to send
    # the fragmented message
    self.assertEqual(len(fake_hid_dev.received_packets), 18)
Ejemplo n.º 5
0
  def testMsgBusy(self):
    fake_hid_dev = util.FakeHidDevice(
        bytearray([0x00, 0x00, 0x00, 0x01]), bytearray([0x01, 0x90, 0x00]))
    t = hidtransport.UsbHidTransport(fake_hid_dev)

    # Each call will retry twice: the first attempt will fail after 2 retreis,
    # the second will succeed on the second retry.
    fake_hid_dev.SetChannelBusyCount(3)
    with mock.patch.object(hidtransport, 'time') as _:
      six.assertRaisesRegex(self, OSError, '^Device Busy', t.SendMsgBytes,
                            [0x00, 0x01, 0x00, 0x00])

      reply = t.SendMsgBytes([0x00, 0x01, 0x00, 0x00])
      self.assertEqual(reply, bytearray([0x01, 0x90, 0x00]))
Ejemplo n.º 6
0
 def testInit(self):
   fake_hid_dev = util.FakeHidDevice(bytearray([0x00, 0x00, 0x00, 0x01]))
   t = hidtransport.UsbHidTransport(fake_hid_dev)
   self.assertEqual(t.cid, bytearray([0x00, 0x00, 0x00, 0x01]))
   self.assertEqual(t.u2fhid_version, 0x01)
Ejemplo n.º 7
0
  def testPing(self):
    fake_hid_dev = util.FakeHidDevice(bytearray([0x00, 0x00, 0x00, 0x01]))
    t = hidtransport.UsbHidTransport(fake_hid_dev)

    reply = t.SendPing(b'1234')
    self.assertEqual(reply, b'1234')