X509_NAME_GET_INDEX_BY_NID(3ossl)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… X509_NAME_GET_INDEX_BY_NID(3ossl) πŸ’…

okay so this is the OpenSSL 3ossl girlies' guide to poking around inside an X509_NAME πŸŽ€ β€” like, the whole "who even are you" of a certificate. scroll responsibly, bestie 🫢


✨ NAME ✨

X509_NAME_get_index_by_NID, X509_NAME_get_index_by_OBJ, X509_NAME_get_entry, X509_NAME_entry_count, X509_NAME_get_text_by_NID, X509_NAME_get_text_by_OBJ β€” literally the X509_NAME lookup and enumeration functions, the whole squad πŸ’…


🌸 SYNOPSIS 🌸

#include <openssl/x509.h>

int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos);
int X509_NAME_get_index_by_OBJ(const X509_NAME *name,
                               const ASN1_OBJECT *obj, int lastpos);

int X509_NAME_entry_count(const X509_NAME *name);
X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc);

int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
                              char *buf, int len);
int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
                              char *buf, int len);

πŸ¦‹ DESCRIPTION πŸ¦‹

so these functions let you examine an X509_NAME structure. and like, X509_NAME is the same thing as the Name type defined in RFC2459 (and in other places too, iykyk) β€” it's what you see in certificate subject and issuer names, bestie. main character of every cert fr fr πŸ‘‘

πŸ’– the index girlies

X509_NAME_get_index_by_NID() and X509_NAME_get_index_by_OBJ() retrieve the next index matching nid or obj after lastpos. lowkey important: lastpos should initially be set to -1 🫢. if there are no more entries, -1 is returned. and if nid is invalid (doesn't correspond to a valid OID) then -2 is returned β€” yes, a whole separate vibe check πŸ’€

πŸŽ€ the counting one

X509_NAME_entry_count() returns the total number of entries in name. slay, that's it, that's the function ✨

🌈 the getting one

X509_NAME_get_entry() retrieves the X509_NAME_ENTRY from name corresponding to index loc. acceptable values for loc run from 0 to (X509_NAME_entry_count(name) - 1). the value returned is an internal pointer which must not be freed β€” not me warning you, okay, that's just how it is πŸ™ˆ

πŸ“ the text ones (read the NOTES before you get attached)

X509_NAME_get_text_by_NID(), X509_NAME_get_text_by_OBJ() retrieve the "text" from the first entry in name which matches nid or obj; if no such entry exists, -1 is returned. at most len bytes will be written and the text written to buf will be null terminated. the length of the output string written is returned excluding the terminating null. and if buf is NULL then the amount of space needed in buf (excluding the final null) is returned. honestly iconic sizing behaviour, ngl πŸ‘‘


πŸ’€ NOTES πŸ’€

X509_NAME_get_text_by_NID() and X509_NAME_get_text_by_OBJ() should be considered deprecated β€” they have various limitations which make them of minimal use in practice. like, actually deprecated, not "deprecated" in a cute way 😭

they can only find the first matching entry and will copy the contents of the field verbatim: this can be highly confusing if the target is a multicharacter string type like a BMPString or a UTF8String. it's giving "verbatim copy of a multibyte string" energy, and that energy is chaos πŸ’€

for a more general solution, X509_NAME_get_index_by_NID() or X509_NAME_get_index_by_OBJ() should be used followed by X509_NAME_get_entry() on any matching indices and then the various X509_NAME_ENTRY utility functions on the result. that's the pipeline, bestie, memorize it 🫢

the list of all relevant NID_* and OBJ_* codes can be found in the source code header files <openssl/obj_mac.h> and/or <openssl/objects.h> πŸ“š

applications which could pass invalid NIDs to X509_NAME_get_index_by_NID() should check for the return value of -2. alternatively the NID validity can be determined first by checking OBJ_nid2obj(nid) is not NULL. do your due diligence, queen πŸ‘‘


⭐ RETURN VALUES ⭐

  • X509_NAME_get_index_by_NID(), X509_NAME_get_index_by_OBJ() β€” return the index of the next matching entry, or -1 if not found. also X509_NAME_get_index_by_NID() can return -2 if the supplied NID is invalid πŸ’…
  • X509_NAME_entry_count() β€” returns the total number of entries, and 0 for failure πŸ™ˆ
  • X509_NAME_get_entry() β€” returns an X509_NAME pointer to the requested entry or NULL if the index is invalid πŸ¦‹

πŸ”₯ EXAMPLES πŸ”₯

process all the entries β€” the full sweep, no skips:

int i;
X509_NAME_ENTRY *e;

for (i = 0; i < X509_NAME_entry_count(nm); i++) {
    e = X509_NAME_get_entry(nm, i);
    /* Do something with e */
}

process all commonName entries β€” the lastpos loop, lowkey the main event πŸ’–:

int lastpos = -1;
X509_NAME_ENTRY *e;

for (;;) {
    lastpos = X509_NAME_get_index_by_NID(nm, NID_commonName, lastpos);
    if (lastpos == -1)
        break;
    e = X509_NAME_get_entry(nm, lastpos);
    /* Do something with e */
}

🌟 SEE ALSO 🌟

ERR_get_error(3), d2i_X509_NAME(3) β€” go read those too, bestie, they're right there πŸŽ€


Copyright 2002-2021 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>.


aaaand that's the page, bestie 🌸 this was OpenSSL 3.5.6, dated 2026-04-07, living under the X509_NAME_GET_INDEX_BY_NID(3ossl) era β€” go forth and enumerate those names, you absolute icon πŸ’œβœ¨

πŸ“¦ where this page comes from