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 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 __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 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 __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 doGetAttrib(self, attribId): """get an attribute attribId: Identifier for the attribute to get return: response are the attribute byte array """ CardConnection.doGetAttrib(self, attribId) hresult, response = SCardGetAttrib(self.hcard, attribId) if hresult != 0: raise SmartcardException('Failed to getAttrib ' + SCardGetErrorMessage(hresult)) return response
def doGetAttrib(self, attribId): """get an attribute attribId: Identifier for the attribute to get return: response are the attribute byte array """ CardConnection.doGetAttrib(self, attribId) hresult, response = SCardGetAttrib(self.hcard, attribId) if hresult != 0: raise SmartcardException( 'Failed to getAttrib ' + SCardGetErrorMessage(hresult)) return response
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 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 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 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 SmartcardException('Failed to control ' + SCardGetErrorMessage(hresult)) data = [(x + 256) % 256 for x in response] return list(data)
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 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 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