OSSL_LIB_CTX(3ossl)
π OSSL_LIB_CTX β the OpenSSL library context, bestie edition π
okay so this is OSSL_LIB_CTX(3ossl) from the OpenSSL manuals, and we're gonna get you fluent in library contexts, no cap. Also living here: OSSL_LIB_CTX_get_data, OSSL_LIB_CTX_new, OSSL_LIB_CTX_new_from_dispatch, OSSL_LIB_CTX_new_child, OSSL_LIB_CTX_free, OSSL_LIB_CTX_load_config, OSSL_LIB_CTX_get0_global_default, and OSSL_LIB_CTX_set0_default. β¨
πΈ SYNOPSIS πΈ
#include <openssl/crypto.h>
typedef struct ossl_lib_ctx_st OSSL_LIB_CTX;
OSSL_LIB_CTX *OSSL_LIB_CTX_new(void);
OSSL_LIB_CTX *OSSL_LIB_CTX_new_from_dispatch(const OSSL_CORE_HANDLE *handle,
const OSSL_DISPATCH *in);
OSSL_LIB_CTX *OSSL_LIB_CTX_new_child(const OSSL_CORE_HANDLE *handle,
const OSSL_DISPATCH *in);
int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file);
void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx);
OSSL_LIB_CTX *OSSL_LIB_CTX_get0_global_default(void);
OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *ctx);
void *OSSL_LIB_CTX_get_data(OSSL_LIB_CTX *ctx, int index);
π¦ DESCRIPTION π¦
so OSSL_LIB_CTX is an internal OpenSSL library context type. apps can allocate their own, but they can also just pass NULL and get a default context, as long as the function in question takes an OSSL_LIB_CTX argument. easy. π
β οΈ real talk: when a non-default library context is in use, you gotta be careful with multi-threaded apps and properly clean up thread-local resources before theOSSL_LIB_CTXgets freed.OPENSSL_thread_stop_ex(3)has the deets. don't skip this one bestie.
Here's the function-by-function rundown, mwah:
OSSL_LIB_CTX_new()β makes you a brand new OpenSSL library context. iconic behavior.OSSL_LIB_CTX_new_from_dispatch()β creates a new OpenSSL library context that's initialised to use callbacks from theOSSL_DISPATCHstructure. mainly for provider authors ngl. thehandleanddispatchstructure args you pass should be the same ones passed to a provider'sOSSL_provider_initfunction. some OpenSSL functions β likeBIO_new_from_core_bio(3)β require the library context to be created this way or they simply won't work. it's giving "no vibes, only structure" πOSSL_LIB_CTX_new_child()β only useful to provider authors, and it does the same thing asOSSL_LIB_CTX_new_from_dispatch()except it additionally links the new library context to the application library context. the new one is a full library context in its own right, but it'll have all the same providers available that exist in the application library context (no reloading needed, slay). if the application loads or unloads providers from the application library context, that gets automatically mirrored in the child library context. β¨
Also: providers that are not loaded in the parent library context can be explicitly loaded into the child library context independently from the parent. those independently-loaded providers will not be mirrored back into the parent, and they won't be affected if the parent later loads the same provider.
A provider may call OSSL_PROVIDER_load(3) with the child library context whenever it needs to. if the provider already exists because it got mirrored from the parent, it stays available and its reference count goes up. if you call OSSL_PROVIDER_load(3) like that, you should subsequently call OSSL_PROVIDER_unload(3) to decrement the reference count.
π¨ and listen:OSSL_PROVIDER_unload(3)must not be called for a provider in the child library context that did not have an earlierOSSL_PROVIDER_load(3)call for that provider in that child library context. periodt.
And it's not just providers, bestie β a child library context also mirrors the default properties (set via EVP_set_default_properties(3)) from the parent library context. but if EVP_set_default_properties(3) gets called directly on a child library context, then the new properties override anything from the parent and the mirroring of properties stops. it's giving independence. π
One more thing: when OSSL_LIB_CTX_new_child() is called from within the scope of a provider's OSSL_provider_init function, the currently-initialising provider isn't yet available in the application's library context, so it likewise won't yet be available in the newly constructed child library context. the moment that OSSL_provider_init function returns, the new provider becomes available in the application's library context and gets mirrored in the child library context too. timing is everything π«
OSSL_LIB_CTX_load_config()β loads a configuration file using the givenctx. this is how you associate a library context with providers that are loaded from a configuration.
π this function must not be called concurrently from multiple threads on a single ctx. no parallel chaos please.
OSSL_LIB_CTX_free()β frees the givenctx, unless it happens to be the default OpenSSL library context (can't touch that one). if the argument isNULL, nothing happens. chill. πOSSL_LIB_CTX_get0_global_default()β returns a concrete (non-NULL) reference to the global default library context. solid. reliable. main character energy.OSSL_LIB_CTX_set0_default()β sets the default OpenSSL library context to bectxin the current thread, and returns the previous default library context. caller better take care to restore that previous default with a subsequent call to this function, fr fr. ifctxisNULLthen no change is made to the default library context, but a pointer to the current library context still gets returned. on a successful call of this function, the returned value will always be a concrete (non-NULL) library context. π
π caution when changing the default library context and starting async jobs (peep ASYNC_start_job(3)): the default library context at the time the job is started gets used throughout the lifetime of that async job, no matter how the calling thread keeps changing defaults in the meantime. which means the calling thread must not free the library context that was the default at the start of the async job before that job finishes. do not do it. π
OSSL_LIB_CTX_get_data()β returns a memory address whose interpretation depends on theindex. theindexargument refers to a context member that's to be retrieved. the values forindexare all private to OpenSSL currently, so applications should not typically call this function. ifctxisNULL, the function operates on the default library context. and yes βOSSL_LIB_CTX_get_data()returns a memory address whose interpretation depends on theindex. (yes we said it twice, it's that important π )
π RETURN VALUES π
OSSL_LIB_CTX_new(),OSSL_LIB_CTX_get0_global_default()andOSSL_LIB_CTX_set0_default()return a library context pointer on success, orNULLon error. classic.OSSL_LIB_CTX_free()doesn't return any value. it's avoid. it just does its thing and leaves. ghosting, but productively. πOSSL_LIB_CTX_load_config()returns1on success,0on error.OSSL_LIB_CTX_get_data()returns a memory address whose interpretation depends on theindex. (again. forever. it's a whole bit.)
β HISTORY β
Every single function on this page was added in OpenSSL 3.0.
OSSL_LIB_CTX_get_data() was introduced in OpenSSL 3.4. a little late to the party but she arrived. π¦
π COPYRIGHT π
Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
and that's the whole page, bestie π«Ά this was 3.5.9, dated 2026-09-29, from the OSSL_LIB_CTX(3ossl) manual β go forth and manage your library contexts responsibly, ily πβ¨
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms