[openssl-commits] [openssl] master update

Richard Levitte levitte at openssl.org
Wed Mar 9 10:06:58 UTC 2016


The branch master has been updated
       via  71a07ca7bf207ccc795330441da76b02c7261091 (commit)
       via  af48aa7197cbaf9e18eede5861182c0a6b2761c1 (commit)
       via  ae6412f3bed37a4a7e8248ccbc31c849f20ac857 (commit)
       via  0231a02963a80a3d2efe43745ba19dba5fe9213f (commit)
       via  40e068d50687edbf9b6d6633563151ab59369747 (commit)
      from  660e7588ed5305554215f3c1174a2a8e0b3b9485 (commit)


- Log -----------------------------------------------------------------
commit 71a07ca7bf207ccc795330441da76b02c7261091
Author: Richard Levitte <levitte at openssl.org>
Date:   Wed Mar 9 10:51:30 2016 +0100

    Convert the dynlocks in e_chil to the new Thread API locks
    
    Reviewed-by: Matt Caswell <matt at openssl.org>

commit af48aa7197cbaf9e18eede5861182c0a6b2761c1
Author: Matt Caswell <matt at openssl.org>
Date:   Wed Mar 9 01:07:26 2016 +0000

    Remove another lock from e_chil
    
    Reviewed-by: Richard Levitte <levitte at openssl.org>

commit ae6412f3bed37a4a7e8248ccbc31c849f20ac857
Author: Matt Caswell <matt at openssl.org>
Date:   Wed Mar 9 00:53:38 2016 +0000

    Always call ENGINE_cleanup() in de-init
    
    Even if we haven't loaded an engine, we might have set up the
    global_engine_lock, so we should still clean up.
    
    Reviewed-by: Richard Levitte <levitte at openssl.org>

commit 0231a02963a80a3d2efe43745ba19dba5fe9213f
Author: Matt Caswell <matt at openssl.org>
Date:   Tue Mar 8 21:50:46 2016 +0000

    Move chil engine to the new thread api
    
    Move the chil engine to use the new thread API. As I don't have access to
    the hardware I can't test this :-(. I think its ok...
    
    Reviewed-by: Richard Levitte <levitte at openssl.org>

commit 40e068d50687edbf9b6d6633563151ab59369747
Author: Matt Caswell <matt at openssl.org>
Date:   Tue Mar 8 16:44:34 2016 +0000

    Move engine library over to using the new thread API
    
    Remove usage of CRYPTO_LOCK_ENGINE
    
    Reviewed-by: Richard Levitte <levitte at openssl.org>

-----------------------------------------------------------------------

Summary of changes:
 crypto/engine/eng_ctrl.c  |  4 +--
 crypto/engine/eng_dyn.c   |  8 +++---
 crypto/engine/eng_init.c  | 13 ++++-----
 crypto/engine/eng_int.h   | 11 ++++++--
 crypto/engine/eng_lib.c   | 14 +++++++++-
 crypto/engine/eng_list.c  | 36 ++++++++++++++-----------
 crypto/engine/eng_pkey.c  | 18 ++++++-------
 crypto/engine/eng_table.c | 16 +++++------
 crypto/engine/tb_asnmth.c |  6 +++--
 crypto/init.c             | 23 +++-------------
 engines/e_chil.c          | 69 ++++++++++++++++++++++++-----------------------
 11 files changed, 116 insertions(+), 102 deletions(-)

diff --git a/crypto/engine/eng_ctrl.c b/crypto/engine/eng_ctrl.c
index 2db9902..c912c1d 100644
--- a/crypto/engine/eng_ctrl.c
+++ b/crypto/engine/eng_ctrl.c
@@ -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);
diff --git a/crypto/engine/eng_dyn.c b/crypto/engine/eng_dyn.c
index 597151b..bdfc00c 100644
--- a/crypto/engine/eng_dyn.c
+++ b/crypto/engine/eng_dyn.c
@@ -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.
diff --git a/crypto/engine/eng_init.c b/crypto/engine/eng_init.c
index ddf552a..f3c0de9 100644
--- a/crypto/engine/eng_init.c
+++ b/crypto/engine/eng_init.c
@@ -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;
diff --git a/crypto/engine/eng_int.h b/crypto/engine/eng_int.h
index 0eee211..f6a54d8 100644
--- a/crypto/engine/eng_int.h
+++ b/crypto/engine/eng_int.h
@@ -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.
diff --git a/crypto/engine/eng_lib.c b/crypto/engine/eng_lib.c
index 15c2c6f..dd47342 100644
--- a/crypto/engine/eng_lib.c
+++ b/crypto/engine/eng_lib.c
@@ -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 */
diff --git a/crypto/engine/eng_list.c b/crypto/engine/eng_list.c
index 8ca1f80..4883d14 100644
--- a/crypto/engine/eng_list.c
+++ b/crypto/engine/eng_list.c
@@ -96,7 +96,7 @@ static void engine_list_cleanup(void)
 
 /*
  * These static functions starting with a lower case "engine_" always take
- * place when CRYPTO_LOCK_ENGINE has been locked up.
+ * place when global_engine_lock has been locked up.
  */
 static int engine_list_add(ENGINE *e)
 {
@@ -184,13 +184,14 @@ ENGINE *ENGINE_get_first(void)
 {
     ENGINE *ret;
 
-    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_list_head;
     if (ret) {
         ret->struct_ref++;
         engine_ref_debug(ret, 0, 1);
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     return ret;
 }
 
@@ -198,13 +199,14 @@ ENGINE *ENGINE_get_last(void)
 {
     ENGINE *ret;
 
-    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_list_tail;
     if (ret) {
         ret->struct_ref++;
         engine_ref_debug(ret, 0, 1);
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     return ret;
 }
 
@@ -216,14 +218,14 @@ ENGINE *ENGINE_get_next(ENGINE *e)
         ENGINEerr(ENGINE_F_ENGINE_GET_NEXT, ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     ret = e->next;
     if (ret) {
         /* Return a valid structural reference to the next ENGINE */
         ret->struct_ref++;
         engine_ref_debug(ret, 0, 1);
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     /* Release the structural reference to the previous ENGINE */
     ENGINE_free(e);
     return ret;
@@ -236,14 +238,14 @@ ENGINE *ENGINE_get_prev(ENGINE *e)
         ENGINEerr(ENGINE_F_ENGINE_GET_PREV, ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     ret = e->prev;
     if (ret) {
         /* Return a valid structural reference to the next ENGINE */
         ret->struct_ref++;
         engine_ref_debug(ret, 0, 1);
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     /* Release the structural reference to the previous ENGINE */
     ENGINE_free(e);
     return ret;
@@ -261,12 +263,12 @@ int ENGINE_add(ENGINE *e)
         ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_ID_OR_NAME_MISSING);
         return 0;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (!engine_list_add(e)) {
         ENGINEerr(ENGINE_F_ENGINE_ADD, ENGINE_R_INTERNAL_LIST_ERROR);
         to_return = 0;
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     return to_return;
 }
 
@@ -278,12 +280,12 @@ int ENGINE_remove(ENGINE *e)
         ENGINEerr(ENGINE_F_ENGINE_REMOVE, ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (!engine_list_remove(e)) {
         ENGINEerr(ENGINE_F_ENGINE_REMOVE, ENGINE_R_INTERNAL_LIST_ERROR);
         to_return = 0;
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     return to_return;
 }
 
@@ -325,7 +327,8 @@ ENGINE *ENGINE_by_id(const char *id)
         ENGINEerr(ENGINE_F_ENGINE_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
         return NULL;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     iterator = engine_list_head;
     while (iterator && (strcmp(id, iterator->id) != 0))
         iterator = iterator->next;
@@ -348,7 +351,7 @@ ENGINE *ENGINE_by_id(const char *id)
             engine_ref_debug(iterator, 0, 1);
         }
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     if (iterator != NULL)
         return iterator;
     /*
@@ -377,10 +380,11 @@ ENGINE *ENGINE_by_id(const char *id)
 
 int ENGINE_up_ref(ENGINE *e)
 {
+    int i;
     if (e == NULL) {
         ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
+    CRYPTO_atomic_add(&e->struct_ref, 1, &i, global_engine_lock);
     return 1;
 }
diff --git a/crypto/engine/eng_pkey.c b/crypto/engine/eng_pkey.c
index e581ac3..ee6ba28 100644
--- a/crypto/engine/eng_pkey.c
+++ b/crypto/engine/eng_pkey.c
@@ -105,13 +105,13 @@ EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
                   ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (e->funct_ref == 0) {
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(global_engine_lock);
         ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, ENGINE_R_NOT_INITIALISED);
         return 0;
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     if (!e->load_privkey) {
         ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
                   ENGINE_R_NO_LOAD_FUNCTION);
@@ -136,13 +136,13 @@ EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
                   ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (e->funct_ref == 0) {
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(global_engine_lock);
         ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NOT_INITIALISED);
         return 0;
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     if (!e->load_pubkey) {
         ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NO_LOAD_FUNCTION);
         return 0;
@@ -167,14 +167,14 @@ int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
                   ERR_R_PASSED_NULL_PARAMETER);
         return 0;
     }
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (e->funct_ref == 0) {
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(global_engine_lock);
         ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
                   ENGINE_R_NOT_INITIALISED);
         return 0;
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     if (!e->load_ssl_client_cert) {
         ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
                   ENGINE_R_NO_LOAD_FUNCTION);
diff --git a/crypto/engine/eng_table.c b/crypto/engine/eng_table.c
index 22390d2..9648c0c 100644
--- a/crypto/engine/eng_table.c
+++ b/crypto/engine/eng_table.c
@@ -130,7 +130,7 @@ int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
 {
     int ret = 0, added = 0;
     ENGINE_PILE tmplate, *fnd;
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (!(*table))
         added = 1;
     if (!int_table_check(table, 1))
@@ -179,7 +179,7 @@ int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
     }
     ret = 1;
  end:
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     return ret;
 }
 
@@ -201,10 +201,10 @@ IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE);
 
 void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
 {
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (int_table_check(table, 0))
         lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e);
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
 }
 
 static void int_cleanup_cb_doall(ENGINE_PILE *p)
@@ -219,13 +219,13 @@ static void int_cleanup_cb_doall(ENGINE_PILE *p)
 
 void engine_table_cleanup(ENGINE_TABLE **table)
 {
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     if (*table) {
         lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall);
         lh_ENGINE_PILE_free(&(*table)->piles);
         *table = NULL;
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
 }
 
 /* return a functional reference for a given 'nid' */
@@ -248,7 +248,7 @@ ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
         return NULL;
     }
     ERR_set_mark();
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     /*
      * Check again inside the lock otherwise we could race against cleanup
      * operations. But don't worry about a fprintf(stderr).
@@ -319,7 +319,7 @@ ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
         fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
                 "'no matching ENGINE'\n", f, l, nid);
 #endif
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     /*
      * Whatever happened, any failed init()s are not failures in this
      * context, so clear our error state.
diff --git a/crypto/engine/tb_asnmth.c b/crypto/engine/tb_asnmth.c
index b0d0d50..28aa8e8 100644
--- a/crypto/engine/tb_asnmth.c
+++ b/crypto/engine/tb_asnmth.c
@@ -233,7 +233,9 @@ const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
     fstr.ameth = NULL;
     fstr.str = str;
     fstr.len = len;
-    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+
+    CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);
+    CRYPTO_THREAD_write_lock(global_engine_lock);
     engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
     /* If found obtain a structural reference to engine */
     if (fstr.e) {
@@ -241,6 +243,6 @@ const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
         engine_ref_debug(fstr.e, 0, 1);
     }
     *pe = fstr.e;
-    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+    CRYPTO_THREAD_unlock(global_engine_lock);
     return fstr.ameth;
 }
diff --git a/crypto/init.c b/crypto/init.c
index b0826ee..c19afd0 100644
--- a/crypto/init.c
+++ b/crypto/init.c
@@ -236,7 +236,6 @@ static void ossl_init_async(void)
 #endif
 
 #ifndef OPENSSL_NO_ENGINE
-static int engine_inited = 0;
 static CRYPTO_ONCE engine_openssl = CRYPTO_ONCE_STATIC_INIT;
 static void ossl_init_engine_openssl(void)
 {
@@ -245,7 +244,6 @@ static void ossl_init_engine_openssl(void)
                     "engine_load_openssl_internal()\n");
 # endif
     engine_load_openssl_internal();
-    engine_inited = 1;
 }
 # if !defined(OPENSSL_NO_HW) && \
     (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV))
@@ -257,7 +255,6 @@ static void ossl_init_engine_cryptodev(void)
                     "engine_load_cryptodev_internal()\n");
 #  endif
     engine_load_cryptodev_internal();
-    engine_inited = 1;
 }
 # endif
 
@@ -270,7 +267,6 @@ static void ossl_init_engine_rdrand(void)
                     "engine_load_rdrand_internal()\n");
 #  endif
     engine_load_rdrand_internal();
-    engine_inited = 1;
 }
 # endif
 static CRYPTO_ONCE engine_dynamic = CRYPTO_ONCE_STATIC_INIT;
@@ -281,7 +277,6 @@ static void ossl_init_engine_dynamic(void)
                     "engine_load_dynamic_internal()\n");
 # endif
     engine_load_dynamic_internal();
-    engine_inited = 1;
 }
 # ifndef OPENSSL_NO_STATIC_ENGINE
 #  if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
@@ -293,7 +288,6 @@ static void ossl_init_engine_padlock(void)
                     "engine_load_padlock_internal()\n");
 #   endif
     engine_load_padlock_internal();
-    engine_inited = 1;
 }
 #  endif
 #  if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
@@ -305,7 +299,6 @@ static void ossl_init_engine_capi(void)
                     "engine_load_capi_internal()\n");
 #   endif
     engine_load_capi_internal();
-    engine_inited = 1;
 }
 #  endif
 static CRYPTO_ONCE engine_dasync = CRYPTO_ONCE_STATIC_INIT;
@@ -316,7 +309,6 @@ static void ossl_init_engine_dasync(void)
                     "engine_load_dasync_internal()\n");
 # endif
     engine_load_dasync_internal();
-    engine_inited = 1;
 }
 #  if !defined(OPENSSL_NO_AFALGENG)
 static OPENSSL_INIT_ONCE engine_afalg = OPENSSL_INIT_ONCE_STATIC_INIT;
@@ -327,7 +319,6 @@ static void ossl_init_engine_afalg(void)
                     "engine_load_afalg_internal()\n");
 #   endif
     engine_load_afalg_internal();
-    engine_inited = 1;
 }
 #  endif
 # endif
@@ -453,16 +444,6 @@ void OPENSSL_cleanup(void)
     }
 #endif
 
-#ifndef OPENSSL_NO_ENGINE
-    if (engine_inited) {
-# ifdef OPENSSL_INIT_DEBUG
-        fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
-                        "ENGINE_cleanup()\n");
-# endif
-        ENGINE_cleanup();
-    }
-#endif
-
     if (load_crypto_strings_inited) {
 #ifdef OPENSSL_INIT_DEBUG
         fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
@@ -474,6 +455,8 @@ void OPENSSL_cleanup(void)
     CRYPTO_THREAD_cleanup_local(&threadstopkey);
 
 #ifdef OPENSSL_INIT_DEBUG
+    fprintf(stderr, "OPENSSL_INIT: OPENSSL_cleanup: "
+                    "ENGINE_cleanup()\n");
     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
                     "CRYPTO_cleanup_all_ex_data()\n");
     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
@@ -482,7 +465,9 @@ void OPENSSL_cleanup(void)
                     "CONF_modules_free()\n");
     fprintf(stderr, "OPENSSL_INIT: OPENSSL_INIT_library_stop: "
                     "RAND_cleanup()\n");
+
 #endif
+    ENGINE_cleanup();
     CRYPTO_cleanup_all_ex_data();
     EVP_cleanup();
     CONF_modules_free();
diff --git a/engines/e_chil.c b/engines/e_chil.c
index 7dc715f..97eacfc 100644
--- a/engines/e_chil.c
+++ b/engines/e_chil.c
@@ -94,6 +94,8 @@
 #  define HWCRHK_LIB_NAME "CHIL engine"
 #  include "e_chil_err.c"
 
+static CRYPTO_RWLOCK *chil_lock;
+
 static int hwcrhk_destroy(ENGINE *e);
 static int hwcrhk_init(ENGINE *e);
 static int hwcrhk_finish(ENGINE *e);
@@ -244,7 +246,7 @@ static const char *engine_hwcrhk_id_alt = "ncipher";
  * into HWCryptoHook_Mutex
  */
 struct HWCryptoHook_MutexValue {
-    int lockid;
+    CRYPTO_RWLOCK *lock;
 };
 
 /*
@@ -355,6 +357,11 @@ static int bind_helper(ENGINE *e)
 #  ifndef OPENSSL_NO_DH
     const DH_METHOD *meth2;
 #  endif
+
+    chil_lock = CRYPTO_THREAD_lock_new();
+    if (chil_lock == NULL)
+        return 0;
+
     if (!ENGINE_set_id(e, engine_hwcrhk_id) ||
         !ENGINE_set_name(e, engine_hwcrhk_name) ||
 #  ifndef OPENSSL_NO_RSA
@@ -398,6 +405,7 @@ static int bind_helper(ENGINE *e)
 
     /* Ensure the hwcrhk error handling is set up */
     ERR_load_HWCRHK_strings();
+
     return 1;
 }
 
@@ -526,6 +534,7 @@ static int hwcrhk_destroy(ENGINE *e)
 {
     free_HWCRHK_LIBNAME();
     ERR_unload_HWCRHK_strings();
+    CRYPTO_THREAD_lock_free(chil_lock);
     return 1;
 }
 
@@ -588,14 +597,10 @@ static int hwcrhk_init(ENGINE *e)
      * does, use them.
      */
     if (disable_mutex_callbacks == 0) {
-        if (CRYPTO_get_dynlock_create_callback() != NULL &&
-            CRYPTO_get_dynlock_lock_callback() != NULL &&
-            CRYPTO_get_dynlock_destroy_callback() != NULL) {
-            hwcrhk_globals.mutex_init = hwcrhk_mutex_init;
-            hwcrhk_globals.mutex_acquire = hwcrhk_mutex_lock;
-            hwcrhk_globals.mutex_release = hwcrhk_mutex_unlock;
-            hwcrhk_globals.mutex_destroy = hwcrhk_mutex_destroy;
-        }
+        hwcrhk_globals.mutex_init = hwcrhk_mutex_init;
+        hwcrhk_globals.mutex_acquire = hwcrhk_mutex_lock;
+        hwcrhk_globals.mutex_release = hwcrhk_mutex_unlock;
+        hwcrhk_globals.mutex_destroy = hwcrhk_mutex_destroy;
     }
 
     /*
@@ -681,32 +686,32 @@ static int hwcrhk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
         {
             BIO *bio = (BIO *)p;
 
-            CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+            CRYPTO_THREAD_write_lock(chil_lock);
             BIO_free(logstream);
             logstream = NULL;
-            if (CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO) > 1)
+            if (BIO_up_ref(bio)
                 logstream = bio;
             else
                 HWCRHKerr(HWCRHK_F_HWCRHK_CTRL, HWCRHK_R_BIO_WAS_FREED);
         }
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(chil_lock);
         break;
     case ENGINE_CTRL_SET_PASSWORD_CALLBACK:
-        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_write_lock(chil_lock);
         password_context.password_callback = (pem_password_cb *)f;
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(chil_lock);
         break;
     case ENGINE_CTRL_SET_USER_INTERFACE:
     case HWCRHK_CMD_SET_USER_INTERFACE:
-        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_write_lock(chil_lock);
         password_context.ui_method = (UI_METHOD *)p;
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(chil_lock);
         break;
     case ENGINE_CTRL_SET_CALLBACK_DATA:
     case HWCRHK_CMD_SET_CALLBACK_DATA:
-        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_write_lock(chil_lock);
         password_context.callback_data = p;
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(chil_lock);
         break;
         /*
          * this enables or disables the "SimpleForkCheck" flag used in the
@@ -714,12 +719,12 @@ static int hwcrhk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
          */
     case ENGINE_CTRL_CHIL_SET_FORKCHECK:
     case HWCRHK_CMD_FORK_CHECK:
-        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_write_lock(chil_lock);
         if (i)
             hwcrhk_globals.flags |= HWCryptoHook_InitFlags_SimpleForkCheck;
         else
             hwcrhk_globals.flags &= ~HWCryptoHook_InitFlags_SimpleForkCheck;
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(chil_lock);
         break;
         /*
          * This will prevent the initialisation function from "installing"
@@ -729,14 +734,14 @@ static int hwcrhk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
          * applications not using multithreading.
          */
     case ENGINE_CTRL_CHIL_NO_LOCKING:
-        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_write_lock(chil_lock);
         disable_mutex_callbacks = 1;
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(chil_lock);
         break;
     case HWCRHK_CMD_THREAD_LOCKING:
-        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_write_lock(chil_lock);
         disable_mutex_callbacks = ((i == 0) ? 0 : 1);
-        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+        CRYPTO_THREAD_unlock(chil_lock);
         break;
 
         /* The command isn't understood by this engine */
@@ -861,14 +866,14 @@ static EVP_PKEY *hwcrhk_load_pubkey(ENGINE *eng, const char *key_id,
             {
                 RSA *rsa = NULL;
 
-                CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
+                CRYPTO_THREAD_write_lock(chil_lock);
                 rsa = res->pkey.rsa;
                 res->pkey.rsa = RSA_new();
                 res->pkey.rsa->n = rsa->n;
                 res->pkey.rsa->e = rsa->e;
                 rsa->n = NULL;
                 rsa->e = NULL;
-                CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
+                CRYPTO_THREAD_unlock(chil_lock);
                 RSA_free(rsa);
             }
             break;
@@ -1136,26 +1141,26 @@ static int hwcrhk_rand_status(void)
 static int hwcrhk_mutex_init(HWCryptoHook_Mutex * mt,
                              HWCryptoHook_CallerContext * cactx)
 {
-    mt->lockid = CRYPTO_get_new_dynlockid();
-    if (mt->lockid == 0)
+    mt->lock = CRYPTO_THREAD_lock_new();
+    if (mt->lock == NULL)
         return 1;               /* failure */
     return 0;                   /* success */
 }
 
 static int hwcrhk_mutex_lock(HWCryptoHook_Mutex * mt)
 {
-    CRYPTO_w_lock(mt->lockid);
+    CRYPTO_THREAD_write_lock(mt->lock);
     return 0;
 }
 
 static void hwcrhk_mutex_unlock(HWCryptoHook_Mutex * mt)
 {
-    CRYPTO_w_unlock(mt->lockid);
+    CRYPTO_THREAD_unlock(mt->lock);
 }
 
 static void hwcrhk_mutex_destroy(HWCryptoHook_Mutex * mt)
 {
-    CRYPTO_destroy_dynlockid(mt->lockid);
+    CRYPTO_THREAD_lock_free(mt->lock);
 }
 
 static int hwcrhk_get_pass(const char *prompt_info,
@@ -1297,13 +1302,11 @@ static void hwcrhk_log_message(void *logstr, const char *message)
 {
     BIO *lstream = NULL;
 
-    CRYPTO_w_lock(CRYPTO_LOCK_BIO);
     if (logstr)
         lstream = *(BIO **)logstr;
     if (lstream) {
         BIO_printf(lstream, "%s\n", message);
     }
-    CRYPTO_w_unlock(CRYPTO_LOCK_BIO);
 }
 
 /*


More information about the openssl-commits mailing list