root/pykcs11/trunk/samples/getinfo.py

Revision 144, 5.2 kB (checked in by lrousseau, 1 month ago)

put the slots loop inside a try/except to iterate over each slot
without exiting on the first error

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2
3 #   Copyright (C) 2006 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 colorize(text, arg):
33     print magenta + text + blue, arg, normal
34
35 try:
36     opts, args = getopt.getopt(sys.argv[1:], "p:s:c:ho", ["pin=", "slot=", "lib=", "help", "opensession"])
37 except getopt.GetoptError:
38     # print help information and exit:
39     usage()
40     sys.exit(2)
41
42 slot = None
43 lib = None
44 open_session = False
45 pin_available = False
46 for o, a in opts:
47     if o in ("-h", "--help"):
48         usage()
49         sys.exit()
50     if o in ("-p", "--pin"):
51         pin = a
52         pin_available = True
53         open_session = True
54     if o in ("-s", "--slot"):
55         slot = int(a)
56     if o in ("-c", "--lib"):
57         lib = a
58     if o in ("-o", "--opensession"):
59         open_session = True
60
61 red = blue = magenta = normal = ""
62 if sys.stdout.isatty() and platform.system().lower() != 'windows':
63     red = "\x1b[01;31m"
64     blue = "\x1b[34m"
65     magenta = "\x1b[35m"
66     normal = "\x1b[0m"
67
68 pkcs11 = PyKCS11.PyKCS11Lib()
69 try:
70     pkcs11.load(lib)
71     info = pkcs11.getInfo()
72     colorize("Library Cryptoki Version:", "%d.%d" % info.cryptokiVersion)
73     colorize("Library manufacturerID:", info.manufacturerID)
74     colorize("Library flags:", info.flags)
75     colorize("Library Description:", info.libraryDescription)
76     colorize("Library Version:", "%d.%d" % info.libraryVersion)
77
78     slots = pkcs11.getSlotList()
79     print "Available Slots:", len(slots), slots
80
81     if len(slots) == 0:
82         sys.exit(2)
83
84     if (None != slot):
85         slots = [ slots[slot] ]
86
87     for slot in slots:
88         i = pkcs11.getSlotInfo(slot)
89         print "Slot n.:", slot
90         colorize("  slotDescription:", i.slotDescription.strip())
91         colorize("  manufacturerID:", i.manufacturerID.strip())
92         colorize("  flags:", i.flags2text())
93         colorize("  hardwareVersion:", i.hardwareVersion)
94         colorize("  firmwareVersion:", i.firmwareVersion)
95
96         try:
97             if open_session:
98                 session = pkcs11.openSession(slot)
99
100             if pin_available:
101                 session.login(pin = pin)
102
103             t = pkcs11.getTokenInfo(slot)
104             print " TokenInfo"
105             colorize("  label:", t.label.strip())
106             colorize("  manufacturerID:", t.manufacturerID.strip())
107             colorize("  model:", t.model.strip())
108             colorize("  serialNumber:", t.serialNumber)
109             colorize("  flags:", t.flags2text())
110             colorize("  ulMaxSessionCount:", t.ulMaxSessionCount)
111             colorize("  ulSessionCount:", t.ulSessionCount)
112             colorize("  ulMaxRwSessionCount:", t.ulMaxRwSessionCount)
113             colorize("  ulRwSessionCount:", t.ulRwSessionCount)
114             colorize("  ulMaxPinLen:", t.ulMaxPinLen)
115             colorize("  ulMinPinLen:", t.ulMinPinLen)
116             colorize("  ulTotalPublicMemory:", t.ulTotalPublicMemory)
117             colorize("  ulFreePublicMemory:", t.ulFreePublicMemory)
118             colorize("  ulTotalPrivateMemory:", t.ulTotalPrivateMemory)
119             colorize("  ulFreePrivateMemory:", t.ulFreePrivateMemory)
120             colorize("  hardwareVersion:", "%d.%d" % t.hardwareVersion)
121             colorize("  firmwareVersion:", "%d.%d" % t.firmwareVersion)
122             colorize("  utcTime:", t.utcTime)
123
124             m = pkcs11.getMechanismList(slot)
125             print "  Mechanism list: "
126             for x in m:
127                 print "   " + blue + x + normal
128                 i = pkcs11.getMechanismInfo(slot, x)
129                 if (not i.flags & PyKCS11.CKF_DIGEST):
130                     if i.ulMinKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION:
131                         colorize("    ulMinKeySize:", i.ulMinKeySize)
132                     if i.ulMaxKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION:
133                         colorize("    ulMaxKeySize:", i.ulMaxKeySize)
134                 colorize("    flags:", i.flags2text())
135
136         except PyKCS11.PyKCS11Error, e:
137             print "Error:", e
138
139     if open_session:
140         s = session.getSessionInfo()
141         print " SessionInfo"
142         colorize("  slotID:", s.slotID)
143         colorize("  state:", s.state2text())
144         colorize("  flags:", s.flags2text())
145         colorize("  ulDeviceError:", s.ulDeviceError)
146
147     if pin_available:
148         session.logout()
149
150     if open_session:
151         session.closeSession()
152
153 except PyKCS11.PyKCS11Error, e:
154     print "Error:", e
155
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