Some more tweaks to ENGINE code.
[openssl.git] / crypto / engine / hw_cswift.c
index 4066d1d164011e9fb75526dd577cf34194e45b5f..15983525e1b9057c1ed1b9d411f02cb8e19fdc9b 100644 (file)
@@ -60,7 +60,6 @@
 #include <openssl/crypto.h>
 #include "cryptlib.h"
 #include <openssl/dso.h>
-#include "engine_int.h"
 #include <openssl/engine.h>
 
 #ifndef OPENSSL_NO_HW
@@ -84,8 +83,9 @@
 #include "vendor_defns/cswift.h"
 #endif
 
-static int cswift_init(ENGINE *);
-static int cswift_finish(ENGINE *);
+static int cswift_init(ENGINE *e);
+static int cswift_finish(ENGINE *e);
+static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
 
 /* BIGNUM stuff */
 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
@@ -111,6 +111,15 @@ static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
                const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 
+/* The definitions for control commands specific to this engine */
+#define CSWIFT_CMD_SO_PATH             ENGINE_CMD_BASE
+static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
+       {CSWIFT_CMD_SO_PATH,
+               "SO_PATH",
+               "Specifies the path to the 'cswift' shared library",
+               ENGINE_CMD_FLAG_STRING},
+       {0, NULL, NULL, 0}
+       };
 
 /* Our internal RSA_METHOD that we provide pointers to */
 static RSA_METHOD cswift_rsa =
@@ -158,26 +167,9 @@ static DH_METHOD cswift_dh =
        NULL
        };
 
-/* Our ENGINE structure. */
-static ENGINE engine_cswift =
-        {
-       "cswift",
-       "CryptoSwift hardware engine support",
-       &cswift_rsa,
-       &cswift_dsa,
-       &cswift_dh,
-       NULL,
-       cswift_mod_exp,
-       cswift_mod_exp_crt,
-       cswift_init,
-       cswift_finish,
-       NULL, /* no ctrl() */
-       NULL, /* no load_privkey() */
-       NULL, /* no load_pubkey() */
-       0, /* no flags */
-       0, 0, /* no references */
-       NULL, NULL /* unlinked */
-        };
+/* Constants used when creating the ENGINE */
+static const char *engine_cswift_id = "cswift";
+static const char *engine_cswift_name = "CryptoSwift hardware engine support";
 
 /* As this is only ever called once, there's no need for locking
  * (indeed - the lock will already be held by our caller!!!) */
@@ -185,6 +177,24 @@ ENGINE *ENGINE_cswift()
        {
        const RSA_METHOD *meth1;
        const DH_METHOD *meth2;
+       ENGINE *ret = ENGINE_new();
+       if(!ret)
+               return NULL;
+       if(!ENGINE_set_id(ret, engine_cswift_id) ||
+                       !ENGINE_set_name(ret, engine_cswift_name) ||
+                       !ENGINE_set_RSA(ret, &cswift_rsa) ||
+                       !ENGINE_set_DSA(ret, &cswift_dsa) ||
+                       !ENGINE_set_DH(ret, &cswift_dh) ||
+                       !ENGINE_set_BN_mod_exp(ret, &cswift_mod_exp) ||
+                       !ENGINE_set_BN_mod_exp_crt(ret, &cswift_mod_exp_crt) ||
+                       !ENGINE_set_init_function(ret, cswift_init) ||
+                       !ENGINE_set_finish_function(ret, cswift_finish) ||
+                       !ENGINE_set_ctrl_function(ret, cswift_ctrl) ||
+                       !ENGINE_set_cmd_defns(ret, cswift_cmd_defns))
+               {
+               ENGINE_free(ret);
+               return NULL;
+               }
 
        /* We know that the "PKCS1_SSLeay()" functions hook properly
         * to the cswift-specific mod_exp and mod_exp_crt so we use
@@ -203,7 +213,7 @@ ENGINE *ENGINE_cswift()
        meth2 = DH_OpenSSL();
        cswift_dh.generate_key = meth2->generate_key;
        cswift_dh.compute_key = meth2->compute_key;
-       return &engine_cswift;
+       return ret;
        }
 
 /* This is a process-global DSO handle used for loading and unloading
@@ -221,7 +231,8 @@ t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
 
 /* Used in the DSO operations. */
-static const char *CSWIFT_LIBNAME = "swift";
+static const char def_CSWIFT_LIBNAME[] = "swift";
+static const char *CSWIFT_LIBNAME = def_CSWIFT_LIBNAME;
 static const char *CSWIFT_F1 = "swAcquireAccContext";
 static const char *CSWIFT_F2 = "swAttachKeyParam";
 static const char *CSWIFT_F3 = "swSimpleRequest";
@@ -328,6 +339,33 @@ static int cswift_finish(ENGINE *e)
        return 1;
        }
 
+static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
+       {
+       int initialised = ((cswift_dso == NULL) ? 0 : 1);
+       switch(cmd)
+               {
+       case CSWIFT_CMD_SO_PATH:
+               if(p == NULL)
+                       {
+                       ENGINEerr(ENGINE_F_CSWIFT_CTRL,
+                               ERR_R_PASSED_NULL_PARAMETER);
+                       return 0;
+                       }
+               if(initialised)
+                       {
+                       ENGINEerr(ENGINE_F_CSWIFT_CTRL,
+                               ENGINE_R_ALREADY_LOADED);
+                       return 0;
+                       }
+               CSWIFT_LIBNAME = (const char *)p;
+               return 1;
+       default:
+               break;
+               }
+       ENGINEerr(ENGINE_F_CSWIFT_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
+       return 0;
+       }
+
 /* Un petit mod_exp */
 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                        const BIGNUM *m, BN_CTX *ctx)