Adapt ENGINE_TABLE_DEBUG to the new generic trace API

Co-authored-by: Dr. Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/8198)
This commit is contained in:
Richard Levitte 2018-12-13 01:42:07 +01:00
parent f4db05df0e
commit f272be676b
4 changed files with 32 additions and 47 deletions

View File

@ -11,6 +11,7 @@
#ifndef HEADER_ENGINE_INT_H #ifndef HEADER_ENGINE_INT_H
# define HEADER_ENGINE_INT_H # define HEADER_ENGINE_INT_H
# include <openssl/trace.h>
# include "internal/cryptlib.h" # include "internal/cryptlib.h"
# include "internal/engine.h" # include "internal/engine.h"
# include "internal/thread_once.h" # include "internal/thread_once.h"
@ -58,14 +59,6 @@ void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb);
/* We need stacks of ENGINEs for use in eng_table.c */ /* We need stacks of ENGINEs for use in eng_table.c */
DEFINE_STACK_OF(ENGINE) DEFINE_STACK_OF(ENGINE)
/*
* If this symbol is defined then engine_table_select(), the function that is
* used by RSA, DSA (etc) code to select registered ENGINEs, cache defaults
* and functional references (etc), will display debugging summaries to
* stderr.
*/
/* #define ENGINE_TABLE_DEBUG */
/* /*
* This represents an implementation table. Dependent code should instantiate * This represents an implementation table. Dependent code should instantiate
* it as a (ENGINE_TABLE *) pointer value set initially to NULL. * it as a (ENGINE_TABLE *) pointer value set initially to NULL.
@ -76,13 +69,10 @@ int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
int setdefault); int setdefault);
void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e); void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e);
void engine_table_cleanup(ENGINE_TABLE **table); void engine_table_cleanup(ENGINE_TABLE **table);
# ifndef ENGINE_TABLE_DEBUG ENGINE *engine_table_select_int(ENGINE_TABLE **table, int nid, const char *f,
ENGINE *engine_table_select(ENGINE_TABLE **table, int nid);
# else
ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
int l); int l);
# define engine_table_select(t,n) engine_table_select_tmp(t,n,OPENSSL_FILE,OPENSSL_LINE) # define engine_table_select(t,n) \
# endif engine_table_select_int(t,n,OPENSSL_FILE,OPENSSL_LINE)
typedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk, typedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk,
ENGINE *def, void *arg); ENGINE *def, void *arg);
void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb, void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,

View File

@ -10,6 +10,7 @@
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/lhash.h> #include <openssl/lhash.h>
#include <openssl/trace.h>
#include "eng_int.h" #include "eng_int.h"
/* The type of the items in the table */ /* The type of the items in the table */
@ -189,29 +190,24 @@ void engine_table_cleanup(ENGINE_TABLE **table)
} }
/* return a functional reference for a given 'nid' */ /* return a functional reference for a given 'nid' */
#ifndef ENGINE_TABLE_DEBUG ENGINE *engine_table_select_int(ENGINE_TABLE **table, int nid, const char *f,
ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)
#else
ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
int l) int l)
#endif
{ {
ENGINE *ret = NULL; ENGINE *ret = NULL;
ENGINE_PILE tmplate, *fnd = NULL; ENGINE_PILE tmplate, *fnd = NULL;
int initres, loop = 0; int initres, loop = 0;
if (!(*table)) { if (!(*table)) {
#ifdef ENGINE_TABLE_DEBUG OSSL_TRACE3(ENGINE_TABLE,
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing " "%s:%d, nid=%d, nothing registered!\n",
"registered!\n", f, l, nid); f, l, nid);
#endif
return NULL; return NULL;
} }
ERR_set_mark(); ERR_set_mark();
CRYPTO_THREAD_write_lock(global_engine_lock); CRYPTO_THREAD_write_lock(global_engine_lock);
/* /*
* Check again inside the lock otherwise we could race against cleanup * Check again inside the lock otherwise we could race against cleanup
* operations. But don't worry about a fprintf(stderr). * operations. But don't worry about a debug printout
*/ */
if (!int_table_check(table, 0)) if (!int_table_check(table, 0))
goto end; goto end;
@ -220,10 +216,9 @@ ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
if (!fnd) if (!fnd)
goto end; goto end;
if (fnd->funct && engine_unlocked_init(fnd->funct)) { if (fnd->funct && engine_unlocked_init(fnd->funct)) {
#ifdef ENGINE_TABLE_DEBUG OSSL_TRACE4(ENGINE_TABLE,
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using " "%s:%d, nid=%d, using ENGINE '%s' cached\n",
"ENGINE '%s' cached\n", f, l, nid, fnd->funct->id); f, l, nid, fnd->funct->id);
#endif
ret = fnd->funct; ret = fnd->funct;
goto end; goto end;
} }
@ -234,10 +229,10 @@ ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
trynext: trynext:
ret = sk_ENGINE_value(fnd->sk, loop++); ret = sk_ENGINE_value(fnd->sk, loop++);
if (!ret) { if (!ret) {
#ifdef ENGINE_TABLE_DEBUG OSSL_TRACE3(ENGINE_TABLE,
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no " "%s:%d, nid=%d, "
"registered implementations would initialise\n", f, l, nid); "no registered implementations would initialise\n",
#endif f, l, nid);
goto end; goto end;
} }
/* Try to initialise the ENGINE? */ /* Try to initialise the ENGINE? */
@ -252,15 +247,13 @@ ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
if (fnd->funct) if (fnd->funct)
engine_unlocked_finish(fnd->funct, 0); engine_unlocked_finish(fnd->funct, 0);
fnd->funct = ret; fnd->funct = ret;
#ifdef ENGINE_TABLE_DEBUG OSSL_TRACE4(ENGINE_TABLE,
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, " "%s:%d, nid=%d, setting default to '%s'\n",
"setting default to '%s'\n", f, l, nid, ret->id); f, l, nid, ret->id);
#endif
} }
#ifdef ENGINE_TABLE_DEBUG OSSL_TRACE4(ENGINE_TABLE,
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using " "%s:%d, nid=%d, using newly initialised '%s'\n",
"newly initialised '%s'\n", f, l, nid, ret->id); f, l, nid, ret->id);
#endif
goto end; goto end;
} }
goto trynext; goto trynext;
@ -271,14 +264,14 @@ ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
*/ */
if (fnd) if (fnd)
fnd->uptodate = 1; fnd->uptodate = 1;
#ifdef ENGINE_TABLE_DEBUG
if (ret) if (ret)
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching " OSSL_TRACE4(ENGINE_TABLE,
"ENGINE '%s'\n", f, l, nid, ret->id); "%s:%d, nid=%d, caching ENGINE '%s'\n",
f, l, nid, ret->id);
else else
fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching " OSSL_TRACE3(ENGINE_TABLE,
"'no matching ENGINE'\n", f, l, nid); "%s:%d, nid=%d, caching 'no matching ENGINE'\n",
#endif f, l, nid);
CRYPTO_THREAD_unlock(global_engine_lock); CRYPTO_THREAD_unlock(global_engine_lock);
/* /*
* Whatever happened, any failed init()s are not failures in this * Whatever happened, any failed init()s are not failures in this

View File

@ -124,6 +124,7 @@ static const struct trace_category_st trace_categories[] = {
TRACE_CATEGORY_(TLS), TRACE_CATEGORY_(TLS),
TRACE_CATEGORY_(TLS_CIPHER), TRACE_CATEGORY_(TLS_CIPHER),
TRACE_CATEGORY_(ENGINE_CONF), TRACE_CATEGORY_(ENGINE_CONF),
TRACE_CATEGORY_(ENGINE_TABLE),
}; };
const char *OSSL_trace_get_category_name(int num) const char *OSSL_trace_get_category_name(int num)

View File

@ -37,7 +37,8 @@ extern "C" {
# define OSSL_TRACE_CATEGORY_TLS 2 # define OSSL_TRACE_CATEGORY_TLS 2
# define OSSL_TRACE_CATEGORY_TLS_CIPHER 3 # define OSSL_TRACE_CATEGORY_TLS_CIPHER 3
# define OSSL_TRACE_CATEGORY_ENGINE_CONF 4 # define OSSL_TRACE_CATEGORY_ENGINE_CONF 4
# define OSSL_TRACE_CATEGORY_NUM 5 # define OSSL_TRACE_CATEGORY_ENGINE_TABLE 5
# define OSSL_TRACE_CATEGORY_NUM 6
/* Returns the trace category number for the given |name| */ /* Returns the trace category number for the given |name| */
int OSSL_trace_get_category_num(const char *name); int OSSL_trace_get_category_num(const char *name);