]> www.wagner.pp.ru Git - oss/ctypescrypto.git/commitdiff
Fixed broken engine.set_default
authorVictor Wagner <vitus@wagner.pp.ru>
Fri, 7 Aug 2015 12:38:59 +0000 (15:38 +0300)
committerVictor Wagner <vitus@wagner.pp.ru>
Fri, 7 Aug 2015 12:38:59 +0000 (15:38 +0300)
ctypescrypto/engine.py
setup.py

index c2858e6ebd5b459a1dceac7eac99dc7d5be3388e..f5bdb7514f007ba6f10e0ae3dadb800a53328e90 100644 (file)
@@ -5,29 +5,61 @@ from ctypes import c_void_p, c_char_p, c_int
 from ctypescrypto import libcrypto
 from ctypescrypto.exception import LibCryptoError
 
-__all__ = ['default', 'set_default']
+__all__ = ['default', 'set_default', 'Engine']
 
 default = None
 
-def set_default(engine):
+class Engine(object):
     """
-    Loads specified engine and sets it as default for all
+    Represents Openssl loadable module (engine).
+    Allows to create PKey objects from private keys stored
+    in the token, accessed by engine
+    """
+    def __init__(self, engine_id, **kwargs):
+        eng = libcrypto.ENGINE_by_id(engine_id)
+        if eng is None:
+            # Try load engine
+            eng = libcrypto.ENGINE_by_id("dynamic")
+            if  eng is None:
+                raise LibCryptoError("Cannot get 'dynamic' engine")
+            if not libcrypto.ENGINE_ctrl_cmd_string(eng, "SO_PATH",
+                                                    engine_id, 0):
+                raise LibCryptoError("Cannot execute ctrl cmd SO_PATH")
+            if not libcrypto.ENGINE_ctrl_cmd_string(eng, "LOAD", None, 0):
+                raise LibCryptoError("Cannot execute ctrl cmd LOAD")
+        if eng is None:
+            raise ValueError("Cannot find engine " + engine)
+        for cmd, value in kwargs.items():
+            if not libcrypto.ENGINE_ctrl_cmd_string(eng, cmd, value, 0):
+                raise LibCryptoError("Cannot execute ctrl cmd %s" % cmd)
+        if not libcrypto.ENGINE_init(eng):
+            raise LibCryptoError("Cannot initialize engine")
+        self.ptr = eng
+
+    def private_key(self, key_id, ui_method = None, ui_data=None):
+        from ctypescrypto.pkey import PKey
+        if ui_method is None:
+            ui_ptr = libcrypto.UI_OpenSSL()
+        else:
+            ui_ptr = ui_method.ptr
+        pkey = libcrypto.ENGINE_load_private_key(self.ptr, key_id, ui_ptr,
+                                                 ui_data)
+        if pkey is None:
+            raise LibCryptoError("Cannot load private key")
+        return PKey(ptr=pkey, cansign=True)
+
+def set_default(eng, algorithms=0xFFFF):
+    """
+    Sets specified engine  as default for all
     algorithms, supported by it
+
+    For compatibility with 0.2.x if string is passed instead
+    of engine, attempts to load engine with this id
     """
+    if not isinstance(eng,Engine):
+        eng=Engine(eng)
     global default
-    eng = libcrypto.ENGINE_by_id(engine)
-    if eng is None:
-        # Try load engine
-        eng = libcrypto.ENGINE_by_id("dynamic")
-        if  eng is None:
-            raise LibCryptoError("Cannot get 'dynamic' engine")
-        if not libcrypto.ENGINE_ctrl_cmd_string(eng, "SO_PATH", engine, 0):
-            raise LibCryptoError("Cannot execute ctrl cmd SO_PATH")
-        if not libcrypto.ENGINE_ctrl_cmd_string(eng, "LOAD", None, 0):
-            raise LibCryptoError("Cannot execute ctrl cmd LOAD")
-    if eng is None:
-        raise ValueError("Cannot find engine " + engine)
-    libcrypto.ENGINE_set_default(eng, c_int(0xFFFF))
+    libcrypto.ENGINE_set_default(eng.ptr, c_int(algorithms))
     default = eng
 
 # Declare function result and arguments for used functions
index bed60db66c28442826b8e07086f7c675acda9a6f..0b85ab9e6116f327c0c8ec0cb0bc669b9aa966dc 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -33,7 +33,7 @@ class MyTests(distutils.cmd.Command):
 
 setup(
     name="ctypescrypto",
-    version="0.3.0",
+    version="0.3.1",
     description="CTypes-based interface for some OpenSSL libcrypto features",
     author="Victor Wagner",
     author_email="vitus@wagner.pp.ru",