Some more tweaks to ENGINE code.
[openssl.git] / crypto / engine / hw_nuron.c
index b5e63906c96503846cb2f3df86fcda890a552b2d..d5697dade667904b2b11870d7642c7bf96469d97 100644 (file)
 #include <openssl/crypto.h>
 #include "cryptlib.h"
 #include <openssl/dso.h>
-#include "engine_int.h"
 #include <openssl/engine.h>
 
 
 #ifndef OPENSSL_NO_HW
 #ifndef OPENSSL_NO_HW_NURON
 
-static const char *NURON_LIBNAME = "nuronssl";
+static const char def_NURON_LIBNAME[] = "nuronssl";
+static const char *NURON_LIBNAME = def_NURON_LIBNAME;
 static const char *NURON_F1 = "nuron_mod_exp";
 
+/* The definitions for control commands specific to this engine */
+#define NURON_CMD_SO_PATH              ENGINE_CMD_BASE
+static const ENGINE_CMD_DEFN nuron_cmd_defns[] = {
+       {NURON_CMD_SO_PATH,
+               "SO_PATH",
+               "Specifies the path to the 'nuronssl' shared library",
+               ENGINE_CMD_FLAG_STRING},
+       {0, NULL, NULL, 0}
+       };
+
 typedef int tfnModExp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m);
 static tfnModExp *pfnModExp = NULL;
 
@@ -83,7 +93,7 @@ static int nuron_init(ENGINE *e)
                return 0;
                }
 
-       pvDSOHandle=DSO_load(NULL, NURON_LIBNAME, NULL,
+       pvDSOHandle = DSO_load(NULL, NURON_LIBNAME, NULL,
                DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
        if(!pvDSOHandle)
                {
@@ -91,7 +101,7 @@ static int nuron_init(ENGINE *e)
                return 0;
                }
 
-       pfnModExp=(tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1);
+       pfnModExp = (tfnModExp *)DSO_bind_func(pvDSOHandle, NURON_F1);
        if(!pfnModExp)
                {
                ENGINEerr(ENGINE_F_NURON_INIT,ENGINE_R_DSO_FUNCTION_NOT_FOUND);
@@ -118,6 +128,31 @@ static int nuron_finish(ENGINE *e)
        return 1;
        }
 
+static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
+       {
+       int initialised = ((pvDSOHandle == NULL) ? 0 : 1);
+       switch(cmd)
+               {
+       case NURON_CMD_SO_PATH:
+               if(p == NULL)
+                       {
+                       ENGINEerr(ENGINE_F_NURON_CTRL,ERR_R_PASSED_NULL_PARAMETER);
+                       return 0;
+                       }
+               if(initialised)
+                       {
+                       ENGINEerr(ENGINE_F_NURON_CTRL,ENGINE_R_ALREADY_LOADED);
+                       return 0;
+                       }
+               NURON_LIBNAME = (const char *)p;
+               return 1;
+       default:
+               break;
+               }
+       ENGINEerr(ENGINE_F_NURON_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
+       return 0;
+}
+
 static int nuron_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,
                         const BIGNUM *m,BN_CTX *ctx)
        {
@@ -230,25 +265,9 @@ static DH_METHOD nuron_dh =
        NULL
        };
 
-static ENGINE engine_nuron =
-       {
-       "nuron",
-       "Nuron hardware engine support",
-       &nuron_rsa,
-       &nuron_dsa,
-       &nuron_dh,
-       NULL,
-       nuron_mod_exp,
-       NULL,
-       nuron_init,
-       nuron_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_nuron_id = "nuron";
+static const char *engine_nuron_name = "Nuron 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!!!) */
@@ -257,6 +276,23 @@ ENGINE *ENGINE_nuron()
        const RSA_METHOD *meth1;
        const DSA_METHOD *meth2;
        const DH_METHOD *meth3;
+       ENGINE *ret = ENGINE_new();
+       if(!ret)
+               return NULL;
+       if(!ENGINE_set_id(ret, engine_nuron_id) ||
+                       !ENGINE_set_name(ret, engine_nuron_name) ||
+                       !ENGINE_set_RSA(ret, &nuron_rsa) ||
+                       !ENGINE_set_DSA(ret, &nuron_dsa) ||
+                       !ENGINE_set_DH(ret, &nuron_dh) ||
+                       !ENGINE_set_BN_mod_exp(ret, nuron_mod_exp) ||
+                       !ENGINE_set_init_function(ret, nuron_init) ||
+                       !ENGINE_set_finish_function(ret, nuron_finish) ||
+                       !ENGINE_set_ctrl_function(ret, nuron_ctrl) ||
+                       !ENGINE_set_cmd_defns(ret, nuron_cmd_defns))
+               {
+               ENGINE_free(ret);
+               return NULL;
+               }
 
        /* We know that the "PKCS1_SSLeay()" functions hook properly
         * to the nuron-specific mod_exp and mod_exp_crt so we use
@@ -282,7 +318,7 @@ ENGINE *ENGINE_nuron()
        meth3=DH_OpenSSL();
        nuron_dh.generate_key=meth3->generate_key;
        nuron_dh.compute_key=meth3->compute_key;
-       return &engine_nuron;
+       return ret;
        }
 
 #endif /* !OPENSSL_NO_HW_NURON */