Skip to content

Commit

Permalink
Logging subsystem for libp11
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrojnar authored and olszomal committed Dec 18, 2024
1 parent fe7d0bd commit a2db977
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/eng_back.c
Original file line number Diff line number Diff line change
Expand Up @@ -1176,11 +1176,15 @@ static int ctx_ctrl_force_login(ENGINE_CTX *ctx)

static int ctx_ctrl_set_vlog(ENGINE_CTX *ctx, void *cb)
{
struct {
void (*vlog)(int, const char *, va_list);
} *vlog_callback = cb;
PKCS11_VLOG_A_CB *vlog_callback = cb;

ctx->vlog = vlog_callback->vlog; /* engine logs */

ctx->vlog = vlog_callback->vlog;
if (!ctx->pkcs11_ctx)
ctx_init_libp11(ctx);
if (!ctx->pkcs11_ctx)
return 0;
PKCS11_vlog_a(ctx->pkcs11_ctx, vlog_callback); /* libp11 logs */
return 1;
}

Expand Down
1 change: 1 addition & 0 deletions src/libp11-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct pkcs11_ctx_private {
void *ui_user_data;
unsigned int forkid;
pthread_mutex_t fork_lock;
void (*vlog_a)(int, const char *, va_list); /* for the logging callback */
};
#define PRIVCTX(_ctx) ((PKCS11_CTX_private *) ((_ctx)->_private))

Expand Down
1 change: 1 addition & 0 deletions src/libp11.exports
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ PKCS11_pkey_meths
ERR_load_PKCS11_strings
PKCS11_set_ui_method
ERR_get_CKR_code
PKCS11_vlog_a
8 changes: 8 additions & 0 deletions src/libp11.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ typedef struct PKCS11_ctx_st {
void *_private;
} PKCS11_CTX;

/** PKCS11 ASCII logging callback */
typedef struct {
void (*vlog)(int, const char *, va_list);
} PKCS11_VLOG_A_CB;

/**
* Create a new libp11 context
*
Expand Down Expand Up @@ -487,6 +492,9 @@ extern int PKCS11_private_decrypt(
int flen, const unsigned char *from,
unsigned char *to, PKCS11_KEY *key, int padding);

/* Set the logging callback */
extern void PKCS11_vlog_a(PKCS11_CTX *pctx, PKCS11_VLOG_A_CB *cb);

/* Function codes */
# define CKR_F_PKCS11_CHANGE_PIN 100
# define CKR_F_PKCS11_CHECK_TOKEN 101
Expand Down
7 changes: 7 additions & 0 deletions src/p11_front.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,11 @@ int PKCS11_verify(int type, const unsigned char *m, unsigned int m_len,
return -1;
}

void PKCS11_vlog_a(PKCS11_CTX *pctx, PKCS11_VLOG_A_CB *cb)
{
PKCS11_CTX_private *ctx = PRIVCTX(pctx);

ctx->vlog_a = cb->vlog;
}

/* vim: set noexpandtab: */
33 changes: 33 additions & 0 deletions src/p11_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,37 @@ int pkcs11_atomic_add(int *value, int amount, pthread_mutex_t *lock)
#endif
}

void pkcs11_log(PKCS11_CTX *pctx, int level, const char *format, ...)
{
va_list args;

va_start(args, format);
if (pctx && PRIVCTX(pctx)->vlog_a) {
/* Log messages through a custom logging function */
va_list args_copy;
char *new_format = NULL;
int len = strlen("libp11: ") + strlen(format) + 1;

new_format = (char *)OPENSSL_malloc((size_t)len);
if (new_format == NULL) {
va_end(args);
return;
}
va_copy(args_copy, args);
BIO_snprintf(new_format, (size_t)len, "libp11: %s", format);
PRIVCTX(pctx)->vlog_a(level, (const char *)new_format, args_copy);
va_end(args_copy);
OPENSSL_free(new_format);
} else if (level <= 3) { /* LOG_ERR */
vfprintf(stderr, format, args);
} else if (level >= 7) { /* LOG_DEBUG */
#ifdef DEBUG
vprintf(format, args);
#endif
} else {
vprintf(format, args);
}
va_end(args);
}

/* vim: set noexpandtab: */

0 comments on commit a2db977

Please sign in to comment.