Attributes and Conversions

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.

pypkcs11.attributes.KEY_TRANSFORMS CK_ATTRIBUTE Types mapped to Python->C transformation functions
class pypkcs11.attributes.Attributes(*args, **kwargs)

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 in pValue (will be set to ulValueLen), 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
static from_c_struct(c_struct)

Build out a dictionary from a c_struct.

Parameters:c_struct – Pointer to an array of CK_ATTRIBUTE structs
Returns:dict
get_c_struct()

Build an array of CK_ATTRIBUTE Structs & return it.

Returns:CK_ATTRIBUTE array
pypkcs11.attributes.c_struct_to_python(c_struct)

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
pypkcs11.attributes.convert_c_ubyte_array_to_string(byte_array)

Converts a ctypes unsigned byte array into a string.

Parameters:byte_array
pypkcs11.attributes.ret_type(c_type)

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.
pypkcs11.attributes.to_bool(val, reverse=False)

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_void_p ptr to pypkcs11.cryptoki.CK_BBOOL,

ctypes.c_ulong size of bool value)

pypkcs11.attributes.to_byte_array(val, reverse=False)

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 to pypkcs11.cryptoki.CK_BYTE array,

ctypes.c_ulong size of array)

pypkcs11.attributes.to_char_array(val, reverse=False)

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 to pypkcs11.cryptoki.CK_CHAR array,

ctypes.c_ulong size of array)

pypkcs11.attributes.to_ck_date(val, reverse=False)

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 to pypkcs11.cryptoki.CK_CHAR array,

ctypes.c_ulong size of array)

pypkcs11.attributes.to_long(val, reverse=False)

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 to ctypes.c_ulong, ctypes.c_ulong

size of long value)

pypkcs11.attributes.to_sub_attributes(val, reverse=False)

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 to pypkcs11.cryptoki.CK_ATTRIBUTE array,

ctypes.c_ulong size of 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

Convert a raw bytestring to hex
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

Convert hex data to a raw bytestring
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]
pypkcs11.conversions.from_bin(bin_)

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
pypkcs11.conversions.from_bytestring(ascii_)

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
pypkcs11.conversions.from_hex(hex_)

Convert a hexademical string to an iterable of integers.

Parameters:hex (str) – Hex string
Returns:Iterator
pypkcs11.conversions.to_bin(ascii_)

Convert an iterable of integers to a binary representation.

Parameters:ascii (iterable) – iterable of integers
Returns:bytestring of the binary values
pypkcs11.conversions.to_bytestring(ascii_)

Convert an iterable of integers into a bytestring.

Parameters:ascii (iterable) – Iterable of integers
Returns:bytestring
pypkcs11.conversions.to_hex(ints)

Convert an iterable of integers to a hexadecimal string.

Parameters:ints (iterable) – Iterable of integers
Returns:bytestring representing the hex data.