| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
|
|---|
| 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 |
|---|