]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/engine.py
Fixed most pylint warning.
[oss/ctypescrypto.git] / ctypescrypto / engine.py
1 """
2 engine loading and configuration
3 """
4 from ctypes import c_void_p, c_char_p, c_int
5 from ctypescrypto import libcrypto
6 from ctypescrypto.exception import LibCryptoError
7
8 __all__ = ['default', 'set_default']
9
10 default = None
11
12 def set_default(engine):
13     """
14     Loads specified engine and sets it as default for all
15     algorithms, supported by it
16     """
17     global default
18     eng = libcrypto.ENGINE_by_id(engine)
19     if eng is None:
20         # Try load engine
21         eng = libcrypto.ENGINE_by_id("dynamic")
22         if  eng is None:
23             raise LibCryptoError("Cannot get 'dynamic' engine")
24         if not libcrypto.ENGINE_ctrl_cmd_string(eng, "SO_PATH", engine, 0):
25             raise LibCryptoError("Cannot execute ctrl cmd SO_PATH")
26         if not libcrypto.ENGINE_ctrl_cmd_string(eng, "LOAD", None, 0):
27             raise LibCryptoError("Cannot execute ctrl cmd LOAD")
28     if eng is None:
29         raise ValueError("Cannot find engine " + engine)
30     libcrypto.ENGINE_set_default(eng, c_int(0xFFFF))
31     default = eng
32
33 # Declare function result and arguments for used functions
34 libcrypto.ENGINE_by_id.restype = c_void_p
35 libcrypto.ENGINE_by_id.argtypes = (c_char_p, )
36 libcrypto.ENGINE_set_default.argtypes = (c_void_p, c_int)
37 libcrypto.ENGINE_ctrl_cmd_string.argtypes = (c_void_p, c_char_p, c_char_p,
38                                              c_int)
39 libcrypto.ENGINE_finish.argtypes = (c_char_p, )