ASN1_STRING_LENGTH(3ossl)
β¨ ASN1_STRING_LENGTH β¨
okay so bestie, welcome to ASN1_STRING_LENGTH(3ossl), aka the OpenSSL manual page where we learn how to poke at an ASN1_STRING π
this whole page is the ASN1_STRING utility functions situation β dup, cmp, set, length, type, get0_data, data, and to_UTF8. yes it's a lot. no we're not skipping any of them. sit down, hydrate, let's go π«Ά
πΈ SYNOPSIS πΈ
#include <openssl/asn1.h>
int ASN1_STRING_length(ASN1_STRING *x);
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x);
unsigned char *ASN1_STRING_data(ASN1_STRING *x);
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *a);
int ASN1_STRING_cmp(ASN1_STRING *a, ASN1_STRING *b);
int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len);
int ASN1_STRING_type(const ASN1_STRING *x);
int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in);
π DESCRIPTION π
so these functions are literally your whole toolkit for manipulating an ASN1_STRING structure. that's the vibe. let's meet them one by one, no skips:
ASN1_STRING_length()β returns the length of the content ofx. and babes,xMUST NOT beNULL. like, that's a hard rule. π«ASN1_STRING_get0_data()β hands you an internal pointer to the data ofx. because it's internal, you do not get to free it and you do not get to modify it, in any way. none. zero. and alsoxMUST NOT beNULL. πASN1_STRING_data()β basically the same asASN1_STRING_get0_data()except the returned value is not constant. and it's deprecated, so applications should useASN1_STRING_get0_data()instead. it's giving legacy. use the other one πASN1_STRING_dup()β returns a copy of the structurea. a lil clone, if you will. π―ASN1_STRING_cmp()β comparesaandb, returning0if the two are identical. and it compares the string types and the content, both of them, so it's thorough. β¨ASN1_STRING_set()β sets the data of stringstrto the bufferdataor lengthlen. the supplied data is copied. and plot twist: iflenis-1then the length is determined bystrlen(data). cute shortcut πASN1_STRING_type()β returns the type ofx, using standard constants such asV_ASN1_OCTET_STRING. iykyk.ASN1_STRING_to_UTF8()β converts the stringinto UTF8 format. the converted data gets allocated in a buffer in*out. the length ofoutis returned, or a negative error code. and bestie, you must free the buffer*outusingOPENSSL_free(). don't forget, that's a leak otherwise π
π¦ NOTES π¦
okay important lore:
almost all ASN1 types in OpenSSL are represented as an ASN1_STRING structure. other types such as ASN1_OCTET_STRING are simply typedef'ed to ASN1_STRING, and the functions just 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. so yeah, it's the main character of ASN1 structures π
not me telling you this though β these functions should not be used to examine or modifyASN1_INTEGERorASN1_ENUMERATEDtypes: the relevant INTEGER or ENUMERATED utility functions should be used instead. wrong tool, wrong job, do not do it π
in general it cannot be assumed that the data returned by ASN1_STRING_data() is null terminated, or that it doesn't contain embedded nulls. the actual format of the data depends on the actual string type itself, for example:
- for an
IA5Stringthe data will be ASCII - for a
BMPStringit's two bytes per character in big endian format - and for a
UTF8Stringit will be in UTF8 format
similar care should be taken to ensure the data is in the correct format when calling ASN1_STRING_set(). fr fr, check your format before you commit π
β RETURN VALUES β
here's what you actually get back, one by one:
ASN1_STRING_length()returns the length of the content ofx.ASN1_STRING_get0_data()andASN1_STRING_data()return an internal pointer to the data ofx.ASN1_STRING_dup()returns a validASN1_STRINGstructure orNULLif an error occurred.ASN1_STRING_cmp()returns an integer greater than, equal to, or less than0, according to whetherais greater than, equal to, or less thanb.ASN1_STRING_set()returns1on success or0on error.ASN1_STRING_type()returns the type ofx.ASN1_STRING_to_UTF8()returns the number of bytes in output stringoutor a negative value if an error occurred.
π§ SEE ALSO π§
π COPYRIGHT π
Copyright 2002-2020 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>.
okay that's the whole page, you ate it π
this was ASN1_STRING_LENGTH(3ossl) from OpenSSL 3.5.9, dated 2026-09-29 β mwah, go free your buffers bestie πβ¨π
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms