Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ctypes, strange structures of PKCS11

Reply
Thread Tools

ctypes, strange structures of PKCS11

 
 
Cornelius Kölbel
Guest
Posts: n/a
 
      09-05-2012
Hi there,

I am trying to use a pkcs11 library (with python 2.7) and address this
library with ctypes.
Alas, I am neither the python black belt guru and C is not my preferred
language.

Till now, I do not want to use pykcs11, since I want to keep my
dependencies low.

I initialized the library, logged in to the token and got a session
(self.hSession).

Now I try to create an AES key using C_CreateKey.

--snip--
def createAES(self, ks=32):
rv=0
mechanism = CK_MECHANISM(CKM_AES_KEY_GEN, NULL, 0)
print "Mech:",mechanism.mechanism
print "Mech:",mechanism.pParameter
print "Mech:",mechanism.usParameterLen

keysize = c_ulong(ks)
klass = CKO_SECRET_KEY
keytype = CKK_AES
label = "testAES"
ck_true = c_ubyte(1)
ck_false = c_ubyte(0)
objHandle = CK_OBJECT_HANDLE()

size=7

CK_TEMPLATE = CK_ATTRIBUTE * 6

template = CK_TEMPLATE(
CK_ATTRIBUTE(CKA_KEY_TYPE, c_void_p(keytype),0),
CK_ATTRIBUTE(CKA_LABEL, cast( label, c_void_p),
len( label )),
CK_ATTRIBUTE(CKA_VALUE_LEN,
cast(byref(keysize),c_void_p), sizeof(keysize) ),
CK_ATTRIBUTE(CKA_PRIVATE,
cast(byref(ck_false),c_void_p), sizeof(ck_false)),
CK_ATTRIBUTE(CKA_TOKEN,
cast(byref(ck_true),c_void_p), sizeof(ck_true)),
CK_ATTRIBUTE(CKA_SENSITIVE,
cast(byref(ck_true),c_void_p), sizeof(ck_true))
)

template_len = c_ulong(size)
print "Template: ", template
print "Template: ", len(template)
print "Handle:", objHandle
print "Handle:", type(addressof(objHandle))
rv = self.etpkcs11.C_GenerateKey(self.hSession,
addressof(mechanism),
addressof(template),
template_len,
objHandle)
print "rv=",rv
print "handle=",objHandle

if rv:
if self.debug: print "Failed to create key: " , rv
raise Exception("createAES - Failed to C_GenerateKey (%s):
%s" % (rv, pkcs11error(rv)) )
else:
if self.debug: print "created key successfully: %s" %
str(handle)
--snap--

Unfortunately I end up with a return value of 32, which means invalid
data -- I guess my template is not that, what is should be.

Any hint on this is highly appreciated.

Kind regards
Cornelius



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iEYEARECAAYFAlBHuOEACgkQGUgIxT8zfHHOugCfVgyab1EWNc iOH/P9rBItg+1t
1fMAoKlZF461bAzaN+pYOsGiEWGnXZfh
=iHUW
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PKCS11 and DER MadDruid C++ 2 11-12-2008 12:11 PM
PKCS11 with jre 1.30 Francesco Java 2 09-11-2008 01:15 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57