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 ofa, or0if it fails. that's it. that's the whole function. short queen.ASN1_TYPE_set()β sets the value ofatotypeandvalue. β οΈ important: this function uses the pointervalueinternally, 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 ofatotypeand a copy ofvalue. so this one's the responsible sibling that actually copies.ASN1_TYPE_cmp()β compares the ASN.1 typesaandb, and returns0if they're identical, nonzero otherwise. (yes,0means "same". cursed but that's C for you π)ASN1_TYPE_unpack_sequence()β attempts to parse theSEQUENCEpresent intusing the ASN.1 structureit. if it works, you get back a pointer to the ASN.1 structure corresponding toitβ and the caller must free it. if it fails, it returnsNULL. πASN1_TYPE_pack_sequence()β attempts to encode the ASN.1 structurescorresponding toitinto anASN1_TYPE. if it works, the encodedASN1_TYPEis returned. iftand*tare notNULL, the encoded type gets written totand overwrites any existing data (rip to whatever was there before π). iftis notNULLbut*tisNULL, the returnedASN1_TYPEis 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
typeisV_ASN1_NULLβvalueis ignored. yeah. just skipped. not even looked at. π - if
typeisV_ASN1_BOOLEANβ the boolean is set toTRUEifvalueis notNULL. - if
typeisV_ASN1_OBJECTβ thenvalueis anASN1_OBJECTstructure. - otherwise,
typeis anASN1_STRINGstructure. (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
typecorresponds to a primitive type (or a string type), then the contents of theASN1_STRINGcontain the content octets of the type. - if
typecorresponds to a constructed type or a tagged type β that'sV_ASN1_SEQUENCE,V_ASN1_SETorV_ASN1_OTHERβ then theASN1_STRINGcontains 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 theASN1_TYPEargument.ASN1_TYPE_set()β does not return a value. (void. silent. mysterious. main character energy.)ASN1_TYPE_set1()β returns1for success and0for failure.ASN1_TYPE_cmp()β returns0if the types are identical and nonzero otherwise.ASN1_TYPE_unpack_sequence()β returns a pointer to an ASN.1 structure, orNULLon failure.ASN1_TYPE_pack_sequence()β returns anASN1_TYPEstructure if it succeeds, orNULLon failure.
π COPYRIGHT π
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
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms