From e9eb486fae220903f039ffae5125894c1e156aa4 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sat, 12 Sep 2015 18:12:05 -0400 Subject: Move YAML parsing to external script so py11 doesn't have to worry about finding attributes.yaml at runtime. --- py11/__init__.py | 5 ++--- py11/attribute_map.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ py11/attributes.py | 16 +++------------ 3 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 py11/attribute_map.py (limited to 'py11') diff --git a/py11/__init__.py b/py11/__init__.py index b67ce8a..da0c946 100644 --- a/py11/__init__.py +++ b/py11/__init__.py @@ -1,7 +1,6 @@ # An attempt at a Python interface to PKCS 11 using the scary ctypes # module from the Python standard library. -from struct import pack, unpack from ctypes import * from .exceptions import * from .types import * @@ -12,7 +11,7 @@ from .prototypes import * class PKCS11 (object): - def __init__(self, so_name = "libpkcs11.so", attributes_initializer = "attributes.yaml"): + def __init__(self, so_name = "libpkcs11.so"): self.so_name = so_name self.so = CDLL(so_name) def raise_on_failure(rv): @@ -22,7 +21,7 @@ class PKCS11 (object): func = getattr(self.so, name) func.restype = raise_on_failure func.argtypes = args - self.adb = AttributeDB(attributes_initializer) + self.adb = AttributeDB() def __getattr__(self, name): return getattr(self.so, name) diff --git a/py11/attribute_map.py b/py11/attribute_map.py new file mode 100644 index 0000000..b689d6e --- /dev/null +++ b/py11/attribute_map.py @@ -0,0 +1,54 @@ +# This file was generated automatically from attributes.yaml by build-py11-attributes. Do not edit this file directly. + +attribute_map = { + 'CKA_COEFFICIENT': 'biginteger', + 'CKA_MECHANISM_TYPE': 'CK_MECHANISM_TYPE', + 'CKA_ID': 'bytearray', + 'CKA_VALUE': 'biginteger', + 'CKA_KEY_GEN_MECHANISM': 'CK_MECHANISM_TYPE', + 'CKA_LABEL': 'rfc2279string', + 'CKA_KEY_TYPE': 'CK_KEY_TYPE', + 'CKA_PRIME_2': 'biginteger', + 'CKA_APPLICATION': 'rfc2279string', + 'CKA_VERIFY': 'CK_BBOOL', + 'CKA_HASH_OF_ISSUER_PUBLIC_KEY': 'bytearray', + 'CKA_SIGN': 'CK_BBOOL', + 'CKA_MODULUS': 'biginteger', + 'CKA_START_DATE': 'CK_DATE', + 'CKA_CERTIFICATE_CATEGORY': 'CK_ULONG', + 'CKA_PRIVATE_EXPONENT': 'biginteger', + 'CKA_ALWAYS_SENSITIVE': 'CK_BBOOL', + 'CKA_LOCAL': 'CK_BBOOL', + 'CKA_PUBLIC_EXPONENT': 'biginteger', + 'CKA_SENSITIVE': 'CK_BBOOL', + 'CKA_WRAP_WITH_TRUSTED': 'CK_BBOOL', + 'CKA_EXTRACTABLE': 'CK_BBOOL', + 'CKA_SUBJECT': 'bytearray', + 'CKA_VERIFY_RECOVER': 'CK_BBOOL', + 'CKA_EXPONENT_1': 'biginteger', + 'CKA_TOKEN': 'CK_BBOOL', + 'CKA_DECRYPT': 'CK_BBOOL', + 'CKA_EC_PARAMS': 'bytearray', + 'CKA_END_DATE': 'CK_DATE', + 'CKA_JAVA_MIDP_SECURITY_DOMAIN': 'CK_ULONG', + 'CKA_MODIFIABLE': 'CK_BBOOL', + 'CKA_CHECK_VALUE': 'bytearray', + 'CKA_URL': 'rfc2279string', + 'CKA_SERIAL_NUMBER': 'bytearray', + 'CKA_MODULUS_BITS': 'CK_ULONG', + 'CKA_WRAP': 'CK_BBOOL', + 'CKA_SIGN_RECOVER': 'CK_BBOOL', + 'CKA_TRUSTED': 'CK_BBOOL', + 'CKA_DERIVE': 'CK_BBOOL', + 'CKA_UNWRAP': 'CK_BBOOL', + 'CKA_NEVER_EXTRACTABLE': 'CK_BBOOL', + 'CKA_PRIVATE': 'CK_BBOOL', + 'CKA_ENCRYPT': 'CK_BBOOL', + 'CKA_EXPONENT_2': 'biginteger', + 'CKA_PRIME_1': 'biginteger', + 'CKA_EC_POINT': 'bytearray', + 'CKA_ISSUER': 'bytearray', + 'CKA_CERTIFICATE_TYPE': 'CK_CERTIFICATE_TYPE', + 'CKA_CLASS': 'CK_OBJECT_CLASS', + 'CKA_OBJECT_ID': 'bytearray', + 'CKA_HASH_OF_SUBJECT_PUBLIC_KEY': 'bytearray'} diff --git a/py11/attributes.py b/py11/attributes.py index 00af79c..7c90768 100644 --- a/py11/attributes.py +++ b/py11/attributes.py @@ -48,24 +48,14 @@ class Attribute_biginteger(Attribute): class AttributeDB(object): - def __init__(self, initializer = None): + def __init__(self): + from .attribute_map import attribute_map self.db = {} - if isinstance(initializer, str): - initializer = self.parse_yaml(initializer) - for attribute_name, type_name in initializer: + for attribute_name, type_name in attribute_map.iteritems(): a = Attribute.new(attribute_name, type_name) self.db[a.name] = a self.db[a.code] = a - @staticmethod - def parse_yaml(yaml_filename): - from yaml import safe_load - with open(yaml_filename) as f: - for y in safe_load(f): - for k, v in y.iteritems(): - if k.startswith("CKA_") and "type" in v: - yield k, v["type"] - def encode(self, k, v): return self.db[k].encode(v) if k in self.db else v -- cgit v1.2.3