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_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

…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 structure s to whatever time the time_t value t represents. and if you hand them s as NULL? 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 structure s to the time you get when you take the time_t value t and add offset_day and offset_sec to it. and listen: both offset_day and offset_sec can be negative if you wanna go back in time before t πŸ•°οΈ. also offset_sec is allowed to be bigger than the number of seconds in a whole day, so go off. and same as before, if s is NULL a 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 structure s to the time represented by the string str. and str must be in the appropriate ASN.1 time format (for example YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ). if s is NULL this function only does a format check on str, nothing else. the string str gets copied into s. πŸ’–
  • ASN1_TIME_set_string_X509() β€” sets ASN1_TIME structure s to the time represented by string str, which must be in the appropriate time format that RFC 5280 requires. what does that mean in practice? it only allows YYMMDDHHMMSSZ and YYYYMMDDHHMMSSZ β€” and just so we're clear, leap second is rejected 😭 β€” and every other ASN.1 time format is simply not allowed. if s is NULL this function only performs a format check on str.
  • ASN1_TIME_normalize() β€” converts an ASN1_GENERALIZEDTIME or ASN1_UTCTIME into a time value that can actually be used in a certificate. the move here is: call it after the ASN1_TIME_set_string() functions and before the ASN1_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 structure s. simple as.
  • ASN1_TIME_print(), ASN1_UTCTIME_print(), ASN1_GENERALIZEDTIME_print() β€” these print the time structure s to the BIO b in a human readable format. the format is MMM 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 either ASN1_DTFLGS_RFC822 or ASN1_DTFLGS_ISO8601. pick your fighter πŸŽ€

πŸŽ€ to_tm + diff

  • ASN1_TIME_to_tm() β€” converts the time s to the standard tm structure. if s is NULL, then the current time is what gets converted. the output time is GMT. the tm_sec, tm_min, tm_hour, tm_mday, tm_wday, tm_yday, tm_mon and tm_year fields of the tm structure get set to proper values, while all the other fields are set to 0. if tm is NULL, this function only performs a format check on s. and heads up: if s is in Generalized format with fractional seconds, like YYYYMMDDHHMMSS.SSSZ, then the fractional seconds are just gonna be lost while converting s to the tm structure. sorry bestie, them's the rules 😭
  • ASN1_TIME_diff() β€” sets *pday and *psec to the time difference between from and to. here's the vibe check on signs:
    • if to represents a time later than from, then one or both (depending on the time difference) of *pday and *psec will be positive.
    • if to represents a time earlier than from, then one or both of *pday and *psec will be negative.
    • if to and from represent the same time, then *pday and *psec will both be zero.
    • if both *pday and *psec are nonzero, they will always have the same sign.
    • the value of *psec will always be less than the number of seconds in a day.
    • and if from or to is NULL, the current time is used.

πŸŽ€ comparing

  • ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() β€” these compare the two times represented by the time structure s and the time_t t.
  • ASN1_TIME_compare() β€” compares the two times represented by the time structures a and b.

πŸŽ€ converting + duplicating

  • ASN1_TIME_to_generalizedtime() β€” converts an ASN1_TIME to an ASN1_GENERALIZEDTIME, regardless of year. if either out or *out are NULL, 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 structure t and 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() and ASN1_GENERALIZEDTIME_set() return a pointer to a time structure or NULL if an error occurred.
  • ASN1_TIME_set_string(), ASN1_UTCTIME_set_string(), ASN1_GENERALIZEDTIME_set_string() and ASN1_TIME_set_string_X509() return 1 if the time value is successfully set and 0 otherwise.
  • ASN1_TIME_normalize() returns 1 on success, and 0 on error.
  • ASN1_TIME_check(), ASN1_UTCTIME_check and ASN1_GENERALIZEDTIME_check() return 1 if the structure is syntactically correct and 0 otherwise.
  • ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() return 1 if the time is successfully printed out and 0 if an I/O error occurred an error occurred (I/O error or invalid time format).
  • ASN1_TIME_to_tm() returns 1 if the time is successfully parsed and 0 if an error occurred (invalid time format).
  • ASN1_TIME_diff() returns 1 for success and 0 for failure. it can fail if the passed-in time structure has invalid syntax, for example.
  • ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() return -1 if s is before t, 0 if s equals t, or 1 if s is after t. -2 is returned on error.
  • ASN1_TIME_compare() returns -1 if a is before b, 0 if a equals b, or 1 if a is after b. -2 is returned on error.
  • ASN1_TIME_to_generalizedtime() returns a pointer to the appropriate time structure on success or NULL if an error occurred.
  • ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() return a pointer to a time structure or NULL if 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.


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