EVP_PKEY_GET_ATTR(3ossl)
π EVP_PKEY_GET_ATTR π
okay bestie, this is the EVP_PKEY_GET_ATTR(3ossl) page from the OpenSSL manual β yes, the whole thing, third section, OpenSSL flavour, we're getting into it. It's all about the EVP_PKEY X509_ATTRIBUTE functions and honestly it's giving "I'm the plumbing behind PKCS12" energy. Let's go β¨
π NAME π
So the whole squad is: EVP_PKEY_get_attr, EVP_PKEY_get_attr_count, EVP_PKEY_get_attr_by_NID, EVP_PKEY_get_attr_by_OBJ, EVP_PKEY_delete_attr, EVP_PKEY_add1_attr, EVP_PKEY_add1_attr_by_OBJ, EVP_PKEY_add1_attr_by_NID, EVP_PKEY_add1_attr_by_txt β and collectively they're the EVP_PKEY X509_ATTRIBUTE functions. That's the whole name section, no gatekeeping π«Ά
πΈ SYNOPSIS πΈ
okay so first you gotta pull in the header, obviously:
#include <openssl/x509.h>
and then here's the full cast, character for character:
int EVP_PKEY_get_attr_count(const EVP_PKEY *key);
int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos);
int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
int lastpos);
X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc);
X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc);
int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr);
int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
const ASN1_OBJECT *obj, int type,
const unsigned char *bytes, int len);
int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
int nid, int type,
const unsigned char *bytes, int len);
int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
const char *attrname, int type,
const unsigned char *bytes, int len);
like, look at all those overloads. it's giving "we have options" π
π¦ DESCRIPTION π¦
So real talk: these functions are used by PKCS12. That's the vibe. That's who's calling. π
EVP_PKEY_get_attr_by_OBJ()β finds the location of the first matching objectobjin the key attribute list. The search starts at the position afterlastpos. If the returned value is positive, then it can be used on the next call toEVP_PKEY_get_attr_by_OBJ()as the value oflastposin order to iterate through the remaining attributes. Andlastposcan be set to any negative value on the first call, in order to start searching from the start of the attribute list. Basically: negative = start from the top, positive return = "keep going from here, bestie" πEVP_PKEY_get_attr_by_NID()β same asEVP_PKEY_get_attr_by_OBJ()except that it passes the numerical identifier (NID)nidassociated with the object. Peep<openssl/obj_mac.h>for a list ofNID_*. π·οΈEVP_PKEY_get_attr()β returns theX509_ATTRIBUTEobject at indexlocin the key attribute list.locshould be in the range from0toEVP_PKEY_get_attr_count() - 1. no off-by-one shenanigans, okay? πEVP_PKEY_delete_attr()β removes theX509_ATTRIBUTEobject at indexlocin the key attribute list. bye bestie πEVP_PKEY_add1_attr()β pushes a copy of the passed inX509_ATTRIBUTEobject to the key attribute list. A new key attribute list is created if required. An error occurs if eitherattrisNULL, or the attribute already exists. so no duplicates, and no nulls β the algorithm said what it said πEVP_PKEY_add1_attr_by_OBJ()β creates a newX509_ATTRIBUTEusingX509_ATTRIBUTE_set1_object()andX509_ATTRIBUTE_set1_data()to assign a newobjwith typetypeand databytesof lengthlen, and then pushes it to the key object's attribute list. Ifobjalready exists in the attribute list then an error occurs. iconic constructor chain, honestly π₯EVP_PKEY_add1_attr_by_NID()β same asEVP_PKEY_add1_attr_by_OBJ()except that it passes the numerical identifier (NID)nidassociated with the object. See<openssl/obj_mac.h>for a list ofNID_*. πEVP_PKEY_add1_attr_by_txt()β same asEVP_PKEY_add1_attr_by_OBJ()except that it passes a nameattrnameassociated with the object. See<openssl/obj_mac.h>for a list ofSN_*names. so yes, you can just type the name like a normal person π
β¨ RETURN VALUES β¨
vibe check on what comes back to you:
EVP_PKEY_get_attr_count()β returns the number of attributes in the key object attribute list, or-1if the attribute list isNULL.EVP_PKEY_get_attr_by_OBJ()β returns-1if either the list is empty OR the object is not found, otherwise it returns the location of the object in the list.EVP_PKEY_get_attr_by_NID()β same asEVP_PKEY_get_attr_by_OBJ(), except that it returns-2if thenidis not known by OpenSSL. yeah, a whole different negative number. iykyk πEVP_PKEY_get_attr()β returns either aX509_ATTRIBUTEorNULLif there is a error.EVP_PKEY_delete_attr()β returns either the removedX509_ATTRIBUTEorNULLif there is a error.EVP_PKEY_add1_attr(),EVP_PKEY_add1_attr_by_OBJ(),EVP_PKEY_add1_attr_by_NID()andEVP_PKEY_add1_attr_by_txt()β return1on success or0otherwise.
π NOTES π
okay this is the important one, don't skip it:
AEVP_PKEYobject's attribute list is initiallyNULL. All the above functions listed will return an error unlessEVP_PKEY_add1_attr()is called. All functions listed assume that the key is notNULL.
so like... the list starts empty-empty, you gotta seed it, and don't you dare pass a NULL key. no cap, that's the deal π«Ά
π§ SEE ALSO π§
X509_ATTRIBUTE(3)β go read that one too, bestie, it's the whole supporting cast π
π COPYRIGHT π
Copyright 2023-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>.
that's the whole page, no crumbs left π and for the record this lovely little doc is from 3.5.0, dated 2026-09-25 β so like, screenshot it, cite it, credit it. mwah π
π¦ 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