X509_ATTRIBUTE(3ossl)

πŸŽ€ X509_ATTRIBUTE πŸŽ€

okay bestie, welcome to X509_ATTRIBUTE(3ossl) β€” this is the OpenSSL page for X509 attribute functions, and honestly? it's giving foundational. we're gonna go through it together, no skipping, i promise. πŸ’…


✨ NAME ✨

so these are all the functions on this page, and yes we're listing every single one because they all deserve their moment:

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

collectively? X509 attribute functions. periodt. 🌈


🌸 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);
void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int atrtype,
                               void *data);
int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr);
ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr);
ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx);

like. look at that. iconic. πŸ’–


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so first things first: X509_ATTRIBUTE objects are used by many standards β€” we're talking X509, X509_REQ, PKCS12, PKCS8, PKCS7 and CMS. they get around, and honestly? good for them. πŸ‘‘

the X509_ATTRIBUTE object is what we use to represent the ASN.1 Attribute as defined in RFC 5280, i.e.

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

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

for example, 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 }

and here's the mapping bestie, write it down: in OpenSSL, AttributeType maps to an ASN1_OBJECT object, and AttributeValue maps to a list of ASN1_TYPE objects. that's the whole vibe. ✨

the following functions are the ones used for X509_ATTRIBUTE objects:


πŸ’œ X509at_get_attr_by_OBJ()

finds the location of the first matching object obj in a list of attributes sk. the search kicks off at the position after lastpos. if what you get back is positive, then you can 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. and on that very first call? set lastpos to any negative value and it'll start searching from the top of the list. easy. 🫢

πŸ’œ X509at_get_attr_by_NID()

same energy as X509at_get_attr_by_OBJ(), except it hands over the numerical identifier (NID) nid associated with the object instead. peek at <openssl/obj_mac.h> for the full list of NID_*.

πŸ’œ X509at_get_attr()

returns the X509_ATTRIBUTE object sitting at index loc in the list of attributes x. loc needs to be in the range from 0 to X509at_get_attr_count() - 1. no out-of-bounds shenanigans, bestie. πŸ’€

πŸ’œ X509at_delete_attr()

removes the X509_ATTRIBUTE object at index loc in the list of attributes x. bye girl! πŸ‘‹

πŸ’œ X509at_add1_attr()

pushes a copy of the passed in X509_ATTRIBUTE object onto the list x. both x and attr must be non NULL or an error will occur. if *x is NULL then a brand new list gets created, otherwise it uses the list you passed in. and an error will occur if an existing attribute (with the same attribute type) already exists in the attribute list. no duplicates allowed, we don't do that here. 🚫

πŸ’œ 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, 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. and if obj already exists in the attribute list, then an error occurs. 🚨

πŸ’œ 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 was already holding an existing ASN1_OBJECT then it gets freed β€” out with the old, in with the new. πŸ’… an error occurs if either attr or obj are NULL, or if the passed in obj 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. 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 passed in data must be in the format required for that object type or an error will occur. if len is not -1 then internally ASN1_STRING_type_new() is used with the passed in attrtype. and if attrtype is 0 the call does nothing except return 1. lowkey the chillest branch of this whole function. 😌

πŸ’œ X509_ATTRIBUTE_create()

creates a new X509_ATTRIBUTE using the 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 passed in attribute attr OR *attr is NULL then a new X509_ATTRIBUTE will be returned, otherwise the passed in X509_ATTRIBUTE is used. heads up though β€” note that 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. πŸ‘€

πŸ’œ X509_ATTRIBUTE_create_by_NID()

same as X509_ATTRIBUTE_create_by_OBJ(), except 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 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. counting is self care. πŸ”’

πŸ’œ 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. ⚠️

πŸ’œ 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, standing in the corner, being decorative. πŸŽ€ 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. that's three whole ways to mess up, so be careful bestie. 😭

πŸ’œ 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 attributes 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 the attribute is not found. 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 many rules but honestly? it's just keeping you safe. πŸ¦‹


and that's part 1 bestie β€” there's more coming so don't go anywhere πŸ’–

πŸ’– RETURN VALUES πŸ’–

okay bestie, this is the part where we find out what all these lil functions hand back to you. spoiler: it's a lot of NULL and a lot of negative numbers, which is honestly so real of them. 🫢

  • X509at_get_attr_count() β€” gives you the number of attributes chillin' in the list x, or -1 if x is NULL. (yeah, -1 is the "girl, there's no list" signal)
  • X509at_get_attr_by_OBJ() β€” returns -1 if either the list is empty OR the object is not found. Otherwise? It hands you the location of the object in the list. πŸ”
  • X509at_get_attr_by_NID() β€” basically X509at_get_attr_by_OBJ()'s twin, except it returns -2 if the nid is not known by OpenSSL. two whole different sad numbers, iconic
  • X509at_get_attr() β€” returns either an X509_ATTRIBUTE or NULL if there's an error. πŸ’€
  • X509at_delete_attr() β€” returns either the removed X509_ATTRIBUTE or NULL if there is a error. (yes, it gives you back the thing it just yeeted, cute)
  • X509_ATTRIBUTE_count() β€” returns -1 on error, otherwise the number of ASN1_TYPE elements. πŸ”’
  • X509_ATTRIBUTE_get0_type() β€” returns NULL on error, otherwise an ASN1_TYPE object. ✨
  • X509_ATTRIBUTE_get0_data() β€” returns NULL if an error occurs, otherwise it returns the data associated with an ASN1_TYPE object. πŸŽ€
  • X509_ATTRIBUTE_set1_object() and X509_ATTRIBUTE_set1_data() β€” return 1 on success, or 0 otherwise. classic 1-or-0 behavior, we love a binary moment
  • 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 is a error. 🌸
  • X509at_add1_attr(), X509at_add1_attr_by_OBJ(), X509at_add1_attr_by_NID() and X509at_add1_attr_by_txt() β€” return NULL on error, otherwise they return a list of X509_ATTRIBUTE. πŸ¦‹
  • 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 only, bestie, don't get greedy
ngl, the pattern here is: NULL = something went wrong, negative number = something went wrong but make it quirky, and 1/0 = the classic success/fail duo. always check your returns, fr fr. πŸ™ˆ

πŸ‘‘ SEE ALSO πŸ‘‘

go read these next, they're the friend group of this page:


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.

yes bestie, you actually have to follow the license, this is not a drill πŸ’…

and that's a wrap on X509_ATTRIBUTE(3ossl)!! πŸŽ€ this beauty is from OpenSSL 3.5.9, dated 2026-09-29, so go forth and handle your attributes responsibly. mwah. πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from