예제 #1
0
    def __init__(self, readernum):
        """
        Initialize the connection to the (physical) smart card via a given reader
        """

        # See which readers are available
        readers = smartcard.System.listReaders()
        if len(readers) <= readernum:
            logging.error("Invalid number of reader '%u' (only %u available)",
                          readernum, len(readers))
            sys.exit()

        # Connect to the reader and its card
        # XXX this is a workaround, see on sourceforge bug #3083254
        # should better use
        # self.reader = smartcard.System.readers()[readernum]
        self.reader = readers[readernum]
        try:
            self.session = smartcard.Session(self.reader)
        except smartcard.Exceptions.CardConnectionException as e:
            logging.error("Error connecting to card: %s", e.message)
            sys.exit()

        logging.info("Connected to card in '%s'", self.reader)

        atexit.register(self.cleanup)
예제 #2
0
    def reset(self, reader=None):
        """scans for readers and renew the connection with the current reader,
if still present. If no connection could be established, self.s is None.

Calling this with no arguments, or None make this function attempt to renew the
connection with the previous reader, or ask the user for one either if there was
no previous reader or if it is not present anymore.

Calling it with a reader creates a new connection with
that reader, or ask the user for one if given reader is not present."""

        if self.s is not None:  # already had a connection
            reader_plugged(self.curreader, False)
            self.s.close()
            self.s = None

        # take given reader or previous reader
        curreader = reader or self.curreader

        readers = safe_list_readers()
        if len(readers) == 0:
            print "no readers present. Smartcard commands are not sent."
            curreader = self.READER_NULL
        elif curreader is None or not reader_plugged(curreader, False)\
                and not str(reader) ==self.READER_NULL: # BUG pyscard=> str()
            if len(readers) == 1:
                curreader = readers[0]
                print "Using only present reader " + str(curreader)
            else:
                curreader = selector(readers, 0)[1]

        if curreader is not None and reader_plugged(curreader, False):
            self.s = smartcard.Session(curreader)

        self.curreader = curreader
예제 #3
0
 def powerUp(self):
     # When powerUp is called multiple times the session is valid (and the
     # card is implicitly powered) we can check for an ATR. But when
     # powerDown has been called, the session gets lost. In this case we
     # must try to reconnect (and power the card).
     try:
         self.session.getATR()
     except smartcard.Exceptions.CardConnectionException as e:
         try:
             self.session = smartcard.Session(self.reader)
         except smartcard.Exceptions.CardConnectionException as e:
             logging.error("Error connecting to card: %s", e.message)
             sys.exit()
예제 #4
0
    def getATR(self):
        # when powerDown has been called, fetching the ATR will throw an error.
        # In this case we must try to reconnect (and then get the ATR).
        try:
            atr = self.session.getATR()
        except smartcard.Exceptions.CardConnectionException as e:
            try:
                # Try to reconnect to the card
                self.session.close()
                self.session = smartcard.Session(self.reader)
                atr = self.session.getATR()
            except smartcard.Exceptions.CardConnectionException as e:
                logging.error("Error getting ATR: %s", e.message)
                sys.exit()

        return "".join([chr(b) for b in atr])
예제 #5
0
    l = len(data)
    hexList = []
    n = 0
    while n < len(data):
        hexList.append(hex(data[n]))
        n += 1
    return (hexList)


readers = smartcard.listReaders()
if len(readers) > 0:
    print "Available readers: ", readers
    print("using reader ", readers[0])

try:
    session = smartcard.Session(readers[0])
except smartcard.Exceptions.NoCardException:
    print('Unable to connect to card or no card in reader')
    exit("Exit on error")

print "\n### START SELECT EF.dir"

SELECT = [0x00, 0xA4, 0x02, 0x0C, 0x02]
EFDIR = [0x2F, 0x00]

try:
    apduCommand = SELECT + EFDIR
    data, sw1, sw2 = session.sendCommandAPDU(apduCommand)
    print "DATA READ: %s " % (data)
    print "SW1 SW2: %02X %02X" % (sw1, sw2)
except: