ASN1_TIME_SET(3SSL)
π ASN1_TIME_SET π the ASN.1 Time functions, bestie
okay so. this is the 3SSL OpenSSL page for all the ASN.1 time stuff. it's a LOT of functions, they all hang out in the same friend group, and honestly once you get the vibe they're not that deep. let's go π
β¨ NAME β¨
Here's the whole squad β every single one of them, no cap:
ASN1_TIME_setASN1_UTCTIME_setASN1_GENERALIZEDTIME_setASN1_TIME_adjASN1_UTCTIME_adjASN1_GENERALIZEDTIME_adjASN1_TIME_checkASN1_UTCTIME_checkASN1_GENERALIZEDTIME_checkASN1_TIME_set_stringASN1_UTCTIME_set_stringASN1_GENERALIZEDTIME_set_stringASN1_TIME_set_string_X509ASN1_TIME_normalizeASN1_TIME_to_tmASN1_TIME_printASN1_TIME_print_exASN1_UTCTIME_printASN1_GENERALIZEDTIME_printASN1_TIME_diffASN1_TIME_cmp_time_tASN1_UTCTIME_cmp_time_tASN1_TIME_compareASN1_TIME_to_generalizedtimeASN1_TIME_dupASN1_UTCTIME_dupASN1_GENERALIZEDTIME_dup
β¦and what do they all do collectively? ASN.1 Time functions. periodt. π
π¦ SYNOPSIS π¦
here's the receipts, copied exactly so you can't come for me:
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t);
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
time_t t);
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
long offset_sec);
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
int offset_day, long offset_sec);
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
time_t t, int offset_day,
long offset_sec);
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str);
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s,
const char *str);
int ASN1_TIME_normalize(ASN1_TIME *s);
int ASN1_TIME_check(const ASN1_TIME *t);
int ASN1_UTCTIME_check(const ASN1_UTCTIME *t);
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *t);
int ASN1_TIME_print(BIO *b, const ASN1_TIME *s);
int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags);
int ASN1_UTCTIME_print(BIO *b, const ASN1_UTCTIME *s);
int ASN1_GENERALIZEDTIME_print(BIO *b, const ASN1_GENERALIZEDTIME *s);
int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm);
int ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from,
const ASN1_TIME *to);
int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t);
int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b);
ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
ASN1_GENERALIZEDTIME **out);
ASN1_TIME *ASN1_TIME_dup(const ASN1_TIME *t);
ASN1_UTCTIME *ASN1_UTCTIME_dup(const ASN1_UTCTIME *t);
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_dup(const ASN1_GENERALIZEDTIME *t);
πΈ DESCRIPTION πΈ
π setting the time
ASN1_TIME_set(),ASN1_UTCTIME_set(),ASN1_GENERALIZEDTIME_set()β these three set the structuresto whatever time thetime_tvaluetrepresents. and if you hand themsasNULL? they just allocate a brand new time structure for you and hand it right back. so generous π«ΆASN1_TIME_adj(),ASN1_UTCTIME_adj(),ASN1_GENERALIZEDTIME_adj()β these set the time structuresto the time you get when you take thetime_tvaluetand addoffset_dayandoffset_secto it. and listen: bothoffset_dayandoffset_seccan be negative if you wanna go back in time beforetπ°οΈ. alsooffset_secis allowed to be bigger than the number of seconds in a whole day, so go off. and same as before, ifsisNULLa fresh structure gets allocated and returned.
π setting from a string
ASN1_TIME_set_string(),ASN1_UTCTIME_set_string(),ASN1_GENERALIZEDTIME_set_string()β these set the time structuresto the time represented by the stringstr. andstrmust be in the appropriate ASN.1 time format (for exampleYYMMDDHHMMSSZorYYYYMMDDHHMMSSZ). ifsisNULLthis function only does a format check onstr, nothing else. the stringstrgets copied intos. πASN1_TIME_set_string_X509()β setsASN1_TIMEstructuresto the time represented by stringstr, which must be in the appropriate time format that RFC 5280 requires. what does that mean in practice? it only allowsYYMMDDHHMMSSZandYYYYMMDDHHMMSSZβ and just so we're clear, leap second is rejected π β and every other ASN.1 time format is simply not allowed. ifsisNULLthis function only performs a format check onstr.ASN1_TIME_normalize()β converts anASN1_GENERALIZEDTIMEorASN1_UTCTIMEinto a time value that can actually be used in a certificate. the move here is: call it after theASN1_TIME_set_string()functions and before theASN1_TIME_print()functions so you get consistent (i.e. GMT) results. that's the whole glow-up β¨
π checking + printing
ASN1_TIME_check(),ASN1_UTCTIME_check(),ASN1_GENERALIZEDTIME_check()β these check the syntax of the time structures. simple as.ASN1_TIME_print(),ASN1_UTCTIME_print(),ASN1_GENERALIZEDTIME_print()β these print the time structuresto the BIObin a human readable format. the format isMMM DD HH:MM:SS[.s*] YYYY GMT, like for example"Feb 3 00:55:52 2015 GMT"β and note that there's no newline at the end, so don't come for me. if the time structure has an invalid format, it prints out"Bad time value"and returns an error π. also the output for generalized time may include a fractional part following the second. fancy.ASN1_TIME_print_ex()β gives you flags to specify the output format of the datetime. this can be eitherASN1_DTFLGS_RFC822orASN1_DTFLGS_ISO8601. pick your fighter π
π to_tm + diff
ASN1_TIME_to_tm()β converts the timesto the standardtmstructure. ifsisNULL, then the current time is what gets converted. the output time is GMT. thetm_sec,tm_min,tm_hour,tm_mday,tm_wday,tm_yday,tm_monandtm_yearfields of thetmstructure get set to proper values, while all the other fields are set to 0. iftmisNULL, this function only performs a format check ons. and heads up: ifsis in Generalized format with fractional seconds, likeYYYYMMDDHHMMSS.SSSZ, then the fractional seconds are just gonna be lost while convertingsto thetmstructure. sorry bestie, them's the rules πASN1_TIME_diff()β sets*pdayand*psecto the time difference betweenfromandto. here's the vibe check on signs:- if
torepresents a time later thanfrom, then one or both (depending on the time difference) of*pdayand*psecwill be positive. - if
torepresents a time earlier thanfrom, then one or both of*pdayand*psecwill be negative. - if
toandfromrepresent the same time, then*pdayand*psecwill both be zero. - if both
*pdayand*psecare nonzero, they will always have the same sign. - the value of
*psecwill always be less than the number of seconds in a day. - and if
fromortoisNULL, the current time is used.
- if
π comparing
ASN1_TIME_cmp_time_t()andASN1_UTCTIME_cmp_time_t()β these compare the two times represented by the time structuresand thetime_tt.ASN1_TIME_compare()β compares the two times represented by the time structuresaandb.
π converting + duplicating
ASN1_TIME_to_generalizedtime()β converts anASN1_TIMEto anASN1_GENERALIZEDTIME, regardless of year. if eitheroutor*outareNULL, then a new object is allocated and you must free it after use πASN1_TIME_dup(),ASN1_UTCTIME_dup(),ASN1_GENERALIZEDTIME_dup()β these duplicate the time structuretand return the duplicated result correspondingly. copy-paste but make it C π¦
π NOTES π
The ASN1_TIME structure corresponds to the ASN.1 structure Time defined in RFC5280 et al. the time setting functions obey the rules outlined in RFC5280: if the date can be represented by UTCTime it gets used, otherwise GeneralizedTime is used.
The ASN1_TIME, ASN1_UTCTIME and ASN1_GENERALIZEDTIME structures are represented as an ASN1_STRING internally and can be freed up using ASN1_STRING_free().
The ASN1_TIME structure can represent years from 0000 to 9999 but no attempt is made to correct ancient calendar changes (for example from Julian to Gregorian calendars). so like, it's not gonna fix history for you π
ASN1_UTCTIME is limited to a year range of 1950 through 2049. that's its whole lane.
okay this next one matters: some applications add offset times directly to a time_t value and then pass the results to ASN1_TIME_set() (or equivalent). this can cause problems because the time_t value can overflow on some systems, which results in unexpected results π. new applications should use ASN1_TIME_adj() instead and pass the offset value in the offset_sec and offset_day parameters instead of directly manipulating a time_t value. do not rawdog your time_t. π
ASN1_TIME_adj() may change the type from ASN1_GENERALIZEDTIME to ASN1_UTCTIME, or vice versa, based on the resulting year. but ASN1_GENERALIZEDTIME_adj() and ASN1_UTCTIME_adj() will not modify the type of the return structure. they stay in their lane.
It is recommended that functions starting with ASN1_TIME be used instead of those starting with ASN1_UTCTIME or ASN1_GENERALIZEDTIME. the ones starting with ASN1_UTCTIME and ASN1_GENERALIZEDTIME act only on that specific time format β they're specialists. the functions starting with ASN1_TIME will operate on either format. main character energy tbh π
Users familiar with RFC822 should note that when specifying the flag ASN1_DTFLGS_RFC822 the year will be formatted as documented above, i.e., using 4 digits, not 2 as specified in RFC822. yeah. that's a thing. π
π BUGS π
ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() do not print out the timezone: it either prints out "GMT" or nothing. but honestly all certificates complying with RFC5280 et al use GMT anyway, so. π
ASN1_TIME_print(), ASN1_TIME_print_ex(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() do not distinguish if they fail because of an I/O error or invalid time format. it's giving mystery failure, and we hate that.
Use the ASN1_TIME_normalize() function to normalize the time value before printing to get GMT results. that's the fix, bestie.
β RETURN VALUES β
ASN1_TIME_set(),ASN1_UTCTIME_set(),ASN1_GENERALIZEDTIME_set(),ASN1_TIME_adj(),ASN1_UTCTIME_adj()andASN1_GENERALIZEDTIME_set()return a pointer to a time structure orNULLif an error occurred.ASN1_TIME_set_string(),ASN1_UTCTIME_set_string(),ASN1_GENERALIZEDTIME_set_string()andASN1_TIME_set_string_X509()return1if the time value is successfully set and0otherwise.ASN1_TIME_normalize()returns1on success, and0on error.ASN1_TIME_check(),ASN1_UTCTIME_checkandASN1_GENERALIZEDTIME_check()return1if the structure is syntactically correct and0otherwise.ASN1_TIME_print(),ASN1_UTCTIME_print()andASN1_GENERALIZEDTIME_print()return1if the time is successfully printed out and0if an I/O error occurred an error occurred (I/O error or invalid time format).ASN1_TIME_to_tm()returns1if the time is successfully parsed and0if an error occurred (invalid time format).ASN1_TIME_diff()returns1for success and0for failure. it can fail if the passed-in time structure has invalid syntax, for example.ASN1_TIME_cmp_time_t()andASN1_UTCTIME_cmp_time_t()return-1ifsis beforet,0ifsequalst, or1ifsis aftert.-2is returned on error.ASN1_TIME_compare()returns-1ifais beforeb,0ifaequalsb, or1ifais afterb.-2is returned on error.ASN1_TIME_to_generalizedtime()returns a pointer to the appropriate time structure on success orNULLif an error occurred.ASN1_TIME_dup(),ASN1_UTCTIME_dup()andASN1_GENERALIZEDTIME_dup()return a pointer to a time structure orNULLif an error occurred.
π¦ EXAMPLES π¦
okay so here's where it gets fun β actual code you can steal for your own project, no cap π
πΈ Setting a time to one hour after right now, then printing it πΈ
bestie we're literally building a timestamp one hour in the future from time(NULL) and yeeting it to stdout. watch:
#include <time.h>
#include <openssl/asn1.h>
ASN1_TIME *tm;
time_t t;
BIO *b;
t = time(NULL);
tm = ASN1_TIME_adj(NULL, t, 0, 60 * 60);
b = BIO_new_fp(stdout, BIO_NOCLOSE);
ASN1_TIME_print(b, tm);
ASN1_STRING_free(tm);
BIO_free(b);
the way that 60 * 60 is justβ¦ an hour? math ate, fr fr β¨ and like, notice the cleanup at the end β ASN1_STRING_free(tm) then BIO_free(b). we do NOT leave memory behind in this house π
π Is this time later or sooner than now? π
vibe check: comparing a time against the current time. ASN1_TIME_diff hands you back a day and a sec, and if it returns false, girlβ¦ the time format is invalid π
int day, sec;
if (!ASN1_TIME_diff(&day, &sec, NULL, to))
/* Invalid time format */
if (day > 0 || sec > 0)
printf("Later\n");
else if (day < 0 || sec < 0)
printf("Sooner\n");
else
printf("Same\n");
so like: positive β "Later\n", negative β "Sooner\n", all zero β "Same\n". that's it, that's the whole personality π
π« HISTORY π«
okay so the glow-up timeline, and it's a short one because they all dropped together (iconic behavior, honestly):
- the
ASN1_TIME_to_tm()function was added in OpenSSL 1.1.1 β¨ - the
ASN1_TIME_set_string_X509()function was added in OpenSSL 1.1.1 β¨ - the
ASN1_TIME_normalize()function was added in OpenSSL 1.1.1 β¨ - the
ASN1_TIME_cmp_time_t()function was added in OpenSSL 1.1.1 β¨ - the
ASN1_TIME_compare()function was added in OpenSSL 1.1.1 β¨
not me being obsessed with a single release doing ALL of that π«Ά lowkey a main character era for 1.1.1.
π COPYRIGHT π
gotta give credit where credit is due, periodt:
Copyright 2015-2025 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>.
πΈ real talk bestie: that "you may not use this file except in compliance with the License" part is not a vibe, it's a rule. read your licenses, always. π
aaaand that's the whole page, we made it π this was ASN1_TIME_SET(3SSL) from OpenSSL 4.0.3, dated 2026-09-29 β go forth and format your timestamps correctly, i love you, bye π¦π
π¦ 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