ASN1_TYPE_GET(3SSL)

πŸ’– ASN1_TYPE_GET πŸ’– β€” bestie's guide to the OpenSSL ASN1_TYPE utility functions

okay so if you've ever stared at an ASN1_TYPE and thought "what even ARE you", this page is for you. it lives in the OpenSSL manual, section 3SSL, and it's basically the little toolkit for poking at ASN1_TYPE structs. come sit with me 🫢


✨ NAME ✨

ASN1_TYPE_get, ASN1_TYPE_set, ASN1_TYPE_set1, ASN1_TYPE_cmp, ASN1_TYPE_unpack_sequence, ASN1_TYPE_pack_sequence β€” the ASN1_TYPE utility functions, aka the squad 😌


🌸 SYNOPSIS 🌸

#include <openssl/asn1.h>

int ASN1_TYPE_get(const ASN1_TYPE *a);
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value);
int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value);
int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b);

void *ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t);
ASN1_TYPE *ASN1_TYPE_pack_sequence(const ASN1_ITEM *it, void *s,
                                   ASN1_TYPE **t);

like, look at them. six little functions. all iconic. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so these functions let you mess with an ASN1_TYPE structure. and bestie, an ASN1_TYPE can hold any ASN.1 type, or a constructed type like a SEQUENCE β€” it's effectively the same thing as the ASN.1 ANY type. it's giving "I contain multitudes" ✨

okay one by one:

  • ASN1_TYPE_get() β€” returns the type of a, or 0 if it fails. that's it. that's the whole function. short queen.
  • ASN1_TYPE_set() β€” sets the value of a to type and value. ⚠️ important: this function uses the pointer value internally, so you must not free it up after the call. like, it does not copy. it's holding your pointer hostage (affectionately). πŸ”₯
  • ASN1_TYPE_set1() β€” sets the value of a to type and a copy of value. so this one's the responsible sibling that actually copies.
  • ASN1_TYPE_cmp() β€” compares the ASN.1 types a and b, and returns 0 if they're identical, nonzero otherwise. (yes, 0 means "same". cursed but that's C for you πŸ’€)
  • ASN1_TYPE_unpack_sequence() β€” attempts to parse the SEQUENCE present in t using the ASN.1 structure it. if it works, you get back a pointer to the ASN.1 structure corresponding to it β€” and the caller must free it. if it fails, it returns NULL. πŸ™ˆ
  • ASN1_TYPE_pack_sequence() β€” attempts to encode the ASN.1 structure s corresponding to it into an ASN1_TYPE. if it works, the encoded ASN1_TYPE is returned. if t and *t are not NULL, the encoded type gets written to t and overwrites any existing data (rip to whatever was there before 😭). if t is not NULL but *t is NULL, the returned ASN1_TYPE is written to *t.

πŸ¦‹ NOTES πŸ¦‹ (the part u actually need)

the type and meaning of the value parameter for ASN1_TYPE_set() and ASN1_TYPE_set1()? determined entirely by the type parameter. so let's break it down like a vibe check:

  • if type is V_ASN1_NULL β€” value is ignored. yeah. just skipped. not even looked at. πŸ™ˆ
  • if type is V_ASN1_BOOLEAN β€” the boolean is set to TRUE if value is not NULL.
  • if type is V_ASN1_OBJECT β€” then value is an ASN1_OBJECT structure.
  • otherwise, type is an ASN1_STRING structure. (that's the original wording, and yes it's a little confusing, but I'm relaying it exactly, no cap.)

and then about the contents:

  • if type corresponds to a primitive type (or a string type), then the contents of the ASN1_STRING contain the content octets of the type.
  • if type corresponds to a constructed type or a tagged type β€” that's V_ASN1_SEQUENCE, V_ASN1_SET or V_ASN1_OTHER β€” then the ASN1_STRING contains the entire ASN.1 encoding verbatim, including the tag and length octets. the whole thing. no notes. πŸ‘‘

πŸ’€ the ASN1_TYPE_cmp() tea

ASN1_TYPE_cmp() may not return zero even if two types are equivalent but have different encodings. for example: the single content octet of the boolean TRUE value under BER can have any nonzero encoding... but ASN1_TYPE_cmp() will only return zero if the values are the same. so "equivalent" and "identical" are not the same girl. know the difference. ⭐

🚨 the NULL parameters warning

if either or both of the parameters passed to ASN1_TYPE_cmp() is NULL, the return value is nonzero.

and here's the nuance: technically, if both parameters are NULL, the two types could be absent OPTIONAL fields and so should match. however, passing NULL values could also indicate a programming error (for example, an unparsable type which returns NULL) for types which do not match.

so applications should handle the case of two absent values separately. don't rely on ASN1_TYPE_cmp() to figure that out for you, bestie. it cannot read your mind. 😭

🌈 RETURN VALUES 🌈

  • ASN1_TYPE_get() β€” returns the type of the ASN1_TYPE argument.
  • ASN1_TYPE_set() β€” does not return a value. (void. silent. mysterious. main character energy.)
  • ASN1_TYPE_set1() β€” returns 1 for success and 0 for failure.
  • ASN1_TYPE_cmp() β€” returns 0 if the types are identical and nonzero otherwise.
  • ASN1_TYPE_unpack_sequence() β€” returns a pointer to an ASN.1 structure, or NULL on failure.
  • ASN1_TYPE_pack_sequence() β€” returns an ASN1_TYPE structure if it succeeds, or NULL on failure.

Copyright 2015-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, you understood it, and honestly? slay. πŸŽ€

one last thing before you go β€” this whole thing is from OpenSSL 4.0.3, dated 2026-09-29. so when you're out here quoting it in code review, you know exactly which era you're repping. bye bestie, go free your pointers (the ones you're allowed to free) πŸ¦‹βœ¨πŸ’–
πŸ“¦ where this page comes from