OSSL_ENCODER..._NEW_FOR_PKEY(3ossl)
π OSSL_ENCODER..._NEW_FOR_PKEY π
okay bestie, welcome to the 3ossl manual, this one's about encoding EVP_PKEYs π
β¨ NAME β¨
OSSL_ENCODER_CTX_new_for_pkey, OSSL_ENCODER_CTX_set_cipher, OSSL_ENCODER_CTX_set_passphrase, OSSL_ENCODER_CTX_set_pem_password_cb, OSSL_ENCODER_CTX_set_passphrase_cb, OSSL_ENCODER_CTX_set_passphrase_ui β encoder routines to encode EVP_PKEYs π
πΈ SYNOPSIS πΈ
#include <openssl/encoder.h>
OSSL_ENCODER_CTX *
OSSL_ENCODER_CTX_new_for_pkey(const EVP_PKEY *pkey, int selection,
const char *output_type,
const char *output_structure,
const char *propquery);
int OSSL_ENCODER_CTX_set_cipher(OSSL_ENCODER_CTX *ctx,
const char *cipher_name,
const char *propquery);
int OSSL_ENCODER_CTX_set_passphrase(OSSL_ENCODER_CTX *ctx,
const unsigned char *kstr,
size_t klen);
int OSSL_ENCODER_CTX_set_pem_password_cb(OSSL_ENCODER_CTX *ctx,
pem_password_cb *cb, void *cbarg);
int OSSL_ENCODER_CTX_set_passphrase_ui(OSSL_ENCODER_CTX *ctx,
const UI_METHOD *ui_method,
void *ui_data);
int OSSL_ENCODER_CTX_set_passphrase_cb(OSSL_ENCODER_CTX *ctx,
OSSL_PASSPHRASE_CALLBACK *cb,
void *cbarg);
π¦ DESCRIPTION π¦
So OSSL_ENCODER_CTX_new_for_pkey() is basically the little utility that does the heavy lifting for you, fr fr. It creates an OSSL_ENCODER_CTX, sniffs out all the applicable encoder implementations, and sets them up β so once you've called it, almost all you have left to do is call stuff like OSSL_ENCODER_to_bio(3). Iconic, honestly. β¨
The output_type decides the final output encoding, and selection lets you pick which parts of the pkey actually get included in the output. output_type gets its own lil deep-dive down in "Output types", and selection is explained in "Selections". π
Under the hood, OSSL_ENCODER_CTX_new_for_pkey() takes the names from the EVP_KEYMGMT(3) implementation that's associated with pkey and uses them to build a whole list of applicable encoder implementations. Those get used to process the pkey into the encoding named by output_type, with the outermost structure named by output_structure β if that's even relevant. All of these implementations get implicitly fetched, with propquery there for finer selection. π§
And listen, if no suitable encoder implementation is found, OSSL_ENCODER_CTX_new_for_pkey() still creates an OSSL_ENCODER_CTX β just with no associated encoder (so OSSL_ENCODER_CTX_get_num_encoders(3) returns zero). That's on purpose, bestie: it lets the caller tell the difference between an error while creating the OSSL_ENCODER_CTX and a straight-up missing encoder implementation, so it can act accordingly. π«Ά
Heads up:OSSL_ENCODER_CTX_new_for_pkey()finalises theOSSL_ENCODER_CTX. After that, theOSSL_ENCODER_CTX_set_*andOSSL_ENCODER_CTX_add_*functions described inOSSL_ENCODER_CTX(3)should not be called. No takebacks. π
OSSL_ENCODER_CTX_set_cipher() tells the implementation which cipher should be used to encrypt encoded keys. The cipher is given by name, cipher_name. How that cipher_name gets interpreted is implementation dependent β the implementation might implement the cipher itself directly, or through other implementations, or it might just choose to fetch it. If it does support fetching the cipher, then it may use propquery as the properties to query for while fetching. Oh, and cipher_name can also be NULL, which results in unencrypted encoding. π
OSSL_ENCODER_CTX_set_passphrase() hands the implementation a pass phrase to use when encrypting the encoded private key. Or, alternatively, you can specify a pass phrase callback with the following functions. π
OSSL_ENCODER_CTX_set_pem_password_cb(), OSSL_ENCODER_CTX_set_passphrase_ui() and OSSL_ENCODER_CTX_set_passphrase_cb() set up a callback method that the implementation can use to prompt for a pass phrase β giving the caller the choice of their preferred pass phrase callback form. These get called indirectly, through an internal OSSL_PASSPHRASE_CALLBACK(3) function. π
π Output types π
The possible EVP_PKEY output types depend on the implementations you've got available. (vibe check: no implementations, no output types, iykyk π)
OpenSSL has built-in implementations for these output types:
"TEXT"β the output is a human readable description of the key.EVP_PKEY_print_private(3),EVP_PKEY_print_public(3)andEVP_PKEY_print_params(3)use this for their output."DER"β the output is the DER encoding of the selection of thepkey."PEM"β the output is the selection of thepkeyin PEM format.
π Selections π
selection can be any one of the values described in "Selections" in EVP_PKEY_fromdata(3).
These are only 'hints' though, because the encoder implementations are free to decide what actually makes sense to include in the output β and that may depend on the desired output. For example? An EC key in a PKCS#8 structure doesn't usually include the public key. Slay. π
β RETURN VALUES β
OSSL_ENCODER_CTX_new_for_pkey() returns a pointer to an OSSL_ENCODER_CTX, or NULL if it couldn't be created. π
OSSL_ENCODER_CTX_set_cipher(), OSSL_ENCODER_CTX_set_passphrase(), OSSL_ENCODER_CTX_set_pem_password_cb(), OSSL_ENCODER_CTX_set_passphrase_ui() and OSSL_ENCODER_CTX_set_passphrase_cb() all return 1 on success, or 0 on failure. Simple as that, periodt. β¨
π SEE ALSO π
provider(7), OSSL_ENCODER(3), OSSL_ENCODER_CTX(3)
π HISTORY π
The functions described here were added in OpenSSL 3.0. So yes, they're giving modern, they're giving new era. π¦
π COPYRIGHT π
Copyright 2019-2022 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, you ate that π«Ά this was 3.5.9, dated 2026-09-2, and it's the OSSL_ENCODER..._NEW_FOR_PKEY(3ossl) manual β now go encrypt something responsibly ππΈβ¨
π¦ 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