OSSL_ENCODER_CTX(3SSL)

πŸ’– OSSL_ENCODER_CTX β€” encoder context routines πŸ’–

hey bestie, welcome to the OSSL_ENCODER_CTX(3SSL) page, aka your group chat for making encoders behave. grab a snack, we're going through everything.


✨ NAME ✨

It's the whole squad of encoder-context routines, and here's every single one of them, no cap:

  • OSSL_ENCODER_CTX
  • OSSL_ENCODER_CTX_new
  • OSSL_ENCODER_settable_ctx_params
  • OSSL_ENCODER_CTX_set_params
  • OSSL_ENCODER_CTX_ctrl_string
  • OSSL_ENCODER_CTX_free
  • OSSL_ENCODER_CTX_set_selection
  • OSSL_ENCODER_CTX_set_output_type
  • OSSL_ENCODER_CTX_set_output_structure
  • OSSL_ENCODER_CTX_add_encoder
  • OSSL_ENCODER_CTX_add_extra
  • OSSL_ENCODER_CTX_get_num_encoders
  • OSSL_ENCODER_INSTANCE
  • OSSL_ENCODER_INSTANCE_get_encoder
  • OSSL_ENCODER_INSTANCE_get_encoder_ctx
  • OSSL_ENCODER_INSTANCE_get_output_type
  • OSSL_ENCODER_INSTANCE_get_output_structure
  • OSSL_ENCODER_CONSTRUCT
  • OSSL_ENCODER_CLEANUP
  • OSSL_ENCODER_CTX_set_construct
  • OSSL_ENCODER_CTX_set_construct_data
  • OSSL_ENCODER_CTX_set_cleanup

and what are they, as a collective? Encoder context routines. iconic. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <openssl/encoder.h>

typedef struct ossl_encoder_ctx_st OSSL_ENCODER_CTX;

OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new();
const OSSL_PARAM *OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER *encoder);
int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX *ctx,
                                const OSSL_PARAM params[]);
int OSSL_ENCODER_CTX_ctrl_string(OSSL_ENCODER_CTX *ctx,
                                 const char *name, const char *val);
void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX *ctx);

int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection);
int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
                                     const char *output_type);
int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
                                          const char *output_structure);

int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder);
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
                               OSSL_LIB_CTX *libctx, const char *propq);
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx);

typedef struct ossl_encoder_instance_st OSSL_ENCODER_INSTANCE;
OSSL_ENCODER *
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst);
void *
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst);
const char *
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst);
const char *
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst);

typedef const void *OSSL_ENCODER_CONSTRUCT(OSSL_ENCODER_INSTANCE *encoder_inst,
                                           void *construct_data);
typedef void OSSL_ENCODER_CLEANUP(void *construct_data);

int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
                                   OSSL_ENCODER_CONSTRUCT *construct);
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
                                        void *construct_data);
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
                                 OSSL_ENCODER_CLEANUP *cleanup);

🌸 DESCRIPTION 🌸

okay so here's the vibe. you want to encode an input object into whatever encoding you're dreaming of, and you don't have to do it alone β€” you do it with a chain of encoder implementations. that means the output from one encoder can literally be the input for the next one in the chain. a relay race, but for bytes. πŸƒβ™€οΈβœ¨

The OSSL_ENCODER_CTX is the main character here β€” it holds all the data about these encoders. Which is what lets you have generic format encoders like DER to PEM, AND also more specialized ones like RSA to DER. both. we can have both. πŸ’…

You must give the final output type, and a chain of encoders must end with an implementation that produces that output type. no notes.

At the beginning of the encoding process, a constructor that you (the caller) provided gets called, to make sure there's an appropriate provider-side object to start with. You set that constructor with OSSL_ENCODER_CTX_set_construct().

OSSL_ENCODER_INSTANCE is an opaque structure holding data about the encoder that's going to be used β€” and that data may be useful for the constructor. There are some functions to extract data from this type, and they're described down in "Constructor". 🫢

πŸŽ€ Functions πŸŽ€

  • OSSL_ENCODER_CTX_new() β€” creates a OSSL_ENCODER_CTX. fresh out the box, bestie.
  • OSSL_ENCODER_settable_ctx_params() β€” returns an OSSL_PARAM(3) array of parameter descriptors.
  • OSSL_ENCODER_CTX_set_params() β€” attempts to set parameters specified with an OSSL_PARAM(3) array params. Parameters that the implementation doesn't recognise should be ignored. (like, they get the silent treatment. respectfully.)
  • OSSL_ENCODER_CTX_ctrl_string() β€” attempts to set a single parameter named name by parsing the specified string value to the type of a settable parameter associated with one of the underlying encoders. An error is reported if no parameter named name is a settable in any of the underlying encoders, the value fails to parse, or the encoder implementation rejects the parsed value. so many ways to fail 😭 but it will tell you.
  • OSSL_ENCODER_CTX_free() β€” frees the given context ctx. If the argument is NULL, nothing is done. (it's giving "if they're not there, we don't speak.")
  • OSSL_ENCODER_CTX_add_encoder() β€” populates the OSSL_ENCODER_CTX ctx with a encoder, to be used to encode an input object.
  • OSSL_ENCODER_CTX_add_extra() β€” finds encoders that further encodes output from already added encoders, and adds them as well. This is used to build encoder chains. chain-building behaviour fr fr. πŸ”—
  • OSSL_ENCODER_CTX_set_selection() β€” sets what should be included in the output.
  • OSSL_ENCODER_CTX_set_output_type() β€” sets the ending output type. This must be specified, and determines if a complete encoder chain is available.
  • OSSL_ENCODER_CTX_set_output_structure() β€” sets the desired output structure. This may be used to determines what encoder implementations may be used. Depending on the type of object being encoded, the output structure may not be relevant. (iykyk, it's a vibe-dependent situation.)
  • OSSL_ENCODER_CTX_get_num_encoders() β€” gets the number of encoders currently added to the context ctx.
  • OSSL_ENCODER_CTX_set_construct() β€” sets the constructor construct.
  • OSSL_ENCODER_CTX_set_construct_data() β€” sets the constructor data that is passed to the constructor every time it's called.
  • OSSL_ENCODER_CTX_set_cleanup() β€” sets the constructor data cleanup function. This is called by OSSL_ENCODER_CTX_free(3).

🧚 Constructor 🧚

A OSSL_ENCODER_CONSTRUCT gets the following arguments:

  • encoder_inst β€” the OSSL_ENCODER_INSTANCE for the encoder from which the constructor gets its data.
  • construct_data β€” the pointer that was set with OSSL_ENCODE_CTX_set_construct_data().

The constructor is expected to return a valid (non-NULL) pointer to a provider-native object that can be used as first input of an encoding chain, or NULL to indicate that an error has occurred. no NULL shenanigans unless something went wrong, bestie.

These utility functions may be used by a constructor:

  • OSSL_ENCODER_INSTANCE_get_encoder() β€” can be used to get the encoder implementation of the encoder instance encoder_inst.
  • OSSL_ENCODER_INSTANCE_get_encoder_ctx() β€” can be used to get the encoder implementation's provider context of the encoder instance encoder_inst.
  • OSSL_ENCODER_INSTANCE_get_output_type() β€” can be used to get the output type for the encoder implementation of the encoder instance encoder_inst. This will never be NULL. periodt.
  • OSSL_ENCODER_INSTANCE_get_output_structure() β€” can be used to get the output structure for the encoder implementation of the encoder instance encoder_inst. This may be NULL. ok so this one's allowed to ghost you.

πŸ”₯ RETURN VALUES πŸ”₯

  • OSSL_ENCODER_CTX_new() returns a pointer to a OSSL_ENCODER_CTX, or NULL if the context structure couldn't be allocated.
  • OSSL_ENCODER_settable_ctx_params() returns an OSSL_PARAM(3) array, or NULL if none is available.
  • OSSL_ENCODER_CTX_set_params() returns 1 if all recognised parameters were valid, or 0 if one of them was invalid or caused some other failure in the implementation.
  • OSSL_ENCODER_CTX_ctrl_string() returns 1 on success and 0 on error.
  • OSSL_ENCODER_CTX_add_encoder(), OSSL_ENCODER_CTX_add_extra(), OSSL_ENCODER_CTX_set_cleanup(), OSSL_ENCODER_CTX_set_construct(), OSSL_ENCODER_CTX_set_construct_data(), OSSL_ENCODER_CTX_set_output_structure(), OSSL_ENCODER_CTX_set_output_type() and OSSL_ENCODER_CTX_set_selection() return 1 on success, or 0 on failure.
⚠️ heads up bestie: they always fail for encoder contexts created in a frozen state via OSSL_ENCODER_CTX_new_for_pkey(3). frozen means frozen. 🧊
  • OSSL_ENCODER_CTX_get_num_encoders() returns the current number of encoders. It returns 0 if ctx is NULL.
  • OSSL_ENCODER_INSTANCE_get_encoder() returns an OSSL_ENCODER pointer on success, or NULL on failure.
  • OSSL_ENCODER_INSTANCE_get_encoder_ctx() returns a provider context pointer on success, or NULL on failure.
  • OSSL_ENCODER_INSTANCE_get_output_type() returns a string with the name of the input type, if relevant. NULL is a valid returned value.
  • OSSL_ENCODER_INSTANCE_get_output_type() returns a string with the name of the output type.
  • OSSL_ENCODER_INSTANCE_get_output_structure() returns a string with the name of the output structure.

(yes, OSSL_ENCODER_INSTANCE_get_output_type() really is listed twice up there, once for the input type and once for the output type. the manpage said it, so I said it. 😭)


πŸ’€ NOTES AND BUGS πŸ’€

The chain mechanism in ENCODE is not yet completely implemented. It affects functions such as OSSL_ENCODER_CTX_add_extra and the inner processing loop. so like, it's not finished yet, don't be shocked.


🌈 SEE ALSO 🌈

provider(7), OSSL_ENCODER(3)


⭐ HISTORY ⭐

The functions described here were added in OpenSSL 3.0. a classic, honestly.


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 OpenSSL 4.0.3, dated 2026-09-29, and it's genuinely been a pleasure. go encode something gorgeous. mwah. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from