OSSL_ENCODER_CTX(3ossl)

πŸ’– OSSL_ENCODER_CTX(3ossl) πŸ’–

okay bestie, welcome to the OpenSSL encoder context page, aka the group chat for all your encoder-context routines ✨. This lives in the 3ossl manual section, and honestly? It's a whole vibe.


🌸 NAME 🌸

OSSL_ENCODER_CTX, OSSL_ENCODER_CTX_new, OSSL_ENCODER_settable_ctx_params, OSSL_ENCODER_CTX_set_params, 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 β€” Encoder context routines πŸŽ€


✨ 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[]);
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 πŸ¦‹

So here's the tea, bestie πŸ«–: encoding some input object into the encoding you want can happen through a chain of encoder implementations. That means the output from one encoder can literally become the input for the next one down the line. It's giving relay race, no cap πŸƒβ€β™€οΈπŸƒβ€β™€οΈ.

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

The final output type must be given, and a chain of encoders has to end with an implementation that actually produces that output type. No ending mid-chain, that's not the vibe.

At the beginning of the encoding process, a constructor provided by the caller 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 about to be used β€” and that data may be useful for the constructor. There are some functions to extract data from this type, described down in "Constructor" πŸ‘‡.

πŸ”§ Functions πŸ”§

  • OSSL_ENCODER_CTX_new() β€” creates a OSSL_ENCODER_CTX. fresh out the box ✨
  • 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 (it's unbothered, it's thriving πŸ’…).
  • OSSL_ENCODER_CTX_free() β€” frees the given context ctx. If the argument is NULL, nothing is done. zero drama.
  • 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 how you build encoder chains πŸ”—.
  • OSSL_ENCODER_CTX_set_output_type() β€” sets the ending output type. This must be specified, and it 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.
  • 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).
⚠️ bestie, listen. These functions: 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_set_construct(), OSSL_ENCODER_CTX_set_construct_data(), and OSSL_ENCODER_CTX_set_cleanup() β€” you should not use them after the context is finalised. In particular, not after calling the function OSSL_ENCODER_CTX_new_for_pkey(). Once it's finalised, it's finalised. periodt. πŸ’€

🧚 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 😭.

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.
  • 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.

πŸ’« 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_add_encoder(), OSSL_ENCODER_CTX_add_extra(), OSSL_ENCODER_CTX_set_construct(), OSSL_ENCODER_CTX_set_construct_data() and OSSL_ENCODER_CTX_set_cleanup() β€” return 1 on success, or 0 on failure.
  • 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.

(ngl bestie, yes, that's the same function name listed twice up there in the original β€” I'm not fixing it, I'm reporting it. iykyk πŸ™ˆ)


πŸ‘‘ 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 giving unfinished business, fr fr πŸ’€


🌈 SEE ALSO 🌈

provider(7), OSSL_ENCODER(3)


⭐ HISTORY ⭐

The functions described here were added in OpenSSL 3.0. iconic behaviour, 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>.


okay that's the whole page bestie, you ate that πŸ“βœ¨ and for the receipts: this was OpenSSL 3.5.9, dated 2026-09-29 β€” OSSL_ENCODER_CTX(3ossl), signing off πŸ’…πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from