Skip to content

Commit 715695d

Browse files
olszomalmtrojnar
authored andcommitted
Fixed re-numeration of slots
1 parent ba4ae57 commit 715695d

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/eng_back.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ int ctx_destroy(ENGINE_CTX *ctx)
284284
return 1;
285285
}
286286

287-
static int ctx_enumerate_slots_unlocked(ENGINE_CTX *ctx, PKCS11_CTX *pkcs11_ctx)
287+
static int ctx_enumerate_slots_unlocked(ENGINE_CTX *ctx)
288288
{
289289
/* PKCS11_update_slots() uses C_GetSlotList() via libp11 */
290-
if (PKCS11_update_slots(pkcs11_ctx, &ctx->slot_list, &ctx->slot_count) < 0) {
290+
if (PKCS11_update_slots(ctx->pkcs11_ctx, &ctx->slot_list, &ctx->slot_count) < 0) {
291291
ctx_log(ctx, 0, "Failed to enumerate slots\n");
292292
return 0;
293293
}
@@ -296,16 +296,6 @@ static int ctx_enumerate_slots_unlocked(ENGINE_CTX *ctx, PKCS11_CTX *pkcs11_ctx)
296296
return 1;
297297
}
298298

299-
static int ctx_enumerate_slots(ENGINE_CTX *ctx, PKCS11_CTX *pkcs11_ctx)
300-
{
301-
int rv;
302-
303-
pthread_mutex_lock(&ctx->lock);
304-
rv = ctx_enumerate_slots_unlocked(ctx, pkcs11_ctx);
305-
pthread_mutex_unlock(&ctx->lock);
306-
return rv;
307-
}
308-
309299
/* Initialize libp11 data: ctx->pkcs11_ctx and ctx->slot_list */
310300
static int ctx_init_libp11_unlocked(ENGINE_CTX *ctx)
311301
{
@@ -326,12 +316,37 @@ static int ctx_init_libp11_unlocked(ENGINE_CTX *ctx)
326316
}
327317
ctx->pkcs11_ctx = pkcs11_ctx;
328318

329-
if (ctx_enumerate_slots_unlocked(ctx, pkcs11_ctx) != 1)
319+
if (ctx_enumerate_slots_unlocked(ctx) != 1)
330320
return -1;
331321

332322
return ctx->pkcs11_ctx && ctx->slot_list ? 0 : -1;
333323
}
334324

325+
static int ctx_init_libp11(ENGINE_CTX *ctx)
326+
{
327+
int rv;
328+
329+
pthread_mutex_lock(&ctx->lock);
330+
rv = ctx_init_libp11_unlocked(ctx);
331+
pthread_mutex_unlock(&ctx->lock);
332+
return rv;
333+
}
334+
335+
static int ctx_enumerate_slots(ENGINE_CTX *ctx)
336+
{
337+
int rv;
338+
339+
if (!ctx->pkcs11_ctx)
340+
ctx_init_libp11(ctx);
341+
if (!ctx->pkcs11_ctx)
342+
return -1;
343+
344+
pthread_mutex_lock(&ctx->lock);
345+
rv = ctx_enumerate_slots_unlocked(ctx);
346+
pthread_mutex_unlock(&ctx->lock);
347+
return rv;
348+
}
349+
335350
/* Function called from ENGINE_init() */
336351
int ctx_init(ENGINE_CTX *ctx)
337352
{
@@ -1050,7 +1065,7 @@ int ctx_engine_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)())
10501065
case CMD_FORCE_LOGIN:
10511066
return ctx_ctrl_force_login(ctx);
10521067
case CMD_RE_ENUMERATE:
1053-
return ctx_enumerate_slots(ctx, ctx->pkcs11_ctx);
1068+
return ctx_enumerate_slots(ctx);
10541069
default:
10551070
ENGerr(ENG_F_CTX_ENGINE_CTRL, ENG_R_UNKNOWN_COMMAND);
10561071
break;

0 commit comments

Comments
 (0)