int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
{
- int ctrl_exists, ref_exists;
+ int ctrl_exists;
+
if (e == NULL) {
ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
- if (!CRYPTO_THREAD_write_lock(global_engine_lock))
- return 0;
- ref_exists = ((e->struct_ref > 0) ? 1 : 0);
- CRYPTO_THREAD_unlock(global_engine_lock);
+
ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
- if (!ref_exists) {
- ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_REFERENCE);
- return 0;
- }
+
/*
* Intercept any "root-level" commands before trying to hand them on to
* ctrl() handlers.
return NULL;
ret->struct_ref = 1;
ENGINE_REF_PRINT(ret, 0, 1);
+ ret->refcnt_lock = CRYPTO_THREAD_lock_new();
+ if (ret->refcnt_lock == NULL) {
+ OPENSSL_free(ret);
+ return NULL;
+ }
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data)) {
+ CRYPTO_THREAD_lock_free(ret->refcnt_lock);
OPENSSL_free(ret);
return NULL;
}
if (e == NULL)
return 1;
- if (not_locked)
- CRYPTO_DOWN_REF(&e->struct_ref, &i, global_engine_lock);
- else
- i = --e->struct_ref;
+ CRYPTO_DOWN_REF(&e->struct_ref, &i, e->refcnt_lock);
ENGINE_REF_PRINT(e, 0, -1);
if (i > 0)
return 1;
e->destroy(e);
engine_remove_dynamic_id(e, not_locked);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);
+ CRYPTO_THREAD_lock_free(e->refcnt_lock);
OPENSSL_free(e);
return 1;
}
{
int conflict = 0;
ENGINE *iterator = NULL;
+ int ref;
if (e == NULL) {
ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CONFLICTING_ENGINE_ID);
return 0;
}
+
+ /*
+ * Having the engine in the list assumes a structural reference.
+ */
+ if (!CRYPTO_UP_REF(&e->struct_ref, &ref, e->refcnt_lock)) {
+ ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
+ return 0;
+ }
+ ENGINE_REF_PRINT(e, 0, 1);
if (engine_list_head == NULL) {
/* We are adding to an empty list. */
- if (engine_list_tail) {
+ if (engine_list_tail != NULL) {
+ CRYPTO_DOWN_REF(&e->struct_ref, &ref, e->refcnt_lock);
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
return 0;
}
} else {
/* We are adding to the tail of an existing list. */
if ((engine_list_tail == NULL) || (engine_list_tail->next != NULL)) {
+ CRYPTO_DOWN_REF(&e->struct_ref, &ref, e->refcnt_lock);
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
return 0;
}
engine_list_tail->next = e;
e->prev = engine_list_tail;
}
- /*
- * Having the engine in the list assumes a structural reference.
- */
- e->struct_ref++;
- ENGINE_REF_PRINT(e, 0, 1);
+
/* However it came to be, e is the last item in the list. */
engine_list_tail = e;
e->next = NULL;
return NULL;
ret = engine_list_head;
if (ret) {
- ret->struct_ref++;
+ int ref;
+
+ if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
+ ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
+ return NULL;
+ }
ENGINE_REF_PRINT(ret, 0, 1);
}
CRYPTO_THREAD_unlock(global_engine_lock);
return NULL;
ret = engine_list_tail;
if (ret) {
- ret->struct_ref++;
+ int ref;
+
+ if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
+ ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
+ return NULL;
+ }
ENGINE_REF_PRINT(ret, 0, 1);
}
CRYPTO_THREAD_unlock(global_engine_lock);
return NULL;
ret = e->next;
if (ret) {
+ int ref;
+
/* Return a valid structural reference to the next ENGINE */
- ret->struct_ref++;
+ if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
+ ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
+ return NULL;
+ }
ENGINE_REF_PRINT(ret, 0, 1);
}
CRYPTO_THREAD_unlock(global_engine_lock);
return NULL;
ret = e->prev;
if (ret) {
+ int ref;
+
/* Return a valid structural reference to the next ENGINE */
- ret->struct_ref++;
+ if (!CRYPTO_UP_REF(&ret->struct_ref, &ref, ret->refcnt_lock)) {
+ ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
+ return NULL;
+ }
ENGINE_REF_PRINT(ret, 0, 1);
}
CRYPTO_THREAD_unlock(global_engine_lock);
iterator = cp;
}
} else {
- iterator->struct_ref++;
+ int ref;
+
+ if (!CRYPTO_UP_REF(&iterator->struct_ref, &ref,
+ iterator->refcnt_lock)) {
+ CRYPTO_THREAD_unlock(global_engine_lock);
+ ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
+ return NULL;
+ }
ENGINE_REF_PRINT(iterator, 0, 1);
}
}
* This prints the engine's pointer address, "struct" or "funct" to
* indicate the reference type, the before and after reference count, and
* the file:line-number pair. The "ENGINE_REF_PRINT" statements must come
- * *after* the change.
+ * *after* the change. Since this is for tracing only we do not concern
+ * ourselves with using atomic primitives for reading the struct_ref
*/
# define ENGINE_REF_PRINT(e, isfunct, diff) \
OSSL_TRACE6(ENGINE_REF_COUNT, \
int flags;
/* reference count on the structure itself */
CRYPTO_REF_COUNT struct_ref;
+ CRYPTO_RWLOCK *refcnt_lock;
/*
* reference count on usability of the engine type. NB: This controls the
* loading and initialisation of any functionality required by this