root/pykcs11/trunk/samples/getinfo.py

Revision 115, 4.1 kB (checked in by lrousseau, 3 years ago)

add getMechanismList() method

See Ticket #11

  • 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
23 def usage():
24     print "Usage:", sys.argv[0],
25     print "[-p pin][--pin=pin]",
26     print "[-s slot][--slot=slot]",
27     print "[-c lib][--lib=lib]",
28     print "[-h][--help]",
29     print "[-o][--opensession]"
30
31 def colorize(text, arg):
32     print magenta + text + blue, arg, normal
33
34 try:
35     opts, args = getopt.getopt(sys.argv[1:], "p:s:c:ho", ["pin=", "slot=", "lib=", "help", "opensession"])
36 except getopt.GetoptError:
37     # print help information and exit:
38     usage()
39     sys.exit(2)
40
41 slot = 0
42 lib = None
43 open_session = False
44 pin_available = False
45 for o, a in opts:
46     if o in ("-h", "--help"):
47         usage()
48         sys.exit()
49     if o in ("-p", "--pin"):
50         pin = a
51         pin_available = True
52         open_session = True
53     if o in ("-s", "--slot"):
54         slot = int(a)
55     if o in ("-d", "--lib"):
56         lib = a
57     if o in ("-o", "--opensession"):
58         open_session = True
59
60 red = blue = magenta = normal = ""
61 if sys.stdout.isatty() and platform.system().lower() != 'windows':
62     red = "\x1b[01;31m"
63     blue = "\x1b[34m"
64     magenta = "\x1b[35m"
65     normal = "\x1b[0m"
66
67 pkcs11 = PyKCS11.PyKCS11Lib()
68 try:
69     pkcs11.load(lib)
70     info = pkcs11.getInfo()
71     colorize("Library Cryptoki Version:", "%d.%d" % info.cryptokiVersion)
72     colorize("Library manufacturerID:", info.manufacturerID)
73     colorize("Library flags:", info.flags)
74     colorize("Library Description:", info.libraryDescription)
75     colorize("Library Version:", "%d.%d" % info.libraryVersion)
76
77     slots = pkcs11.getSlotList()
78     print "Available Slots:", len(slots)
79
80     if len(slots) == 0:
81         sys.exit(2)
82
83     i = pkcs11.getSlotInfo(slots[slot])
84     print "Slot n.:", slot
85     colorize("  slotDescription:", i.slotDescription.strip())
86     colorize("  manufacturerID:", i.manufacturerID.strip())
87     colorize("  flags:", i.flags2text())
88     colorize("  hardwareVersion:", i.hardwareVersion)
89     colorize("  firmwareVersion:", i.firmwareVersion)
90
91     if open_session:
92         session = pkcs11.openSession(slots[slot])
93
94     if pin_available:
95         session.login(pin = pin)
96
97     t = pkcs11.getTokenInfo(slots[slot])
98     print " TokenInfo"
99     colorize("  label:", t.label.strip())
100     colorize("  manufacturerID:", t.manufacturerID.strip())
101     colorize("  model:", t.model.strip())
102     colorize("  serialNumber:", t.serialNumber)
103     colorize("  flags:", t.flags2text())
104     colorize("  ulMaxSessionCount:", t.ulMaxSessionCount)
105     colorize("  ulSessionCount:", t.ulSessionCount)
106     colorize("  ulMaxRwSessionCount:", t.ulMaxRwSessionCount)
107     colorize("  ulRwSessionCount:", t.ulRwSessionCount)
108     colorize("  ulMaxPinLen:", t.ulMaxPinLen)
109     colorize("  ulMinPinLen:", t.ulMinPinLen)
110     colorize("  ulTotalPublicMemory:", t.ulTotalPublicMemory)
111     colorize("  ulFreePublicMemory:", t.ulFreePublicMemory)
112     colorize("  ulTotalPrivateMemory:", t.ulTotalPrivateMemory)
113     colorize("  ulFreePrivateMemory:", t.ulFreePrivateMemory)
114     colorize("  hardwareVersion:", "%d.%d" % t.hardwareVersion)
115     colorize("  firmwareVersion:", "%d.%d" % t.firmwareVersion)
116     colorize("  utcTime:", t.utcTime)
117
118     m = pkcs11.getMechanismList(slots[slot])
119     print "  Mechanism list: "
120     for x in m:
121         print "   " + blue + x + normal
122
123     if pin_available:
124         session.logout()
125
126     if open_session:
127         session.closeSession()
128
129 except PyKCS11.PyKCS11Error, e:
130     print "Error:", e
131
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