root/pykcs11/trunk/samples/getinfo.py

Revision 167, 5.6 kB (checked in by lrousseau, 11 months ago)

define a getinfo() function and call it if the script is called directly

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2
3 #   Copyright (C) 2006-2009 Ludovic Rousseau (ludovic.rousseau@free.fr)
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18
19 import PyKCS11
20 import getopt, sys
21 import platform
22 import time
23
24 def usage():
25     print "Usage:", sys.argv[0],
26     print "[-p pin][--pin=pin]",
27     print "[-s slot][--slot=slot]",
28     print "[-c lib][--lib=lib]",
29     print "[-h][--help]",
30     print "[-o][--opensession]"
31
32 def getinfo(lib, pin = None, open_session = False, slot = None):
33     def colorize(text, arg):
34         print magenta + text + blue, arg, normal
35
36     red = blue = magenta = normal = ""
37     if sys.stdout.isatty() and platform.system().lower() != 'windows':
38         red = "\x1b[01;31m"
39         blue = "\x1b[34m"
40         magenta = "\x1b[35m"
41         normal = "\x1b[0m"
42
43     pkcs11 = PyKCS11.PyKCS11Lib()
44     pkcs11.load(lib)
45     info = pkcs11.getInfo()
46     colorize("Library Cryptoki Version:", "%d.%d" % info.cryptokiVersion)
47     colorize("Library manufacturerID:", info.manufacturerID)
48     colorize("Library flags:", info.flags)
49     colorize("Library Description:", info.libraryDescription)
50     colorize("Library Version:", "%d.%d" % info.libraryVersion)
51
52     slots = pkcs11.getSlotList()
53     print "Available Slots:", len(slots), slots
54
55     if len(slots) == 0:
56         sys.exit(2)
57
58     if (None != slot):
59         slots = [ slots[slot] ]
60
61     for slot in slots:
62         i = pkcs11.getSlotInfo(slot)
63         print "Slot n.:", slot
64         colorize("  slotDescription:", i.slotDescription.strip())
65         colorize("  manufacturerID:", i.manufacturerID.strip())
66         colorize("  flags:", i.flags2text())
67         colorize("  hardwareVersion:", i.hardwareVersion)
68         colorize("  firmwareVersion:", i.firmwareVersion)
69
70         try:
71             if open_session:
72                 session = pkcs11.openSession(slot)
73
74             if pin_available:
75                 print " Using pin:", pin
76                 session.login(pin = pin)
77
78             t = pkcs11.getTokenInfo(slot)
79             print " TokenInfo"
80             colorize("  label:", t.label.strip())
81             colorize("  manufacturerID:", t.manufacturerID.strip())
82             colorize("  model:", t.model.strip())
83             colorize("  serialNumber:", t.serialNumber)
84             colorize("  flags:", t.flags2text())
85             colorize("  ulMaxSessionCount:", t.ulMaxSessionCount)
86             colorize("  ulSessionCount:", t.ulSessionCount)
87             colorize("  ulMaxRwSessionCount:", t.ulMaxRwSessionCount)
88             colorize("  ulRwSessionCount:", t.ulRwSessionCount)
89             colorize("  ulMaxPinLen:", t.ulMaxPinLen)
90             colorize("  ulMinPinLen:", t.ulMinPinLen)
91             colorize("  ulTotalPublicMemory:", t.ulTotalPublicMemory)
92             colorize("  ulFreePublicMemory:", t.ulFreePublicMemory)
93             colorize("  ulTotalPrivateMemory:", t.ulTotalPrivateMemory)
94             colorize("  ulFreePrivateMemory:", t.ulFreePrivateMemory)
95             colorize("  hardwareVersion:", "%d.%d" % t.hardwareVersion)
96             colorize("  firmwareVersion:", "%d.%d" % t.firmwareVersion)
97             colorize("  utcTime:", t.utcTime)
98
99             m = pkcs11.getMechanismList(slot)
100             print "  Mechanism list: "
101             for x in m:
102                 print "   " + blue + x + normal
103                 i = pkcs11.getMechanismInfo(slot, x)
104                 if (not i.flags & PyKCS11.CKF_DIGEST):
105                     if i.ulMinKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION:
106                         colorize("    ulMinKeySize:", i.ulMinKeySize)
107                     if i.ulMaxKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION:
108                         colorize("    ulMaxKeySize:", i.ulMaxKeySize)
109                 colorize("    flags:", i.flags2text())
110
111         except PyKCS11.PyKCS11Error, e:
112             print "Error:", e
113
114     if open_session:
115         s = session.getSessionInfo()
116         print " SessionInfo"
117         colorize("  slotID:", s.slotID)
118         colorize("  state:", s.state2text())
119         colorize("  flags:", s.flags2text())
120         colorize("  ulDeviceError:", s.ulDeviceError)
121
122     if pin_available:
123         session.logout()
124
125     if open_session:
126         session.closeSession()
127
128 if __name__ == '__main__':
129     try:
130         opts, args = getopt.getopt(sys.argv[1:], "p:s:c:ho", ["pin=", "slot=", "lib=", "help", "opensession"])
131     except getopt.GetoptError:
132         # print help information and exit:
133         usage()
134         sys.exit(2)
135
136     slot = None
137     lib = None
138     pin = None
139     open_session = False
140     pin_available = False
141     for o, a in opts:
142         if o in ("-h", "--help"):
143             usage()
144             sys.exit()
145         if o in ("-p", "--pin"):
146             pin = a
147             pin_available = True
148             open_session = True
149         if o in ("-s", "--slot"):
150             slot = int(a)
151         if o in ("-c", "--lib"):
152             lib = a
153         if o in ("-o", "--opensession"):
154             open_session = True
155
156     try:
157         getinfo(lib, pin = pin, open_session = open_session, slot = slot)
158     except PyKCS11.PyKCS11Error, e:
159         print "Error:", e
160
161
Note: See TracBrowser for help on using the browser.
(C) 2006 bit4id srl, for informations please contact info@bit4id.com
visitors since August 21, 2006