EVP_PKEY_ASN1_METHOD(3ossl)
β¨ EVP_PKEY_ASN1_METHOD π
okay bestie, welcome to OpenSSL lore, this is the EVP_PKEY_ASN1_METHOD(3ossl) page and it's a whole vibe. this one is long so it comes in three parts β we're on part one rn, so buckle up π
πΈ NAME πΈ
so like, this page is all about EVP_PKEY_ASN1_METHOD and the little family of functions that create, copy, free, register and decorate it. it's giving "manipulating and registering EVP_PKEY_ASN1_METHOD structure" energy, no cap.
here's literally every single one of them, bestie, so nobody gets left out π
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 the whole point of the crew is manipulating and registering EVP_PKEY_ASN1_METHOD structure. iconic. periodt. π«Ά
π SYNOPSIS π
ok so this is the part where you copy-paste, don't freestyle it, bestie β every character matters:
#include <openssl/evp.h>
typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
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);
ok that's a LOT of function pointers and honestly it's giving "the whole toolbox" π₯ but don't stress, we're gonna walk through every single one, bestie. keep scrolling, part two is coming and it's lowkey the juiciest part πβ¨
π DESCRIPTION π
okay bestie so EVP_PKEY_ASN1_METHOD is this whole structure that basically holds a whole lil toolbox of ASN.1 conversion, printing and info methods for one specific public key algorithm. it's giving "everything this algorithm needs to talk ASN.1" energy. π
and here's the tea: these EVP_PKEY_ASN1_METHOD objects live in two places:
- one is a built-in array β that's the standard methods for the different algorithms, the OG squad β¨
- the other is a stack of user-defined, application-specific methods, and you get to mess with those using
EVP_PKEY_asn1_add0(3). π«Ά
β¨ Methods β¨
these methods are the underlying implementations of a particular public key algorithm, and they're represented by the EVP_PKEY object. iykyk.
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);
so pub_decode() and pub_encode() get called to decode / encode X509_PUBKEY ASN.1 parameters to / from pk. they MUST return 0 on error and 1 on success, no cap. they get called by X509_PUBKEY_get0(3) and X509_PUBKEY_set(3). π
pub_cmp() shows up when two public keys need to be compared. it MUST return 1 when the keys are equal, 0 otherwise. it gets called by EVP_PKEY_eq(3).
pub_print() is the one that prints a public key in humanly readable text to out, indented indent spaces. it MUST return 0 on error, 1 on success, and it's called by EVP_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() and priv_encode() decode / encode the PKCS8_PRIV_KEY_INFO form private key to / from pk. MUST return 0 on error, 1 on success. they're called by EVP_PKCS82PKEY(3) and EVP_PKEY2PKCS8(3). π
priv_print() prints a private key in humanly readable text to out, indented indent spaces. MUST return 0 on error, 1 on success, and EVP_PKEY_print_private(3) is the one calling it.
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, and EVP_PKEY_get_size(3) calls it.
pkey_bits() returns the key size in bits, and EVP_PKEY_get_bits(3) calls it. (the third one's just chillin' in the signature block, bestie, that's the whole list fr fr)
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() and param_encode() decode / encode DER formatted parameters to / from pk. MUST return 0 on error, 1 on success. they're called by PEM_read_bio_Parameters(3) and the file: OSSL_STORE_LOADER(3). π¦
param_missing() returns 0 if a key parameter is missing, otherwise 1. it's called by EVP_PKEY_missing_parameters(3).
param_copy() copies key parameters from from to to. MUST return 0 on error, 1 on success. called by EVP_PKEY_copy_parameters(3).
param_cmp() compares the parameters of keys a and b. it MUST return 1 when the keys are equal, 0 when not equal, or a negative number on error. called by EVP_PKEY_parameters_eq(3).
param_print() prints the private key parameters in humanly readable text to out, indented indent spaces. MUST return 0 on error, 1 on success. called by EVP_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 to out, indented indent spaces. sigalg holds the exact signature algorithm. and bestie, listen β if the signature in sig doesn't correspond to what this method expects, then X509_signature_dump() must be used as a last resort. it MUST return 0 on error, 1 on success, and it's called by X509_signature_print(3). π
void (*pkey_free) (EVP_PKEY *pkey);
pkey_free() helps free the internals of pkey. it's called by EVP_PKEY_free(3), EVP_PKEY_set_type(3), EVP_PKEY_set_type_str(3), and EVP_PKEY_assign(3).
int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
pkey_ctrl() adds extra algorithm-specific control. it gets called by EVP_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), ... β the list literally trails off, iconic behavior honestly. π
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() and old_priv_encode() decode / encode the private key pkey from / to a DER formatted array. these are exclusively used to help decode / encode older (pre PKCS#8) PEM formatted encrypted private keys. old_priv_decode() MUST return 0 on error, 1 on success. old_priv_encode() MUST return the same kind of values as i2d_PrivateKey(). they're called by d2i_PrivateKey(3) and i2d_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() and item_verify() are what make it possible to have algorithm-specific signatures and algorithm-specific verification of them. π
item_sign() MUST return one of:
<=0β error π1β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β error π1β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() is used to set custom X509_SIG_INFO parameters. it MUST return 0 on error, or 1 on success. it's called as part of X509_check_purpose(3), X509_check_ca(3) and X509_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() and pkey_param_check() are the validity checkers β for the key-pair, the public component and the parameters, respectively. they MUST return 0 for an invalid key, or 1 for a valid key. they're called by EVP_PKEY_check(3), EVP_PKEY_public_check(3) and EVP_PKEY_param_check(3) respectively. vibe check but make it cryptography π
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() and set_pub_key() set the raw private and public key data for an EVP_PKEY. they MUST return 0 on error, or 1 on success. they're called by EVP_PKEY_new_raw_private_key(3) and EVP_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_cnt() returns the internal key's dirty count. 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 the EVP_KEYMGMT(3) interface β but only if that provider supports importing key material, bestie. no importing, no exporting, that's the rule.
β¨ Functions β¨
EVP_PKEY_asn1_new() creates and returns a brand new EVP_PKEY_ASN1_METHOD object, and associates the given id, flags, pem_str and info. so: id is a NID, pem_str is the PEM type string, info is a descriptive string. π
the following flags are supported:
ASN1_PKEY_SIGPARAM_NULL
if ASN1_PKEY_SIGPARAM_NULL is set, then the signature algorithm parameters are given the type V_ASN1_NULL by default β otherwise they'll be given the type V_ASN1_UNDEF (i.e. the parameter is omitted). see X509_ALGOR_set0(3) for more info. π¦
EVP_PKEY_asn1_copy() copies an EVP_PKEY_ASN1_METHOD object from src to dst. this function is not thread safe, so it's recommended to only use this when initializing the application. π
EVP_PKEY_asn1_free() frees an existing EVP_PKEY_ASN1_METHOD pointed by ameth. if the argument is NULL, nothing is done. simple as that.
EVP_PKEY_asn1_add0() adds ameth to the user defined stack of methods β unless another EVP_PKEY_ASN1_METHOD with 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 NID to for the EVP_PKEY_ASN1_METHOD with NID from β unless another EVP_PKEY_ASN1_METHOD with 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() and EVP_PKEY_asn1_set_get_pub_key() set the diverse methods of the given EVP_PKEY_ASN1_METHOD object. that's a whole lotta setters, and honestly? obsessed. π
EVP_PKEY_get0_asn1() finds the EVP_PKEY_ASN1_METHOD associated with the key pkey. π«Ά
π RETURN VALUES π
EVP_PKEY_asn1_new() returns NULL on error, or a pointer to an EVP_PKEY_ASN1_METHOD object otherwise.
EVP_PKEY_asn1_add0() and EVP_PKEY_asn1_add_alias() return 0 on error, or 1 on success.
EVP_PKEY_get0_asn1() returns NULL on error, or a pointer to a constant EVP_PKEY_ASN1_METHOD object otherwise. β¨
β¨ HISTORY β¨
okay so history lesson time, bestie πβ¨. 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 the whole glow-up π
. If your old code was giving "why is this pointer not matching" energy, now you know π«Ά.
π 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.
And that's the page, bestie π«Άβ¨. This one comes from OpenSSL 3.5.9, dated 2026-09-29, and it's literally EVP_PKEY_ASN1_METHOD(3ossl) β iconic, no cap ππ.
π¦ 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