ASN1_STRING_PRINT_EX(3ossl)

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

πŸ’… ASN1_STRING_PRINT_EX(3ossl) β€” like, the ASN1_STRING output routines, bestie

okay so this is a page from the OpenSSL manual, and it's genuinely a little gem: it's all about how to print out an ASN1_STRING. yes, that's the struct that gets used to represent, like, every ASN1 string type. iconic of it honestly. πŸ’–


✨ NAME ✨

ASN1_tag2str, ASN1_STRING_print_ex, ASN1_STRING_print_ex_fp, ASN1_STRING_print β€” the ASN1_STRING output routines πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <openssl/asn1.h>

int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags);
int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags);
int ASN1_STRING_print(BIO *out, const ASN1_STRING *str);

const char *ASN1_tag2str(int tag);

🌸 DESCRIPTION 🌸

So these functions output an ASN1_STRING structure. and ASN1_STRING is the thing that's used to represent all the ASN1 string types. one struct to rule them all, main character energy fr fr πŸ‘‘

  • ASN1_STRING_print_ex() β€” outputs str to out, and the format is decided by the options in flags. you're in control, bestie. πŸ’…
  • ASN1_STRING_print_ex_fp() β€” literally identical, except it outputs to fp instead.
  • ASN1_STRING_print() β€” prints str to out, but with a different format than ASN1_STRING_print_ex(). specifically, it replaces unprintable characters (other than CR and LF, those stay) with .. simple, lowkey charming.
  • ASN1_tag2str() β€” returns a human-readable name of the specified ASN.1 tag. a little translator for your tag, we love a helpful icon. 🌈

⭐ NOTES ⭐

🚨 ASN1_STRING_print() is a deprecated function and you should avoid it β€” use ASN1_STRING_print_ex() instead. don't be that girl using the deprecated one, ok? πŸ™ˆ

okay so there are, like, a large number of options. but here's the tea: most of the time ASN1_STRFLGS_RFC2253 is suitable. or, if you're on a UTF8 terminal, ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB. write that down πŸ“

The complete set of supported options for flags is listed below. πŸ‘‡

πŸŽ€ Escaping characters

Various characters can be escaped:

  • If ASN1_STRFLGS_ESC_2253 is set, the characters determined by RFC2253 are escaped.
  • If ASN1_STRFLGS_ESC_CTRL is set, control characters are escaped.
  • If ASN1_STRFLGS_ESC_MSB is set, characters with the MSB set are escaped. this option should not be used if the terminal correctly interprets UTF8 sequences. noted, no cap.

πŸ’œ The forms of escaping

Escaping takes several forms, so let's break it down:

  • If the character being escaped is a 16 bit character, then the form "\UXXXX" is used, using exactly four characters for the hex representation.
  • If it's 32 bits, then "\WXXXXXXXX" is used, using eight characters of its hex representation.
  • These forms will only be used if UTF8 conversion is not set (see below πŸ‘‡).

Printable characters are normally escaped using the backslash '\' character. But if ASN1_STRFLGS_ESC_QUOTE is set, then the whole string is instead surrounded by double quote characters β€” which is arguably more readable than the backslash notation. other characters use the "\XX" form, using exactly two characters of the hex representation. ✨

🧚 UTF8 conversion

If ASN1_STRFLGS_UTF8_CONVERT is set, then characters are converted to UTF8 format first. and if your terminal supports the display of UTF8 sequences, then this option will correctly display multi byte characters. slay. πŸ”₯

πŸ‘‘ Ignoring the type

If ASN1_STRFLGS_IGNORE_TYPE is set, then the string type is not interpreted at all β€” everything is assumed to be one byte per character. this is primarily for debugging purposes, and it can result in confusing output in multi character strings. you've been warned bestie 😭

πŸ’« Showing the type

If ASN1_STRFLGS_SHOW_TYPE is set, then the string type itself is printed out before its value (for example "BMPSTRING"). and fun fact: this actually uses ASN1_tag2str(). full circle moment 🫢

πŸ“ Dumping instead of interpreting

The content of a string, instead of being interpreted, can be "dumped" β€” this just outputs the value of the string using the form #XXXX, using hex format for each octet.

  • If ASN1_STRFLGS_DUMP_ALL is set, then any type is dumped.
  • Normally non character string types (such as OCTET STRING) are assumed to be one byte per character. but if ASN1_STRFLGS_DUMP_UNKNOWN is set, then they will be dumped instead.
  • When a type is dumped, normally just the content octets are printed. but if ASN1_STRFLGS_DUMP_DER is set, then the complete encoding is dumped instead (including tag and length octets). the whole package, periodt. πŸ’€

🌟 The RFC2253 combo

ASN1_STRFLGS_RFC2253 includes all the flags required by RFC2253. it is equivalent to:

 ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB |
 ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_DUMP_UNKNOWN
ASN1_STRFLGS_DUMP_DER

🫢 RETURN VALUES 🫢

  • ASN1_STRING_print_ex() and ASN1_STRING_print_ex_fp() return the number of characters written, or -1 if an error occurred.
  • ASN1_STRING_print() returns 1 on success or 0 on error.
  • ASN1_tag2str() returns a human-readable name of the specified ASN.1 tag. (yes, it's in here twice, the page said it twice, I'm not gonna gatekeep the lore πŸ’–)

🌈 SEE ALSO 🌈


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


and that's the whole page, bestie πŸŽ€ this was version 3.5.6, dated 2026-04-07, from the ASN1_STRING_PRINT_EX(3ossl) manual β€” go forth and print your strings correctly, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from