]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/engine.py
Begin to implement python3 support. Now tests for oid, bio, cipher, digest, mac and...
[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,pyver
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         if pyver > 2 or isinstance(engine_id, unicode):
20             engine_id = engine_id.encode('utf-8')
21         eng = libcrypto.ENGINE_by_id(engine_id)
22         if eng is None:
23             # Try load engine
24             eng = libcrypto.ENGINE_by_id("dynamic")
25             if  eng is None:
26                 raise LibCryptoError("Cannot get 'dynamic' engine")
27             if not libcrypto.ENGINE_ctrl_cmd_string(eng, "SO_PATH",
28                                                     engine_id, 0):
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")
32         if eng is None:
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")
39         self.ptr = eng
40
41     def private_key(self, key_id, ui_method = None, ui_data=None):
42         from ctypescrypto.pkey import PKey
43         if ui_method is None:
44             ui_ptr = libcrypto.UI_OpenSSL()
45         else:
46             ui_ptr = ui_method.ptr
47         pkey = libcrypto.ENGINE_load_private_key(self.ptr, key_id, ui_ptr,
48                                                  ui_data)
49         if pkey is None:
50             raise LibCryptoError("Cannot load private key")
51         return PKey(ptr=pkey, cansign=True)
52
53 def set_default(eng, algorithms=0xFFFF):
54     """
55     Sets specified engine  as default for all
56     algorithms, supported by it
57
58     For compatibility with 0.2.x if string is passed instead
59     of engine, attempts to load engine with this id
60     """
61     if not isinstance(eng,Engine):
62         eng=Engine(eng)
63     global default
64     libcrypto.ENGINE_set_default(eng.ptr, c_int(algorithms))
65     default = eng
66
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,
72                                              c_int)
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