OSSL_TRACE_SET_CHANNEL(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… OSSL_TRACE_SET_CHANNEL πŸ’…

okay bestie, welcome to the 3ossl corner of the manual β€” this is the page for OSSL_trace_set_channel, OSSL_trace_set_prefix, OSSL_trace_set_suffix, OSSL_trace_set_callback, and OSSL_trace_cb, aka the whole "turn trace output on" friend group. 🫢


✨ NAME ✨

OSSL_trace_set_channel, OSSL_trace_set_prefix, OSSL_trace_set_suffix, OSSL_trace_set_callback, OSSL_trace_cb β€” enabling trace output. that's it, that's the vibe. πŸ’–


🌸 SYNOPSIS 🌸

#include <openssl/trace.h>

typedef size_t (*OSSL_trace_cb)(const char *buf, size_t cnt,
                                int category, int cmd, void *data);

void OSSL_trace_set_channel(int category, BIO *bio);
void OSSL_trace_set_prefix(int category, const char *prefix);
void OSSL_trace_set_suffix(int category, const char *suffix);
void OSSL_trace_set_callback(int category, OSSL_trace_cb cb, void  *data);

πŸ¦‹ DESCRIPTION πŸ¦‹

so, if it's available (peep "Configure Tracing" down below πŸ‘€), your application can request internal trace output. and this output? it comes in the form of free text. for humans. to read. we love a readable text moment. πŸ“–

the trace output is split into categories, and each one can be enabled individually. every category gets enabled individually by attaching a so-called trace channel to it β€” and in the simplest case that's literally just a BIO object you write the tracing output for that category into. alternatively, the application can provide a tracer callback to get more fine-grained trace information. that callback gets wrapped internally by a dedicated BIO object. sneakily. 🧚

for the tracing code itself, both trace channel types are indistinguishable. they're called a simple trace channel and a callback trace channel, respectively. no cap. ✨

OSSL_TRACE_ENABLED(3) can check whether tracing is currently enabled for a given category. and functions like OSSL_TRACE1(3) plus macros like OSSL_TRACE_BEGIN(3) are how you actually produce that free-text trace output.

πŸŽ€ Functions πŸŽ€

  • OSSL_trace_set_channel() β€” this is the one that enables the given trace category by attaching the BIO bio object as a (simple) trace channel. and on success, ownership of the BIO is transferred to the channel β€” so the caller must not free it directly. like, don't. put it down. πŸ™ˆ
  • OSSL_trace_set_prefix() and OSSL_trace_set_suffix() β€” these add an extra line for each channel, output before and after a group of tracing output. what actually counts as an "output group" is decided by the code producing the output, not by you. the lines you give here are considered immutable; if you want more dynamic tracing prefixes, set a callback with OSSL_trace_set_callback() instead. πŸ’…
  • OSSL_trace_set_callback() β€” enables the given trace category by giving it the tracer callback cb with the associated data data, which simply gets passed straight through to cb whenever it's called. the callback function is internally wrapped by a dedicated BIO object β€” the so-called callback trace channel. use this when you want to transform the trace output into something suitable for your application's needs, like, where a prefix and suffix line just aren't enough. 🀌

OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually exclusive β€” calling one of them clears whatever the previous call set. no keeping both, bestie. 😭

calling OSSL_trace_set_channel() with NULL for channel, or OSSL_trace_set_callback() with NULL for cb, disables tracing for the given category. bye felicia. πŸ‘‹

πŸ”₯ Trace callback πŸ”₯

the tracer callback must return a size_t β€” which must be zero on error, and otherwise return the number of bytes that were output. it receives a text buffer buf with cnt bytes of text, plus the category, a control number cmd, and the data that was passed to OSSL_trace_set_callback().

the possible control numbers are:

  • OSSL_TRACE_CTRL_BEGIN β€” the callback is called from OSSL_trace_begin(), which gives the callback the chance to output a dynamic starting line, or set a prefix that should be output at the beginning of each line, or do something other. your call. ✨
  • OSSL_TRACE_CTRL_WRITE β€” this callback is called whenever data is written to the BIO by some regular BIO output routine. an arbitrary number of OSSL_TRACE_CTRL_WRITE callbacks can happen inside a group marked by a pair of OSSL_TRACE_CTRL_BEGIN and OSSL_TRACE_CTRL_END calls β€” but never outside such a group. rules are rules. 🚧
  • OSSL_TRACE_CTRL_END β€” the callback is called from OSSL_trace_end(), which gives the callback the chance to output a dynamic ending line, or reset the line prefix that was set with OSSL_TRACE_CTRL_BEGIN, or do something other. 🎬

🌈 Trace categories 🌈

the trace categories are simple numbers, available through macros. iconic.

  • OSSL_TRACE_CATEGORY_TRACE β€” traces the OpenSSL trace API itself. more precisely, this generates trace output any time a new trace hook is set. meta, honestly. πŸͺž
  • OSSL_TRACE_CATEGORY_INIT β€” traces OpenSSL library initialization and cleanup. ⚠️ this one needs special care: OpenSSL does automatic cleanup after exit from main(), and any tracing output done during that cleanup will be lost if the tracing channel or callback were cleaned away prematurely. a suggestion is to make such cleanup part of a function that's registered very early with atexit(3). save yourself the heartbreak. πŸ’”
  • OSSL_TRACE_CATEGORY_TLS β€” traces the TLS/SSL protocol. πŸ‘‘
  • OSSL_TRACE_CATEGORY_TLS_CIPHER β€” traces the ciphers used by the TLS/SSL protocol.
  • OSSL_TRACE_CATEGORY_CONF β€” traces details about the provider and engine configuration.
  • OSSL_TRACE_CATEGORY_ENGINE_TABLE β€” traces the ENGINE algorithm table selection. more precisely, functions like ENGINE_get_pkey_asn1_meth_engine(), ENGINE_get_pkey_meth_engine(), ENGINE_get_cipher_engine(), ENGINE_get_digest_engine(), will generate trace summaries of the handling of internal tables.
  • OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT β€” traces the ENGINE reference counting. more precisely, both reference counts in the ENGINE structure will be monitored, with a line of trace output generated for each change. watch it like a hawk. πŸ¦…
  • OSSL_TRACE_CATEGORY_PKCS5V2 β€” traces PKCS#5 v2 key generation.
  • OSSL_TRACE_CATEGORY_PKCS12_KEYGEN β€” traces PKCS#12 key generation.
  • OSSL_TRACE_CATEGORY_PKCS12_DECRYPT β€” traces PKCS#12 decryption.
  • OSSL_TRACE_CATEGORY_X509V3_POLICY β€” traces X509v3 policy processing. more precisely, this generates the complete policy tree at various point during evaluation.
  • OSSL_TRACE_CATEGORY_BN_CTX β€” traces BIGNUM context operations.
  • OSSL_TRACE_CATEGORY_CMP β€” traces CMP client and server activity.
  • OSSL_TRACE_CATEGORY_STORE β€” traces STORE operations.
  • OSSL_TRACE_CATEGORY_DECODER β€” traces decoder operations.
  • OSSL_TRACE_CATEGORY_ENCODER β€” traces encoder operations.
  • OSSL_TRACE_CATEGORY_REF_COUNT β€” traces decrementing certain ASN.1 structure references.
  • OSSL_TRACE_CATEGORY_HTTP β€” traces the HTTP client, such as message headers being sent and received. πŸ“¨

there's also OSSL_TRACE_CATEGORY_ALL, which works as a fallback and can be used to get all trace output.

⚠️ note, however, that in this case all trace output will effectively be associated with the 'ALL' category β€” which is undesirable if your application intends to include the category name in the trace output. in that case it's better to register separate channels for each trace category instead. iykyk. πŸ’…

⭐ RETURN VALUES ⭐

OSSL_trace_set_channel(), OSSL_trace_set_prefix(), OSSL_trace_set_suffix(), and OSSL_trace_set_callback() return 1 on success, or 0 on failure. that's the whole story. βœ…


πŸ“ EXAMPLES πŸ“

in all the examples below, the trace-producing code is assumed to be the following:

int foo = 42;
const char bar[] = { 0,  1,  2,  3,  4,  5,  6,  7,
                     8,  9, 10, 11, 12, 13, 14, 15 };

OSSL_TRACE_BEGIN(TLS) {
    BIO_puts(trc_out, "foo: ");
    BIO_printf(trc_out, "%d\n", foo);
    BIO_dump(trc_out, bar, sizeof(bar));
} OSSL_TRACE_END(TLS);

πŸ’œ Simple example πŸ’œ

an example with just a channel and constant prefix / suffix. lowkey the easiest slay.

int main(int argc, char *argv[])
{
    BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
    OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_SSL, err);
    OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_SSL, "BEGIN TRACE[TLS]");
    OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_SSL, "END TRACE[TLS]");

    /* ... work ... */
}

when the trace-producing code above is performed, this is what gets output on standard error:

BEGIN TRACE[TLS]
foo: 42
0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
END TRACE[TLS]

🧚 Advanced example 🧚

this one uses the callback, and depends on pthreads functionality. fancy fr fr.

static size_t cb(const char *buf, size_t cnt,
                int category, int cmd, void *vdata)
{
    BIO *bio = vdata;
    const char *label = NULL;

    switch (cmd) {
    case OSSL_TRACE_CTRL_BEGIN:
        label = "BEGIN";
        break;
    case OSSL_TRACE_CTRL_END:
        label = "END";
        break;
    }

    if (label != NULL) {
        union {
            pthread_t tid;
            unsigned long ltid;
        } tid;

        tid.tid = pthread_self();
        BIO_printf(bio, "%s TRACE[%s]:%lx\n",
                   label, OSSL_trace_get_category_name(category), tid.ltid);
    }
    return (size_t)BIO_puts(bio, buf);
}

int main(int argc, char *argv[])
{
    BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
    OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_SSL, cb, err);

    /* ... work ... */
}

the output is almost the same as for the simple example above. πŸ‘€

BEGIN TRACE[TLS]:7f9eb0193b80
foo: 42
0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
END TRACE[TLS]:7f9eb0193b80

🌟 NOTES 🌟

πŸ”§ Configure Tracing πŸ”§

by default, the OpenSSL library is built with tracing disabled. so to actually use the tracing functionality documented here, you gotta configure and build OpenSSL with the 'enable-trace' option. non-negotiable. πŸ› οΈ

when the library is built with tracing disabled, the macro OPENSSL_NO_TRACE is defined in <openssl/opensslconf.h> and all functions described here are inoperational β€” i.e. they will do nothing. literally nothing. πŸ’€


πŸ‘€ SEE ALSO πŸ‘€

OSSL_TRACE_ENABLED(3), OSSL_TRACE_BEGIN(3), OSSL_TRACE1(3), atexit(3)


πŸ“œ HISTORY πŸ“œ

OSSL_trace_set_channel(), OSSL_trace_set_prefix(), OSSL_trace_set_suffix(), and OSSL_trace_set_callback() were all added in OpenSSL 3.0. a whole era. ✨


Copyright 2019-2023 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 page, bestie 🫢 β€” brought to you by version 3.5.0, dated 2026-09-25, living its life as OSSL_TRACE_SET_CHANNEL(3ossl). go forth and trace responsibly. πŸ’…βœ¨

πŸ“¦ where this page comes from