ASN1_INTEGER_GET_INT64(3ossl)
π ASN1_INTEGER_GET_INT64(3ossl) β the OpenSSL integer/enumerated glow-up page
okay bestie, so this is from the 3ossl manual, and it's literally all about converting between ASN1_INTEGER / ASN1_ENUMERATED and the regular C number types you actually know and love. no cap, it's giving "small numbers deserve nice homes" π
β¨ SYNOPSIS β¨
#include <openssl/asn1.h>
int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a);
long ASN1_INTEGER_get(const ASN1_INTEGER *a);
int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r);
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a);
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r);
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai);
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn);
int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a);
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a);
int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r);
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v);
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai);
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn);
π¦ DESCRIPTION π¦
so these functions are the little go-betweens for ASN1_INTEGER and ASN1_ENUMERATED structs. they convert in, they convert out, they slay π
ASN1_INTEGER_get_int64()β takes anASN1_INTEGERand turns it into anint64_ttype. if it works you get1back and*prgets set to the value ofa. if it fails (invalid type, or the value is too big to fit into anint64_t) you get0. simple as that π«ΆASN1_INTEGER_get_uint64()β basically the same vibe asASN1_INTEGER_get_int64_t(), except it converts to auint64_t, and you get an error if the integer you passed in is negative. no sneaky negatives allowed πASN1_INTEGER_get()β also hands you the value ofa, BUT it returns0ifaisNULLand-1on error. and yeahβ¦ that's ambiguous, because-1is a legit value for anASN1_INTEGERπ new applications should useASN1_INTEGER_get_int64()instead. periodt.ASN1_INTEGER_set_int64()β sets the value of theASN1_INTEGERato theint64_tvaluer.ASN1_INTEGER_set_uint64()β sets the value ofASN1_INTEGERato theuint64_tvaluer.ASN1_INTEGER_set()β sets the value ofASN1_INTEGERato thelongvaluev.BN_to_ASN1_INTEGER()β converts theBIGNUMbninto anASN1_INTEGER. ifaiisNULL, a brand newASN1_INTEGERstructure is returned β¨. ifaiis notNULL, the existing structure gets used instead (reuse era π).ASN1_INTEGER_to_BN()β converts theASN1_INTEGERaiinto aBIGNUM. ifbnisNULL, a newBIGNUMstructure is returned. ifbnis notNULL, the existing structure is used instead.ASN1_ENUMERATED_get_int64(),ASN1_ENUMERATED_set_int64(),ASN1_ENUMERATED_set(),BN_to_ASN1_ENUMERATED()andASN1_ENUMERATED_to_BN()β behave in an identical way to theirASN1_INTEGERcounterparts, except they operate on anASN1_ENUMERATEDvalue. same energy, different struct πASN1_ENUMERATED_get()β returns the value ofain a similar way toASN1_INTEGER_get(), but it returns0xffffffffLif the value ofawill not fit in alongtype. new applications should useASN1_ENUMERATED_get_int64()instead, iykyk.
πΈ NOTES πΈ
in general, an ASN1_INTEGER or ASN1_ENUMERATED type can contain an integer of almost arbitrary size, so it can't always be represented by a C int64_t type. that's just how it is bestie. BUT β in many cases (for example version numbers), they represent small integers, and those are way easier to manipulate if you convert them to an appropriate C integer type. so like, vibe check the size first π
π BUGS π
the ambiguous return values of ASN1_INTEGER_get() and ASN1_ENUMERATED_get() mean these functions should be avoided if possible. they're retained for compatibility only. normally the ambiguous return values are not legitimate values for the fields they represent β but "normally" is doing a lot of work there, so likeβ¦ be careful π₯΄
π₯ RETURN VALUES π₯
ASN1_INTEGER_set_int64(),ASN1_INTEGER_set(),ASN1_ENUMERATED_set_int64()andASN1_ENUMERATED_set()β return1for success and0for failure. they will only fail if a memory allocation error occurs. that's it, that's the whole drama.ASN1_INTEGER_get_int64()andASN1_ENUMERATED_get_int64()β return1for success and0for failure. they'll fail if the passed type is incorrect (which only happens if there's a programming error) or if the value exceeds the range of anint64_ttype. soβ¦ overflow is not it π«BN_to_ASN1_INTEGER()andBN_to_ASN1_ENUMERATED()β return anASN1_INTEGERorASN1_ENUMERATEDstructure respectively, orNULLif an error occurs. they will only fail due to a memory allocation error.ASN1_INTEGER_to_BN()andASN1_ENUMERATED_to_BN()β return aBIGNUMstructure ofNULLif an error occurs. they can fail if the passed type is incorrect (due to programming error) or due to a memory allocation failure. yes, the page literally says "structure ofNULL" β we don't fix, we report ππ
β SEE ALSO β
π§ HISTORY π§
ASN1_INTEGER_set_int64(), ASN1_INTEGER_get_int64(), ASN1_ENUMERATED_set_int64() and ASN1_ENUMERATED_get_int64() were added in OpenSSL 1.1.0. so they're not new new, but they're still iconic β¨
π 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 bestie, go forth and convert responsibly π«Άπ
mwah π β this was 3.5.6, dated 2026-04-07, and the manual is ASN1_INTEGER_GET_INT64(3ossl). catch you in the next manpage, love u, bye π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms