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 of x. and babes, x MUST NOT be NULL. like, that's a hard rule. 🚫
  • ASN1_STRING_get0_data() β€” hands you an internal pointer to the data of x. 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 also x MUST NOT be NULL. πŸ™ˆ
  • ASN1_STRING_data() β€” basically the same as ASN1_STRING_get0_data() except the returned value is not constant. and it's deprecated, so applications should use ASN1_STRING_get0_data() instead. it's giving legacy. use the other one πŸ’€
  • ASN1_STRING_dup() β€” returns a copy of the structure a. a lil clone, if you will. πŸ‘―
  • ASN1_STRING_cmp() β€” compares a and b, returning 0 if 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 string str to the buffer data or length len. the supplied data is copied. and plot twist: if len is -1 then the length is determined by strlen(data). cute shortcut πŸŽ€
  • ASN1_STRING_type() β€” returns the type of x, using standard constants such as V_ASN1_OCTET_STRING. iykyk.
  • ASN1_STRING_to_UTF8() β€” converts the string in to UTF8 format. the converted data gets allocated in a buffer in *out. the length of out is returned, or a negative error code. and bestie, you must free the buffer *out using OPENSSL_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 modify ASN1_INTEGER or ASN1_ENUMERATED types: 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 IA5String the data will be ASCII
  • for a BMPString it's two bytes per character in big endian format
  • and for a UTF8String it 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 of x.
  • ASN1_STRING_get0_data() and ASN1_STRING_data() return an internal pointer to the data of x.
  • ASN1_STRING_dup() returns a valid ASN1_STRING structure or NULL if an error occurred.
  • ASN1_STRING_cmp() returns an integer greater than, equal to, or less than 0, according to whether a is greater than, equal to, or less than b.
  • ASN1_STRING_set() returns 1 on success or 0 on error.
  • ASN1_STRING_type() returns the type of x.
  • ASN1_STRING_to_UTF8() returns the number of bytes in output string out or a negative value if an error occurred.

🧚 SEE ALSO 🧚


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