def testcase_Test_ISO78164_ErrorCheckingChain(self): """Test error chain with Test checker followed by ISO7816-4 checker.""" errorchain = [] errorchain = [ErrorCheckingChain(errorchain, TestErrorChecker()), ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker())] # TestErrorChecker is answering first, i.e. CustomSWException for sw2 in [0x00, 0x81] + list(range(0xc0, 0xcf + 1)): self.assertRaises(CustomSWException, errorchain[0], [], 0x63, sw2)
def connectReaderFromCardFun(envi, cardIndex=None): "establish a link between the shell and a card" if "connection" in envi and envi["connection"] != None: print "there is already a connection to reader " + envi[ "connectedReader"] return if cardIndex == None: indice = 0 if len(cardobserver.cardList) > 1: Executer.printOnShell( "Warning : the information comes from the first card available in the list, there are others cards" ) else: indice = cardIndex if len(cardobserver.cardList) == 0: Executer.printOnShell("there is no card available") return try: card = cardobserver.cardList[indice] except IndexError: Executer.printOnShell("invalid indice") return False try: envi["connection"] = card.createConnection( ) #create a connection on the reader except Exception as e: Executer.printOnShell(str(e)) return False try: envi["connection"].connect() #create a connection on the card except Exception as e: del envi["connection"] Executer.printOnShell(str(e)) return False card.connection = envi["connection"] card.connection.card = card envi["connectedReader"] = card.connection.getReader() errorchain = [] errorchain = [ ErrorCheckingChain(errorchain, ISO7816_8ErrorChecker()), ErrorCheckingChain(errorchain, ISO7816_9ErrorChecker()), ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker()) ] envi["connection"].setErrorCheckingChain(errorchain) return True
def testcase_ISO78164_Test_ErrorCheckingChain(self): """Test error chain with ISO7816-4 checker followed by Test checker.""" errorchain = [] errorchain = [ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker()), ErrorCheckingChain(errorchain, TestErrorChecker())] # ISO7816-4 is answering first on the next, i.e # WarningProcessingException for sw2 in [0x00, 0x81] + list(range(0xc0, 0xcf + 1)): self.assertRaises( smartcard.sw.SWExceptions.WarningProcessingException, errorchain[0], [], 0x63, sw2)
def __init__(self, clientkey_enc, clientcert_enc, clientkey_sig, clientcert_sig, tp_key, tp_cert, key_transport=False, mutual_auth=True, cert_verif=True, debug=False, chan=0, vuln=True): self.vuln = vuln if chan > 3 or chan < 0: raise ValueError("Logical channel number must be in [0, 3]") self.cla = self.DEFAULT_CLA + chan self.security_lvl = 0x42 # ANY_AUTHENTICATED + C_DECRYPTION self.clientKey_enc = RsaKeyHolder(clientkey_enc) self.clientCert_enc = CVCertificate(clientcert_enc) self.clientKey_sig = RsaKeyHolder(clientkey_sig) self.clientCert_sig = CVCertificate(clientcert_sig) self.tp_key = RsaKeyHolder(tp_key) self.tp_cert = CVCertificate(tp_cert) self.cardKey_enc = None self.cardCert_enc = None self.cardKey_sig = None self.cardCert_sig = None self.key_transport = key_transport self.mutual_auth = mutual_auth self.cert_verif = cert_verif self.session_keys = [] self.session_ivs = [] self.client_secret = [] self.card_secret = [] # Wait for any card to be put on a reader, and initiate a connection card_type = AnyCardType() card_request = CardRequest(timeout=None, cardType=card_type) self.card = card_request.waitforcard() self.card.connection.connect() error_chain = [] error_chain = [ ErrorCheckingChain(error_chain, ISO7816_4ErrorChecker()) ] self.card.connection.setErrorCheckingChain(error_chain) # Set an observer for debugging if debug: observer = SCP10Observer() self.card.connection.addObserver(observer)
def __init__(self): self.curreader = None self.s = None self.reset() self.command_acc = "" # empty unterminated command # error detection errorchain = [] errorchain = [ErrorCheckingChain(errorchain, ISO7816_9ErrorChecker())] errorchain = [ErrorCheckingChain(errorchain, ISO7816_8ErrorChecker())] errorchain = [ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker())] self.errorchain = errorchain # readline history histfile = os.path.expanduser('~/.scriptim_history') try: init_history(histfile) except NameError: pass
def testcase_ISO78164_Test_ErrorCheckingChain_filtering(self): """Test error chain with ISO7816-4 checker followed by Test checker.""" errorchain = [] errorchain = [ ErrorCheckingChain(errorchain, ISO7816_8ErrorChecker()), ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker()), ErrorCheckingChain(errorchain, ISO7816_4_SW1ErrorChecker()) ] # don't care about Warning Exceptions errorchain[0].addFilterException( smartcard.sw.SWExceptions.WarningProcessingException) for sw2 in range(0x00, 0xff): # should not raise errorchain[0]([], 0x62, sw2) errorchain[0]([], 0x63, sw2) # should raise self.assertRaises( smartcard.sw.SWExceptions.ExecutionErrorException, errorchain[0], [], 0x64, sw2)
def __init__(self, connection=None): self.connection = connection self.ef = None self.ef_size = None self.cached_efs = {} self.start_time = time() self.part_start_time = {} if not self.connection: r = get_first_reader() self.connection = r.createConnection() errorchain = [] errorchain = [ErrorCheckingChain(errorchain, ISO7816_9ErrorChecker())] errorchain = [ErrorCheckingChain(errorchain, ISO7816_8ErrorChecker())] errorchain = [ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker())] self.connection.setErrorCheckingChain(errorchain) self.connection.addSWExceptionToFilter(WarningProcessingException) # observer = ConsoleCardConnectionObserver() # self.connection.addObserver(observer) self.connection.connect()
from smartcard.sw.ErrorCheckingChain import ErrorCheckingChain from smartcard.sw.ISO7816_4ErrorChecker import ISO7816_4ErrorChecker from smartcard.sw.ISO7816_8ErrorChecker import ISO7816_8ErrorChecker from smartcard.sw.SWExceptions import SWException, WarningProcessingException import hashlib # request any card cardtype = AnyCardType() cardrequest = CardRequest(timeout=10, cardType=cardtype) cardservice = cardrequest.waitforcard() # our error checking chain errorchain = [] errorchain = [ ErrorCheckingChain(errorchain, ISO7816_8ErrorChecker()), ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker()) ] cardservice.connection.setErrorCheckingChain(errorchain) # a console tracer observer = ConsoleCardConnectionObserver() cardservice.connection.addObserver(observer) # send a few apdus; exceptions will occur upon errors cardservice.connection.connect() def as_list(ba): return [x for x in ba]
SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02] DF_TELECOM = [0x7F, 0x10] if __name__ == '__main__': print 'Insert a card within 10 seconds' print 'Cards without a DF_TELECOM will except' # request any card cardtype = AnyCardType() cardrequest = CardRequest(timeout=10, cardType=cardtype) cardservice = cardrequest.waitforcard() # our error checking chain errorchain = [] errorchain = [ErrorCheckingChain([], MyErrorChecker())] cardservice.connection.setErrorCheckingChain(errorchain) # attach the console tracer observer = ConsoleCardConnectionObserver() cardservice.connection.addObserver(observer) # send a few apdus; exceptions will occur upon errors cardservice.connection.connect() try: SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02] DF_TELECOM = [0x7F, 0x10] apdu = SELECT + DF_TELECOM response, sw1, sw2 = cardservice.connection.transmit(apdu) if sw1 == 0x9F:
DF_TELECOM = [0x7F, 0x10] if __name__ == '__main__': print('Insert a card within 10 seconds') print('Cards without a DF_TELECOM will except') # request any card type cardtype = AnyCardType() cardrequest = CardRequest(timeout=10, cardType=cardtype) cardservice = cardrequest.waitforcard() # use ISO7816-4 and ISO7816-8 error checking strategy # first check iso7816_8 errors, then iso7816_4 errors errorchain = [] errorchain = [ErrorCheckingChain(errorchain, ISO7816_9ErrorChecker())] errorchain = [ErrorCheckingChain(errorchain, ISO7816_8ErrorChecker())] errorchain = [ErrorCheckingChain(errorchain, ISO7816_4ErrorChecker())] cardservice.connection.setErrorCheckingChain(errorchain) # filter Warning Processing Exceptions (sw1 = 0x62 or 0x63) cardservice.connection.addSWExceptionToFilter(WarningProcessingException) # attach the console tracer observer = ConsoleCardConnectionObserver() cardservice.connection.addObserver(observer) # connect to the card and perform a few transmits cardservice.connection.connect() try:
# return False # for (targ, curr, mask) in zip(gExpectedAtr, atr, gExpectedMask): # if targ != (curr & mask): # return False # return True class MyErrorChecker(ErrorChecker): def __call__(self, data, sw1, sw2): global gLastErrCode gLastErrCode = '%02x%02x' % (sw1, sw2) gErrorchain = [] gErrorchain = [ErrorCheckingChain(gErrorchain, MyErrorChecker()), ErrorCheckingChain(gErrorchain, ISO7816_8ErrorChecker()), ErrorCheckingChain(gErrorchain, ISO7816_4ErrorChecker())] class DFTELECOMObserver(CardObserver): def __init__(self): self.observer = ConsoleCardConnectionObserver() def update(self, observable, actions): global gCard (addedcards, removedcards) = actions for card in addedcards: if checkAtrMatch(card.atr): card.connection = card.createConnection()
# coding=utf-8 from smartcard.sw.ErrorCheckingChain import ErrorCheckingChain from smartcard.sw.ISO7816_4ErrorChecker import ISO7816_4ErrorChecker from smartcard.sw.ISO7816_8ErrorChecker import ISO7816_8ErrorChecker from smartcard.sw.ISO7816_9ErrorChecker import ISO7816_9ErrorChecker ErrorChainWrapper = [] ErrorChainWrapper = [ ErrorCheckingChain(ErrorChainWrapper, ISO7816_9ErrorChecker()) ] ErrorChainWrapper = [ ErrorCheckingChain(ErrorChainWrapper, ISO7816_8ErrorChecker()) ] ErrorChainWrapper = [ ErrorCheckingChain(ErrorChainWrapper, ISO7816_4ErrorChecker()) ]