root/pykcs11/trunk/samples/LowLevel/dumpit.py

Revision 92, 5.3 kB (checked in by midori, 2 years ago)

-updated high level interface and samples to latest low level interface
-minor fix: the CK_OBJECT_HANDLE and CK_SESSION_HANDLE wrapeprs are now declared as simple types (unsigned long) so the value() method returns a number nad assign() method accepts a number.
-increased version to 1.1.1 (resources and setup.py)

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2
3 #   Copyright (C) 2004 Midori (midori -- a-t -- paipai dot net)
4 #   Copyright (C) 2006 Ludovic Rousseau (ludovic.rousseau@free.fr)
5 #
6 # This file is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19
20 from PyKCS11.LowLevel import *
21
22 a = CPKCS11Lib()
23 info = CK_INFO()
24 slotInfo = CK_SLOT_INFO()
25 lib = "incryptoki2.dll"
26 session = CK_SESSION_HANDLE()
27 sessionInfo = CK_SESSION_INFO()
28 tokenInfo = CK_TOKEN_INFO()
29 slotList = ckintlist()
30 pin = "12345678"
31
32 print "Load of " + lib + ": " + str(a.Load(lib, 1))
33 print "C_GetInfo: " + hex(a.C_GetInfo(info))
34 print "Library manufacturerID: " + info.GetManufacturerID()
35 del info
36
37 print "C_GetSlotList(NULL): " + hex(a.C_GetSlotList(0, slotList))
38 print "\tAvailable Slots: " + str(len(slotList))
39
40 for x in range(len(slotList)):
41     print "\tC_SlotInfo(): " + hex(a.C_GetSlotInfo(slotList[x], slotInfo))
42     print "\t\tSlot N." + str(x) + ": ID=" + str(slotList[x]) + ", name='" + slotInfo.GetSlotDescription() + "'"
43     print "\tC_OpenSession(): " + hex(a.C_OpenSession(slotList[x], CKF_SERIAL_SESSION|CKF_RW_SESSION, session))
44     print "\t\tSession:" + str(session)
45     print "\tC_GetSessionInfo(): " + hex(a.C_GetSessionInfo(session, sessionInfo))
46     print "\t\tSessionInfo: state=" + hex(sessionInfo.state) + ", flags=" + hex(sessionInfo.flags)
47
48     print "\tC_GetTokenInfo(): " + hex(a.C_GetTokenInfo(slotList[x], tokenInfo))
49     print "\t\tTokenInfo: Label=" + tokenInfo.GetLabel() + ", ManufacturerID=" + tokenInfo.GetManufacturerID()
50     print "\t\tTokenInfo: flags=" + hex(tokenInfo.flags) + ", Model=" + tokenInfo.GetModel()
51    
52     print "\tC_Login(): " + hex(a.C_Login(session, CKU_USER, pin))
53     print "\tC_Logout(): " + hex(a.C_Logout(session))
54     print "\tC_CloseSession(): " + hex(a.C_CloseSession(session))
55
56 print "C_OpenSession(): " + hex(a.C_OpenSession(slotList[0], CKF_SERIAL_SESSION, session))
57 print "C_Login(): " + hex(a.C_Login(session, CKU_USER, pin))
58
59 SearchResult = ckobjlist(10)
60 SearchTemplate = ckattrlist(0)
61 #SearchTemplate[0].SetNum(CKA_CLASS, CKO_CERTIFICATE)
62 #SearchTemplate[1].SetBool(CKA_TOKEN, True)
63
64 print "C_FindObjectsInit: " +  hex(a.C_FindObjectsInit(session, SearchTemplate))
65 print "C_FindObjects: " +  hex(a.C_FindObjects(session, SearchResult))
66 print "C_FindObjectsFinal: " +  hex(a.C_FindObjectsFinal(session))
67
68 attributes = [
69         ["CKA_CLASS", CKA_CLASS],
70         ["CKA_TOKEN", CKA_TOKEN],
71         ["CKA_PRIVATE", CKA_PRIVATE],
72         ["CKA_LABEL", CKA_LABEL],
73         ["CKA_APPLICATION", CKA_APPLICATION],
74         ["CKA_VALUE", CKA_VALUE],
75         ["CKA_CERTIFICATE_TYPE", CKA_CERTIFICATE_TYPE],
76         ["CKA_ISSUER", CKA_ISSUER],
77         ["CKA_SERIAL_NUMBER", CKA_SERIAL_NUMBER],
78         ["CKA_KEY_TYPE", CKA_KEY_TYPE],
79         ["CKA_SUBJECT", CKA_SUBJECT],
80         ["CKA_ID", CKA_ID],
81         ["CKA_SENSITIVE", CKA_SENSITIVE],
82         ["CKA_ENCRYPT", CKA_ENCRYPT],
83         ["CKA_DECRYPT", CKA_DECRYPT],
84         ["CKA_WRAP", CKA_WRAP],
85         ["CKA_UNWRAP", CKA_UNWRAP],
86         ["CKA_SIGN", CKA_SIGN],
87         ["CKA_SIGN_RECOVER", CKA_SIGN_RECOVER],
88         ["CKA_VERIFY", CKA_VERIFY],
89         ["CKA_VERIFY_RECOVER", CKA_VERIFY_RECOVER],
90         ["CKA_DERIVE", CKA_DERIVE],
91         ["CKA_START_DATE", CKA_START_DATE],
92         ["CKA_END_DATE", CKA_END_DATE],
93         ["CKA_MODULUS", CKA_MODULUS],
94         ["CKA_MODULUS_BITS", CKA_MODULUS_BITS],
95         ["CKA_PUBLIC_EXPONENT", CKA_PUBLIC_EXPONENT],
96         ["CKA_PRIVATE_EXPONENT", CKA_PRIVATE_EXPONENT],
97         ["CKA_PRIME_1", CKA_PRIME_1],
98         ["CKA_PRIME_2", CKA_PRIME_2],
99         ["CKA_EXPONENT_1", CKA_EXPONENT_1],
100         ["CKA_EXPONENT_2", CKA_EXPONENT_2],
101         ["CKA_COEFFICIENT", CKA_COEFFICIENT],
102         ["CKA_PRIME", CKA_PRIME],
103         ["CKA_SUBPRIME", CKA_SUBPRIME],
104         ["CKA_BASE", CKA_BASE],
105         ["CKA_VALUE_BITS", CKA_VALUE_BITS],
106         ["CKA_VALUE_LEN", CKA_VALUE_LEN],
107         ["CKA_EXTRACTABLE", CKA_EXTRACTABLE],
108         ["CKA_LOCAL", CKA_LOCAL],
109         ["CKA_NEVER_EXTRACTABLE", CKA_NEVER_EXTRACTABLE],
110         ["CKA_ALWAYS_SENSITIVE", CKA_ALWAYS_SENSITIVE],
111         ["CKA_MODIFIABLE", CKA_MODIFIABLE],
112         ["CKA_ECDSA_PARAMS", CKA_ECDSA_PARAMS],
113         ["CKA_EC_POINT", CKA_EC_POINT],
114         ]
115
116 for x in SearchResult:
117         print "object: " + hex(x.value())
118         valTemplate = ckattrlist(1)
119         for attr in attributes:
120                 valTemplate[0].Reset()
121                 valTemplate[0].SetType(attr[1])
122                 # first call to get the attribute size and reserve the memory
123                 a.C_GetAttributeValue(session, x, valTemplate)
124                 # second call to get the attribute value
125                 rv = a.C_GetAttributeValue(session, x, valTemplate)
126                 if (rv == CKR_OK):
127                         print "\t" + attr[0] + ": ",
128                         if (valTemplate[0].IsNum()):
129                                 print valTemplate[0].GetNum()
130                         if (valTemplate[0].IsBool()):
131                                 print valTemplate[0].GetBool()
132                         if (valTemplate[0].IsString()):
133                                 print valTemplate[0].GetString()
134                         if (valTemplate[0].IsBin()):
135                                 print "(" + str(valTemplate[0].GetLen()) + " bytes)",
136                                 print map(hex, valTemplate[0].GetBin())
137    
138 print "C_Logout(): " + hex(a.C_Logout(session))
139 print "C_CloseSession(): " + hex(a.C_CloseSession(session))
140 print "C_Finalize(): " + hex(a.C_Finalize())
141 print a.Unload()
142
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