OSSL_LIB_CTX(3ossl)

πŸ“¦ version 15.1, in freebsd 15.1

πŸ¦‹ OSSL_LIB_CTX β€” the OpenSSL library context πŸ¦‹

okay bestie, welcome to OSSL_LIB_CTX(3ossl), the OpenSSL manual page that's literally about library contexts β€” the little universe every OpenSSL thing lives inside. It's giving backstage-pass energy, and we're going in. πŸ’…βœ¨


πŸŽ€ NAME πŸŽ€

OSSL_LIB_CTX, 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, OSSL_LIB_CTX_set0_default β€” the OpenSSL library context, and all its little helper besties. 🫢


✨ 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. Lowkey iconic. Apps may allocate their own, but they may also just pass NULL to use a default context wherever a function takes an OSSL_LIB_CTX argument. Easy.

⚠️ When a non-default library context is in use, be careful with multi-threaded apps β€” you gotta properly clean up thread local resources before the OSSL_LIB_CTX gets freed. Peep OPENSSL_thread_stop_ex(3) for the full tea. β˜•
  • OSSL_LIB_CTX_new() β€” creates a brand new OpenSSL library context. Fresh out the box. 🎁
  • OSSL_LIB_CTX_new_from_dispatch() β€” creates a new OpenSSL library context that's initialised to use callbacks from the OSSL_DISPATCH structure. This is mainly for provider authors, iykyk. The handle and dispatch structure arguments you pass should be the same ones that got passed to a provider's OSSL_provider_init function. Some OpenSSL functions β€” like BIO_new_from_core_bio(3) β€” literally require the library context to be created this way in order to work. no cap.
  • OSSL_LIB_CTX_new_child() β€” only useful to provider authors, and it does the same thing as OSSL_LIB_CTX_new_from_dispatch() except it additionally links the new library context to the application library context. The new library context is a full library context in its own right, but it'll have all the same providers available to it that are available in the application library context (without having to reload them β€” ate πŸ’…). If the application loads or unloads providers from the application library context, that gets automatically mirrored in the child library context. Obsessed with the syncing, honestly.

And it gets better: providers that are not loaded in the parent library context can be explicitly loaded into the child library context independently from the parent library context. Providers loaded independently like this will not be mirrored in the parent library context, and won't be affected if the parent library context subsequently loads the same provider. Independent queen behavior. πŸ‘‘

A provider may call OSSL_PROVIDER_load(3) with the child library context as required. If the provider already exists because it's being mirrored from the parent library context, then it stays available and its reference count gets increased. If OSSL_PROVIDER_load(3) is called in this way, then OSSL_PROVIDER_unload(3) should be subsequently called to decrement the reference count. But heads up:

πŸ’€ OSSL_PROVIDER_unload(3) must not be called for a provider in the child library context that did not have an earlier OSSL_PROVIDER_load(3) call for that provider in that child library context. Don't do it, bestie. Not the vibe.

In addition to providers, a child library context also mirrors the default properties (set via EVP_set_default_properties(3)) from the parent library context. If EVP_set_default_properties(3) is called directly on a child library context, then the new properties will override anything from the parent library context, and mirroring of the properties will stop. Mirror broken, honestly kind of a moment. πŸͺžπŸ’”

When OSSL_LIB_CTX_new_child() is called from within the scope of a provider's OSSL_provider_init function, the currently initialising provider is not yet available in the application's library context β€” and therefore will similarly not yet be available in the newly constructed child library context. Timing is everything. ⏳ As soon as the OSSL_provider_init function returns, the new provider is available in the application's library context and will be similarly mirrored in the child library context.

  • OSSL_LIB_CTX_load_config() β€” loads a configuration file using the given ctx. This can be used to associate a library context with providers that are loaded from a configuration. Cute, right? πŸŽ€ But: this function must not be called concurrently from multiple threads on a single ctx. One at a time, please.
  • OSSL_LIB_CTX_free() β€” frees the given ctx, unless it happens to be the default OpenSSL library context. (The default one is not getting got, sorry.) If the argument is NULL, nothing is done. Peaceful. 🌸
  • OSSL_LIB_CTX_get0_global_default() β€” returns a concrete (non-NULL) reference to the global default library context. Always a real one. 🫢
  • OSSL_LIB_CTX_set0_default() β€” sets the default OpenSSL library context to be ctx in the current thread. The previous default library context is returned. Care should be taken by the caller to restore the previous default library context with a subsequent call of this function. If ctx is NULL then no change is made to the default library context, but a pointer to the current library context is still returned. On a successful call of this function, the returned value will always be a concrete (non-NULL) library context. Main character function, honestly. πŸ‘‘
⚠️ Care should be taken when changing the default library context and starting async jobs (see ASYNC_start_job(3)), because the default library context at the time the job is started will be used throughout the lifetime of an async job β€” no matter how the calling thread makes further default library context changes in the meantime. That means the calling thread must not free the library context that was the default at the start of the async job before that job has finished. Do not do it. 😭
  • OSSL_LIB_CTX_get_data() β€” returns a memory address whose interpretation depends on the index. The index argument refers to a context member which is to be retrieved. The values for index are all private to OpenSSL currently, and so applications should not typically call this function. If ctx is NULL, then the function operates on the default library context. And yeah β€” OSSL_LIB_CTX_get_data() returns a memory address whose interpretation depends on the index. (Yes, they said it twice. It's that important. πŸ’€)

🌈 RETURN VALUES 🌈

  • OSSL_LIB_CTX_new(), OSSL_LIB_CTX_get0_global_default() and OSSL_LIB_CTX_set0_default() return a library context pointer on success, or NULL on error. Slay or flop, no in between. ✨
  • OSSL_LIB_CTX_free() doesn't return any value. It's above returns.
  • OSSL_LIB_CTX_load_config() returns 1 on success, 0 on error.
  • OSSL_LIB_CTX_get_data() returns a memory address whose interpretation depends on the index.

πŸ¦‹ HISTORY πŸ¦‹

All of the functions described on this page were added in OpenSSL 3.0.

OSSL_LIB_CTX_get_data() was introduced in OpenSSL 3.4. Fresh-ish addition, we love to see it. πŸŽ€


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 β€” fr fr no cap. this was OpenSSL 3.5.6, dated 2026-04-07, page OSSL_LIB_CTX(3ossl). go forth and context responsibly πŸ’œπŸ«ΆπŸŒΈ

πŸ“¦ where this page comes from