Index: /pykcs11/trunk/samples/getinfo.py =================================================================== --- /pykcs11/trunk/samples/getinfo.py (revision 169) +++ /pykcs11/trunk/samples/getinfo.py (revision 173) @@ -19,4 +19,90 @@ import PyKCS11 import platform +import sys + +class getInfo(object): + red = blue = magenta = normal = "" + + def colorize(self, text, arg): + print self.magenta + text + self.blue, arg, self.normal + + def __init__(self, lib = None): + if sys.stdout.isatty() and platform.system().lower() != 'windows': + self.red = "\x1b[01;31m" + self.blue = "\x1b[34m" + self.magenta = "\x1b[35m" + self.normal = "\x1b[0m" + + self.pkcs11 = PyKCS11.PyKCS11Lib() + self.pkcs11.load(lib) + + def getSlotInfo(self, slot): + i = self.pkcs11.getSlotInfo(slot) + print "Slot n.:", slot + self.colorize(" slotDescription:", i.slotDescription.strip()) + self.colorize(" manufacturerID:", i.manufacturerID.strip()) + self.colorize(" flags:", i.flags2text()) + self.colorize(" hardwareVersion:", i.hardwareVersion) + self.colorize(" firmwareVersion:", i.firmwareVersion) + + def getTokenInfo(self, slot): + t = self.pkcs11.getTokenInfo(slot) + print " TokenInfo" + self.colorize(" label:", t.label.strip()) + self.colorize(" manufacturerID:", t.manufacturerID.strip()) + self.colorize(" model:", t.model.strip()) + self.colorize(" serialNumber:", t.serialNumber) + self.colorize(" flags:", t.flags2text()) + self.colorize(" ulMaxSessionCount:", t.ulMaxSessionCount) + self.colorize(" ulSessionCount:", t.ulSessionCount) + self.colorize(" ulMaxRwSessionCount:", t.ulMaxRwSessionCount) + self.colorize(" ulRwSessionCount:", t.ulRwSessionCount) + self.colorize(" ulMaxPinLen:", t.ulMaxPinLen) + self.colorize(" ulMinPinLen:", t.ulMinPinLen) + self.colorize(" ulTotalPublicMemory:", t.ulTotalPublicMemory) + self.colorize(" ulFreePublicMemory:", t.ulFreePublicMemory) + self.colorize(" ulTotalPrivateMemory:", t.ulTotalPrivateMemory) + self.colorize(" ulFreePrivateMemory:", t.ulFreePrivateMemory) + self.colorize(" hardwareVersion:", "%d.%d" % t.hardwareVersion) + self.colorize(" firmwareVersion:", "%d.%d" % t.firmwareVersion) + self.colorize(" utcTime:", t.utcTime) + + def getMechanismInfo(self, slot): + m = self.pkcs11.getMechanismList(slot) + print " Mechanism list: " + for x in m: + self.colorize(" ", x) + i = self.pkcs11.getMechanismInfo(slot, x) + if (not i.flags & PyKCS11.CKF_DIGEST): + if i.ulMinKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: + self.colorize(" ulMinKeySize:", i.ulMinKeySize) + if i.ulMaxKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: + self.colorize(" ulMaxKeySize:", i.ulMaxKeySize) + self.colorize(" flags:", i.flags2text()) + + def getInfo(self): + info = self.pkcs11.getInfo() + self.colorize("Library Cryptoki Version:", "%d.%d" % info.cryptokiVersion) + self.colorize("Library manufacturerID:", info.manufacturerID) + self.colorize("Library flags:", info.flags) + self.colorize("Library Description:", info.libraryDescription) + self.colorize("Library Version:", "%d.%d" % info.libraryVersion) + + def getSessionInfo(self, slot, pin = None): + session = self.pkcs11.openSession(slot) + s = session.getSessionInfo() + + if pin: + print " Using pin:", pin + session.login(pin) + + print " SessionInfo" + self.colorize(" slotID:", s.slotID) + self.colorize(" state:", s.state2text()) + self.colorize(" flags:", s.flags2text()) + self.colorize(" ulDeviceError:", s.ulDeviceError) + + if pin: + session.logout() def usage(): @@ -28,102 +114,6 @@ print "[-o][--opensession]" -def getinfo(lib, pin = None, open_session = False, slot = None): - def colorize(text, arg): - print magenta + text + blue, arg, normal - - red = blue = magenta = normal = "" - if sys.stdout.isatty() and platform.system().lower() != 'windows': - red = "\x1b[01;31m" - blue = "\x1b[34m" - magenta = "\x1b[35m" - normal = "\x1b[0m" - - pkcs11 = PyKCS11.PyKCS11Lib() - pkcs11.load(lib) - info = pkcs11.getInfo() - colorize("Library Cryptoki Version:", "%d.%d" % info.cryptokiVersion) - colorize("Library manufacturerID:", info.manufacturerID) - colorize("Library flags:", info.flags) - colorize("Library Description:", info.libraryDescription) - colorize("Library Version:", "%d.%d" % info.libraryVersion) - - slots = pkcs11.getSlotList() - print "Available Slots:", len(slots), slots - - if len(slots) == 0: - sys.exit(2) - - if (None != slot): - slots = [ slots[slot] ] - - for slot in slots: - i = pkcs11.getSlotInfo(slot) - print "Slot n.:", slot - colorize(" slotDescription:", i.slotDescription.strip()) - colorize(" manufacturerID:", i.manufacturerID.strip()) - colorize(" flags:", i.flags2text()) - colorize(" hardwareVersion:", i.hardwareVersion) - colorize(" firmwareVersion:", i.firmwareVersion) - - try: - if open_session: - session = pkcs11.openSession(slot) - - if pin_available: - print " Using pin:", pin - session.login(pin = pin) - - t = pkcs11.getTokenInfo(slot) - print " TokenInfo" - colorize(" label:", t.label.strip()) - colorize(" manufacturerID:", t.manufacturerID.strip()) - colorize(" model:", t.model.strip()) - colorize(" serialNumber:", t.serialNumber) - colorize(" flags:", t.flags2text()) - colorize(" ulMaxSessionCount:", t.ulMaxSessionCount) - colorize(" ulSessionCount:", t.ulSessionCount) - colorize(" ulMaxRwSessionCount:", t.ulMaxRwSessionCount) - colorize(" ulRwSessionCount:", t.ulRwSessionCount) - colorize(" ulMaxPinLen:", t.ulMaxPinLen) - colorize(" ulMinPinLen:", t.ulMinPinLen) - colorize(" ulTotalPublicMemory:", t.ulTotalPublicMemory) - colorize(" ulFreePublicMemory:", t.ulFreePublicMemory) - colorize(" ulTotalPrivateMemory:", t.ulTotalPrivateMemory) - colorize(" ulFreePrivateMemory:", t.ulFreePrivateMemory) - colorize(" hardwareVersion:", "%d.%d" % t.hardwareVersion) - colorize(" firmwareVersion:", "%d.%d" % t.firmwareVersion) - colorize(" utcTime:", t.utcTime) - - m = pkcs11.getMechanismList(slot) - print " Mechanism list: " - for x in m: - print " " + blue + x + normal - i = pkcs11.getMechanismInfo(slot, x) - if (not i.flags & PyKCS11.CKF_DIGEST): - if i.ulMinKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: - colorize(" ulMinKeySize:", i.ulMinKeySize) - if i.ulMaxKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: - colorize(" ulMaxKeySize:", i.ulMaxKeySize) - colorize(" flags:", i.flags2text()) - - except PyKCS11.PyKCS11Error, e: - print "Error:", e - - if open_session: - s = session.getSessionInfo() - print " SessionInfo" - colorize(" slotID:", s.slotID) - colorize(" state:", s.state2text()) - colorize(" flags:", s.flags2text()) - colorize(" ulDeviceError:", s.ulDeviceError) - - if pin_available: - session.logout() - - if open_session: - session.closeSession() - if __name__ == '__main__': - import getopt, sys + import getopt try: @@ -154,8 +144,23 @@ open_session = True - try: - getinfo(lib, pin = pin, open_session = open_session, slot = slot) - except PyKCS11.PyKCS11Error, e: - print "Error:", e + gi = getInfo(lib) + gi.getInfo() + slots = gi.pkcs11.getSlotList() + print "Available Slots:", len(slots), slots + if len(slots) == 0: + sys.exit(2) + + if slot: + slots = [ slots[slot] ] + + for slot in slots: + try: + gi.getSlotInfo(slot) + gi.getSessionInfo(slot, pin) + gi.getTokenInfo(slot) + gi.getMechanismInfo(slot) + except PyKCS11.PyKCS11Error, e: + print "Error:", e +