Skip to content

Commit 1a780a0

Browse files
committed
Make UTIL_CTX opaque
1 parent 761ef67 commit 1a780a0

File tree

3 files changed

+171
-153
lines changed

3 files changed

+171
-153
lines changed

src/eng_back.c

Lines changed: 26 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@
3333
#include <string.h>
3434

3535
struct engine_ctx_st {
36-
/* Engine configuration */
37-
int debug_level; /* level of debug output */
38-
void (*vlog)(int, const char *, va_list); /* for the logging callback */
39-
char *module;
40-
char *init_args;
36+
UTIL_CTX *util_ctx;
4137
pthread_mutex_t lock;
4238

43-
UTIL_CTX *util_ctx;
39+
/* Logging */
40+
int debug_level; /* level of debug output */
41+
void (*vlog)(int, const char *, va_list); /* for the logging callback */
4442
};
4543

4644
#if defined(_WIN32) || defined(_WIN64)
@@ -103,12 +101,12 @@ ENGINE_CTX *ENGINE_CTX_new()
103101

104102
mod = getenv("PKCS11_MODULE_PATH");
105103
if (mod) {
106-
ctx->module = OPENSSL_strdup(mod);
104+
UTIL_CTX_set_module(ctx->util_ctx, mod);
107105
} else {
108106
#ifdef DEFAULT_PKCS11_MODULE
109-
ctx->module = OPENSSL_strdup(DEFAULT_PKCS11_MODULE);
107+
UTIL_CTX_set_module(ctx->util_ctx, DEFAULT_PKCS11_MODULE);
110108
#else
111-
ctx->module = NULL;
109+
UTIL_CTX_set_module(ctx->util_ctx, NULL);
112110
#endif
113111
}
114112
ctx->debug_level = LOG_NOTICE;
@@ -121,74 +119,28 @@ int ENGINE_CTX_destroy(ENGINE_CTX *ctx)
121119
{
122120
if (ctx) {
123121
UTIL_CTX_free(ctx->util_ctx);
124-
OPENSSL_free(ctx->module);
125-
OPENSSL_free(ctx->init_args);
126122
pthread_mutex_destroy(&ctx->lock);
127123
OPENSSL_free(ctx);
128124
}
129125
return 1;
130126
}
131127

132-
static int ENGINE_CTX_enumerate_slots_unlocked(ENGINE_CTX *ctx)
133-
{
134-
/* PKCS11_update_slots() uses C_GetSlotList() via libp11 */
135-
if (PKCS11_update_slots(ctx->util_ctx->pkcs11_ctx, &ctx->util_ctx->slot_list, &ctx->util_ctx->slot_count) < 0) {
136-
ENGINE_CTX_log(ctx, LOG_INFO, "Failed to enumerate slots\n");
137-
return 0;
138-
}
139-
ENGINE_CTX_log(ctx, LOG_NOTICE, "Found %u slot%s\n", ctx->util_ctx->slot_count,
140-
ctx->util_ctx->slot_count <= 1 ? "" : "s");
141-
return 1;
142-
}
143-
144-
/* Initialize libp11 data: ctx->util_ctx->pkcs11_ctx and ctx->util_ctx->slot_list */
145-
static int ENGINE_CTX_init_libp11_unlocked(ENGINE_CTX *ctx)
146-
{
147-
PKCS11_CTX *pkcs11_ctx;
148-
149-
if (ctx->util_ctx->pkcs11_ctx && ctx->util_ctx->slot_list)
150-
return 0;
151-
152-
ENGINE_CTX_log(ctx, LOG_NOTICE, "PKCS#11: Initializing the engine: %s\n", ctx->module);
153-
154-
pkcs11_ctx = PKCS11_CTX_new();
155-
PKCS11_set_vlog_a_method(pkcs11_ctx, ctx->vlog);
156-
PKCS11_CTX_init_args(pkcs11_ctx, ctx->init_args);
157-
PKCS11_set_ui_method(pkcs11_ctx, ctx->util_ctx->ui_method, ctx->util_ctx->callback_data);
158-
if (PKCS11_CTX_load(pkcs11_ctx, ctx->module) < 0) {
159-
ENGINE_CTX_log(ctx, LOG_ERR, "Unable to load module %s\n", ctx->module);
160-
PKCS11_CTX_free(pkcs11_ctx);
161-
return -1;
162-
}
163-
ctx->util_ctx->pkcs11_ctx = pkcs11_ctx;
164-
165-
if (ENGINE_CTX_enumerate_slots_unlocked(ctx) != 1)
166-
return -1;
167-
168-
return ctx->util_ctx->pkcs11_ctx && ctx->util_ctx->slot_list ? 0 : -1;
169-
}
170-
171-
static int ENGINE_CTX_init_libp11(ENGINE_CTX *ctx)
128+
static int ENGINE_CTX_enumerate_slots(ENGINE_CTX *ctx)
172129
{
173130
int rv;
174131

175132
pthread_mutex_lock(&ctx->lock);
176-
rv = ENGINE_CTX_init_libp11_unlocked(ctx);
133+
rv = UTIL_CTX_enumerate_slots(ctx->util_ctx);
177134
pthread_mutex_unlock(&ctx->lock);
178135
return rv;
179136
}
180137

181-
static int ENGINE_CTX_enumerate_slots(ENGINE_CTX *ctx)
138+
static int ENGINE_CTX_init_libp11(ENGINE_CTX *ctx)
182139
{
183140
int rv;
184141

185-
if (!ctx->util_ctx->pkcs11_ctx)
186-
ENGINE_CTX_init_libp11(ctx);
187-
if (!ctx->util_ctx->pkcs11_ctx)
188-
return -1;
189-
190142
pthread_mutex_lock(&ctx->lock);
191-
rv = ENGINE_CTX_enumerate_slots_unlocked(ctx);
143+
rv = UTIL_CTX_init_libp11(ctx->util_ctx);
192144
pthread_mutex_unlock(&ctx->lock);
193145
return rv;
194146
}
@@ -209,17 +161,7 @@ int ENGINE_CTX_init(ENGINE_CTX *ctx)
209161
int ENGINE_CTX_finish(ENGINE_CTX *ctx)
210162
{
211163
if (ctx) {
212-
if (ctx->util_ctx->slot_list) {
213-
PKCS11_release_all_slots(ctx->util_ctx->pkcs11_ctx,
214-
ctx->util_ctx->slot_list, ctx->util_ctx->slot_count);
215-
ctx->util_ctx->slot_list = NULL;
216-
ctx->util_ctx->slot_count = 0;
217-
}
218-
if (ctx->util_ctx->pkcs11_ctx) {
219-
PKCS11_CTX_unload(ctx->util_ctx->pkcs11_ctx);
220-
PKCS11_CTX_free(ctx->util_ctx->pkcs11_ctx);
221-
ctx->util_ctx->pkcs11_ctx = NULL;
222-
}
164+
UTIL_CTX_free_libp11(ctx->util_ctx);
223165
}
224166
return 1;
225167
}
@@ -236,14 +178,14 @@ EVP_PKEY *ENGINE_CTX_load_pubkey(ENGINE_CTX *ctx, const char *s_key_id,
236178
pthread_mutex_lock(&ctx->lock);
237179

238180
/* Delayed libp11 initialization */
239-
if (ENGINE_CTX_init_libp11_unlocked(ctx)) {
181+
if (UTIL_CTX_init_libp11(ctx->util_ctx)) {
240182
ENGerr(ENG_F_CTX_LOAD_OBJECT, ENG_R_INVALID_PARAMETER);
241183
pthread_mutex_unlock(&ctx->lock);
242184
return NULL;
243185
}
244186

245-
ctx->util_ctx->ui_method = ui_method;
246-
ctx->util_ctx->callback_data = callback_data;
187+
UTIL_CTX_ctrl_set_user_interface(ctx->util_ctx, ui_method);
188+
UTIL_CTX_ctrl_set_callback_data(ctx->util_ctx, callback_data);
247189
evp_pkey = UTIL_CTX_get_pubkey_from_uri(ctx->util_ctx, s_key_id);
248190

249191
pthread_mutex_unlock(&ctx->lock);
@@ -265,14 +207,14 @@ EVP_PKEY *ENGINE_CTX_load_privkey(ENGINE_CTX *ctx, const char *s_key_id,
265207
pthread_mutex_lock(&ctx->lock);
266208

267209
/* Delayed libp11 initialization */
268-
if (ENGINE_CTX_init_libp11_unlocked(ctx)) {
210+
if (UTIL_CTX_init_libp11(ctx->util_ctx)) {
269211
ENGerr(ENG_F_CTX_LOAD_OBJECT, ENG_R_INVALID_PARAMETER);
270212
pthread_mutex_unlock(&ctx->lock);
271213
return NULL;
272214
}
273215

274-
ctx->util_ctx->ui_method = ui_method;
275-
ctx->util_ctx->callback_data = callback_data;
216+
UTIL_CTX_ctrl_set_user_interface(ctx->util_ctx, ui_method);
217+
UTIL_CTX_ctrl_set_callback_data(ctx->util_ctx, callback_data);
276218
evp_pkey = UTIL_CTX_get_privkey_from_uri(ctx->util_ctx, s_key_id);
277219

278220
pthread_mutex_unlock(&ctx->lock);
@@ -290,13 +232,6 @@ EVP_PKEY *ENGINE_CTX_load_privkey(ENGINE_CTX *ctx, const char *s_key_id,
290232
/* Engine ctrl request handling */
291233
/******************************************************************************/
292234

293-
static int ENGINE_CTX_ctrl_set_module(ENGINE_CTX *ctx, const char *modulename)
294-
{
295-
OPENSSL_free(ctx->module);
296-
ctx->module = modulename ? OPENSSL_strdup(modulename) : NULL;
297-
return 1;
298-
}
299-
300235
static int ENGINE_CTX_ctrl_set_debug_level(ENGINE_CTX *ctx, int level)
301236
{
302237
ctx->debug_level = level;
@@ -322,7 +257,7 @@ static int ENGINE_CTX_ctrl_load_cert(ENGINE_CTX *ctx, void *p)
322257
pthread_mutex_lock(&ctx->lock);
323258

324259
/* Delayed libp11 initialization */
325-
if (ENGINE_CTX_init_libp11_unlocked(ctx)) {
260+
if (UTIL_CTX_init_libp11(ctx->util_ctx)) {
326261
ENGerr(ENG_F_CTX_LOAD_OBJECT, ENG_R_INVALID_PARAMETER);
327262
pthread_mutex_unlock(&ctx->lock);
328263
return 0;
@@ -339,48 +274,14 @@ static int ENGINE_CTX_ctrl_load_cert(ENGINE_CTX *ctx, void *p)
339274
return 1;
340275
}
341276

342-
static int ENGINE_CTX_ctrl_set_init_args(ENGINE_CTX *ctx, const char *init_args_orig)
343-
{
344-
OPENSSL_free(ctx->init_args);
345-
ctx->init_args = init_args_orig ? OPENSSL_strdup(init_args_orig) : NULL;
346-
return 1;
347-
}
348-
349-
static int ENGINE_CTX_ctrl_set_user_interface(ENGINE_CTX *ctx, UI_METHOD *ui_method)
350-
{
351-
ctx->util_ctx->ui_method = ui_method;
352-
if (ctx->util_ctx->pkcs11_ctx) /* libp11 is already initialized */
353-
PKCS11_set_ui_method(ctx->util_ctx->pkcs11_ctx,
354-
ctx->util_ctx->ui_method, ctx->util_ctx->callback_data);
355-
return 1;
356-
}
357-
358-
static int ENGINE_CTX_ctrl_set_callback_data(ENGINE_CTX *ctx, void *callback_data)
359-
{
360-
ctx->util_ctx->callback_data = callback_data;
361-
if (ctx->util_ctx->pkcs11_ctx) /* libp11 is already initialized */
362-
PKCS11_set_ui_method(ctx->util_ctx->pkcs11_ctx,
363-
ctx->util_ctx->ui_method, ctx->util_ctx->callback_data);
364-
return 1;
365-
}
366-
367-
static int ENGINE_CTX_ctrl_force_login(ENGINE_CTX *ctx)
368-
{
369-
ctx->util_ctx->force_login = 1;
370-
return 1;
371-
}
372-
373277
static int ENGINE_CTX_ctrl_set_vlog(ENGINE_CTX *ctx, void *cb)
374278
{
375279
struct {
376280
PKCS11_VLOG_A_CB vlog;
377281
} *vlog_callback = cb;
378282

379283
ctx->vlog = vlog_callback->vlog;
380-
ctx->util_ctx->vlog = vlog_callback->vlog;
381-
382-
if (ctx->util_ctx->pkcs11_ctx) /* already initialized */
383-
PKCS11_set_vlog_a_method(ctx->util_ctx->pkcs11_ctx, ctx->vlog); /* update */
284+
UTIL_CTX_set_vlog_a(ctx->util_ctx, vlog_callback->vlog);
384285

385286
return 1;
386287
}
@@ -392,7 +293,7 @@ int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)())
392293
/*int initialised = ((pkcs11_dso == NULL) ? 0 : 1); */
393294
switch (cmd) {
394295
case CMD_MODULE_PATH:
395-
return ENGINE_CTX_ctrl_set_module(ctx, (const char *)p);
296+
return UTIL_CTX_set_module(ctx->util_ctx, (const char *)p);
396297
case CMD_PIN:
397298
return UTIL_CTX_set_pin(ctx->util_ctx, (const char *)p);
398299
case CMD_VERBOSE:
@@ -402,15 +303,16 @@ int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)())
402303
case CMD_LOAD_CERT_CTRL:
403304
return ENGINE_CTX_ctrl_load_cert(ctx, p);
404305
case CMD_INIT_ARGS:
405-
return ENGINE_CTX_ctrl_set_init_args(ctx, (const char *)p);
306+
return UTIL_CTX_set_init_args(ctx->util_ctx, (const char *)p);
406307
case ENGINE_CTRL_SET_USER_INTERFACE:
407308
case CMD_SET_USER_INTERFACE:
408-
return ENGINE_CTX_ctrl_set_user_interface(ctx, (UI_METHOD *)p);
309+
return UTIL_CTX_ctrl_set_user_interface(ctx->util_ctx, (UI_METHOD *)p);
409310
case ENGINE_CTRL_SET_CALLBACK_DATA:
410311
case CMD_SET_CALLBACK_DATA:
411-
return ENGINE_CTX_ctrl_set_callback_data(ctx, p);
312+
return UTIL_CTX_ctrl_set_callback_data(ctx->util_ctx, p);
412313
case CMD_FORCE_LOGIN:
413-
return ENGINE_CTX_ctrl_force_login(ctx);
314+
UTIL_CTX_set_force_login(ctx->util_ctx, 1);
315+
return 1;
414316
case CMD_RE_ENUMERATE:
415317
return ENGINE_CTX_enumerate_slots(ctx);
416318
case CMD_VLOG_A:

src/util.h

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,24 @@ typedef struct engine_ctx_st ENGINE_CTX; /* opaque */
5252

5353
typedef struct util_ctx_st UTIL_CTX;
5454

55-
struct util_ctx_st {
56-
/* Configuration */
57-
int debug_level; /* level of debug output */
58-
void (*vlog)(int, const char *, va_list); /* for the logging callback */
59-
UI_METHOD *ui_method;
60-
void *callback_data;
61-
62-
/*
63-
* The PIN used for login. Cache for the ctx_get_pin function.
64-
* The memory for this PIN is always owned internally,
65-
* and may be freed as necessary. Before freeing, the PIN
66-
* must be whitened, to prevent security holes.
67-
*/
68-
char *pin;
69-
size_t pin_length;
70-
int forced_pin;
71-
int force_login;
72-
73-
/* Current operations */
74-
PKCS11_CTX *pkcs11_ctx;
75-
PKCS11_SLOT *slot_list;
76-
unsigned int slot_count;
77-
};
78-
7955
UTIL_CTX *UTIL_CTX_new();
80-
8156
void UTIL_CTX_free(UTIL_CTX *ctx);
82-
57+
int UTIL_CTX_set_module(UTIL_CTX *ctx, const char *module);
58+
int UTIL_CTX_set_init_args(UTIL_CTX *ctx, const char *init_args);
59+
int UTIL_CTX_ctrl_set_user_interface(UTIL_CTX *ctx, UI_METHOD *ui_method);
60+
int UTIL_CTX_ctrl_set_callback_data(UTIL_CTX *ctx, void *callback_data);
61+
int UTIL_CTX_enumerate_slots(UTIL_CTX *ctx);
62+
int UTIL_CTX_init_libp11(UTIL_CTX *ctx);
63+
int UTIL_CTX_free_libp11(UTIL_CTX *ctx);
64+
65+
void UTIL_CTX_set_vlog_a(UTIL_CTX *ctx, PKCS11_VLOG_A_CB vlog);
8366
void UTIL_CTX_log(UTIL_CTX *ctx, int level, const char *format, ...);
8467

8568
int UTIL_CTX_set_pin(UTIL_CTX *ctx, const char *pin);
69+
void UTIL_CTX_set_force_login(UTIL_CTX *ctx, int force_login);
8670

8771
X509 *UTIL_CTX_get_cert_from_uri(UTIL_CTX *ctx, const char *object_uri);
88-
8972
EVP_PKEY *UTIL_CTX_get_pubkey_from_uri(UTIL_CTX *ctx, const char *s_key_id);
90-
9173
EVP_PKEY *UTIL_CTX_get_privkey_from_uri(UTIL_CTX *ctx, const char *s_key_id);
9274

9375
#endif /* _UTIL_LIBP11_H */

0 commit comments

Comments
 (0)