Пример #1
0
 def send_apdu(self, cl, ins, p1, p2, data):
     apdu = [cl, ins, p1, p2, len(data)] + [byte2int(b) for b in data]
     hresult, response = SCardTransmit(self._card, self._protocol, apdu)
     if hresult != SCARD_S_SUCCESS:
         raise Exception('Failed to transmit: ' +
                         SCardGetErrorMessage(hresult))
     status = response[-2] << 8 | response[-1]
     return b''.join(int2byte(i) for i in response[:-2]), status
Пример #2
0
def collect_signatures(connection, output, finish):
    """Collect signatures from the card and output, until finish."""
    card = connection.component.hcard
    proto = translateprotocolheader(connection.component.getProtocol())
    i = 0
    while not finish(i):
        elapsed = -time.perf_counter_ns()
        result = SCardTransmit(card, proto, SIGN)
        elapsed += time.perf_counter_ns()
        resp = [(x + 256) % 256 for x in result[1][:-2]]
        output(i, elapsed, bytes(resp))
        i += 1
Пример #3
0
    def transceive(self, bytes):

        if not self.card_connection.hcard:
            raise PCSCNotConnected("Tried to transit to non-open connection: {}".format(self.card_connection))

        protocol = self.card_connection.getProtocol()
        pcscprotocolheader = translateprotocolheader(protocol)

        # http://pyscard.sourceforge.net/epydoc/smartcard.scard.scard-module.html#SCardTransmit
        hresult, response = SCardTransmit(self.card_connection.hcard, pcscprotocolheader, bytes)

        if hresult != 0:
            raise CardConnectionException('Failed to transmit with protocol ' + str(pcscprotocolheader) + '. ' + SCardGetErrorMessage(hresult))
        return response
Пример #4
0
    def launch_command(self, reader_name, command):
        try:
            hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
            assert hresult == SCARD_S_SUCCESS

            hresult, hcard, dwActiveProtocol = SCardConnect(
                hcontext, reader_name, SCARD_SHARE_SHARED,
                SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)

            # hresult, response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0xCA,0x00,0x00,0x00])
            hresult, response = SCardTransmit(hcard, dwActiveProtocol, command)

            return response[:len(response) - 2]
        except SystemError as err:
            print("Error in launch command : {}".format(err))
            return None
Пример #5
0
    def update(self, observable, actions):
        addedcards, removedcards = actions

        for card in addedcards:
            connection = card.createConnection()
            connection.connect()

            # This will log raw card traffic to console
            connection.addObserver(ConsoleCardConnectionObserver())
            comp = connection.component

            if not comp.hcard:
                raise Exception(
                    "Tried to transit to non-open connection: {}".format(comp))

            protocol = comp.getProtocol()
            pcscprotocolheader = translateprotocolheader(protocol)

            # http://pyscard.sourceforge.net/epydoc/smartcard.scard.scard-module.html#SCardTransmit
            msg = [0xff, 0xca, 0x00, 0x00, 0x04]

            hresult, response = SCardTransmit(comp.hcard, pcscprotocolheader,
                                              msg)

            if hresult != 0:
                raise Exception('Failed to transmit with protocol ' +
                                str(pcscprotocolheader) + '. ' +
                                SCardGetErrorMessage(hresult))

            resp_bytes = ['{0:02x}'.format(r) for r in response]
            status = ''.join(resp_bytes[-2:])

            if status != '9000':
                return
                # raise Exception('Could not read card.')

            serial = ''.join(resp_bytes[0:4])
            self.on_card(serial)
Пример #6
0
def transmit_command(command):
    hresult, response = SCardTransmit(hcard, dwActiveProtocol, command)
    if hresult != SCARD_S_SUCCESS or response[1] != 0x9F:
        raise error("Failed to select ARA: " + SCardGetErrorMessage(hresult))
    print_hex_response(response)
Пример #7
0
while True:
    hresult, newstates = SCardGetStatusChange(hcontext, 5000, newstates)
    for reader, eventstate, atr in newstates:
        if eventstate & SCARD_STATE_PRESENT:
            # play_sound('success')
            hresult, hcard, dw_active_protocol = SCardConnect(
                hcontext, reader, SCARD_SHARE_SHARED,
                SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
            # Turn off NFC reader default buzzer
            # hresult, response = SCardTransmit(
            #     hcard,
            #     dw_active_protocol,
            #     [0xFF, 0x00, 0x52, 0x00, 0x00]
            # )
            hresult, reader, state, protocol, atr = SCardStatus(hcard)
            hresult, response = SCardTransmit(hcard, dw_active_protocol,
                                              [0xFF, 0xCA, 0x00, 0x00, 0x00])
            if response[-2:] == [0x90, 0x00]:
                tag_id = response[:-2]
                # POST the card data
                data = {
                    'lens': {
                        'atr': hex_array(atr),
                        'uid': hex_array(tag_id)
                    },
                    'tap_datetime': datetime_now(),  # ISO8601 format
                    'label': LABEL,
                    'data': {
                        'lens_reader': {
                            'mac_address': get_mac_address(),
                            'reader_ip': ip_address,
                            'reader_name': reader_name,