X509_ATTRIBUTE(3SSL)

✨ X509_ATTRIBUTE ✨

okay bestie so this is part of the OpenSSL manual, section 3SSL, and it's literally all about the X509_ATTRIBUTE family of functions πŸ’– we're talking attributes, the ASN.1 kind, the ones hiding inside X509, X509_REQ, PKCS12, PKCS8, PKCS7 and CMS. them. no cap.


πŸŽ€ NAME πŸŽ€

X509_ATTRIBUTE, X509at_get_attr, X509at_get_attr_count, X509at_get_attr_by_NID, X509at_get_attr_by_OBJ, X509at_delete_attr, X509at_add1_attr, X509at_add1_attr_by_OBJ, X509at_add1_attr_by_NID, X509at_add1_attr_by_txt, X509at_get0_data_by_OBJ, X509_ATTRIBUTE_create, X509_ATTRIBUTE_create_by_NID, X509_ATTRIBUTE_create_by_OBJ, X509_ATTRIBUTE_create_by_txt, X509_ATTRIBUTE_set1_object, X509_ATTRIBUTE_set1_data, X509_ATTRIBUTE_count, X509_ATTRIBUTE_get0_data, X509_ATTRIBUTE_get0_object, X509_ATTRIBUTE_get0_type β€” a whole friend group of X509 attribute functions 🫢


🌸 SYNOPSIS 🌸

#include <openssl/x509.h>

typedef struct x509_attributes_st X509_ATTRIBUTE;

int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x);
int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
                           int lastpos);
int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
                           const ASN1_OBJECT *obj, int lastpos);
X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc);
X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc);
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
                                           X509_ATTRIBUTE *attr);
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
                                                  **x, const ASN1_OBJECT *obj,
                                                  int type,
                                                  const unsigned char *bytes,
                                                  int len);
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
                                                  **x, int nid, int type,
                                                  const unsigned char *bytes,
                                                  int len);
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
                                                  **x, const char *attrname,
                                                  int type,
                                                  const unsigned char *bytes,
                                                  int len);
void *X509at_get0_data_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *x,
                              const ASN1_OBJECT *obj, int lastpos, int type);
X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value);
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
                                             int atrtype, const void *data,
                                             int len);
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
                                             const ASN1_OBJECT *obj,
                                             int atrtype, const void *data,
                                             int len);
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
                                             const char *atrname, int type,
                                             const unsigned char *bytes,
                                             int len);
int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj);
int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
                             const void *data, int len);
const void *X509_ATTRIBUTE_get0_data(const X509_ATTRIBUTE *attr, int idx,
                                     int atrtype, void *data);
int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr);
const ASN1_OBJECT *X509_ATTRIBUTE_get0_object(const X509_ATTRIBUTE *attr);
const ASN1_TYPE *X509_ATTRIBUTE_get0_type(const X509_ATTRIBUTE *attr, int idx);

πŸ’… DESCRIPTION πŸ’…

X509_ATTRIBUTE objects get used by SO many standards bestie, like X509, X509_REQ, PKCS12, PKCS8, PKCS7 and CMS. it's giving universal.

so the X509_ATTRIBUTE object is the glow-up of the ASN.1 Attribute, exactly as defined in RFC 5280, which is literally:

Attribute ::= SEQUENCE {
  type             AttributeType,
  values    SET OF AttributeValue }

AttributeType ::= OBJECT IDENTIFIER
AttributeValue ::= ANY -- DEFINED BY AttributeType

and for the vibe check, CMS defines the signing-time attribute like this:

id-signingTime OBJECT IDENTIFIER ::= { iso(1) member-body(2)
    us(840) rsadsi(113549) pkcs(1) pkcs9(9) 5 }

SigningTime ::= Time

Time ::= CHOICE {
  utcTime UTCTime,
  generalizedTime GeneralizedTime }

in OpenSSL, AttributeType maps to an ASN1_OBJECT object and AttributeValue maps to a list of ASN1_TYPE objects. that's the whole lore. 🀝

the following functions are the ones that serve X509_ATTRIBUTE objects. let's go, one by one, no skips. πŸ’–


  • X509at_get_attr_by_OBJ() β€” finds the location of the first matching object obj in a list of attributes sk πŸ” the search starts at the position after lastpos. if what you get back is positive, you can totally feed it into the next call to X509at_get_attr_by_OBJ() as the value of lastpos so you can iterate through all the remaining attributes, slay. and lastpos can be set to any negative value on that first call, which makes the search start from the very beginning of the list. main character search energy ✨
  • X509at_get_attr_by_NID() β€” same situation as X509at_get_attr_by_OBJ() except it passes the numerical identifier (NID) nid that's associated with the object πŸ’« see <openssl/obj_mac.h> for the list of NID_*.
  • X509at_get_attr() β€” returns the X509_ATTRIBUTE object sitting at index loc in the list of attributes x πŸ‘‘ loc should be in the range from 0 to X509at_get_attr_count() - 1.
  • X509at_delete_attr() β€” removes the X509_ATTRIBUTE object at index loc in the list of attributes x πŸ—‘οΈ bye bestie.
  • X509at_add1_attr() β€” pushes a copy of the X509_ATTRIBUTE object you pass in onto the list x 🫢 both x and attr must be non NULL or an error will occur, so like, don't be messy. if *x is NULL then a brand new list gets created, otherwise it uses the list you passed in. and heads up: an error will occur if an existing attribute (with the same attribute type) already exists in the attribute list 😭
  • X509at_add1_attr_by_OBJ() β€” creates a new X509_ATTRIBUTE using X509_ATTRIBUTE_set1_object() and X509_ATTRIBUTE_set1_data() to assign a new obj with type type and data bytes of length len, and then pushes it onto the attribute list x 🌟 both x and attr must be non NULL or an error will occur. if *x is NULL then a new attribute list is created. if obj already exists in the attribute list then an error occurs, periodt. πŸ’€
  • X509at_add1_attr_by_NID() β€” same as X509at_add1_attr_by_OBJ() except it passes the numerical identifier (NID) nid associated with the object. see <openssl/obj_mac.h> for a list of NID_* πŸ¦‹
  • X509at_add1_attr_by_txt() β€” same as X509at_add1_attr_by_OBJ() except it passes a name attrname associated with the object. see <openssl/obj_mac.h> for a list of SN_* names 🌸
  • X509_ATTRIBUTE_set1_object() β€” assigns an ASN1_OBJECT obj to the attribute attr πŸŽ€ if attr already contained an existing ASN1_OBJECT then that one gets freed, rip. an error occurs if either attr or obj are NULL, or if the obj you passed in cannot be duplicated.
  • X509_ATTRIBUTE_set1_data() β€” pushes a new ASN1_TYPE object onto the attr attributes list πŸ’₯ the new object is assigned a copy of the data in data of size len. okay so if attrtype has the flag MBSTRING_FLAG set, then a table lookup using the attr attributes NID is used to set an ASN1_STRING using ASN1_STRING_set_by_NID(), and the data you passed in must be in the format required for that object type or an error will occur β€” no improvising. if len is not -1 then internally ASN1_STRING_type_new() is used with the attrtype you passed in. and if attrtype is 0, the call does literally nothing except return 1 πŸ™ˆ
  • X509_ATTRIBUTE_create() β€” creates a new X509_ATTRIBUTE using nid to set the ASN1_OBJECT OID and the atrtype and value to set the ASN1_TYPE ✨
  • X509_ATTRIBUTE_create_by_OBJ() β€” uses X509_ATTRIBUTE_set1_object() and X509_ATTRIBUTE_set1_data() to assign a new obj with type atrtype and data data of length len 🫢 if the attribute you passed in, attr OR *attr, is NULL then a new X509_ATTRIBUTE will be returned, otherwise the X509_ATTRIBUTE you passed in gets used. and note this bestie: the ASN1_OBJECT obj is pushed onto the attributes existing list of objects, which could be an issue if the attributes <ASN1_OBJECT> was different 😬 lowkey a trap.
  • X509_ATTRIBUTE_create_by_NID() β€” same as X509_ATTRIBUTE_create_by_OBJ() except that it passes the numerical identifier (NID) nid associated with the object. see <openssl/obj_mac.h> for a list of NID_* 🌈
  • X509_ATTRIBUTE_create_by_txt() β€” same as X509_ATTRIBUTE_create_by_OBJ() except that it passes a name atrname associated with the object. see <openssl/obj_mac.h> for a list of SN_* names 🧚
  • X509_ATTRIBUTE_count() β€” returns the number of ASN1_TYPE objects in an attribute attr πŸ”’ count 'em.
  • X509_ATTRIBUTE_get0_type() β€” returns the ASN1_TYPE object at index idx in the attribute list attr πŸ’œ idx should be in the range of 0 to X509_ATTRIBUTE_count() - 1 or an error will occur, so stay in your lane.
  • X509_ATTRIBUTE_get0_data() β€” returns the data of an ASN1_TYPE object at index idx in the attribute attr πŸ“ data is unused and can be set to NULL, it's just there for the vibes. an error will occur if the attribute type atrtype does not match the type of the ASN1_TYPE object at index idx OR if atrtype is either V_ASN1_BOOLEAN or V_ASN1_NULL OR if the idx is not in the range 0 to X509_ATTRIBUTE_count() - 1. three ways to fail, pick your fighter 😭
  • X509at_get0_data_by_OBJ() β€” finds the first attribute in an attribute list x that matches the obj, starting at index lastpos, and returns the data retrieved from the found attribute's first ASN1_TYPE object πŸ”₯ an error will occur if the attribute type type does not match the type of the ASN1_TYPE object OR if type is either V_ASN1_BOOLEAN or V_ASN1_NULL OR if the attribute is not found. also if lastpos is less than -1 then an error will occur if there are multiple objects in the list x that match obj. and if lastpos is less than -2 then an error will occur if there is more than one ASN1_TYPE object in the found attribute. so like, very strict. very demure. very mindful. πŸ‘‘

okay warning bestie: those V_ASN1_BOOLEAN and V_ASN1_NULL cases are real and they will bite you. it's not a maybe. πŸ’€

πŸ’– RETURN VALUES πŸ’–

okay bestie, this is the part where we find out what you actually get back from each of these, so pay attention, this is giving important πŸ’…

  • X509at_get_attr_count() β€” hands you the number of attributes chillin' in the list x, or -1 if x is NULL. like if you passed nothing, you get nothing back, but with a minus sign for drama 😭
  • X509at_get_attr_by_OBJ() β€” returns -1 if the list is empty OR the object just isn't in there (rude, honestly). otherwise it returns the location of the object in the list. so -1 means "nope, not here" twice over, and you literally cannot tell which one it was, that's just the vibe 🫢
  • X509at_get_attr_by_NID() β€” same energy as X509at_get_attr_by_OBJ(), except it returns -2 if the nid isn't even known by OpenSSL. so like, -2 is "girl what nid is that, never heard of her" πŸ’€
  • X509at_get_attr() β€” returns either an X509_ATTRIBUTE or NULL if there's an error. simple. clean. iconic.
  • X509at_delete_attr() β€” returns either the removed X509_ATTRIBUTE or NULL if there's an error. it hands you the thing it just deleted, which is lowkey iconic behavior ✨
  • X509_ATTRIBUTE_count() β€” returns -1 on error, otherwise the number of ASN1_TYPE elements. yes bestie, -1 is the error girl again, she's everywhere πŸŽ€
  • X509_ATTRIBUTE_get0_type() β€” returns NULL on error, otherwise an ASN1_TYPE object. NULL = something went wrong, no cap.
  • X509_ATTRIBUTE_get0_data() β€” returns NULL if an error occurs, otherwise it gives you the data associated with an ASN1_TYPE object. the actual payload, the juicy part πŸ’–
  • X509_ATTRIBUTE_set1_object() and X509_ATTRIBUTE_set1_data() β€” return 1 on success, or 0 otherwise. classic 1-or-0 binary situation, no in-between, no nuance 🌸
  • X509_ATTRIBUTE_create(), X509_ATTRIBUTE_create_by_OBJ(), X509_ATTRIBUTE_create_by_NID() and X509_ATTRIBUTE_create_by_txt() β€” return either an X509_ATTRIBUTE on success, or NULL if there's an error. make it, or don't πŸ¦‹
  • X509at_add1_attr(), X509at_add1_attr_by_OBJ(), X509at_add1_attr_by_NID() and X509at_add1_attr_by_txt() β€” return NULL on error, otherwise you get back a list of X509_ATTRIBUTE. note the NULL here means error, not "empty", don't get it twisted πŸ‘‘
  • X509at_get0_data_by_OBJ() β€” returns the data retrieved from the found attribute's first ASN1_TYPE object, or NULL if an error occurs. first one, bestie, not all of them, just the first πŸ“
pro tip from me to you: the negative numbers in this section are not vibes, they're error codes. -1 and -2 are doing a lot of heavy lifting here and you gotta know which function returns which. memorize the chart, slay the day πŸ’…

πŸ”₯ SEE ALSO πŸ”₯

go read these next, they're all in the group chat:


Copyright 2023-2026 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 a wrap on X509_ATTRIBUTE(3SSL) bestie!! this whole thing was 4.0.3, dated 2026-09-29, and you absolutely ate reading all of it πŸ’–βœ¨πŸ† go forth and get your attribute data, you main character 🫢🌈⭐

πŸ“¦ where this page comes from