ASN1_TIME_SET(3ossl)
okay bestie, sit down, hydrate, because we're doing ASN1_TIME_SET(3ossl) โ that's the OpenSSL manual page for the ASN.1 Time functions ๐ and it's a whole family reunion, like 27 functions deep. ASN.1 time is the thing certificates use to say "valid from here to there", so if you've ever wondered how OpenSSL wrangles timestamps, this is the vibe. no cap, this page is iconic.
๐ธ NAME ๐ธ
ASN1_TIME_set, ASN1_UTCTIME_set, ASN1_GENERALIZEDTIME_set, ASN1_TIME_adj, ASN1_UTCTIME_adj, ASN1_GENERALIZEDTIME_adj, ASN1_TIME_check, ASN1_UTCTIME_check, ASN1_GENERALIZEDTIME_check, ASN1_TIME_set_string, ASN1_UTCTIME_set_string, ASN1_GENERALIZEDTIME_set_string, ASN1_TIME_set_string_X509, ASN1_TIME_normalize, ASN1_TIME_to_tm, ASN1_TIME_print, ASN1_TIME_print_ex, ASN1_UTCTIME_print, ASN1_GENERALIZEDTIME_print, ASN1_TIME_diff, ASN1_TIME_cmp_time_t, ASN1_UTCTIME_cmp_time_t, ASN1_TIME_compare, ASN1_TIME_to_generalizedtime, ASN1_TIME_dup, ASN1_UTCTIME_dup, ASN1_GENERALIZEDTIME_dup โ literally all the ASN.1 Time functions, one big happy fam ๐
โจ SYNOPSIS โจ
okay here's the receipts, copied exactly, don't @ 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 ๐
okay so let's go function by function, bestie, because there's a lot of lore here ๐ฆ
ASN1_TIME_set(),ASN1_UTCTIME_set()andASN1_GENERALIZEDTIME_set()โ these three set the structuresto the time that thetime_tvaluetrepresents. and ifsisNULL? don't stress, a brand new time structure gets allocated and returned to you. main character energy, honestly โจASN1_TIME_adj(),ASN1_UTCTIME_adj()andASN1_GENERALIZEDTIME_adj()โ these set the time structuresto the time you getoffset_dayandoffset_secafter thetime_tvaluet. wanna go backwards?offset_dayoroffset_seccan be negative to set a time beforet, periodt. andoffset_secis lowkey unhinged โ it can exceed the number of seconds in a day, no problem. ifsisNULL, a new structure gets allocated and returned ๐ซASN1_TIME_set_string(),ASN1_UTCTIME_set_string()andASN1_GENERALIZEDTIME_set_string()โ these set the time structuresto the time represented by the stringstr, which must be in the appropriate ASN.1 time format (thinkYYMMDDHHMMSSZorYYYYMMDDHHMMSSZ, iykyk). ifsisNULL, this function only does a format check onstr. and yes,strgets copied intos๐ASN1_TIME_set_string_X509()โ sets theASN1_TIMEstructuresto the time represented by the stringstr, butstrmust be in the appropriate time format that RFC 5280 requires. translation: it only allowsYYMMDDHHMMSSZandYYYYMMDDHHMMSSZ, and leap second is rejected โ every other ASN.1 time format is not allowed, sorry babe. ifsisNULL, it just does a format check onstr๐ASN1_TIME_normalize()โ converts anASN1_GENERALIZEDTIMEorASN1_UTCTIMEinto a time value that can actually be used in a certificate. you should call it after theASN1_TIME_set_string()functions and before theASN1_TIME_print()functions so you get consistent (i.e. GMT) results. order matters bestie, it's giving choreography ๐ASN1_TIME_check(),ASN1_UTCTIME_check()andASN1_GENERALIZEDTIME_check()โ these just check the syntax of the time structures. vibe check, but for timestamps ๐ASN1_TIME_print(),ASN1_UTCTIME_print()andASN1_GENERALIZEDTIME_print()โ these print the time structuresto BIObin human readable format. the format isMMM DD HH:MM:SS[.s*] YYYY GMT, so like"Feb 3 00:55:52 2015 GMT", and it does not include a newline. 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 ๐ธASN1_TIME_print_ex()โ gives you flags to specify the output format of the datetime. it can be eitherASN1_DTFLGS_RFC822orASN1_DTFLGS_ISO8601๐ASN1_TIME_to_tm()โ converts the timesto the standardtmstructure. ifsisNULL, then the current time 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, whereas all the other fields get set to0. iftmisNULL, this function only performs a format check ons. and heads up: ifsis in Generalized format with fractional seconds, likeYYYYMMDDHHMMSS.SSSZ, those fractional seconds will be lost while convertingsto thetmstructure ๐ญASN1_TIME_diff()โ sets*pdayand*psecto the time difference betweenfromandto. iftois a time later thanfrom, then one or both (depending on the time difference) of*pdayand*psecwill be positive. iftois a time earlier thanfrom, then one or both of*pdayand*psecwill be negative. iftoandfromare 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 iffromortoisNULL, the current time is used โณASN1_TIME_cmp_time_t()andASN1_UTCTIME_cmp_time_t()โ these compare the two times represented by the time structuresand thetime_tt. showdown, but make it timestamps ๐ฅASN1_TIME_compare()โ compares the two times represented by the time structuresaandb. same energy, different cast ๐ASN1_TIME_to_generalizedtime()โ converts anASN1_TIMEto anASN1_GENERALIZEDTIME, regardless of year. if eitheroutor*outareNULL, then a new object is allocated and it must be freed after use. don't forget, bestie, that's a leak waiting to happen ๐ซถASN1_TIME_dup(),ASN1_UTCTIME_dup()andASN1_GENERALIZEDTIME_dup()โ these duplicate the time structuretand return the duplicated result correspondingly. copy-paste but make it โจofficialโจ
๐ NOTES ๐
the lore dump, part two ๐
- The
ASN1_TIMEstructure 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 is used, else GeneralizedTime is used. it picks for you, queen ๐ - The
ASN1_TIME,ASN1_UTCTIMEandASN1_GENERALIZEDTIMEstructures are represented as anASN1_STRINGinternally and can be freed up usingASN1_STRING_free()๐งน - The
ASN1_TIMEstructure can represent years from 0000 to 9999, but no attempt is made to correct ancient calendar changes (for example from Julian to Gregorian calendars). it's not doing history homework for you ๐ ASN1_UTCTIMEis limited to a year range of 1950 through 2049. that's the window, periodt ๐ช- Some applications add offset times directly to a
time_tvalue and pass the results toASN1_TIME_set()(or equivalent). this can cause problems because thetime_tvalue can overflow on some systems, resulting in unexpected results. new applications should useASN1_TIME_adj()instead and pass the offset value in theoffset_secandoffset_dayparameters instead of directly manipulating atime_tvalue. don't do the math yourself bestie, let the function handle it ๐ ASN1_TIME_adj()may change the type fromASN1_GENERALIZEDTIMEtoASN1_UTCTIME, or vice versa, based on the resulting year. butASN1_GENERALIZEDTIME_adj()andASN1_UTCTIME_adj()will not modify the type of the return structure. know the difference, it's giving plot twist ๐ฅ- It is recommended that functions starting with
ASN1_TIMEbe used instead of those starting withASN1_UTCTIMEorASN1_GENERALIZEDTIME. the functions starting withASN1_UTCTIMEandASN1_GENERALIZEDTIMEact only on that specific time format, whereas the functions starting withASN1_TIMEwill operate on either format. flexible queen behaviour ๐ - Users familiar with RFC822 should note that when specifying the flag
ASN1_DTFLGS_RFC822the year will be formatted as documented above, i.e., using 4 digits, not 2 as specified in RFC822. not me calling out RFC822 like this ๐ญ
๐ BUGS ๐
the fine print, bestie, read it before you ship ๐จ
ASN1_TIME_print(),ASN1_UTCTIME_print()andASN1_GENERALIZEDTIME_print()do not print out the timezone: it either prints out"GMT"or nothing. but all certificates complying with RFC5280 et al use GMT anyway, so likeโฆ we move ๐ถโโ๏ธASN1_TIME_print(),ASN1_TIME_print_ex(),ASN1_UTCTIME_print()andASN1_GENERALIZEDTIME_print()do not distinguish if they fail because of an I/O error or an invalid time format. one error, two possible causes, no clarification ๐- Use the
ASN1_TIME_normalize()function to normalize the time value before printing to get GMT results. this is the fix, babes, use it ๐ซถ
๐ RETURN VALUES ๐
what you actually get back, no guessing:
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.
and bestie, we are not done โ there's more page where that came from, so don't go anywhere ๐
๐ธ EXAMPLES ๐ธ
Okay bestie, here's where it gets fun โ actual code you can vibe with. โจ
๐ Set a time structure to one hour after the current time and print it out
Like, literally "what time is it gonna be in an hour" and then shout it at the terminal. Iconic behaviour, no notes. ๐
#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);
See how the 60 * 60 is doing the heavy lifting? That's one hour in seconds, bestie. Math ate. ๐งฎโจ And ASN1_TIME_adj with NULL as the first arg means "make me a fresh one, don't touch anything existing" โ main character energy fr. And then we clean up with ASN1_STRING_free and BIO_free because we are responsible queens, periodt. ๐
โณ Determine if one time is later or sooner than the current time
The classic "is this before or after now" moment. We're comparing to to here, and NULL means now, iykyk.
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");
Lowkey the day/sec combo is such a vibe โ positive means later, negative means sooner, both zero means it's literally the same moment. And if ASN1_TIME_diff returns falsy? ๐จ That's the invalid time format case, so don't you dare treat that as "Same". Guard your code, bestie. ๐
๐ HISTORY ๐
So like, the glow-up timeline of these functions โ all of them dropped in OpenSSL 1.1.1, which was a moment. ๐
- 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.
Five functions, one release. That's not a debut, that's a whole era. ๐ฆโจ
๐ COPYRIGHT ๐
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 though โ "you may not use this file except in compliance with the License" is not a drill. Read the license, bestie. It's giving legal, not optional. ๐
And that's the whole page, babes! ๐ซถ This one's from OpenSSL 3.5.9, dated 2026-09-29 โ so yeah, fresh, current, and absolutely slaying in the ASN1_TIME_SET(3ossl) section. Go forth and handle your timestamps responsibly. Time zones can't hurt you anymore. ๐โจ๐
๐ฆ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 ยท Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 ยท Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 ยท Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 ยท Apache-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms