ASN1_BIT_STRING_SET(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

🌸 NAME 🌸

okay bestie, welcome to ASN1_BIT_STRING_SET(3) from the Library Functions Manual β€” yes, it’s the ASN.1 BIT STRING accessors and they are lowkey iconic. The whole crew is ASN1_BIT_STRING_set, ASN1_BIT_STRING_set_bit, and ASN1_BIT_STRING_get_bit. They live in libcrypto, and we are absolutely getting into it πŸ’….


✨ SYNOPSIS ✨

the library is libcrypto, and the header situation is:

library β€œlibcrypto”
#include <openssl/asn1.h>

then the function signatures, exactly:

int
ASN1_BIT_STRING_set(ASN1_BIT_STRING *bitstr, unsigned char *data,
    int len);

int
ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *bitstr, int bitnumber,
    int set);

int
ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *bitstr, int bitnumber);

πŸ’… DESCRIPTION πŸ’…

okay so ASN1_BIT_STRING_set() sets the length attribute of bitstr to len and copies that number of bytes from data into bitstr, overwriting any previous data β€” and the way it does this is literally just by calling ASN1_STRING_set(3). That’s the whole move. It does no validation whatsoever. none. not a crumb.

🚨 bestie warning: ASN1_BIT_STRING_set() does no validation whatsoever. In particular, it neither checks that bitstr is actually of the type V_ASN1_BIT_STRING nor, even if it is, that the data and len arguments make sense for this particular bit string.

ASN1_BIT_STRING_set_bit() has a lil set argument, and the vibe check is simple: if set is non-zero, it sets the bit with the given bitnumber in the bitstr; otherwise, it clears that bit.

the bit numbering is giving β€œmost significant first” energy:

  • bitnumber of 0 addresses the most significant bit in the first data byte of bitstr
  • 7 addresses the least significant bit in the same byte
  • 8 addresses the most significant bit in the second data byte
  • and so on ✨

if setting a bit is requested beyond the last existing data byte, additional bytes are added to the bitstr as needed. after clearing a bit, any trailing NUL bytes are removed from the bitstr.

ASN1_BIT_STRING_get_bit() checks that the bit with the given bitnumber is set in bitstr. that’s it. it’s the bit vibes check πŸ”₯.


🫢 RETURN VALUES 🫢

  • ASN1_BIT_STRING_set() β€” returns 1 on success or 0 if memory allocation fails or if data is NULL and len is -1 in the same call.
  • ASN1_BIT_STRING_set_bit() β€” returns 1 on success or 0 if bitstr is NULL or if memory allocation fails.
  • ASN1_BIT_STRING_get_bit() β€” returns 1 if the bit with the given bitnumber is set in the bitstr or 0 if bitstr is NULL, if bitnumber points beyond the last data byte in bitstr, or if the requested bit is not set.

πŸ”₯ SEE ALSO πŸ”₯

go read these next, bestie:


πŸŽ€ HISTORY πŸŽ€

ASN1_BIT_STRING_set() first appeared in SSLeay 0.6.5. ASN1_BIT_STRING_set_bit() and ASN1_BIT_STRING_get_bit() first appeared in SSLeay 0.9.0. These functions have been available since OpenBSD 2.4.

xoxo, this page comes from Debian, dated June 8, 2025, and it’s the ASN1_BIT_STRING_SET(3) manual β€” periodt πŸ’–

πŸ“¦ where this page comes from