]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/engine.py
79f6218aa0a3beee5558bec9f357d9f1cb47cd5d
[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', 'Engine']
9
10 default = None
11
12 class Engine(object):
13     """
14     Represents Openssl loadable module (engine).
15     Allows to create PKey objects from private keys stored
16     in the token, accessed by engine
17     """
18     def __init__(self, engine_id, **kwargs):
19         eng = libcrypto.ENGINE_by_id(engine_id)
20         if eng is None:
21             # Try load engine
22             eng = libcrypto.ENGINE_by_id("dynamic")
23             if  eng is None:
24                 raise LibCryptoError("Cannot get 'dynamic' engine")
25             if not libcrypto.ENGINE_ctrl_cmd_string(eng, "SO_PATH",
26                                                     engine_id, 0):
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")
30         if eng is None:
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")
37         self.ptr = eng
38
39     def private_key(self, key_id, ui_method = None, ui_data=None):
40         from ctypescrypto.pkey import PKey
41         if ui_method is None:
42             ui_ptr = libcrypto.UI_OpenSSL()
43         else:
44             ui_ptr = ui_method.ptr
45         pkey = libcrypto.ENGINE_load_private_key(self.ptr, key_id, ui_ptr,
46                                                  ui_data)
47         if pkey is None:
48             raise LibCryptoError("Cannot load private key")
49         return PKey(ptr=pkey, cansign=True)
50
51 def set_default(eng, algorithms=0xFFFF):
52     """
53     Sets specified engine  as default for all
54     algorithms, supported by it
55
56     For compatibility with 0.2.x if string is passed instead
57     of engine, attempts to load engine with this id
58     """
59     if not isinstance(eng,Engine):
60         eng=Engine(eng)
61     global default
62     libcrypto.ENGINE_set_default(eng.ptr, c_int(algorithms))
63     default = eng
64
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,
70                                              c_int)
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