Commit 40e068d5 authored by Matt Caswell's avatar Matt Caswell Committed by Richard Levitte
Browse files

Move engine library over to using the new thread API



Remove usage of CRYPTO_LOCK_ENGINE

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
parent 660e7588
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -182,9 +182,9 @@ int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
        ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_write_lock(global_engine_lock);
    ref_exists = ((e->struct_ref > 0) ? 1 : 0);
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_unlock(global_engine_lock);
    ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
    if (!ref_exists) {
        ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
+4 −4
Original line number Diff line number Diff line
@@ -217,7 +217,7 @@ static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
    c->DYNAMIC_F1 = "v_check";
    c->DYNAMIC_F2 = "bind_engine";
    c->dir_load = 1;
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_write_lock(global_engine_lock);
    if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
                                                       dynamic_ex_data_idx))
        == NULL) {
@@ -226,7 +226,7 @@ static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
        *ctx = c;
        c = NULL;
    }
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_unlock(global_engine_lock);
    /*
     * If we lost the race to set the context, c is non-NULL and *ctx is the
     * context of the thread that won.
@@ -256,14 +256,14 @@ static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
            ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX);
            return NULL;
        }
        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
        CRYPTO_THREAD_write_lock(global_engine_lock);
        /* Avoid a race by checking again inside this lock */
        if (dynamic_ex_data_idx < 0) {
            /* Good, someone didn't beat us to it */
            dynamic_ex_data_idx = new_idx;
            new_idx = -1;
        }
        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
        CRYPTO_THREAD_unlock(global_engine_lock);
        /*
         * In theory we could "give back" the index here if (new_idx>-1), but
         * it's not possible and wouldn't gain us much if it were.
+7 −6
Original line number Diff line number Diff line
@@ -101,10 +101,10 @@ int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
    engine_ref_debug(e, 1, -1);
    if ((e->funct_ref == 0) && e->finish) {
        if (unlock_for_handlers)
            CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
            CRYPTO_THREAD_unlock(global_engine_lock);
        to_return = e->finish(e);
        if (unlock_for_handlers)
            CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
            CRYPTO_THREAD_write_lock(global_engine_lock);
        if (!to_return)
            return 0;
    }
@@ -125,9 +125,10 @@ int ENGINE_init(ENGINE *e)
        ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
    CRYPTO_THREAD_write_lock(global_engine_lock);
    ret = engine_unlocked_init(e);
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_unlock(global_engine_lock);
    return ret;
}

@@ -138,9 +139,9 @@ int ENGINE_finish(ENGINE *e)

    if (e == NULL)
        return 1;
    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_write_lock(global_engine_lock);
    to_return = engine_unlocked_finish(e, 1);
    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
    CRYPTO_THREAD_unlock(global_engine_lock);
    if (!to_return) {
        ENGINEerr(ENGINE_F_ENGINE_FINISH, ENGINE_R_FINISH_FAILED);
        return 0;
+9 −2
Original line number Diff line number Diff line
@@ -65,12 +65,15 @@
# define HEADER_ENGINE_INT_H

# include "internal/cryptlib.h"
# include "internal/threads.h"
# include <internal/engine.h>

#ifdef  __cplusplus
extern "C" {
#endif

extern CRYPTO_RWLOCK *global_engine_lock;

/*
 * If we compile with this symbol defined, then both reference counts in the
 * ENGINE structure will be monitored with a line of output on stderr for
@@ -97,7 +100,7 @@ extern "C" {
/*
 * Any code that will need cleanup operations should use these functions to
 * register callbacks. ENGINE_cleanup() will call all registered callbacks in
 * order. NB: both the "add" functions assume CRYPTO_LOCK_ENGINE to already be
 * order. NB: both the "add" functions assume the engine lock to already be
 * held (in "write" mode).
 */
typedef void (ENGINE_CLEANUP_CB) (void);
@@ -144,7 +147,7 @@ void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
/*
 * Internal versions of API functions that have control over locking. These
 * are used between C files when functionality needs to be shared but the
 * caller may already be controlling of the CRYPTO_LOCK_ENGINE lock.
 * caller may already be controlling of the engine lock.
 */
int engine_unlocked_init(ENGINE *e);
int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers);
@@ -167,6 +170,10 @@ void engine_set_all_null(ENGINE *e);
void engine_pkey_meths_free(ENGINE *e);
void engine_pkey_asn1_meths_free(ENGINE *e);

/* Once initialisation function */
extern CRYPTO_ONCE engine_lock_init;
void do_engine_lock_init(void);

/*
 * This is a structure for storing implementations of various crypto
 * algorithms and functions.
+13 −1
Original line number Diff line number Diff line
@@ -59,12 +59,23 @@
#include "eng_int.h"
#include <openssl/rand.h>

CRYPTO_RWLOCK *global_engine_lock;

CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT;

/* The "new"/"free" stuff first */

void do_engine_lock_init(void)
{
    global_engine_lock = CRYPTO_THREAD_lock_new();
}

ENGINE *ENGINE_new(void)
{
    ENGINE *ret;

    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);

    ret = OPENSSL_zalloc(sizeof(*ret));
    if (ret == NULL) {
        ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
@@ -108,7 +119,7 @@ int engine_free_util(ENGINE *e, int locked)
    if (e == NULL)
        return 1;
    if (locked)
        i = CRYPTO_add(&e->struct_ref, -1, CRYPTO_LOCK_ENGINE);
        CRYPTO_atomic_add(&e->struct_ref, -1, &i, global_engine_lock);
    else
        i = --e->struct_ref;
    engine_ref_debug(e, 0, -1)
@@ -201,6 +212,7 @@ void ENGINE_cleanup(void)
     * registering a cleanup callback.
     */
    RAND_set_rand_method(NULL);
    CRYPTO_THREAD_lock_free(global_engine_lock);
}

/* Now the "ex_data" support */
Loading