2 engine loading and configuration
4 from ctypes import c_void_p, c_char_p, c_int
5 from ctypescrypto import libcrypto,pyver
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 if pyver > 2 or isinstance(engine_id, unicode):
20 engine_id = engine_id.encode('utf-8')
21 eng = libcrypto.ENGINE_by_id(engine_id)
24 eng = libcrypto.ENGINE_by_id("dynamic")
26 raise LibCryptoError("Cannot get 'dynamic' engine")
27 if not libcrypto.ENGINE_ctrl_cmd_string(eng, "SO_PATH",
29 raise LibCryptoError("Cannot execute ctrl cmd SO_PATH")
30 if not libcrypto.ENGINE_ctrl_cmd_string(eng, "LOAD", None, 0):
31 raise LibCryptoError("Cannot execute ctrl cmd LOAD")
33 raise ValueError("Cannot find engine " + engine)
34 for cmd, value in kwargs.items():
35 if not libcrypto.ENGINE_ctrl_cmd_string(eng, cmd, value, 0):
36 raise LibCryptoError("Cannot execute ctrl cmd %s" % cmd)
37 if not libcrypto.ENGINE_init(eng):
38 raise LibCryptoError("Cannot initialize engine")
41 def private_key(self, key_id, ui_method = None, ui_data=None):
42 from ctypescrypto.pkey import PKey
44 ui_ptr = libcrypto.UI_OpenSSL()
46 ui_ptr = ui_method.ptr
47 pkey = libcrypto.ENGINE_load_private_key(self.ptr, key_id, ui_ptr,
50 raise LibCryptoError("Cannot load private key")
51 return PKey(ptr=pkey, cansign=True)
53 def set_default(eng, algorithms=0xFFFF):
55 Sets specified engine as default for all
56 algorithms, supported by it
58 For compatibility with 0.2.x if string is passed instead
59 of engine, attempts to load engine with this id
61 if not isinstance(eng,Engine):
64 libcrypto.ENGINE_set_default(eng.ptr, c_int(algorithms))
67 # Declare function result and arguments for used functions
68 libcrypto.ENGINE_by_id.restype = c_void_p
69 libcrypto.ENGINE_by_id.argtypes = (c_char_p, )
70 libcrypto.ENGINE_set_default.argtypes = (c_void_p, c_int)
71 libcrypto.ENGINE_ctrl_cmd_string.argtypes = (c_void_p, c_char_p, c_char_p,
73 libcrypto.ENGINE_finish.argtypes = (c_char_p, )
74 libcrypto.ENGINE_init.argtypes = (c_void_p, )
75 libcrypto.UI_OpenSSL.restype = c_void_p
76 libcrypto.ENGINE_load_private_key.argtypes = (c_void_p, c_char_p, c_void_p, c_void_p)
77 libcrypto.ENGINE_load_private_key.restype = c_void_p