Changeset 173

Show
Ignore:
Timestamp:
10/26/09 15:40:07 (9 months ago)
Author:
lrousseau
Message:

create a class getInfo()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pykcs11/trunk/samples/getinfo.py

    r169 r173  
    1919import PyKCS11 
    2020import platform 
     21import sys 
     22 
     23class getInfo(object): 
     24    red = blue = magenta = normal = "" 
     25 
     26    def colorize(self, text, arg): 
     27        print self.magenta + text + self.blue, arg, self.normal 
     28 
     29    def __init__(self, lib = None): 
     30        if sys.stdout.isatty() and platform.system().lower() != 'windows': 
     31            self.red = "\x1b[01;31m" 
     32            self.blue = "\x1b[34m" 
     33            self.magenta = "\x1b[35m" 
     34            self.normal = "\x1b[0m" 
     35 
     36        self.pkcs11 = PyKCS11.PyKCS11Lib() 
     37        self.pkcs11.load(lib) 
     38 
     39    def getSlotInfo(self, slot): 
     40        i = self.pkcs11.getSlotInfo(slot) 
     41        print "Slot n.:", slot 
     42        self.colorize("  slotDescription:", i.slotDescription.strip()) 
     43        self.colorize("  manufacturerID:", i.manufacturerID.strip()) 
     44        self.colorize("  flags:", i.flags2text()) 
     45        self.colorize("  hardwareVersion:", i.hardwareVersion) 
     46        self.colorize("  firmwareVersion:", i.firmwareVersion) 
     47 
     48    def getTokenInfo(self, slot): 
     49        t = self.pkcs11.getTokenInfo(slot) 
     50        print " TokenInfo" 
     51        self.colorize("  label:", t.label.strip()) 
     52        self.colorize("  manufacturerID:", t.manufacturerID.strip()) 
     53        self.colorize("  model:", t.model.strip()) 
     54        self.colorize("  serialNumber:", t.serialNumber) 
     55        self.colorize("  flags:", t.flags2text()) 
     56        self.colorize("  ulMaxSessionCount:", t.ulMaxSessionCount) 
     57        self.colorize("  ulSessionCount:", t.ulSessionCount) 
     58        self.colorize("  ulMaxRwSessionCount:", t.ulMaxRwSessionCount) 
     59        self.colorize("  ulRwSessionCount:", t.ulRwSessionCount) 
     60        self.colorize("  ulMaxPinLen:", t.ulMaxPinLen) 
     61        self.colorize("  ulMinPinLen:", t.ulMinPinLen) 
     62        self.colorize("  ulTotalPublicMemory:", t.ulTotalPublicMemory) 
     63        self.colorize("  ulFreePublicMemory:", t.ulFreePublicMemory) 
     64        self.colorize("  ulTotalPrivateMemory:", t.ulTotalPrivateMemory) 
     65        self.colorize("  ulFreePrivateMemory:", t.ulFreePrivateMemory) 
     66        self.colorize("  hardwareVersion:", "%d.%d" % t.hardwareVersion) 
     67        self.colorize("  firmwareVersion:", "%d.%d" % t.firmwareVersion) 
     68        self.colorize("  utcTime:", t.utcTime) 
     69 
     70    def getMechanismInfo(self, slot): 
     71        m = self.pkcs11.getMechanismList(slot) 
     72        print "  Mechanism list: " 
     73        for x in m: 
     74            self.colorize("  ", x) 
     75            i = self.pkcs11.getMechanismInfo(slot, x) 
     76            if (not i.flags & PyKCS11.CKF_DIGEST): 
     77                if i.ulMinKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: 
     78                    self.colorize("    ulMinKeySize:", i.ulMinKeySize) 
     79                if i.ulMaxKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: 
     80                    self.colorize("    ulMaxKeySize:", i.ulMaxKeySize) 
     81            self.colorize("    flags:", i.flags2text()) 
     82 
     83    def getInfo(self): 
     84        info = self.pkcs11.getInfo() 
     85        self.colorize("Library Cryptoki Version:", "%d.%d" % info.cryptokiVersion) 
     86        self.colorize("Library manufacturerID:", info.manufacturerID) 
     87        self.colorize("Library flags:", info.flags) 
     88        self.colorize("Library Description:", info.libraryDescription) 
     89        self.colorize("Library Version:", "%d.%d" % info.libraryVersion) 
     90 
     91    def getSessionInfo(self, slot, pin = None): 
     92        session = self.pkcs11.openSession(slot) 
     93        s = session.getSessionInfo() 
     94 
     95        if pin: 
     96            print " Using pin:", pin 
     97            session.login(pin) 
     98 
     99        print " SessionInfo" 
     100        self.colorize("  slotID:", s.slotID) 
     101        self.colorize("  state:", s.state2text()) 
     102        self.colorize("  flags:", s.flags2text()) 
     103        self.colorize("  ulDeviceError:", s.ulDeviceError) 
     104 
     105        if pin: 
     106            session.logout() 
    21107 
    22108def usage(): 
     
    28114    print "[-o][--opensession]" 
    29115 
    30 def getinfo(lib, pin = None, open_session = False, slot = None): 
    31     def colorize(text, arg): 
    32         print magenta + text + blue, arg, normal 
    33  
    34     red = blue = magenta = normal = "" 
    35     if sys.stdout.isatty() and platform.system().lower() != 'windows': 
    36         red = "\x1b[01;31m" 
    37         blue = "\x1b[34m" 
    38         magenta = "\x1b[35m" 
    39         normal = "\x1b[0m" 
    40  
    41     pkcs11 = PyKCS11.PyKCS11Lib() 
    42     pkcs11.load(lib) 
    43     info = pkcs11.getInfo() 
    44     colorize("Library Cryptoki Version:", "%d.%d" % info.cryptokiVersion) 
    45     colorize("Library manufacturerID:", info.manufacturerID) 
    46     colorize("Library flags:", info.flags) 
    47     colorize("Library Description:", info.libraryDescription) 
    48     colorize("Library Version:", "%d.%d" % info.libraryVersion) 
    49  
    50     slots = pkcs11.getSlotList() 
    51     print "Available Slots:", len(slots), slots 
    52  
    53     if len(slots) == 0: 
    54         sys.exit(2) 
    55  
    56     if (None != slot): 
    57         slots = [ slots[slot] ] 
    58  
    59     for slot in slots: 
    60         i = pkcs11.getSlotInfo(slot) 
    61         print "Slot n.:", slot 
    62         colorize("  slotDescription:", i.slotDescription.strip()) 
    63         colorize("  manufacturerID:", i.manufacturerID.strip()) 
    64         colorize("  flags:", i.flags2text()) 
    65         colorize("  hardwareVersion:", i.hardwareVersion) 
    66         colorize("  firmwareVersion:", i.firmwareVersion) 
    67  
    68         try: 
    69             if open_session: 
    70                 session = pkcs11.openSession(slot) 
    71  
    72             if pin_available: 
    73                 print " Using pin:", pin 
    74                 session.login(pin = pin) 
    75  
    76             t = pkcs11.getTokenInfo(slot) 
    77             print " TokenInfo" 
    78             colorize("  label:", t.label.strip()) 
    79             colorize("  manufacturerID:", t.manufacturerID.strip()) 
    80             colorize("  model:", t.model.strip()) 
    81             colorize("  serialNumber:", t.serialNumber) 
    82             colorize("  flags:", t.flags2text()) 
    83             colorize("  ulMaxSessionCount:", t.ulMaxSessionCount) 
    84             colorize("  ulSessionCount:", t.ulSessionCount) 
    85             colorize("  ulMaxRwSessionCount:", t.ulMaxRwSessionCount) 
    86             colorize("  ulRwSessionCount:", t.ulRwSessionCount) 
    87             colorize("  ulMaxPinLen:", t.ulMaxPinLen) 
    88             colorize("  ulMinPinLen:", t.ulMinPinLen) 
    89             colorize("  ulTotalPublicMemory:", t.ulTotalPublicMemory) 
    90             colorize("  ulFreePublicMemory:", t.ulFreePublicMemory) 
    91             colorize("  ulTotalPrivateMemory:", t.ulTotalPrivateMemory) 
    92             colorize("  ulFreePrivateMemory:", t.ulFreePrivateMemory) 
    93             colorize("  hardwareVersion:", "%d.%d" % t.hardwareVersion) 
    94             colorize("  firmwareVersion:", "%d.%d" % t.firmwareVersion) 
    95             colorize("  utcTime:", t.utcTime) 
    96  
    97             m = pkcs11.getMechanismList(slot) 
    98             print "  Mechanism list: " 
    99             for x in m: 
    100                 print "   " + blue + x + normal 
    101                 i = pkcs11.getMechanismInfo(slot, x) 
    102                 if (not i.flags & PyKCS11.CKF_DIGEST): 
    103                     if i.ulMinKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: 
    104                         colorize("    ulMinKeySize:", i.ulMinKeySize) 
    105                     if i.ulMaxKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION: 
    106                         colorize("    ulMaxKeySize:", i.ulMaxKeySize) 
    107                 colorize("    flags:", i.flags2text()) 
    108  
    109         except PyKCS11.PyKCS11Error, e: 
    110             print "Error:", e 
    111  
    112     if open_session: 
    113         s = session.getSessionInfo() 
    114         print " SessionInfo" 
    115         colorize("  slotID:", s.slotID) 
    116         colorize("  state:", s.state2text()) 
    117         colorize("  flags:", s.flags2text()) 
    118         colorize("  ulDeviceError:", s.ulDeviceError) 
    119  
    120     if pin_available: 
    121         session.logout() 
    122  
    123     if open_session: 
    124         session.closeSession() 
    125  
    126116if __name__ == '__main__': 
    127     import getopt, sys 
     117    import getopt 
    128118 
    129119    try: 
     
    154144            open_session = True 
    155145 
    156     try: 
    157         getinfo(lib, pin = pin, open_session = open_session, slot = slot) 
    158     except PyKCS11.PyKCS11Error, e: 
    159         print "Error:", e 
     146    gi = getInfo(lib) 
     147    gi.getInfo() 
    160148 
     149    slots = gi.pkcs11.getSlotList() 
     150    print "Available Slots:", len(slots), slots 
    161151 
     152    if len(slots) == 0: 
     153        sys.exit(2) 
     154 
     155    if slot: 
     156        slots = [ slots[slot] ] 
     157 
     158    for slot in slots: 
     159        try: 
     160            gi.getSlotInfo(slot) 
     161            gi.getSessionInfo(slot, pin) 
     162            gi.getTokenInfo(slot) 
     163            gi.getMechanismInfo(slot) 
     164        except PyKCS11.PyKCS11Error, e: 
     165            print "Error:", e 
     166 
(C) 2006 bit4id srl, for informations please contact info@bit4id.com
visitors since August 21, 2006