Attributes and Conversions¶
Contents
This module contains a wrapper around the key attributes and the template struct generation to make it possible to create templates in python and easily convert them into templates in C.
-
pycryptoki.attributes.
KEY_TRANSFORMS
CK_ATTRIBUTE Types mapped to Python->C transformation functions¶
-
pycryptoki.attributes.
ret_type
(c_type)[source]¶ Decorator to set a returned C Type so we can determine what type to use for an AutoCArray
Parameters: c_type – Default return-type of the transform function.
-
pycryptoki.attributes.
to_long
(val, reverse=False)[source]¶ Convert a integer/long value to a pValue, ulValueLen tuple
Parameters: - val – Value to convert
- reverse – Whether to convert from C -> Python
Returns: (
ctypes.c_void_p
ptr toctypes.c_ulong
,ctypes.c_ulong
size of long value)
-
pycryptoki.attributes.
to_bool
(val, reverse=False)[source]¶ Convert a boolean-ish value to a pValue, ulValueLen tuple.
Parameters: - val – Value to convert
- reverse – Whether to convert from C -> Python
Returns: ctypes.c_ulong
size of bool value)
-
pycryptoki.attributes.
to_char_array
(val, reverse=False)[source]¶ Convert the given string or list of string values into a char array.
This is slightly different than to_byte_array, which has different assumptions as to the format of the input.
Parameters: - val – Value to convert
- reverse – Whether to convert from C -> Python
Returns: (
ctypes.c_void_p
ptr topycryptoki.cryptoki.CK_CHAR
array,ctypes.c_ulong
size of array)
-
pycryptoki.attributes.
to_ck_date
(val, reverse=False)[source]¶ Transform a date string, date dictionary, or date object into a PKCS11 readable form (YYYYMMDD)
Parameters: - val – Value to convert
- reverse – Whether to convert from C -> Python
Returns: (
ctypes.c_void_p
ptr topycryptoki.cryptoki.CK_CHAR
array,ctypes.c_ulong
size of array)
-
pycryptoki.attributes.
to_pka_key_status
(val, reverse=False)[source]¶ Transform a Per Key Authorization Key Status object into a PKCS11 readable byte string
Parameters: - val – Value to convert
- reverse – Whether to convert from C -> Python
Returns: (
ctypes.c_void_p
ptr topycryptoki.cryptoki.CK_KEY_STATUS
object,ctypes.c_ulong
size of array)
-
pycryptoki.attributes.
to_byte_array
(val, reverse=False)[source]¶ Converts an arbitrarily sized integer, list, or byte array into a byte array.
It’ll zero-pad the bit length so it’s a multiple of 8, then convert the int to binary, split the binary string into sections of 8, then place each section into a slot in a
ctypes.c_ubyte
array (converting to small int).Parameters: - val – Value to convert
- reverse – Whether to convert from C -> Python
Returns: (
ctypes.c_void_p
ptr topycryptoki.cryptoki.CK_BYTE
array,ctypes.c_ulong
size of array)
-
pycryptoki.attributes.
to_sub_attributes
(val, reverse=False)[source]¶ Convert to another Attributes class & return the struct.
Parameters: - val – Value to convert
- reverse – Whether to convert from C -> Python
Returns: (
ctypes.c_void_p
ptr topycryptoki.cryptoki.CK_ATTRIBUTE
array,ctypes.c_ulong
size of array)
-
class
pycryptoki.attributes.
Attributes
(*args, **kwargs)[source]¶ Python container for handling PKCS11 Attributes.
Provides
get_c_struct()
, that would returns a list of C Structs, each with the following structure:class CK_ATTRIBUTE(Structure): ''' Defines type, value and length of an attribute: c_ulong type; c_void_p pValue; c_ulong ulValueLen; ''' pass
This list of structs can be used with
C_GetAttributeValue()
to get the length of the value that will be placed inpValue
(will be set toulValueLen
), or if you already know the length required you can ‘blank fill’pValue
for direct use.You can also provide new transformations in the form of a dictionary that will be preferred to the
KEY_TRANSFORMS
dictionary. This is passed in only as a keyword argument:transform = {1L: lambda x: return x**2}` attrs = Attributes({...}, new_transforms=transform) # attrs.get_c_struct will use the lambda expression in the transform dictionary # for key 1L
-
get_c_struct
()[source]¶ Build an array of
CK_ATTRIBUTE
Structs & return it.Returns: CK_ATTRIBUTE
array
-
static
from_c_struct
(c_struct)[source]¶ Build out a dictionary from a c_struct.
Parameters: c_struct – Pointer to an array of CK_ATTRIBUTE
structsReturns: dict
-
-
pycryptoki.attributes.
c_struct_to_python
(c_struct)[source]¶ Converts a C struct to a python dictionary.
Parameters: c_struct – The c struct to convert into a dictionary in python Returns: Returns a python dictionary which represents the C struct passed in
-
pycryptoki.attributes.
convert_c_ubyte_array_to_string
(byte_array)[source]¶ Converts a ctypes unsigned byte array into a string.
Parameters: byte_array –
Conversions¶
Provide low-level conversions between common data types.
The from_xyz
functions should all return an iterator over a list of integers,
representing the individual bytes in the passed-in value.
The to_xyz
functions take in an iterable of integers and convert it to the specified type.
Example 1
raw_bytes = from_bytestring(b"Some test data") assert raw_bytes = [83, 111, 109, 101, 32, 116, 101, 115, 116, 32, 100, 97, 116, 97] hex_data = to_hex(from_bytestring(b"Some test data")) assert hex_data == b'536f6d6520746573742064617461'
Example 2
bytestring_data = to_bytestring(from_hex(b'536f6d6520746573742064617461')) assert bytestring_data == b"Some test data" raw_bytes = list(from_hex(b'536f6d6520746573742064617461')) assert raw_bytes == [83, 111, 109, 101, 32, 116, 101, 115, 116, 32, 100, 97, 116, 97]
-
pycryptoki.conversions.
from_bytestring
(ascii_)[source]¶ Convert an iterable of strings into an iterable of integers.
Note
For bytestrings on python3, this does effectively nothing, since iterating over a bytestring in python 3 will return integers.
Parameters: ascii – String to convert Returns: iterator
-
pycryptoki.conversions.
to_bytestring
(ascii_)[source]¶ Convert an iterable of integers into a bytestring.
Parameters: ascii (iterable) – Iterable of integers Returns: bytestring
-
pycryptoki.conversions.
from_bin
(bin_)[source]¶ Convert a string-representation of binary into a list of integers.
Parameters: bin (str) – String representation of binary data (ex: “10110111”) Returns: iterator over integers
-
pycryptoki.conversions.
to_bin
(ascii_)[source]¶ Convert an iterable of integers to a binary representation.
Parameters: ascii (iterable) – iterable of integers Returns: bytestring of the binary values