ASN1_STRING_LENGTH(3)
π¦ ASN1_STRING_LENGTH(3) β ASN1_STRING utility functions (Library Functions Manual) π¦
β¨ NAME β¨
ASN1_STRING_cmp, ASN1_OCTET_STRING_cmp, ASN1_STRING_data, ASN1_STRING_dup, ASN1_OCTET_STRING_dup, ASN1_STRING_get0_data, ASN1_STRING_length, ASN1_STRING_length_set, ASN1_STRING_set0, ASN1_STRING_set, ASN1_OCTET_STRING_set, ASN1_STRING_copy, ASN1_STRING_to_UTF8, ASN1_STRING_type β ASN1_STRING utility functions π
Okay so these are the ASN1 string utility girlies. They all hang out around ASN1_STRING, and yes, they are absolutely main character energy in the ASN.1 world.
π SYNOPSIS π
#include <openssl/asn1.h>
int
ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b);
int
ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a,
const ASN1_OCTET_STRING *b);
unsigned char *
ASN1_STRING_data(ASN1_STRING *x);
ASN1_STRING *
ASN1_STRING_dup(const ASN1_STRING *a);
ASN1_OCTET_STRING *
ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *a);
const unsigned char *
ASN1_STRING_get0_data(const ASN1_STRING *x);
int
ASN1_STRING_length(const ASN1_STRING *x);
void
ASN1_STRING_length_set(ASN1_STRING *x, int len);
void
ASN1_STRING_set0(ASN1_STRING *str, void *data, int len);
int
ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);
int
ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const unsigned char *data,
int len);
int
ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *src);
int
ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in);
int
ASN1_STRING_type(const ASN1_STRING *x);
πΈ DESCRIPTION πΈ
These functions manipulate ASN1_STRING structures. Think of ASN1_STRING as the it-girl struct and these functions as her very specific, very useful besties.
ASN1_STRING_cmp()β compares the type, the length, and the content ofaandb. It's a full three-part vibe check, no skipping.ASN1_OCTET_STRING_cmp()β does exactly the same asASN1_STRING_cmp()without providing any type safety. Same behavior, but lowkey feral about types.ASN1_STRING_data()β is similar toASN1_STRING_get0_data()except that the returned value is not constant. This function is deprecated. Applications should useASN1_STRING_get0_data()instead. πASN1_STRING_dup()β allocates a newASN1_STRINGobject and copies the type, length, data, and flags fromainto it. Full clone behavior, no cap.ASN1_OCTET_STRING_dup()β does exactly the same asASN1_STRING_dup()without providing any type safety.ASN1_STRING_get0_data()β returns an internal pointer to the data ofx. It should not be freed or modified in any way. Like, do not touch it, bestie.ASN1_STRING_length()β returns the length attribute ofx, measured in bytes.ASN1_STRING_length_set()β sets the length attribute ofxtolen. It may putxinto an inconsistent internal state. π¨ASN1_STRING_set0()β frees any data stored instr, sets the length attribute tolenbytes, and sets the data attribute todata, transferring ownership, without doing any validation. It's giving "trust me bro" energy.ASN1_STRING_set()β sets the length attribute ofstrtolenand copies that number of bytes fromdataintostr, overwriting any previous data. Iflenis-1, thenstrlen(data)is used instead oflen. IfdataisNULL, the content ofstrremains uninitialized; that is not considered an error unlesslenis negative.ASN1_OCTET_STRING_set()β does exactly the same asASN1_STRING_set()without providing any type safety.ASN1_STRING_copy()β copies the length and data ofsrcintodstusingASN1_STRING_set()and changes the type and flags ofdstto match the type and flags ofsrc.ASN1_STRING_to_UTF8()β converts the stringinto UTF-8 format. The converted data is copied into a newly allocated buffer*out. The buffer*outshould be freed usingfree(3).ASN1_STRING_type()β returns the type ofx. If the bitV_ASN1_NEGis set in the return value,xis an ASN.1INTEGERorENUMERATEDobject with a negative value.
Almost all ASN.1 types are represented as ASN1_STRING structures. Other types such as ASN1_OCTET_STRING are simply typedefed to ASN1_STRING and the functions call the ASN1_STRING equivalents. ASN1_STRING is also used for some CHOICE types which consist entirely of primitive string types such as DirectoryString and Time.
π«Ά These functions should not be used to examine or modifyASN1_INTEGERorASN1_ENUMERATEDtypes: the relevantINTEGERorENUMERATEDutility functions should be used instead. That's the boundary, bestie. Respect it.
In general it cannot be assumed that the data returned by ASN1_STRING_get0_data() and ASN1_STRING_data() is NUL terminated, and it may contain embedded NUL characters. The format of the data depends on the string type: for example for an IA5String the data contains ASCII characters, for a BMPString two bytes per character in big endian format, and for a UTF8String UTF-8 characters.
Similar care should be taken to ensure the data is in the correct format when calling ASN1_STRING_set() or ASN1_STRING_set0(). Format check before you wreak havoc, periodt.
π RETURN VALUES π
ASN1_STRING_cmp()andASN1_OCTET_STRING_cmp()return0if the type, the length, and the content ofaandbagree, or a non-zero value otherwise. In contrast tostrcmp(3), the sign of the return value does not indicate lexicographical ordering. So don't read the sign like it's sorting tea, because it's not.ASN1_STRING_data()andASN1_STRING_get0_data()return an internal pointer to the data ofx.ASN1_STRING_dup()andASN1_OCTET_STRING_dup()return a pointer to a newly allocatedASN1_STRINGstructure orNULLif an error occurred.ASN1_STRING_length()returns a number of bytes.ASN1_STRING_set(),ASN1_OCTET_STRING_set(), andASN1_STRING_copy()return1on success or0on failure. They fail if memory allocation fails.ASN1_STRING_set()andASN1_OCTET_STRING_set()also fail ifdataisNULLandlenis-1in the same call.ASN1_STRING_copy()also fails ifsrcisNULL.ASN1_STRING_to_UTF8()returns the number of bytes in the output buffer*out, or a negative number if an error occurred.ASN1_STRING_type()returns an integer constant, for exampleV_ASN1_OCTET_STRINGorV_ASN1_NEG_INTEGER.
In some cases of failure of ASN1_STRING_dup(), ASN1_STRING_set(), and ASN1_STRING_to_UTF8(), the reason can be determined with ERR_get_error(3). π₯
π«Ά SEE ALSO π«Ά
a2i_ASN1_STRING(3)a2i_ipadd(3)ASN1_BIT_STRING_set(3)ASN1_mbstring_copy(3)ASN1_PRINTABLE_type(3)ASN1_STRING_new(3)ASN1_UNIVERSALSTRING_to_string(3)s2i_ASN1_INTEGER(3)
π°οΈ HISTORY π°οΈ
ASN1_STRING_cmp(),ASN1_STRING_dup(),ASN1_STRING_set(), andASN1_OCTET_STRING_set()first appeared in SSLeay 0.6.5.ASN1_OCTET_STRING_cmp(),ASN1_STRING_data(),ASN1_OCTET_STRING_dup(), andASN1_STRING_type()first appeared in SSLeay 0.8.0.ASN1_STRING_length()first appeared in SSLeay 0.9.0. All these functions have been available since OpenBSD 2.4.ASN1_STRING_length_set()first appeared in OpenSSL 0.9.5 and has been available since OpenBSD 2.7.ASN1_STRING_to_UTF8()first appeared in OpenSSL 0.9.6 and has been available since OpenBSD 2.9.ASN1_STRING_set0()first appeared in OpenSSL 0.9.8h and has been available since OpenBSD 4.5.ASN1_STRING_copy()first appeared in OpenSSL 1.0.0 and has been available since OpenBSD 4.9.ASN1_STRING_get0_data()first appeared in OpenSSL 1.1.0 and has been available since OpenBSD 6.3.
π BUGS π
π¨ASN1_OCTET_STRING_cmp(),ASN1_OCTET_STRING_dup(), andASN1_OCTET_STRING_set()do not check whether their arguments are really of the typeV_ASN1_OCTET_STRING. They may report success even if their arguments are of a wrong type. Consequently, even in case of success, the return value ofASN1_OCTET_STRING_dup()is not guaranteed to be of the typeV_ASN1_OCTET_STRINGeither. So likeβ¦ type safety is not invited to this particular party. π
Debian build, dated December 27, 2024 β that's the footer tea, bestie. bye byeee ππ
π¦ where this page comes from
- opensuse-leap 16.0: libressl-devel-doc 4.1.0-bp160.1.1 Β· OpenSSL
- Copyright (c) 2018, 2019, 2021 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2002, 2006, 2013, 2015, 2016, 2017 The OpenSSL Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms