Skip to content

Commit

Permalink
Simplify UI interface for util_uri.c
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrojnar committed Feb 20, 2025
1 parent a3dce56 commit ff3b110
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 36 deletions.
19 changes: 11 additions & 8 deletions src/eng_back.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ int ENGINE_CTX_finish(ENGINE_CTX *ctx)
/******************************************************************************/

EVP_PKEY *ENGINE_CTX_load_pubkey(ENGINE_CTX *ctx, const char *s_key_id,
UI_METHOD *ui_method, void *callback_data)
UI_METHOD *ui_method, void *ui_data)
{
EVP_PKEY *evp_pkey;

Expand All @@ -176,8 +176,7 @@ EVP_PKEY *ENGINE_CTX_load_pubkey(ENGINE_CTX *ctx, const char *s_key_id,
return NULL;
}

UTIL_CTX_ctrl_set_user_interface(ctx->util_ctx, ui_method);
UTIL_CTX_ctrl_set_callback_data(ctx->util_ctx, callback_data);
UTIL_CTX_set_ui_method(ctx->util_ctx, ui_method, ui_data);
evp_pkey = UTIL_CTX_get_pubkey_from_uri(ctx->util_ctx, s_key_id);

pthread_mutex_unlock(&ctx->lock);
Expand All @@ -192,7 +191,7 @@ EVP_PKEY *ENGINE_CTX_load_pubkey(ENGINE_CTX *ctx, const char *s_key_id,
}

EVP_PKEY *ENGINE_CTX_load_privkey(ENGINE_CTX *ctx, const char *s_key_id,
UI_METHOD *ui_method, void *callback_data)
UI_METHOD *ui_method, void *ui_data)
{
EVP_PKEY *evp_pkey;

Expand All @@ -205,8 +204,7 @@ EVP_PKEY *ENGINE_CTX_load_privkey(ENGINE_CTX *ctx, const char *s_key_id,
return NULL;
}

UTIL_CTX_ctrl_set_user_interface(ctx->util_ctx, ui_method);
UTIL_CTX_ctrl_set_callback_data(ctx->util_ctx, callback_data);
UTIL_CTX_set_ui_method(ctx->util_ctx, ui_method, ui_data);
evp_pkey = UTIL_CTX_get_privkey_from_uri(ctx->util_ctx, s_key_id);

pthread_mutex_unlock(&ctx->lock);
Expand Down Expand Up @@ -285,6 +283,9 @@ static int ENGINE_CTX_ctrl_set_vlog(ENGINE_CTX *ctx, void *cb)

int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)())
{
static UI_METHOD *ui_method = NULL;
static void *ui_data = NULL;

(void)i; /* We don't currently take integer parameters */
(void)f; /* We don't currently take callback parameters */
/*int initialised = ((pkcs11_dso == NULL) ? 0 : 1); */
Expand All @@ -303,10 +304,12 @@ int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)())
return UTIL_CTX_set_init_args(ctx->util_ctx, (const char *)p);
case ENGINE_CTRL_SET_USER_INTERFACE:
case CMD_SET_USER_INTERFACE:
return UTIL_CTX_ctrl_set_user_interface(ctx->util_ctx, (UI_METHOD *)p);
ui_method = p;
return UTIL_CTX_set_ui_method(ctx->util_ctx, ui_method, ui_data);
case ENGINE_CTRL_SET_CALLBACK_DATA:
case CMD_SET_CALLBACK_DATA:
return UTIL_CTX_ctrl_set_callback_data(ctx->util_ctx, p);
ui_data = p;
return UTIL_CTX_set_ui_method(ctx->util_ctx, ui_method, ui_data);
case CMD_FORCE_LOGIN:
UTIL_CTX_set_force_login(ctx->util_ctx, 1);
return 1;
Expand Down
8 changes: 4 additions & 4 deletions src/eng_front.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@ static int engine_finish(ENGINE *engine)
}

static EVP_PKEY *load_pubkey(ENGINE *engine, const char *s_key_id,
UI_METHOD *ui_method, void *callback_data)
UI_METHOD *ui_method, void *ui_data)
{
ENGINE_CTX *ctx;

ctx = ENGINE_CTX_get(engine);
if (!ctx)
return 0;
bind_helper_methods(engine);
return ENGINE_CTX_load_pubkey(ctx, s_key_id, ui_method, callback_data);
return ENGINE_CTX_load_pubkey(ctx, s_key_id, ui_method, ui_data);
}

static EVP_PKEY *load_privkey(ENGINE *engine, const char *s_key_id,
UI_METHOD *ui_method, void *callback_data)
UI_METHOD *ui_method, void *ui_data)
{
ENGINE_CTX *ctx;
EVP_PKEY *pkey;
Expand Down Expand Up @@ -208,7 +208,7 @@ static EVP_PKEY *load_privkey(ENGINE *engine, const char *s_key_id,
}
}
#endif
pkey = ENGINE_CTX_load_privkey(ctx, s_key_id, ui_method, callback_data);
pkey = ENGINE_CTX_load_privkey(ctx, s_key_id, ui_method, ui_data);
#ifdef EVP_F_EVP_PKEY_SET1_ENGINE
/* EVP_PKEY_set1_engine() is required for OpenSSL 1.1.x,
* but otherwise setting pkey->engine breaks OpenSSL 1.0.2 */
Expand Down
4 changes: 2 additions & 2 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ int ENGINE_CTX_finish(ENGINE_CTX *ctx);
int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)());

EVP_PKEY *ENGINE_CTX_load_pubkey(ENGINE_CTX *ctx, const char *s_key_id,
UI_METHOD *ui_method, void *callback_data);
UI_METHOD *ui_method, void *ui_data);

EVP_PKEY *ENGINE_CTX_load_privkey(ENGINE_CTX *ctx, const char *s_key_id,
UI_METHOD *ui_method, void *callback_data);
UI_METHOD *ui_method, void *ui_data);

void ENGINE_CTX_log(ENGINE_CTX *ctx, int level, const char *format, ...)
#ifdef __GNUC__
Expand Down
3 changes: 1 addition & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ UTIL_CTX *UTIL_CTX_new();
void UTIL_CTX_free(UTIL_CTX *ctx);
int UTIL_CTX_set_module(UTIL_CTX *ctx, const char *module);
int UTIL_CTX_set_init_args(UTIL_CTX *ctx, const char *init_args);
int UTIL_CTX_ctrl_set_user_interface(UTIL_CTX *ctx, UI_METHOD *ui_method);
int UTIL_CTX_ctrl_set_callback_data(UTIL_CTX *ctx, void *callback_data);
int UTIL_CTX_set_ui_method(UTIL_CTX *ctx, UI_METHOD *ui_method, void *ui_data);
int UTIL_CTX_enumerate_slots(UTIL_CTX *ctx);
int UTIL_CTX_init_libp11(UTIL_CTX *ctx);
void UTIL_CTX_free_libp11(UTIL_CTX *ctx);
Expand Down
30 changes: 10 additions & 20 deletions src/util_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ struct util_ctx_st {
char *module;
char *init_args;
UI_METHOD *ui_method;
void *callback_data;
void *ui_data;

/* Logging */
int debug_level; /* level of debug output */
void (*vlog)(int, const char *, va_list); /* for the logging callback */

/*
* The PIN used for login. Cache for the ctx_get_pin function.
* The memory for this PIN is always owned internally,
* and may be freed as necessary. Before freeing, the PIN
* must be whitened, to prevent security holes.
* The PIN used for login, cached by the UTIL_CTX_set_pin function.
* The memory for this PIN is always owned internally, and may be freed
* as necessary. Before freeing, the PIN must be cleansed.
*/
char *pin;
size_t pin_length;
Expand Down Expand Up @@ -102,21 +101,12 @@ int UTIL_CTX_set_init_args(UTIL_CTX *ctx, const char *init_args)
return 1;
}

int UTIL_CTX_ctrl_set_user_interface(UTIL_CTX *ctx, UI_METHOD *ui_method)
int UTIL_CTX_set_ui_method(UTIL_CTX *ctx, UI_METHOD *ui_method, void *ui_data)
{
ctx->ui_method = ui_method;
ctx->ui_data = ui_data;
if (ctx->pkcs11_ctx) /* libp11 is already initialized */
PKCS11_set_ui_method(ctx->pkcs11_ctx,
ctx->ui_method, ctx->callback_data);
return 1;
}

int UTIL_CTX_ctrl_set_callback_data(UTIL_CTX *ctx, void *callback_data)
{
ctx->callback_data = callback_data;
if (ctx->pkcs11_ctx) /* libp11 is already initialized */
PKCS11_set_ui_method(ctx->pkcs11_ctx,
ctx->ui_method, ctx->callback_data);
PKCS11_set_ui_method(ctx->pkcs11_ctx, ui_method, ui_data);
return 1;
}

Expand Down Expand Up @@ -150,7 +140,7 @@ int UTIL_CTX_init_libp11(UTIL_CTX *ctx)
pkcs11_ctx = PKCS11_CTX_new();
PKCS11_set_vlog_a_method(pkcs11_ctx, ctx->vlog);
PKCS11_CTX_init_args(pkcs11_ctx, ctx->init_args);
PKCS11_set_ui_method(pkcs11_ctx, ctx->ui_method, ctx->callback_data);
PKCS11_set_ui_method(pkcs11_ctx, ctx->ui_method, ctx->ui_data);
if (PKCS11_CTX_load(pkcs11_ctx, ctx->module) < 0) {
UTIL_CTX_log(ctx, LOG_ERR, "Unable to load module %s\n", ctx->module);
PKCS11_CTX_free(pkcs11_ctx);
Expand Down Expand Up @@ -378,8 +368,8 @@ static int UTIL_CTX_get_pin(UTIL_CTX *ctx, const char *token_label)
UTIL_CTX_log(ctx, LOG_ERR, "UI_new failed\n");
return 0;
}
if (ctx->callback_data)
UI_add_user_data(ui, ctx->callback_data);
if (ctx->ui_data)
UI_add_user_data(ui, ctx->ui_data);

UTIL_CTX_set_pin(ctx, NULL);
ctx->pin = OPENSSL_malloc(MAX_PIN_LENGTH+1);
Expand Down

0 comments on commit ff3b110

Please sign in to comment.