Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the ossl3 context to be allocated once #99

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,44 @@ typedef struct ossl3_library_context {
OSSL_PROVIDER *default_provider;
} ossl3_context_t;

static ossl3_context_t *init_ossl3_ctx()
static pthread_once_t global_ossl3_ctx_init = PTHREAD_ONCE_INIT;
static ossl3_context_t *global_ossl3_ctx = NULL;

static void init_global_ossl3_ctx(void)
{
ossl3_context_t *ctx = OPENSSL_malloc(sizeof(ossl3_context_t));
if (!ctx) return NULL;
if (!ctx) return;

ctx->libctx = OSSL_LIB_CTX_new();
if (!ctx->libctx) {
OPENSSL_free(ctx);
return NULL;
return;
}

/* Load both legacy and default provider as both may be needed */
/* if they fail keep going and an error will be raised when we try to
* fetch the cipher later */
ctx->legacy_provider = OSSL_PROVIDER_load(ctx->libctx, "legacy");
ctx->default_provider = OSSL_PROVIDER_load(ctx->libctx, "default");
return ctx;
global_ossl3_ctx = ctx;
}

static ossl3_context_t *get_ossl3_ctx()
{
int ret;

ret = pthread_once(&global_ossl3_ctx_init, init_global_ossl3_ctx);
if (ret != 0) {
return NULL;
}

return global_ossl3_ctx;
}

static void free_ossl3_ctx(ossl3_context_t *ctx)
__attribute__((destructor))
static void free_ossl3_ctx()
{
ossl3_context_t *ctx = global_ossl3_ctx;
if (ctx == NULL) return;
if (ctx->legacy_provider) OSSL_PROVIDER_unload(ctx->legacy_provider);
if (ctx->default_provider) OSSL_PROVIDER_unload(ctx->default_provider);
Expand Down Expand Up @@ -178,7 +195,7 @@ int MD4_HASH(struct ntlm_buffer *payload,
EVP_MD *md;
int ret;

ossl3_ctx = init_ossl3_ctx();
ossl3_ctx = get_ossl3_ctx();
if (ossl3_ctx == NULL) {
ret = ERR_CRYPTO;
goto done;
Expand All @@ -193,7 +210,6 @@ int MD4_HASH(struct ntlm_buffer *payload,
ret = mdx_hash(md, payload, result);

done:
free_ossl3_ctx(ossl3_ctx);
return ret;
#else
return mdx_hash(EVP_md4(), payload, result);
Expand Down
Loading