def connect(self, protocol=None, mode=None, disposition=None): '''Disconnect and reconnect in exclusive mode PCSCCardconnections.''' CardConnectionDecorator.connect(self, protocol, mode, disposition) component = self.component while True: if isinstance( component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): pcscprotocol = PCSCCardConnection.translateprotocolmask( protocol) if 0 == pcscprotocol: pcscprotocol = component.getProtocol() if component.hcard is not None: hresult = SCardDisconnect(component.hcard, SCARD_LEAVE_CARD) if hresult != 0: raise CardConnectionException( 'Failed to disconnect: ' + SCardGetErrorMessage(hresult)) hresult, component.hcard, dwActiveProtocol = SCardConnect( component.hcontext, str(component.reader), SCARD_SHARE_EXCLUSIVE, pcscprotocol) if hresult != 0: raise CardConnectionException( 'Failed to connect with SCARD_SHARE_EXCLUSIVE' + SCardGetErrorMessage(hresult)) # print('reconnected exclusive') break if hasattr(component, 'component'): component = component.component else: break
def getATR(self): """Return card ATR""" CardConnection.getATR(self) if None == self.hcard: raise CardConnectionException('Card not connected') hresult, reader, state, protocol, atr = SCardStatus(self.hcard) if hresult != 0: raise CardConnectionException('Failed to get status: ' + SCardGetErrorMessage(hresult)) return atr
def reconnect(self, protocol=None, mode=None, disposition=None): """Reconnect to the card. If protocol is not specified, connect with the default connection protocol. If mode is not specified, connect with SCARD_SHARE_SHARED. If disposition is not specified, do a warm reset (SCARD_RESET_CARD)""" CardConnection.reconnect(self, protocol) if self.hcard is None: raise CardConnectionException('Card not connected') pcscprotocol = translateprotocolmask(protocol) if 0 == pcscprotocol: pcscprotocol = self.getProtocol() if mode is None: mode = SCARD_SHARE_SHARED # store the way to dispose the card if disposition is None: disposition = SCARD_RESET_CARD self.disposition = disposition hresult, dwActiveProtocol = SCardReconnect(self.hcard, mode, pcscprotocol, self.disposition) if hresult != 0: self.hcard = None if hresult in (SCARD_W_REMOVED_CARD, SCARD_E_NO_SMARTCARD): raise NoCardException('Unable to reconnect', hresult=hresult) else: raise CardConnectionException( 'Unable to reconnect with protocol: ' + \ dictProtocol[pcscprotocol] + '. ' + \ SCardGetErrorMessage(hresult)) protocol = 0 if dwActiveProtocol == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1: # special case for T0 | T1 # this happen when mode=SCARD_SHARE_DIRECT and no protocol is # then negociated with the card protocol = CardConnection.T0_protocol | CardConnection.T1_protocol else: for p in dictProtocol: if p == dwActiveProtocol: protocol = eval("CardConnection.%s_protocol" % dictProtocol[p]) PCSCCardConnection.setProtocol(self, protocol)
def updateReadersStateList(self): # If there is no observer remaining, there is no need to update the readers states list if not self.obs: return self.mutex.acquire() hresult, readers_list = SCardListReaders(self.hcontext, []) if hresult != SCARD_S_SUCCESS: raise CardConnectionException('Unable to list readers: ' + SCardGetErrorMessage(hresult)) # Add the reader in the readers state list if it is not present for reader in readers_list: found = False for state in self.readers_state_list: if state[0] == reader: found = True break if not found: self.readers_state_list.append((reader, SCARD_STATE_UNAWARE, [])) # Remove the reader from the readers state list if it is not present in the readers list for state in self.readers_state_list: if state[0] not in readers_list: self.readers_state_list.remove(state) # Use Pnp Notification only if supported if self.isPnpSupported(): self.readers_state_list.append(('\\\\?PnP?\\Notification', SCARD_STATE_UNAWARE, [])) self.mutex.release()
def __del__(self): """Destructor. Clean PCSC connection resources.""" # race condition: module CardConnection # can disappear before __del__ is called self.disconnect() hresult = SCardReleaseContext(self.hcontext) if hresult != 0: raise CardConnectionException('Failed to release context: ' + SCardGetErrorMessage(hresult)) CardConnection.__del__(self)
def doTransmit(self, bytes, protocol=None): """Transmit an apdu to the card and return response apdu. @param bytes: command apdu to transmit (list of bytes) @param protocol: the transmission protocol, from CardConnection.T0_protocol, CardConnection.T1_protocol, or CardConnection.RAW_protocol @return: a tuple (response, sw1, sw2) where sw1 is status word 1, e.g. 0x90 sw2 is status word 2, e.g. 0x1A response are the response bytes excluding status words """ if protocol is None: protocol = self.getProtocol() CardConnection.doTransmit(self, bytes, protocol) pcscprotocolheader = translateprotocolheader(protocol) if 0 == pcscprotocolheader: raise CardConnectionException( 'Invalid protocol in transmit: must be ' + \ 'CardConnection.T0_protocol, ' + \ 'CardConnection.T1_protocol, or ' + \ 'CardConnection.RAW_protocol') if self.hcard is None: raise CardConnectionException('Card not connected') hresult, response = SCardTransmit(self.hcard, pcscprotocolheader, bytes) if hresult != 0: raise CardConnectionException( 'Failed to transmit with protocol ' + \ dictProtocolHeader[pcscprotocolheader] + '. ' + \ SCardGetErrorMessage(hresult)) if len(response) < 2: raise CardConnectionException('Card returned no valid response') sw1 = (response[-2] + 256) % 256 sw2 = (response[-1] + 256) % 256 data = [(x + 256) % 256 for x in response[:-2]] return list(data), sw1, sw2
def __init__(self, reader): """Construct a new PCSC card connection. reader: the reader in which the smartcard to connect to is located. """ CardConnection.__init__(self, reader) self.hcard = None hresult, self.hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != 0: raise CardConnectionException('Failed to establish context : ' + SCardGetErrorMessage(hresult))
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
def run(self): """Runs until stopEvent is notified, and notify observers of all reader insertion/removal. """ logging.debug("thread running: %d", self.observable.countObservers()) while not self.stopEvent.isSet(): try: added_readers = [] removed_readers = [] # Update the readers state list to add potentially new found readers and delete removed ones self.observable.updateReadersStateList() logging.debug("listening for changes...") hresult, new_readers_state = SCardGetStatusChange(self.observable.hcontext, self.polling_timeout, self.observable.getReadersStateList()) logging.debug("changes acquired!") logging.debug("states: %s", new_readers_state) # Listen only to others result errors if hresult != SCARD_S_SUCCESS and hresult != SCARD_E_UNKNOWN_READER and hresult != SCARD_E_TIMEOUT: if hresult == SCARD_E_CANCELLED: break else: raise CardConnectionException('Unable to get status change: ' + SCardGetErrorMessage(hresult)) # Update observable readers state list and search for added or removed cards self.observable.setReadersStateList(new_readers_state) for state in self.observable.getReadersStateList(): reader, event, atr = state if event & SCARD_STATE_CHANGED: if event & SCARD_STATE_UNKNOWN or event & SCARD_STATE_IGNORE or event & SCARD_STATE_UNAVAILABLE: # If the event is telling that a reader is not available/existing remove it from readers list logging.debug("reader removed: %s", reader) removed_readers.append(reader) self.observable.removeReader(reader) elif reader not in self.observable.getReadersList() and not reader == '\\\\?PnP?\\Notification': # If the event is telling that there is change on a reader which is not present in the readers list, add it logging.debug("reader added: %s", reader) added_readers.append(reader) self.observable.addReader(reader) # Update observers if we have added or removed cards if added_readers != [] or removed_readers != []: self.observable.setChanged() self.observable.notifyObservers((added_readers, removed_readers)) except Exception: # FIXME Tighten the exceptions caught by this block traceback.print_exc() self.stopEvent.set()
def doTransmit(self, bytes, protocol=None): """Transmit an apdu to the card and return response apdu. bytes: command apdu to transmit (list of bytes) protocol: the transmission protocol, from CardConnection.T0_protocol, CardConnection.T1_protocol, or CardConnection.RAW_protocol return: a tuple (response, sw1, sw2) where sw1 is status word 1, e.g. 0x90 sw2 is status word 2, e.g. 0x1A response are the response bytes excluding status words """ if None == protocol: protocol = self.getProtocol() CardConnection.doTransmit(self, bytes, protocol) pcscprotocolheader = translateprotocolheader(protocol) if 0 == pcscprotocolheader: raise CardConnectionException( 'Invalid protocol in transmit: must be CardConnection.T0_protocol, CardConnection.T1_protocol, or CardConnection.RAW_protocol' ) if None == self.hcard: raise CardConnectionException('Card not connected') hresult, response = SCardTransmit(self.hcard, pcscprotocolheader, bytes) if hresult != 0: raise CardConnectionException( 'Failed to transmit with protocol ' + dictProtocolHeader[pcscprotocolheader] + '. ' + SCardGetErrorMessage(hresult)) sw1 = (response[-2] + 256) % 256 sw2 = (response[-1] + 256) % 256 data = map(lambda x: (x + 256) % 256, response[:-2]) return data, sw1, sw2
def unlock(self): '''Unlock card with SCardEndTransaction.''' component = self.component while True: if isinstance(component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): hresult = SCardEndTransaction(component.hcard, SCARD_LEAVE_CARD) if 0 != hresult: raise CardConnectionException('Failed to unlock with SCardEndTransaction' + SCardGetErrorMessage(hresult)) else: #print 'unlocked' pass break if hasattr(component, 'component'): component = component.component else: break
def disconnect(self): """Disconnect from the card.""" # when __del__() is invoked in response to a module being deleted, # e.g., when execution of the program is done, other globals referenced # by the __del__() method may already have been deleted. # this causes CardConnection.disconnect to except with a TypeError try: CardConnection.disconnect(self) except TypeError: pass if None != self.hcard: hresult = SCardDisconnect(self.hcard, self.disposition) if hresult != 0: raise CardConnectionException('Failed to disconnect: ' + SCardGetErrorMessage(hresult)) self.hcard = None
def doControl(self, controlCode, bytes=[]): """Transmit a control command to the reader and return response. controlCode: control command bytes: command data to transmit (list of bytes) return: response are the response bytes (if any) """ CardConnection.doControl(self, controlCode, bytes) hresult, response = SCardControl(self.hcard, controlCode, bytes) if hresult != 0: raise CardConnectionException('Failed to control ' + SCardGetErrorMessage(hresult)) data = map(lambda x: (x + 256) % 256, response) return data
def open(self): PcscReader.open(self) # We could pass this into connect, but this is clearer self.atr = ATR(self.conn.getATR()) if DEBUG: print('ATR: %s' % self.atr) self.atr.dump() if not self.atr.isT0Supported(): self.close() raise CardConnectionException('Reader reports T0 protocol not supported') if DEBUG: print('Firmware version %s' % self.firmware_version()) self.pn532.set_retries(0, 0, 0)
def connect(self, protocol=None, mode=None, disposition=None): """Connect to the card. If protocol is not specified, connect with the default connection protocol. If mode is not specified, connect with SCARD_SHARE_SHARED.""" CardConnection.connect(self, protocol) pcscprotocol = translateprotocolmask(protocol) if 0 == pcscprotocol: pcscprotocol = self.getProtocol() if mode == None: mode = SCARD_SHARE_SHARED # store the way to dispose the card if disposition == None: disposition = SCARD_UNPOWER_CARD self.disposition = disposition hresult, self.hcard, dwActiveProtocol = SCardConnect( self.hcontext, str(self.reader), mode, pcscprotocol) if hresult != 0: self.hcard = None if SCARD_W_REMOVED_CARD == hresult: raise NoCardException( 'Unable to connect: ' + \ SCardGetErrorMessage(hresult)) else: raise CardConnectionException( 'Unable to connect with protocol: ' + \ dictProtocol[pcscprotocol] + '. ' + \ SCardGetErrorMessage(hresult)) protocol = 0 if dwActiveProtocol == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1: # special case for T0 | T1 # this happen when mode=SCARD_SHARE_DIRECT and no protocol is # then negociated with the card protocol = CardConnection.T0_protocol | CardConnection.T1_protocol else: for p in dictProtocol: if p == dwActiveProtocol: protocol = eval("CardConnection.%s_protocol" % dictProtocol[p]) PCSCCardConnection.setProtocol(self, protocol)
def lock(self): '''Lock card with SCardBeginTransaction.''' component = self.component while True: if isinstance( component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): hresult = SCardBeginTransaction(component.hcard) if 0 != hresult: raise CardConnectionException( 'Failed to lock with SCardBeginTransaction: ' + SCardGetErrorMessage(hresult)) else: # print('locked') pass break if hasattr(component, 'component'): component = component.component else: break
def connect(self, protocol=None, mode=None, disposition=None): """Connect to the card. If protocol is not specified, connect with the default connection protocol. If mode is not specified, connect with SCARD_SHARE_SHARED.""" CardConnection.connect(self, protocol) pcscprotocol = translateprotocolmask(protocol) if 0 == pcscprotocol: pcscprotocol = self.getProtocol() if mode == None: mode = SCARD_SHARE_SHARED # store the way to dispose the card if disposition == None: disposition = SCARD_UNPOWER_CARD self.disposition = disposition hresult, self.hcard, dwActiveProtocol = SCardConnect( self.hcontext, str(self.reader), mode, pcscprotocol) if hresult != 0: self.hcard = None if SCARD_W_REMOVED_CARD == hresult: raise NoCardException('Unable to connect: ' + SCardGetErrorMessage(hresult)) else: raise CardConnectionException( 'Unable to connect with protocol: ' + dictProtocol[pcscprotocol] + '. ' + SCardGetErrorMessage(hresult)) protocol = 0 for p in dictProtocol: if p == dwActiveProtocol: protocol = eval("CardConnection.%s_protocol" % dictProtocol[p]) PCSCCardConnection.setProtocol(self, protocol)
def getReaders(self): if self.hcontext is None: self.__establishContext() hresult, readers_list = SCardListReaders(self.hcontext, []) if hresult != SCARD_S_SUCCESS: raise CardConnectionException('Unable to list readers: ' + SCardGetErrorMessage(hresult))
def run(self): """Runs until stopEvent is notified, and notify observers of all card insertion/removal. """ logging.debug("thread running: %d", self.observable.countObservers()) while not self.stopEvent.isSet(): try: added_cards = [] removed_cards = [] # Update the readers state list to add potentially new found readers and delete removed ones self.observable.updateReadersStateList() logging.debug("listening for changes...") hresult, new_readers_state = SCardGetStatusChange( self.observable.hcontext, self.polling_timeout, self.observable.getReadersStateList()) logging.debug("changes acquired!") logging.debug("states: %s", new_readers_state) # Listen only to others result errors if hresult != SCARD_S_SUCCESS and hresult != SCARD_E_UNKNOWN_READER and hresult != SCARD_E_TIMEOUT: if hresult == SCARD_E_CANCELLED: break else: raise CardConnectionException( 'Unable to get status change: ' + SCardGetErrorMessage(hresult)) # Update observable readers state list and search for added or removed cards self.observable.setReadersStateList(new_readers_state) for state in self.observable.getReadersStateList(): reader, event, atr = state if event & SCARD_STATE_CHANGED: # Check if we have a card present and an atr (is mute + atr a thing ?) if (event & SCARD_STATE_PRESENT or event & SCARD_STATE_MUTE) and len(atr) != 0: # If the event is telling that a card is present/mute add it to the cards list card = Card(reader, atr) logging.debug( "card added with atr: %s on reader %s", card.atr, card.reader) added_cards.append(card) self.observable.addCard(reader, atr) # Check if we have a card empty slot and if the card is in the list (change+empty can happen after SCARD_STATE_UNAWARE fo ex.) elif event & SCARD_STATE_EMPTY and reader in self.observable.getCardsList( ).keys(): # If the event is telling that reader is empty remove it from the cards list atr = self.observable.getCardsList().get(reader) card = Card(reader, atr) logging.debug( "card removed with atr: %s on reader %s", card.atr, card.reader) removed_cards.append(card) self.observable.removeCard(reader) elif event & SCARD_STATE_UNKNOWN or event & SCARD_STATE_IGNORE or event & SCARD_STATE_UNAVAILABLE: # If the event is telling that a reader is not available/existing remove the card on it from the cards list if reader in self.observable.cards_list.keys(): logging.debug( "reader removed, card removed with atr: %s on reader %s", card.atr, card.reader) removed_cards.append(card) self.observable.removeCard(reader) # Update observers if we have added or removed cards if added_cards != [] or removed_cards != []: self.observable.setChanged() self.observable.notifyObservers( (added_cards, removed_cards)) except Exception: # FIXME Tighten the exceptions caught by this block traceback.print_exc() self.stopEvent.set()
def __releaseContext(self): hresult = SCardReleaseContext(self.hcontext) if hresult != SCARD_S_SUCCESS: raise CardConnectionException('Failed to release context: ' + SCardGetErrorMessage(hresult)) self.hcontext = None
def __establishContext(self): hresult, self.hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: raise CardConnectionException('Failed to establish context: ' + SCardGetErrorMessage(hresult))