Skip to content

Commit

Permalink
LEGACY PROV: Reimplement the ERR building blocks in upcall terms
Browse files Browse the repository at this point in the history
This involves the following functions:

ERR_new(), ERR_set_debug(), ERR_set_error(), ERR_vset_error(),
ERR_set_mark(), ERR_clear_last_mark(), ERR_pop_to_mark(void)

Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from openssl/openssl#17474)
  • Loading branch information
levitte committed Jan 21, 2022
1 parent fbe8870 commit 8c2e588
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions providers/legacyprov.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <openssl/core.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/params.h>
#include "prov/provider_ctx.h"
#include "prov/implementations.h"
Expand All @@ -33,6 +34,22 @@ OSSL_provider_init_fn ossl_legacy_provider_init;
# define OSSL_provider_init ossl_legacy_provider_init
#endif

#ifndef STATIC_LEGACY
/*
* Should these function pointers be stored in the provider side provctx?
* Could they ever be different from one init to the next? We assume not for
* now.
*/

/* Functions provided by the core */
static OSSL_FUNC_core_new_error_fn *c_new_error;
static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
static OSSL_FUNC_core_vset_error_fn *c_vset_error;
static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
#endif

/* Parameters we provide to the core */
static const OSSL_PARAM legacy_param_types[] = {
OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
Expand Down Expand Up @@ -185,6 +202,41 @@ int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
void **provctx)
{
OSSL_LIB_CTX *libctx = NULL;
#ifndef STATIC_LEGACY
const OSSL_DISPATCH *tmp;
#endif

#ifndef STATIC_LEGACY
for (tmp = in; tmp->function_id != 0; tmp++) {
/*
* We do not support the scenario of an application linked against
* multiple versions of libcrypto (e.g. one static and one dynamic),
* but sharing a single legacy.so. We do a simple sanity check here.
*/
#define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;
switch (tmp->function_id) {
case OSSL_FUNC_CORE_NEW_ERROR:
set_func(c_new_error, OSSL_FUNC_core_new_error(tmp));
break;
case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(tmp));
break;
case OSSL_FUNC_CORE_VSET_ERROR:
set_func(c_vset_error, OSSL_FUNC_core_vset_error(tmp));
break;
case OSSL_FUNC_CORE_SET_ERROR_MARK:
set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(tmp));
break;
case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
set_func(c_clear_last_error_mark,
OSSL_FUNC_core_clear_last_error_mark(tmp));
break;
case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(tmp));
break;
}
}
#endif

if ((*provctx = ossl_prov_ctx_new()) == NULL
|| (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) {
Expand All @@ -200,3 +252,53 @@ int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,

return 1;
}

#ifndef STATIC_LEGACY
/*
* Provider specific implementation of libcrypto functions in terms of
* upcalls.
*/

/*
* For ERR functions, we pass a NULL context. This is valid to do as long
* as only error codes that the calling libcrypto supports are used.
*/
void ERR_new(void)
{
c_new_error(NULL);
}

void ERR_set_debug(const char *file, int line, const char *func)
{
c_set_error_debug(NULL, file, line, func);
}

void ERR_set_error(int lib, int reason, const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
va_end(args);
}

void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
{
c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
}

int ERR_set_mark(void)
{
return c_set_error_mark(NULL);
}

int ERR_clear_last_mark(void)
{
return c_clear_last_error_mark(NULL);
}

int ERR_pop_to_mark(void)
{
return c_pop_error_to_mark(NULL);
}
#endif

0 comments on commit 8c2e588

Please sign in to comment.