Example #1
0
    def send_apdu(self, ins, p1=0, p2=0, data=b''):
        """
        Sends an APDU to the device, and waits for a response.
        """
        if data is None:
            data = b''
        elif isinstance(data, int):
            data = int2byte(data)

        size = len(data)
        l0 = size >> 16 & 0xff
        l1 = size >> 8 & 0xff
        l2 = size & 0xff
        apdu_data = struct.pack('B B B B B B B %is B B' % size, 0, ins, p1, p2,
                                l0, l1, l2, data, 0x04, 0x00)
        try:
            resp = self._do_send_apdu(apdu_data)
        except Exception as e:
            # TODO Use six.reraise if/when Six becomes an agreed dependency.
            raise exc.DeviceError(e)
        status = struct.unpack('>H', resp[-2:])[0]
        data = resp[:-2]
        if status != APDU_OK:
            raise exc.APDUError(status)
        return data
Example #2
0
    def send_apdu(self, ins, p1=0, p2=0, data=''):
        """
        Sends an APDU to the device, and waits for a response.
        """
        if data is None:
            data = ''
        elif isinstance(data, int):
            data = chr(data)

        size = len(data)
        l0 = size >> 16 & 0xff
        l1 = size >> 8 & 0xff
        l2 = size & 0xff
        apdu_data = "%c%c%c%c%c%c%c%s%c%c" % \
            (0, ins, p1, p2, l0, l1, l2, data, 0x04, 0x00)
        try:
            resp = self._do_send_apdu(apdu_data)
        except Exception as e:
            # Wrap exception, keeping trace
            raise exc.DeviceError(e), None, sys.exc_info()[2]
        status = int(resp[-2:].encode('hex'), 16)
        data = resp[:-2]
        if status != APDU_OK:
            raise exc.APDUError(status)
        return data
Example #3
0
 def test_init(self):
     error = exc.APDUError(0x3039)
     self.assertEqual(error.args[0], '0x3039')
     self.assertEqual(error.code, 0x3039)
     self.assertEqual(error.sw1, 0x30)
     self.assertEqual(error.sw2, 0x39)
Example #4
0
 def send_apdu(self, ins, p1=0, p2=0, data=b''):
     if ins == INS_ENROLL:
         return self._register(data)
     elif ins == INS_SIGN:
         return self._authenticate(data)
     raise exc.APDUError(0x6d00)  # INS not supported.