2 engine loading and configuration
4 from ctypes import c_void_p, c_char_p, c_int
5 from ctypescrypto import libcrypto
6 from ctypescrypto.exception import LibCryptoError
8 __all__ = ['default', 'set_default', 'Engine']
14 Represents Openssl loadable module (engine).
15 Allows to create PKey objects from private keys stored
16 in the token, accessed by engine
18 def __init__(self, engine_id, **kwargs):
19 eng = libcrypto.ENGINE_by_id(engine_id)
22 eng = libcrypto.ENGINE_by_id("dynamic")
24 raise LibCryptoError("Cannot get 'dynamic' engine")
25 if not libcrypto.ENGINE_ctrl_cmd_string(eng, "SO_PATH",
27 raise LibCryptoError("Cannot execute ctrl cmd SO_PATH")
28 if not libcrypto.ENGINE_ctrl_cmd_string(eng, "LOAD", None, 0):
29 raise LibCryptoError("Cannot execute ctrl cmd LOAD")
31 raise ValueError("Cannot find engine " + engine)
32 for cmd, value in kwargs.items():
33 if not libcrypto.ENGINE_ctrl_cmd_string(eng, cmd, value, 0):
34 raise LibCryptoError("Cannot execute ctrl cmd %s" % cmd)
35 if not libcrypto.ENGINE_init(eng):
36 raise LibCryptoError("Cannot initialize engine")
39 def private_key(self, key_id, ui_method = None, ui_data=None):
40 from ctypescrypto.pkey import PKey
42 ui_ptr = libcrypto.UI_OpenSSL()
44 ui_ptr = ui_method.ptr
45 pkey = libcrypto.ENGINE_load_private_key(self.ptr, key_id, ui_ptr,
48 raise LibCryptoError("Cannot load private key")
49 return PKey(ptr=pkey, cansign=True)
51 def set_default(eng, algorithms=0xFFFF):
53 Sets specified engine as default for all
54 algorithms, supported by it
56 For compatibility with 0.2.x if string is passed instead
57 of engine, attempts to load engine with this id
59 if not isinstance(eng,Engine):
62 libcrypto.ENGINE_set_default(eng.ptr, c_int(algorithms))
65 # Declare function result and arguments for used functions
66 libcrypto.ENGINE_by_id.restype = c_void_p
67 libcrypto.ENGINE_by_id.argtypes = (c_char_p, )
68 libcrypto.ENGINE_set_default.argtypes = (c_void_p, c_int)
69 libcrypto.ENGINE_ctrl_cmd_string.argtypes = (c_void_p, c_char_p, c_char_p,
71 libcrypto.ENGINE_finish.argtypes = (c_char_p, )
72 libcrypto.ENGINE_init.argtypes = (c_void_p, )
73 libcrypto.UI_OpenSSL.restype = c_void_p
74 libcrypto.ENGINE_load_private_key.argtypes = (c_void_p, c_char_p, c_void_p, c_void_p)
75 libcrypto.ENGINE_load_private_key.restype = c_void_p