X509_NAME_GET_INDEX_BY_NID(3ossl)
π 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()andX509_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-1if not found. alsoX509_NAME_get_index_by_NID()can return-2if the supplied NID is invalid πX509_NAME_entry_count()β returns the total number of entries, and0for failure πX509_NAME_get_entry()β returns anX509_NAMEpointer to the requested entry orNULLif 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 π§
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
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms