2016-05-17 18:52:22 +00:00
|
|
|
/*
|
2018-03-20 13:00:17 +00:00
|
|
|
* Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2001-09-25 20:00:51 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2001-09-25 20:00:51 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2001-09-25 20:00:51 +00:00
|
|
|
#include <openssl/evp.h>
|
2004-04-19 17:46:04 +00:00
|
|
|
#include <openssl/lhash.h>
|
2001-09-25 20:00:51 +00:00
|
|
|
#include "eng_int.h"
|
|
|
|
|
|
|
|
/* The type of the items in the table */
|
2016-01-11 14:11:13 +00:00
|
|
|
struct st_engine_pile {
|
2015-01-22 03:40:55 +00:00
|
|
|
/* The 'nid' of this algorithm/mode */
|
|
|
|
int nid;
|
|
|
|
/* ENGINEs that implement this algorithm/mode. */
|
|
|
|
STACK_OF(ENGINE) *sk;
|
|
|
|
/* The default ENGINE to perform this algorithm/mode. */
|
|
|
|
ENGINE *funct;
|
|
|
|
/*
|
|
|
|
* Zero if 'sk' is newer than the cached 'funct', non-zero otherwise
|
|
|
|
*/
|
|
|
|
int uptodate;
|
2016-01-11 14:11:13 +00:00
|
|
|
};
|
2008-05-26 11:24:29 +00:00
|
|
|
|
2004-06-19 03:58:42 +00:00
|
|
|
/* The type exposed in eng_int.h */
|
2015-01-22 03:40:55 +00:00
|
|
|
struct st_engine_table {
|
|
|
|
LHASH_OF(ENGINE_PILE) piles;
|
|
|
|
}; /* ENGINE_TABLE */
|
2001-09-25 20:00:51 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
typedef struct st_engine_pile_doall {
|
|
|
|
engine_table_doall_cb *cb;
|
|
|
|
void *arg;
|
|
|
|
} ENGINE_PILE_DOALL;
|
2007-11-21 17:25:58 +00:00
|
|
|
|
2004-06-19 03:58:42 +00:00
|
|
|
/* Global flags (ENGINE_TABLE_FLAG_***). */
|
2001-09-25 20:00:51 +00:00
|
|
|
static unsigned int table_flags = 0;
|
|
|
|
|
|
|
|
/* API function manipulating 'table_flags' */
|
|
|
|
unsigned int ENGINE_get_table_flags(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return table_flags;
|
|
|
|
}
|
2008-05-26 11:24:29 +00:00
|
|
|
|
2001-09-25 20:00:51 +00:00
|
|
|
void ENGINE_set_table_flags(unsigned int flags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
table_flags = flags;
|
|
|
|
}
|
2001-09-25 20:00:51 +00:00
|
|
|
|
|
|
|
/* Internal functions for the "piles" hash table */
|
|
|
|
static unsigned long engine_pile_hash(const ENGINE_PILE *c)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return c->nid;
|
|
|
|
}
|
2008-05-26 11:24:29 +00:00
|
|
|
|
2001-09-25 20:00:51 +00:00
|
|
|
static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return a->nid - b->nid;
|
|
|
|
}
|
|
|
|
|
2001-09-25 20:00:51 +00:00
|
|
|
static int int_table_check(ENGINE_TABLE **t, int create)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
LHASH_OF(ENGINE_PILE) *lh;
|
2008-05-26 11:24:29 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (*t)
|
|
|
|
return 1;
|
|
|
|
if (!create)
|
|
|
|
return 0;
|
2015-12-24 15:51:23 +00:00
|
|
|
if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
|
|
|
*t = (ENGINE_TABLE *)lh;
|
|
|
|
return 1;
|
|
|
|
}
|
2001-09-25 20:00:51 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Privately exposed (via eng_int.h) functions for adding and/or removing
|
|
|
|
* ENGINEs from the implementation table
|
|
|
|
*/
|
2001-10-08 14:44:38 +00:00
|
|
|
int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
|
2015-01-22 03:40:55 +00:00
|
|
|
ENGINE *e, const int *nids, int num_nids,
|
|
|
|
int setdefault)
|
|
|
|
{
|
|
|
|
int ret = 0, added = 0;
|
|
|
|
ENGINE_PILE tmplate, *fnd;
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_write_lock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!(*table))
|
|
|
|
added = 1;
|
|
|
|
if (!int_table_check(table, 1))
|
|
|
|
goto end;
|
|
|
|
if (added)
|
|
|
|
/* The cleanup callback needs to be added */
|
|
|
|
engine_cleanup_add_first(cleanup);
|
|
|
|
while (num_nids--) {
|
|
|
|
tmplate.nid = *nids;
|
|
|
|
fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
|
|
|
|
if (!fnd) {
|
2015-05-02 03:10:31 +00:00
|
|
|
fnd = OPENSSL_malloc(sizeof(*fnd));
|
2015-10-30 11:12:26 +00:00
|
|
|
if (fnd == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
|
|
|
fnd->uptodate = 1;
|
|
|
|
fnd->nid = *nids;
|
|
|
|
fnd->sk = sk_ENGINE_new_null();
|
|
|
|
if (!fnd->sk) {
|
|
|
|
OPENSSL_free(fnd);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
fnd->funct = NULL;
|
|
|
|
(void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
|
2018-03-02 08:27:39 +00:00
|
|
|
if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) {
|
|
|
|
sk_ENGINE_free(fnd->sk);
|
|
|
|
OPENSSL_free(fnd);
|
|
|
|
goto end;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-02-05 20:23:54 +00:00
|
|
|
/* A registration shouldn't add duplicate entries */
|
2015-01-22 03:40:55 +00:00
|
|
|
(void)sk_ENGINE_delete_ptr(fnd->sk, e);
|
|
|
|
/*
|
|
|
|
* if 'setdefault', this ENGINE goes to the head of the list
|
|
|
|
*/
|
|
|
|
if (!sk_ENGINE_push(fnd->sk, e))
|
|
|
|
goto end;
|
|
|
|
/* "touch" this ENGINE_PILE */
|
|
|
|
fnd->uptodate = 0;
|
|
|
|
if (setdefault) {
|
|
|
|
if (!engine_unlocked_init(e)) {
|
|
|
|
ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER,
|
|
|
|
ENGINE_R_INIT_FAILED);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (fnd->funct)
|
|
|
|
engine_unlocked_finish(fnd->funct, 0);
|
|
|
|
fnd->funct = e;
|
|
|
|
fnd->uptodate = 1;
|
|
|
|
}
|
|
|
|
nids++;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
end:
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_unlock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-24 16:20:54 +00:00
|
|
|
static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
/* Iterate the 'c->sk' stack removing any occurrence of 'e' */
|
|
|
|
while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) {
|
|
|
|
(void)sk_ENGINE_delete(pile->sk, n);
|
|
|
|
pile->uptodate = 0;
|
|
|
|
}
|
|
|
|
if (pile->funct == e) {
|
|
|
|
engine_unlocked_finish(e, 0);
|
|
|
|
pile->funct = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 16:20:54 +00:00
|
|
|
IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE);
|
2008-05-26 11:24:29 +00:00
|
|
|
|
2001-09-25 20:00:51 +00:00
|
|
|
void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_write_lock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (int_table_check(table, 0))
|
2015-12-24 16:20:54 +00:00
|
|
|
lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e);
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_unlock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-09-25 20:00:51 +00:00
|
|
|
|
2008-05-26 11:24:29 +00:00
|
|
|
static void int_cleanup_cb_doall(ENGINE_PILE *p)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-05-01 14:15:18 +00:00
|
|
|
if (!p)
|
|
|
|
return;
|
2015-01-22 03:40:55 +00:00
|
|
|
sk_ENGINE_free(p->sk);
|
|
|
|
if (p->funct)
|
|
|
|
engine_unlocked_finish(p->funct, 0);
|
|
|
|
OPENSSL_free(p);
|
|
|
|
}
|
|
|
|
|
2001-09-25 20:00:51 +00:00
|
|
|
void engine_table_cleanup(ENGINE_TABLE **table)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_write_lock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (*table) {
|
2015-12-24 16:07:21 +00:00
|
|
|
lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall);
|
2015-01-22 03:40:55 +00:00
|
|
|
lh_ENGINE_PILE_free(&(*table)->piles);
|
|
|
|
*table = NULL;
|
|
|
|
}
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_unlock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-09-25 20:00:51 +00:00
|
|
|
|
2004-06-19 03:58:42 +00:00
|
|
|
/* return a functional reference for a given 'nid' */
|
2001-09-25 20:00:51 +00:00
|
|
|
#ifndef ENGINE_TABLE_DEBUG
|
|
|
|
ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)
|
|
|
|
#else
|
2015-01-22 03:40:55 +00:00
|
|
|
ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
|
|
|
|
int l)
|
2001-09-25 20:00:51 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ENGINE *ret = NULL;
|
|
|
|
ENGINE_PILE tmplate, *fnd = NULL;
|
|
|
|
int initres, loop = 0;
|
2001-09-25 20:00:51 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!(*table)) {
|
2001-09-25 20:00:51 +00:00
|
|
|
#ifdef ENGINE_TABLE_DEBUG
|
2015-01-22 03:40:55 +00:00
|
|
|
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "
|
|
|
|
"registered!\n", f, l, nid);
|
2001-09-25 20:00:51 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ERR_set_mark();
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_write_lock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Check again inside the lock otherwise we could race against cleanup
|
|
|
|
* operations. But don't worry about a fprintf(stderr).
|
|
|
|
*/
|
|
|
|
if (!int_table_check(table, 0))
|
|
|
|
goto end;
|
|
|
|
tmplate.nid = nid;
|
|
|
|
fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
|
|
|
|
if (!fnd)
|
|
|
|
goto end;
|
|
|
|
if (fnd->funct && engine_unlocked_init(fnd->funct)) {
|
2001-09-25 20:00:51 +00:00
|
|
|
#ifdef ENGINE_TABLE_DEBUG
|
2015-01-22 03:40:55 +00:00
|
|
|
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
|
|
|
|
"ENGINE '%s' cached\n", f, l, nid, fnd->funct->id);
|
2001-09-25 20:00:51 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = fnd->funct;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (fnd->uptodate) {
|
|
|
|
ret = fnd->funct;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
trynext:
|
|
|
|
ret = sk_ENGINE_value(fnd->sk, loop++);
|
|
|
|
if (!ret) {
|
2001-09-25 20:00:51 +00:00
|
|
|
#ifdef ENGINE_TABLE_DEBUG
|
2015-01-22 03:40:55 +00:00
|
|
|
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "
|
|
|
|
"registered implementations would initialise\n", f, l, nid);
|
2001-09-25 20:00:51 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
/* Try to initialise the ENGINE? */
|
|
|
|
if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
|
|
|
|
initres = engine_unlocked_init(ret);
|
|
|
|
else
|
|
|
|
initres = 0;
|
|
|
|
if (initres) {
|
|
|
|
/* Update 'funct' */
|
|
|
|
if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
|
|
|
|
/* If there was a previous default we release it. */
|
|
|
|
if (fnd->funct)
|
|
|
|
engine_unlocked_finish(fnd->funct, 0);
|
|
|
|
fnd->funct = ret;
|
2001-09-25 20:00:51 +00:00
|
|
|
#ifdef ENGINE_TABLE_DEBUG
|
2015-01-22 03:40:55 +00:00
|
|
|
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "
|
|
|
|
"setting default to '%s'\n", f, l, nid, ret->id);
|
2001-09-25 20:00:51 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-09-25 20:00:51 +00:00
|
|
|
#ifdef ENGINE_TABLE_DEBUG
|
2015-01-22 03:40:55 +00:00
|
|
|
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
|
|
|
|
"newly initialised '%s'\n", f, l, nid, ret->id);
|
2001-09-25 20:00:51 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
goto trynext;
|
|
|
|
end:
|
|
|
|
/*
|
|
|
|
* If it failed, it is unlikely to succeed again until some future
|
|
|
|
* registrations have taken place. In all cases, we cache.
|
|
|
|
*/
|
|
|
|
if (fnd)
|
|
|
|
fnd->uptodate = 1;
|
2001-09-25 20:00:51 +00:00
|
|
|
#ifdef ENGINE_TABLE_DEBUG
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ret)
|
|
|
|
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
|
|
|
|
"ENGINE '%s'\n", f, l, nid, ret->id);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
|
|
|
|
"'no matching ENGINE'\n", f, l, nid);
|
2001-09-25 20:00:51 +00:00
|
|
|
#endif
|
2016-03-08 16:44:34 +00:00
|
|
|
CRYPTO_THREAD_unlock(global_engine_lock);
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Whatever happened, any failed init()s are not failures in this
|
|
|
|
* context, so clear our error state.
|
|
|
|
*/
|
|
|
|
ERR_pop_to_mark();
|
|
|
|
return ret;
|
|
|
|
}
|
2007-11-21 17:25:58 +00:00
|
|
|
|
|
|
|
/* Table enumeration */
|
|
|
|
|
2015-12-24 16:20:54 +00:00
|
|
|
static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
|
|
|
|
}
|
|
|
|
|
2015-12-24 16:20:54 +00:00
|
|
|
IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL);
|
2007-11-21 17:25:58 +00:00
|
|
|
|
|
|
|
void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
|
2015-01-22 03:40:55 +00:00
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
ENGINE_PILE_DOALL dall;
|
|
|
|
dall.cb = cb;
|
|
|
|
dall.arg = arg;
|
|
|
|
if (table)
|
2015-12-24 16:20:54 +00:00
|
|
|
lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|