root/pykcs11/trunk/samples/getinfo.py

Revision 210, 4.4 kB (checked in by lrousseau, 1 month ago)

update copyright

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2
3 #   Copyright (C) 2006-2010 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 platform
21 import sys
22
23
24 class getInfo(object):
25     red = blue = magenta = normal = ""
26
27     def colorize(self, text, arg):
28         print self.magenta + text + self.blue, arg, self.normal
29
30     def display(self, obj, indent=""):
31         dico = obj.to_dict()
32         for key in sorted(dico.keys()):
33             type = obj.fields[key]
34             left = indent + key + ":"
35             if type == "flags":
36                 self.colorize(left, ", ".join(dico[key]))
37             elif type == "pair":
38                 self.colorize(left, "%d.%d" % dico[key])
39             else:
40                 self.colorize(left, dico[key])
41
42     def __init__(self, lib=None):
43         if sys.stdout.isatty() and platform.system().lower() != 'windows':
44             self.red = "\x1b[01;31m"
45             self.blue = "\x1b[34m"
46             self.magenta = "\x1b[35m"
47             self.normal = "\x1b[0m"
48
49         self.pkcs11 = PyKCS11.PyKCS11Lib()
50         self.pkcs11.load(lib)
51
52     def getSlotInfo(self, slot):
53         print "Slot n.:", slot
54         self.display(self.pkcs11.getSlotInfo(slot), " ")
55
56     def getTokenInfo(self, slot):
57         print " TokenInfo"
58         self.display(self.pkcs11.getTokenInfo(slot), "  ")
59
60     def getMechanismInfo(self, slot):
61         print "  Mechanism list: "
62         m = self.pkcs11.getMechanismList(slot)
63         for x in m:
64             self.colorize("  ", x)
65             i = self.pkcs11.getMechanismInfo(slot, x)
66             if not i.flags & PyKCS11.CKF_DIGEST:
67                 if i.ulMinKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION:
68                     self.colorize("    ulMinKeySize:", i.ulMinKeySize)
69                 if i.ulMaxKeySize != PyKCS11.CK_UNAVAILABLE_INFORMATION:
70                     self.colorize("    ulMaxKeySize:", i.ulMaxKeySize)
71             self.colorize("    flags:", ", ".join(i.flags2text()))
72
73     def getInfo(self):
74         self.display(self.pkcs11.getInfo())
75
76     def getSessionInfo(self, slot, pin=None):
77         print " SessionInfo",
78         session = self.pkcs11.openSession(slot)
79
80         if pin:
81             print "(using pin: %s)" % pin
82             session.login(pin)
83         else:
84             print
85
86         self.display(session.getSessionInfo(), "  ")
87
88         if pin:
89             session.logout()
90
91
92 def usage():
93     print "Usage:", sys.argv[0],
94     print "[-p pin][--pin=pin]",
95     print "[-s slot][--slot=slot]",
96     print "[-c lib][--lib=lib]",
97     print "[-h][--help]",
98     print "[-o][--opensession]"
99
100 if __name__ == '__main__':
101     import getopt
102
103     try:
104         opts, args = getopt.getopt(sys.argv[1:], "p:s:c:ho",
105             ["pin=", "slot=", "lib=", "help", "opensession"])
106     except getopt.GetoptError:
107         # print help information and exit:
108         usage()
109         sys.exit(2)
110
111     slot = None
112     lib = None
113     pin = None
114     open_session = False
115     pin_available = False
116     for o, a in opts:
117         if o in ("-h", "--help"):
118             usage()
119             sys.exit()
120         if o in ("-p", "--pin"):
121             pin = a
122             pin_available = True
123             open_session = True
124         if o in ("-s", "--slot"):
125             slot = int(a)
126         if o in ("-c", "--lib"):
127             lib = a
128         if o in ("-o", "--opensession"):
129             open_session = True
130
131     gi = getInfo(lib)
132     gi.getInfo()
133
134     slots = gi.pkcs11.getSlotList()
135     print "Available Slots:", len(slots), slots
136
137     if len(slots) == 0:
138         sys.exit(2)
139
140     if slot:
141         slots = [slots[slot]]
142
143     for slot in slots:
144         try:
145             gi.getSlotInfo(slot)
146             gi.getSessionInfo(slot, pin)
147             gi.getTokenInfo(slot)
148             gi.getMechanismInfo(slot)
149         except PyKCS11.PyKCS11Error, e:
150             print "Error:", e
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