Make the 'dynamic' ENGINE bundle up the loading application/library's
locking callbacks to pass to the loaded library (in addition to the
existing mem, ex_data, and err callbacks). Also change the default
implementation of the "bind_engine" function to apply those callbacks, ie.
the IMPLEMENT_DYNAMIC_BIND_FN macro.
diff --git a/crypto/engine/engine.h b/crypto/engine/engine.h
index 149ae9a..7c0b2f6 100644
--- a/crypto/engine/engine.h
+++ b/crypto/engine/engine.h
@@ -504,18 +504,37 @@
  * and memory management function pointers to the loaded library. These should
  * be used/set in the loaded library code so that the loading application's
  * 'state' will be used/changed in all operations. */
-typedef void *(*dynamic_MEM_malloc_cb)(size_t);
-typedef void *(*dynamic_MEM_realloc_cb)(void *, size_t);
-typedef void (*dynamic_MEM_free_cb)(void *);
+typedef void *(*dyn_MEM_malloc_cb)(size_t);
+typedef void *(*dyn_MEM_realloc_cb)(void *, size_t);
+typedef void (*dyn_MEM_free_cb)(void *);
 typedef struct st_dynamic_MEM_fns {
-	dynamic_MEM_malloc_cb			malloc_cb;
-	dynamic_MEM_realloc_cb			realloc_cb;
-	dynamic_MEM_free_cb			free_cb;
+	dyn_MEM_malloc_cb			malloc_cb;
+	dyn_MEM_realloc_cb			realloc_cb;
+	dyn_MEM_free_cb				free_cb;
 	} dynamic_MEM_fns;
+/* FIXME: Perhaps the memory and locking code (crypto.h) should declare and use
+ * these types so we (and any other dependant code) can simplify a bit?? */
+typedef void (*dyn_lock_locking_cb)(int,int,const char *,int);
+typedef int (*dyn_lock_add_lock_cb)(int*,int,int,const char *,int);
+typedef struct CRYPTO_dynlock_value *(*dyn_dynlock_create_cb)(
+						const char *,int);
+typedef void (*dyn_dynlock_lock_cb)(int,struct CRYPTO_dynlock_value *,
+						const char *,int);
+typedef void (*dyn_dynlock_destroy_cb)(struct CRYPTO_dynlock_value *,
+						const char *,int);
+typedef struct st_dynamic_LOCK_fns {
+	dyn_lock_locking_cb			lock_locking_cb;
+	dyn_lock_add_lock_cb			lock_add_lock_cb;
+	dyn_dynlock_create_cb			dynlock_create_cb;
+	dyn_dynlock_lock_cb			dynlock_lock_cb;
+	dyn_dynlock_destroy_cb			dynlock_destroy_cb;
+	} dynamic_LOCK_fns;
+/* The top-level structure */
 typedef struct st_dynamic_fns {
 	const ERR_FNS				*err_fns;
 	const CRYPTO_EX_DATA_IMPL		*ex_data_fns;
 	dynamic_MEM_fns				mem_fns;
+	dynamic_LOCK_fns			lock_fns;
 	} dynamic_fns;
 
 /* The version checking function should be of this prototype. NB: The
@@ -555,6 +574,11 @@
 		if(!CRYPTO_set_mem_functions(fns->mem_fns.malloc_cb, \
 			fns->mem_fns.realloc_cb, fns->mem_fns.free_cb)) \
 			return 0; \
+		CRYPTO_set_locking_callback(fns->lock_fns.lock_locking_cb); \
+		CRYPTO_set_add_lock_callback(fns->lock_fns.lock_add_lock_cb); \
+		CRYPTO_set_dynlock_create_callback(fns->lock_fns.dynlock_create_cb); \
+		CRYPTO_set_dynlock_lock_callback(fns->lock_fns.dynlock_lock_cb); \
+		CRYPTO_set_dynlock_destroy_callback(fns->lock_fns.dynlock_destroy_cb); \
 		if(!CRYPTO_set_ex_data_implementation(fns->ex_data_fns)) \
 			return 0; \
 		if(!ERR_set_implementation(fns->err_fns)) return 0; \
diff --git a/crypto/engine/engine_dyn.c b/crypto/engine/engine_dyn.c
index d4f00f7b..1e5eded 100644
--- a/crypto/engine/engine_dyn.c
+++ b/crypto/engine/engine_dyn.c
@@ -375,13 +375,21 @@
 	/* First binary copy the ENGINE structure so that we can roll back if
 	 * the hand-over fails */
 	memcpy(&cpy, e, sizeof(ENGINE));
-	/* Provide the ERR, "ex_data", and memory callbacks so the loaded
-	 * library uses our own state (and locking) rather than its own. */
+	/* Provide the ERR, "ex_data", memory, and locking callbacks so the
+	 * loaded library uses our state rather than its own. FIXME: As noted in
+	 * engine.h, much of this would be simplified if each area of code
+	 * provided its own "summary" structure of all related callbacks. It
+	 * would also increase opaqueness. */
 	fns.err_fns = ERR_get_implementation();
 	fns.ex_data_fns = CRYPTO_get_ex_data_implementation();
 	CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb,
 				&fns.mem_fns.realloc_cb,
 				&fns.mem_fns.free_cb);
+	fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback();
+	fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback();
+	fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
+	fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
+	fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
 	/* Try to bind the ENGINE onto our own ENGINE structure */
 	if(!ctx->bind_engine(e, ctx->engine_id, &fns))
 		{