Some more tweaks to ENGINE code.
[openssl.git] / crypto / engine / engine_lib.c
index 1df07af03a68c1e17bcc7583349e06d67382462a..3b8fe6936a0db12162548b29bdeab7076b74f58a 100644 (file)
@@ -153,7 +153,7 @@ int ENGINE_init(ENGINE *e)
        if((e->funct_ref == 0) && e->init)
                /* This is the first functional reference and the engine
                 * requires initialisation so we do it now. */
-               to_return = e->init();
+               to_return = e->init(e);
        if(to_return)
                {
                /* OK, we return a functional reference which is also a
@@ -200,7 +200,7 @@ int ENGINE_finish(ENGINE *e)
                {
                CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
                /* CODE ALERT: This *IS* supposed to be "=" and NOT "==" :-) */
-               if((to_return = e->finish()))
+               if((to_return = e->finish(e)))
                        {
                        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
                        /* Cleanup the functional reference which is also a
@@ -230,18 +230,19 @@ EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
        if(e->funct_ref == 0)
                {
+               CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
                ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
                        ENGINE_R_NOT_INITIALISED);
                return 0;
                }
+       CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
        if (!e->load_privkey)
                {
                ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
                        ENGINE_R_NO_LOAD_FUNCTION);
                return 0;
                }
-       CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
-       pkey = e->load_privkey(key_id, passphrase);
+       pkey = e->load_privkey(e, key_id, passphrase);
        if (!pkey)
                {
                ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
@@ -265,18 +266,19 @@ EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
        if(e->funct_ref == 0)
                {
+               CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
                ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
                        ENGINE_R_NOT_INITIALISED);
                return 0;
                }
+       CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
        if (!e->load_pubkey)
                {
                ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
                        ENGINE_R_NO_LOAD_FUNCTION);
                return 0;
                }
-       CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
-       pkey = e->load_pubkey(key_id, passphrase);
+       pkey = e->load_pubkey(e, key_id, passphrase);
        if (!pkey)
                {
                ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
@@ -286,28 +288,29 @@ EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
        return pkey;
        }
 
-/* Initialise a engine type for use (or up its functional reference count
- * if it's already in use). */
 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
        {
+       int ctrl_exists, ref_exists;
        if(e == NULL)
                {
                ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
                return 0;
                }
        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
-       if(e->struct_ref == 0)
+       ref_exists = ((e->struct_ref > 0) ? 1 : 0);
+       ctrl_exists = (e->ctrl ? 1 : 0);
+       CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+       if(!ref_exists)
                {
                ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
                return 0;
                }
-       if (!e->ctrl)
+       if (!ctrl_exists)
                {
                ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
                return 0;
                }
-       CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
-       return e->ctrl(cmd, i, p, f);
+       return e->ctrl(e, cmd, i, p, f);
        }
 
 static ENGINE *engine_get_default_type(ENGINE_TYPE t)