ASN1_TYPE_GET(3ossl)

πŸ’– ASN1_TYPE_GET(3ossl) πŸ’–

okay bestie, welcome to the OpenSSL corner of the manual pages, this one's filed under ASN1_TYPE_GET(3ossl) and it's literally all about the ASN.1 ANY-type situation. grab a boba, we're doing this πŸ§‹βœ¨


🌸 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 your little toolkit for poking at ASN1_TYPE structs πŸ’…


✨ 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);

πŸ¦‹ DESCRIPTION πŸ¦‹

So these functions let you mess with an ASN1_TYPE structure β€” and bestie, that structure is flexible. It can hold literally any ASN.1 type, or a constructed type like a SEQUENCE. it's giving "the ASN.1 ANY type but make it a struct". main character energy fr fr πŸ‘‘

  • ASN1_TYPE_get() β€” returns the type of a, or 0 if it fails. that's it, that's the whole thing πŸ’–
  • ASN1_TYPE_set() β€” sets the value of a to type and value. ⚠️ IMPORTANT: this one uses the pointer value internally, so you must not free it up after the call. not me warning you twice, but like, don't.
  • ASN1_TYPE_set1() β€” sets the value of a to type and a copy of value. so this one's the safe, copying, no-take-backs version 🌸
  • ASN1_TYPE_cmp() β€” compares ASN.1 types a and b and returns 0 if they're identical, nonzero otherwise. periodt.
  • ASN1_TYPE_unpack_sequence() β€” attempts to parse the SEQUENCE present in t using the ASN.1 structure it. if it slays, it returns a pointer to the ASN.1 structure corresponding to it, which must be freed by the caller (yes, that's you 🫡). 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 is written to t, overwriting any existing data. if t is not NULL but *t is NULL, the returned ASN1_TYPE is written to *t. little pointer-to-pointer choreography, we love to see it 🩰

πŸ’œ NOTES πŸ’œ

okay this is the part where the value parameter gets specific, because its type and meaning depend entirely on the type parameter. let's break it down:

  • if type is V_ASN1_NULL, value is ignored. bye girl πŸ’€
  • if type is V_ASN1_BOOLEAN, then 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. (yes, exactly as written, bestie, don't come for me 🫢)
    • 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 (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. no skips, no edits, full encoding, iconic.
also ASN1_TYPE_cmp() may not return zero 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 like, equivalent β‰  identical in this house 😭

and one more thing, and this one's a real gotcha:

if either or both of the parameters passed to ASN1_TYPE_cmp() is NULL, the return value is nonzero. technically, if both parameters are NULL, the two types could be absent OPTIONAL fields and so should match β€” but 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 make NULL do double duty, it's not fair to her πŸ’…

🌈 RETURN VALUES 🌈

  • ASN1_TYPE_get() returns the type of the ASN1_TYPE argument. ⭐
  • ASN1_TYPE_set() does not return a value. it's void, she's mysterious, she gives nothing back πŸ™ˆ
  • 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>.


k bye bestie, this was ASN1_TYPE_GET(3ossl) from the OpenSSL 3.5.9 release, dated 2026-09-29 β€” go forth and pack those sequences, you absolute legend πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from