EVP_PKEY_ASN1_METHOD(3ossl)
π EVP_PKEY_ASN1_METHOD β the OpenSSL manual page, part 1 π
okay so bestie, buckle up π this is EVP_PKEY_ASN1_METHOD(3ossl) and it's literally the page about manipulating and registering the EVP_PKEY_ASN1_METHOD structure. it's giving "backend lore" and honestly? iconic. there's a LOT of names packed in here so let's meet the whole cast first π
β¨ NAME β¨
say hi to the whole squad, because this page covers all of these babes:
EVP_PKEY_ASN1_METHODEVP_PKEY_asn1_newEVP_PKEY_asn1_copyEVP_PKEY_asn1_freeEVP_PKEY_asn1_add0EVP_PKEY_asn1_add_aliasEVP_PKEY_asn1_set_publicEVP_PKEY_asn1_set_privateEVP_PKEY_asn1_set_paramEVP_PKEY_asn1_set_freeEVP_PKEY_asn1_set_ctrlEVP_PKEY_asn1_set_itemEVP_PKEY_asn1_set_siginfEVP_PKEY_asn1_set_checkEVP_PKEY_asn1_set_public_checkEVP_PKEY_asn1_set_param_checkEVP_PKEY_asn1_set_security_bitsEVP_PKEY_asn1_set_set_priv_keyEVP_PKEY_asn1_set_set_pub_keyEVP_PKEY_asn1_set_get_priv_keyEVP_PKEY_asn1_set_get_pub_keyEVP_PKEY_get0_asn1
and what's the vibe of the whole group? manipulating and registering EVP_PKEY_ASN1_METHOD structure. that's the assignment. periodt π
πΈ SYNOPSIS πΈ
first things first, you gotta pull in the header β no cap, nothing works without it:
#include <openssl/evp.h>
then we got this little typedef, which is lowkey just a struct with a glow-up:
typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
and here's every single function signature, copy-pasted exactly because these are not the vibe to get wrong π
EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
const char *pem_str,
const char *info);
void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
const EVP_PKEY_ASN1_METHOD *src);
void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth);
int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth);
int EVP_PKEY_asn1_add_alias(int to, int from);
void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
int (*pub_decode) (EVP_PKEY *pk,
const X509_PUBKEY *pub),
int (*pub_encode) (X509_PUBKEY *pub,
const EVP_PKEY *pk),
int (*pub_cmp) (const EVP_PKEY *a,
const EVP_PKEY *b),
int (*pub_print) (BIO *out,
const EVP_PKEY *pkey,
int indent, ASN1_PCTX *pctx),
int (*pkey_size) (const EVP_PKEY *pk),
int (*pkey_bits) (const EVP_PKEY *pk));
void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
int (*priv_decode) (EVP_PKEY *pk,
const PKCS8_PRIV_KEY_INFO
*p8inf),
int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
const EVP_PKEY *pk),
int (*priv_print) (BIO *out,
const EVP_PKEY *pkey,
int indent,
ASN1_PCTX *pctx));
void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
int (*param_decode) (EVP_PKEY *pkey,
const unsigned char **pder,
int derlen),
int (*param_encode) (const EVP_PKEY *pkey,
unsigned char **pder),
int (*param_missing) (const EVP_PKEY *pk),
int (*param_copy) (EVP_PKEY *to,
const EVP_PKEY *from),
int (*param_cmp) (const EVP_PKEY *a,
const EVP_PKEY *b),
int (*param_print) (BIO *out,
const EVP_PKEY *pkey,
int indent,
ASN1_PCTX *pctx));
void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
void (*pkey_free) (EVP_PKEY *pkey));
void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
long arg1, void *arg2));
void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
int (*item_verify) (EVP_MD_CTX *ctx,
const ASN1_ITEM *it,
void *asn,
X509_ALGOR *a,
ASN1_BIT_STRING *sig,
EVP_PKEY *pkey),
int (*item_sign) (EVP_MD_CTX *ctx,
const ASN1_ITEM *it,
void *asn,
X509_ALGOR *alg1,
X509_ALGOR *alg2,
ASN1_BIT_STRING *sig));
void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
int (*siginf_set) (X509_SIG_INFO *siginf,
const X509_ALGOR *alg,
const ASN1_STRING *sig));
void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
int (*pkey_check) (const EVP_PKEY *pk));
void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
int (*pkey_pub_check) (const EVP_PKEY *pk));
void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
int (*pkey_param_check) (const EVP_PKEY *pk));
void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
int (*pkey_security_bits) (const EVP_PKEY
*pk));
void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
int (*set_priv_key) (EVP_PKEY *pk,
const unsigned char
*priv,
size_t len));
void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
int (*set_pub_key) (EVP_PKEY *pk,
const unsigned char *pub,
size_t len));
void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
int (*get_priv_key) (const EVP_PKEY *pk,
unsigned char *priv,
size_t *len));
void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
int (*get_pub_key) (const EVP_PKEY *pk,
unsigned char *pub,
size_t *len));
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey);
likeβ¦ look at all those function pointers π«Ά pub_decode, pub_encode, priv_decode, item_verify, set_priv_key, get_pub_keyβ¦ it's a whole roster of callbacks and honestly they ate. keep this block close, bestie, because everything that comes next is about what all of them actually do π
and yeah β that's only part 1. more is coming, so don't go anywhere π
π DESCRIPTION π
Okay so picture this: EVP_PKEY_ASN1_METHOD is a struct that holds a whole little toolkit of ASN.1 conversion, printing, and info methods for one specific public key algorithm. It's giving "backstage pass for one algorithm" energy.
There are two places where these EVP_PKEY_ASN1_METHOD objects live:
- one is a built-in array representing the standard methods for different algorithms π§
- the other is a stack of user-defined application-specific methods, and you can manipulate that stack with
EVP_PKEY_asn1_add0(3)π
π οΈ Methods π οΈ
The methods are the underlying implementations of a particular public key algorithm present by the EVP_PKEY object. Yes bestie, this is where the algorithm actually does its thing.
int (*pub_decode) (EVP_PKEY *pk, const X509_PUBKEY *pub);
int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *pctx);
pub_decode()andpub_encode()β these are called to decode / encodeX509_PUBKEYASN.1 parameters to / frompk. They MUST return0on error,1on success. They're called byX509_PUBKEY_get0(3)andX509_PUBKEY_set(3).pub_cmp()β called when two public keys are compared. It MUST return1when the keys are equal,0otherwise. It's called byEVP_PKEY_eq(3).pub_print()β called to print a public key in humanly readable text toout, indentedindentspaces. It MUST return0on error,1on success. It's called byEVP_PKEY_print_public(3).
int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *pctx);
priv_decode()andpriv_encode()β these decode / encode thePKCS8_PRIV_KEY_INFOform private key to / frompk. They MUST return0on error,1on success. They're called byEVP_PKCS82PKEY(3)andEVP_PKEY2PKCS8(3).priv_print()β called to print a private key in humanly readable text toout, indentedindentspaces. It MUST return0on error,1on success. It's called byEVP_PKEY_print_private(3).
int (*pkey_size) (const EVP_PKEY *pk);
int (*pkey_bits) (const EVP_PKEY *pk);
int (*pkey_security_bits) (const EVP_PKEY *pk);
pkey_size()β returns the key size in bytes. It's called byEVP_PKEY_get_size(3).pkey_bits()β returns the key size in bits. It's called byEVP_PKEY_get_bits(3).pkey_security_bits()β also part of this trio! The page lists it right here but doesn't give it a separate little blurb, and I'm not out here inventing facts, bestie π«Ά
int (*param_decode) (EVP_PKEY *pkey,
const unsigned char **pder, int derlen);
int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
int (*param_missing) (const EVP_PKEY *pk);
int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
ASN1_PCTX *pctx);
param_decode()andparam_encode()β these decode / encode DER formatted parameters to / frompk. They MUST return0on error,1on success. They're called byPEM_read_bio_Parameters(3)and thefile:OSSL_STORE_LOADER(3).param_missing()β returns0if a key parameter is missing, otherwise1. It's called byEVP_PKEY_missing_parameters(3).param_copy()β copies key parameters fromfromtoto. It MUST return0on error,1on success. It's called byEVP_PKEY_copy_parameters(3).param_cmp()β compares the parameters of keysaandb. It MUST return1when the keys are equal,0when not equal, or a negative number on error. It's called byEVP_PKEY_parameters_eq(3).param_print()β prints the private key parameters in humanly readable text toout, indentedindentspaces. It MUST return0on error,1on success. It's called byEVP_PKEY_print_params(3).
int (*sig_print) (BIO *out,
const X509_ALGOR *sigalg, const ASN1_STRING *sig,
int indent, ASN1_PCTX *pctx);
sig_print()β prints a signature in humanly readable text toout, indentedindentspaces.sigalgcontains the exact signature algorithm. If the signature insigdoesn't correspond to what this method expects,X509_signature_dump()must be used as a last resort. It MUST return0on error,1on success. It's called byX509_signature_print(3).
void (*pkey_free) (EVP_PKEY *pkey);
pkey_free()β helps freeing the internals ofpkey. It's called byEVP_PKEY_free(3),EVP_PKEY_set_type(3),EVP_PKEY_set_type_str(3), andEVP_PKEY_assign(3).
int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
pkey_ctrl()β adds extra algorithm specific control. It's called byEVP_PKEY_get_default_digest_nid(3),EVP_PKEY_set1_encoded_public_key(3),EVP_PKEY_get1_encoded_public_key(3),PKCS7_SIGNER_INFO_set(3),PKCS7_RECIP_INFO_set(3), ...
int (*old_priv_decode) (EVP_PKEY *pkey,
const unsigned char **pder, int derlen);
int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
old_priv_decode()andold_priv_encode()β these decode / encode the private keypkeyfrom / to a DER formatted array. These are exclusively used to help decoding / encoding older (pre PKCS#8) PEM formatted encrypted private keys.old_priv_decode()MUST return0on error,1on success.old_priv_encode()MUST return the same kind of values asi2d_PrivateKey(). They're called byd2i_PrivateKey(3)andi2d_PrivateKey(3).
int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey);
int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
X509_ALGOR *alg1, X509_ALGOR *alg2,
ASN1_BIT_STRING *sig);
item_sign()anditem_verify()β these methods make it possible to have algorithm specific signatures and verification of them. Iconic behavior, honestly.
item_sign() MUST return one of:
<=0β error1βitem_sign()did everything, OpenSSL internals just needs to pass the signature length back.2βitem_sign()did nothing, OpenSSL internal standard routines are expected to continue with the default signature production.3βitem_sign()set the algorithm identifieralgor1andalgor2, OpenSSL internals should just sign using those algorithms.
item_verify() MUST return one of:
<=0β error1βitem_sign()did everything, OpenSSL internals just needs to pass the signature length back.2βitem_sign()did nothing, OpenSSL internal standard routines are expected to continue with the default signature production.
item_verify() and item_sign() are called by ASN1_item_verify(3) and ASN1_item_sign(3), and by extension, X509_verify(3), X509_REQ_verify(3), X509_sign(3), X509_REQ_sign(3), ...
int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
const ASN1_STRING *sig);
siginf_set()β used to set customX509_SIG_INFOparameters. It MUST return0on error, or1on success. It's called as part ofX509_check_purpose(3),X509_check_ca(3)andX509_check_issued(3).
int (*pkey_check) (const EVP_PKEY *pk);
int (*pkey_public_check) (const EVP_PKEY *pk);
int (*pkey_param_check) (const EVP_PKEY *pk);
pkey_check(),pkey_public_check()andpkey_param_check()β used to check the validity ofpkfor key-pair, public component and parameters, respectively. They MUST return0for an invalid key, or1for a valid key. They are called byEVP_PKEY_check(3),EVP_PKEY_public_check(3)andEVP_PKEY_param_check(3)respectively.
int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len);
int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len);
set_priv_key()andset_pub_key()β used to set the raw private and public key data for anEVP_PKEY. They MUST return0on error, or1on success. They are called byEVP_PKEY_new_raw_private_key(3), andEVP_PKEY_new_raw_public_key(3)respectively.
size_t (*dirty) (const EVP_PKEY *pk);
void *(*export_to) (const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
dirty/dirty_cnt()β the page saysdirty_cnt()returns the internal key's dirty count (yep, the member right above is listed asdirty, keeping it exactly as written π). This can be used to synchronise different copies of the same keys.export_to()β exports the key material from the given key to a provider, through theEVP_KEYMGMT(3)interface, if that provider supports importing key material.
β¨ Functions β¨
EVP_PKEY_asn1_new()β creates and returns a newEVP_PKEY_ASN1_METHODobject, and associates the givenid,flags,pem_strandinfo.idis a NID,pem_stris the PEM type string,infois a descriptive string. The following flags are supported:ASN1_PKEY_SIGPARAM_NULLβ ifASN1_PKEY_SIGPARAM_NULLis set, then the signature algorithm parameters are given the typeV_ASN1_NULLby default, otherwise they will be given the typeV_ASN1_UNDEF(i.e. the parameter is omitted). SeeX509_ALGOR_set0(3)for more information.
EVP_PKEY_asn1_copy()β copies anEVP_PKEY_ASN1_METHODobject fromsrctodst. This function is not thread safe, it's recommended to only use this when initializing the application.EVP_PKEY_asn1_free()β frees an existingEVP_PKEY_ASN1_METHODpointed byameth. If the argument isNULL, nothing is done.EVP_PKEY_asn1_add0()β addsamethto the user defined stack of methods unless anotherEVP_PKEY_ASN1_METHODwith the same NID is already there. This function is not thread safe, it's recommended to only use this when initializing the application.EVP_PKEY_asn1_add_alias()β creates an alias with the NIDtofor theEVP_PKEY_ASN1_METHODwith NIDfromunless anotherEVP_PKEY_ASN1_METHODwith the same NID is already added. This function is not thread safe, it's recommended to only use this when initializing the application.EVP_PKEY_asn1_set_public(),EVP_PKEY_asn1_set_private(),EVP_PKEY_asn1_set_param(),EVP_PKEY_asn1_set_free(),EVP_PKEY_asn1_set_ctrl(),EVP_PKEY_asn1_set_item(),EVP_PKEY_asn1_set_siginf(),EVP_PKEY_asn1_set_check(),EVP_PKEY_asn1_set_public_check(),EVP_PKEY_asn1_set_param_check(),EVP_PKEY_asn1_set_security_bits(),EVP_PKEY_asn1_set_set_priv_key(),EVP_PKEY_asn1_set_set_pub_key(),EVP_PKEY_asn1_set_get_priv_key()andEVP_PKEY_asn1_set_get_pub_key()β these set the diverse methods of the givenEVP_PKEY_ASN1_METHODobject.EVP_PKEY_get0_asn1()β finds theEVP_PKEY_ASN1_METHODassociated with the keypkey.
πΈ HISTORY πΈ
Okay so bestie, tiny lil' glow-up moment in the history books πβ¨ β the signature of the pub_decode functional argument of EVP_PKEY_asn1_set_public() has changed in OpenSSL 3.0 so its pub parameter is now constified. That's it, that's the lore π«Ά (const-ified, iconic behavior honestly π
).
π COPYRIGHT π
Copyright 2017-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>. ππ
Fr fr, read the license before you go doing stuff β it's giving "required reading" energy, no cap πβ¨
And that's a wrap on this whole page, bestie!! π You made it to the very end β main character energy fr fr ππ
. This lil' beauty is from 3.5.0, dated 2026-09-25, and it lives at EVP_PKEY_ASN1_METHOD(3ossl) β go forth and slay those ASN.1 methods ππ¦π
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms