#include <string.h>
#include <unistd.h>
#include <openssl/opensslconf.h>
#include <openssl/err.h>
#include "cryptlib.h"
#include "eng_int.h"
static ENGINE *engine_list_head = NULL;
static ENGINE *engine_list_tail = NULL;
static void
engine_list_cleanup(void)
{
ENGINE *iterator = engine_list_head;
while (iterator != NULL && ENGINE_remove(iterator))
iterator = engine_list_head;
}
static int
engine_list_add(ENGINE *e)
{
int conflict = 0;
ENGINE *iterator = NULL;
if (e == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
iterator = engine_list_head;
while (iterator && !conflict) {
conflict = (strcmp(iterator->id, e->id) == 0);
iterator = iterator->next;
}
if (conflict) {
ENGINEerror(ENGINE_R_CONFLICTING_ENGINE_ID);
return 0;
}
if (engine_list_head == NULL) {
if (engine_list_tail) {
ENGINEerror(ENGINE_R_INTERNAL_LIST_ERROR);
return 0;
}
engine_list_head = e;
e->prev = NULL;
engine_cleanup_add_last(engine_list_cleanup);
} else {
if ((engine_list_tail == NULL) ||
(engine_list_tail->next != NULL)) {
ENGINEerror(ENGINE_R_INTERNAL_LIST_ERROR);
return 0;
}
engine_list_tail->next = e;
e->prev = engine_list_tail;
}
e->struct_ref++;
engine_ref_debug(e, 0, 1)
engine_list_tail = e;
e->next = NULL;
return 1;
}
static int
engine_list_remove(ENGINE *e)
{
ENGINE *iterator;
if (e == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
iterator = engine_list_head;
while (iterator && (iterator != e))
iterator = iterator->next;
if (iterator == NULL) {
ENGINEerror(ENGINE_R_ENGINE_IS_NOT_IN_LIST);
return 0;
}
if (e->next)
e->next->prev = e->prev;
if (e->prev)
e->prev->next = e->next;
if (engine_list_head == e)
engine_list_head = e->next;
if (engine_list_tail == e)
engine_list_tail = e->prev;
engine_free_util(e, 0);
return 1;
}
ENGINE *
ENGINE_get_first(void)
{
ENGINE *ret;
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
ret = engine_list_head;
if (ret) {
ret->struct_ref++;
engine_ref_debug(ret, 0, 1)
}
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
return ret;
}
ENGINE *
ENGINE_get_last(void)
{
ENGINE *ret;
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
ret = engine_list_tail;
if (ret) {
ret->struct_ref++;
engine_ref_debug(ret, 0, 1)
}
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
return ret;
}
ENGINE *
ENGINE_get_next(ENGINE *e)
{
ENGINE *ret = NULL;
if (e == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
ret = e->next;
if (ret) {
ret->struct_ref++;
engine_ref_debug(ret, 0, 1)
}
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
ENGINE_free(e);
return ret;
}
ENGINE *
ENGINE_get_prev(ENGINE *e)
{
ENGINE *ret = NULL;
if (e == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
ret = e->prev;
if (ret) {
ret->struct_ref++;
engine_ref_debug(ret, 0, 1)
}
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
ENGINE_free(e);
return ret;
}
int
ENGINE_add(ENGINE *e)
{
int to_return = 1;
if (e == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if ((e->id == NULL) || (e->name == NULL)) {
ENGINEerror(ENGINE_R_ID_OR_NAME_MISSING);
}
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
if (!engine_list_add(e)) {
ENGINEerror(ENGINE_R_INTERNAL_LIST_ERROR);
to_return = 0;
}
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
return to_return;
}
int
ENGINE_remove(ENGINE *e)
{
int to_return = 1;
if (e == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
if (!engine_list_remove(e)) {
ENGINEerror(ENGINE_R_INTERNAL_LIST_ERROR);
to_return = 0;
}
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
return to_return;
}
static void
engine_cpy(ENGINE *dest, const ENGINE *src)
{
dest->id = src->id;
dest->name = src->name;
#ifndef OPENSSL_NO_RSA
dest->rsa_meth = src->rsa_meth;
#endif
#ifndef OPENSSL_NO_DSA
dest->dsa_meth = src->dsa_meth;
#endif
#ifndef OPENSSL_NO_DH
dest->dh_meth = src->dh_meth;
#endif
#ifndef OPENSSL_NO_ECDH
dest->ecdh_meth = src->ecdh_meth;
#endif
#ifndef OPENSSL_NO_ECDSA
dest->ecdsa_meth = src->ecdsa_meth;
#endif
#ifndef OPENSSL_NO_EC
dest->ec_meth = src->ec_meth;
#endif
dest->rand_meth = src->rand_meth;
dest->store_meth = src->store_meth;
dest->ciphers = src->ciphers;
dest->digests = src->digests;
dest->pkey_meths = src->pkey_meths;
dest->destroy = src->destroy;
dest->init = src->init;
dest->finish = src->finish;
dest->ctrl = src->ctrl;
dest->load_privkey = src->load_privkey;
dest->load_pubkey = src->load_pubkey;
dest->cmd_defns = src->cmd_defns;
dest->flags = src->flags;
}
ENGINE *
ENGINE_by_id(const char *id)
{
ENGINE *iterator;
if (id == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
iterator = engine_list_head;
while (iterator && (strcmp(id, iterator->id) != 0))
iterator = iterator->next;
if (iterator) {
if (iterator->flags & ENGINE_FLAGS_BY_ID_COPY) {
ENGINE *cp = ENGINE_new();
if (!cp)
iterator = NULL;
else {
engine_cpy(cp, iterator);
iterator = cp;
}
} else {
iterator->struct_ref++;
engine_ref_debug(iterator, 0, 1)
}
}
CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
if (iterator == NULL) {
ENGINEerror(ENGINE_R_NO_SUCH_ENGINE);
ERR_asprintf_error_data("id=%s", id);
}
return iterator;
}
int
ENGINE_up_ref(ENGINE *e)
{
int refs;
if (e == NULL) {
ENGINEerror(ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
refs = CRYPTO_add(&e->struct_ref, 1, CRYPTO_LOCK_ENGINE);
return refs > 1 ? 1 : 0;
}