1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos ASN1_TIME_set, ASN1_UTCTIME_set, ASN1_GENERALIZEDTIME_set, 6 1.1 christos ASN1_TIME_adj, ASN1_UTCTIME_adj, ASN1_GENERALIZEDTIME_adj, 7 1.1 christos ASN1_TIME_check, ASN1_UTCTIME_check, ASN1_GENERALIZEDTIME_check, 8 1.1 christos ASN1_TIME_set_string, ASN1_UTCTIME_set_string, ASN1_GENERALIZEDTIME_set_string, 9 1.1 christos ASN1_TIME_set_string_X509, 10 1.1 christos ASN1_TIME_normalize, 11 1.1 christos ASN1_TIME_to_tm, 12 1.2 christos ASN1_TIME_print, ASN1_TIME_print_ex, ASN1_UTCTIME_print, ASN1_GENERALIZEDTIME_print, 13 1.1 christos ASN1_TIME_diff, 14 1.1 christos ASN1_TIME_cmp_time_t, ASN1_UTCTIME_cmp_time_t, 15 1.1 christos ASN1_TIME_compare, 16 1.2 christos ASN1_TIME_to_generalizedtime, 17 1.2 christos ASN1_TIME_dup, ASN1_UTCTIME_dup, ASN1_GENERALIZEDTIME_dup - ASN.1 Time functions 18 1.1 christos 19 1.1 christos =head1 SYNOPSIS 20 1.1 christos 21 1.1 christos ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t); 22 1.1 christos ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t); 23 1.1 christos ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, 24 1.1 christos time_t t); 25 1.1 christos 26 1.1 christos ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day, 27 1.1 christos long offset_sec); 28 1.1 christos ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, 29 1.1 christos int offset_day, long offset_sec); 30 1.1 christos ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, 31 1.1 christos time_t t, int offset_day, 32 1.1 christos long offset_sec); 33 1.1 christos 34 1.1 christos int ASN1_TIME_set_string(ASN1_TIME *s, const char *str); 35 1.1 christos int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str); 36 1.1 christos int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str); 37 1.1 christos int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, 38 1.1 christos const char *str); 39 1.1 christos 40 1.1 christos int ASN1_TIME_normalize(ASN1_TIME *s); 41 1.1 christos 42 1.1 christos int ASN1_TIME_check(const ASN1_TIME *t); 43 1.1 christos int ASN1_UTCTIME_check(const ASN1_UTCTIME *t); 44 1.1 christos int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *t); 45 1.1 christos 46 1.1 christos int ASN1_TIME_print(BIO *b, const ASN1_TIME *s); 47 1.2 christos int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags); 48 1.1 christos int ASN1_UTCTIME_print(BIO *b, const ASN1_UTCTIME *s); 49 1.1 christos int ASN1_GENERALIZEDTIME_print(BIO *b, const ASN1_GENERALIZEDTIME *s); 50 1.1 christos 51 1.1 christos int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm); 52 1.1 christos int ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from, 53 1.1 christos const ASN1_TIME *to); 54 1.1 christos 55 1.1 christos int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t); 56 1.1 christos int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t); 57 1.1 christos 58 1.1 christos int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b); 59 1.1 christos 60 1.1 christos ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, 61 1.1 christos ASN1_GENERALIZEDTIME **out); 62 1.1 christos 63 1.2 christos ASN1_TIME *ASN1_TIME_dup(const ASN1_TIME *t); 64 1.2 christos ASN1_UTCTIME *ASN1_UTCTIME_dup(const ASN1_UTCTIME *t); 65 1.2 christos ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_dup(const ASN1_GENERALIZEDTIME *t); 66 1.2 christos 67 1.1 christos =head1 DESCRIPTION 68 1.1 christos 69 1.1 christos The ASN1_TIME_set(), ASN1_UTCTIME_set() and ASN1_GENERALIZEDTIME_set() 70 1.2 christos functions set the structure I<s> to the time represented by the time_t 71 1.2 christos value I<t>. If I<s> is NULL a new time structure is allocated and returned. 72 1.1 christos 73 1.1 christos The ASN1_TIME_adj(), ASN1_UTCTIME_adj() and ASN1_GENERALIZEDTIME_adj() 74 1.2 christos functions set the time structure I<s> to the time represented 75 1.2 christos by the time I<offset_day> and I<offset_sec> after the time_t value I<t>. 76 1.2 christos The values of I<offset_day> or I<offset_sec> can be negative to set a 77 1.2 christos time before I<t>. The I<offset_sec> value can also exceed the number of 78 1.2 christos seconds in a day. If I<s> is NULL a new structure is allocated 79 1.1 christos and returned. 80 1.1 christos 81 1.1 christos The ASN1_TIME_set_string(), ASN1_UTCTIME_set_string() and 82 1.2 christos ASN1_GENERALIZEDTIME_set_string() functions set the time structure I<s> 83 1.2 christos to the time represented by string I<str> which must be in appropriate ASN.1 84 1.2 christos time format (for example YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ). If I<s> is NULL 85 1.2 christos this function performs a format check on I<str> only. The string I<str> 86 1.2 christos is copied into I<s>. 87 1.1 christos 88 1.2 christos ASN1_TIME_set_string_X509() sets B<ASN1_TIME> structure I<s> to the time 89 1.2 christos represented by string I<str> which must be in appropriate time format 90 1.1 christos that RFC 5280 requires, which means it only allows YYMMDDHHMMSSZ and 91 1.1 christos YYYYMMDDHHMMSSZ (leap second is rejected), all other ASN.1 time format 92 1.2 christos are not allowed. If I<s> is NULL this function performs a format check 93 1.2 christos on I<str> only. 94 1.1 christos 95 1.2 christos The ASN1_TIME_normalize() function converts an B<ASN1_GENERALIZEDTIME> or 96 1.2 christos B<ASN1_UTCTIME> into a time value that can be used in a certificate. It 97 1.1 christos should be used after the ASN1_TIME_set_string() functions and before 98 1.1 christos ASN1_TIME_print() functions to get consistent (i.e. GMT) results. 99 1.1 christos 100 1.1 christos The ASN1_TIME_check(), ASN1_UTCTIME_check() and ASN1_GENERALIZEDTIME_check() 101 1.2 christos functions check the syntax of the time structure I<s>. 102 1.1 christos 103 1.1 christos The ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() 104 1.2 christos functions print the time structure I<s> to BIO I<b> in human readable 105 1.3 christos format. It will be of the format MMM DD HH:MM:SS[.s*] YYYY GMT, for example 106 1.3 christos "Feb E<32>3 00:55:52 2015 GMT", which does not include a newline. 107 1.2 christos If the time structure has invalid format it prints out "Bad time value" and 108 1.2 christos returns an error. The output for generalized time may include a fractional part 109 1.1 christos following the second. 110 1.1 christos 111 1.2 christos ASN1_TIME_print_ex() provides I<flags> to specify the output format of the 112 1.2 christos datetime. This can be either B<ASN1_DTFLGS_RFC822> or B<ASN1_DTFLGS_ISO8601>. 113 1.2 christos 114 1.2 christos ASN1_TIME_to_tm() converts the time I<s> to the standard I<tm> structure. 115 1.2 christos If I<s> is NULL, then the current time is converted. The output time is GMT. 116 1.2 christos The I<tm_sec>, I<tm_min>, I<tm_hour>, I<tm_mday>, I<tm_wday>, I<tm_yday>, 117 1.2 christos I<tm_mon> and I<tm_year> fields of I<tm> structure are set to proper values, 118 1.2 christos whereas all other fields are set to 0. If I<tm> is NULL this function performs 119 1.2 christos a format check on I<s> only. If I<s> is in Generalized format with fractional 120 1.1 christos seconds, e.g. YYYYMMDDHHMMSS.SSSZ, the fractional seconds will be lost while 121 1.2 christos converting I<s> to I<tm> structure. 122 1.1 christos 123 1.2 christos ASN1_TIME_diff() sets I<*pday> and I<*psec> to the time difference between 124 1.2 christos I<from> and I<to>. If I<to> represents a time later than I<from> then 125 1.2 christos one or both (depending on the time difference) of I<*pday> and I<*psec> 126 1.2 christos will be positive. If I<to> represents a time earlier than I<from> then 127 1.2 christos one or both of I<*pday> and I<*psec> will be negative. If I<to> and I<from> 128 1.2 christos represent the same time then I<*pday> and I<*psec> will both be zero. 129 1.2 christos If both I<*pday> and I<*psec> are nonzero they will always have the same 130 1.2 christos sign. The value of I<*psec> will always be less than the number of seconds 131 1.2 christos in a day. If I<from> or I<to> is NULL the current time is used. 132 1.1 christos 133 1.1 christos The ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() functions compare 134 1.2 christos the two times represented by the time structure I<s> and the time_t I<t>. 135 1.1 christos 136 1.1 christos The ASN1_TIME_compare() function compares the two times represented by the 137 1.2 christos time structures I<a> and I<b>. 138 1.1 christos 139 1.2 christos The ASN1_TIME_to_generalizedtime() function converts an B<ASN1_TIME> to an 140 1.2 christos B<ASN1_GENERALIZEDTIME>, regardless of year. If either I<out> or 141 1.2 christos I<*out> are NULL, then a new object is allocated and must be freed after use. 142 1.2 christos 143 1.2 christos The ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() functions 144 1.2 christos duplicate the time structure I<t> and return the duplicated result 145 1.2 christos correspondingly. 146 1.1 christos 147 1.1 christos =head1 NOTES 148 1.1 christos 149 1.2 christos The B<ASN1_TIME> structure corresponds to the ASN.1 structure B<Time> 150 1.1 christos defined in RFC5280 et al. The time setting functions obey the rules outlined 151 1.1 christos in RFC5280: if the date can be represented by UTCTime it is used, else 152 1.1 christos GeneralizedTime is used. 153 1.1 christos 154 1.2 christos The B<ASN1_TIME>, B<ASN1_UTCTIME> and B<ASN1_GENERALIZEDTIME> structures are 155 1.2 christos represented as an B<ASN1_STRING> internally and can be freed up using 156 1.2 christos ASN1_STRING_free(). 157 1.1 christos 158 1.2 christos The B<ASN1_TIME> structure can represent years from 0000 to 9999 but no attempt 159 1.1 christos is made to correct ancient calendar changes (for example from Julian to 160 1.1 christos Gregorian calendars). 161 1.1 christos 162 1.2 christos B<ASN1_UTCTIME> is limited to a year range of 1950 through 2049. 163 1.1 christos 164 1.1 christos Some applications add offset times directly to a time_t value and pass the 165 1.1 christos results to ASN1_TIME_set() (or equivalent). This can cause problems as the 166 1.1 christos time_t value can overflow on some systems resulting in unexpected results. 167 1.1 christos New applications should use ASN1_TIME_adj() instead and pass the offset value 168 1.2 christos in the I<offset_sec> and I<offset_day> parameters instead of directly 169 1.1 christos manipulating a time_t value. 170 1.1 christos 171 1.2 christos ASN1_TIME_adj() may change the type from B<ASN1_GENERALIZEDTIME> to 172 1.2 christos B<ASN1_UTCTIME>, or vice versa, based on the resulting year. 173 1.2 christos ASN1_GENERALIZEDTIME_adj() and ASN1_UTCTIME_adj() will not modify the type 174 1.2 christos of the return structure. 175 1.2 christos 176 1.2 christos It is recommended that functions starting with B<ASN1_TIME> be used instead of 177 1.2 christos those starting with B<ASN1_UTCTIME> or B<ASN1_GENERALIZEDTIME>. The functions 178 1.2 christos starting with B<ASN1_UTCTIME> and B<ASN1_GENERALIZEDTIME> act only on that 179 1.2 christos specific time format. The functions starting with B<ASN1_TIME> will operate on 180 1.2 christos either format. 181 1.1 christos 182 1.3 christos Users familiar with RFC822 should note that when specifying the flag 183 1.3 christos B<ASN1_DTFLGS_RFC822> the year will be formatted as documented above, 184 1.3 christos i.e., using 4 digits, not 2 as specified in RFC822. 185 1.3 christos 186 1.1 christos =head1 BUGS 187 1.1 christos 188 1.2 christos ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() do 189 1.2 christos not print out the timezone: it either prints out "GMT" or nothing. But all 190 1.1 christos certificates complying with RFC5280 et al use GMT anyway. 191 1.1 christos 192 1.2 christos ASN1_TIME_print(), ASN1_TIME_print_ex(), ASN1_UTCTIME_print() and 193 1.2 christos ASN1_GENERALIZEDTIME_print() do not distinguish if they fail because 194 1.2 christos of an I/O error or invalid time format. 195 1.2 christos 196 1.1 christos Use the ASN1_TIME_normalize() function to normalize the time value before 197 1.1 christos printing to get GMT results. 198 1.1 christos 199 1.2 christos =head1 RETURN VALUES 200 1.2 christos 201 1.2 christos ASN1_TIME_set(), ASN1_UTCTIME_set(), ASN1_GENERALIZEDTIME_set(), 202 1.2 christos ASN1_TIME_adj(), ASN1_UTCTIME_adj() and ASN1_GENERALIZEDTIME_set() return 203 1.2 christos a pointer to a time structure or NULL if an error occurred. 204 1.2 christos 205 1.2 christos ASN1_TIME_set_string(), ASN1_UTCTIME_set_string(), 206 1.2 christos ASN1_GENERALIZEDTIME_set_string() and ASN1_TIME_set_string_X509() return 207 1.2 christos 1 if the time value is successfully set and 0 otherwise. 208 1.2 christos 209 1.2 christos ASN1_TIME_normalize() returns 1 on success, and 0 on error. 210 1.2 christos 211 1.2 christos ASN1_TIME_check(), ASN1_UTCTIME_check and ASN1_GENERALIZEDTIME_check() return 1 212 1.2 christos if the structure is syntactically correct and 0 otherwise. 213 1.2 christos 214 1.2 christos ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() 215 1.2 christos return 1 if the time is successfully printed out and 216 1.2 christos 0 if an I/O error occurred an error occurred (I/O error or invalid time format). 217 1.2 christos 218 1.2 christos ASN1_TIME_to_tm() returns 1 if the time is successfully parsed and 0 if an 219 1.2 christos error occurred (invalid time format). 220 1.2 christos 221 1.2 christos ASN1_TIME_diff() returns 1 for success and 0 for failure. It can fail if the 222 1.2 christos passed-in time structure has invalid syntax, for example. 223 1.2 christos 224 1.2 christos ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() return -1 if I<s> is 225 1.2 christos before I<t>, 0 if I<s> equals I<t>, or 1 if I<s> is after I<t>. -2 is returned 226 1.2 christos on error. 227 1.2 christos 228 1.2 christos ASN1_TIME_compare() returns -1 if I<a> is before I<b>, 0 if I<a> equals I<b>, 229 1.2 christos or 1 if I<a> is after I<b>. -2 is returned on error. 230 1.2 christos 231 1.2 christos ASN1_TIME_to_generalizedtime() returns a pointer to the appropriate time 232 1.2 christos structure on success or NULL if an error occurred. 233 1.2 christos 234 1.2 christos ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() return a 235 1.2 christos pointer to a time structure or NULL if an error occurred. 236 1.2 christos 237 1.1 christos =head1 EXAMPLES 238 1.1 christos 239 1.1 christos Set a time structure to one hour after the current time and print it out: 240 1.1 christos 241 1.1 christos #include <time.h> 242 1.1 christos #include <openssl/asn1.h> 243 1.1 christos 244 1.1 christos ASN1_TIME *tm; 245 1.1 christos time_t t; 246 1.1 christos BIO *b; 247 1.1 christos 248 1.1 christos t = time(NULL); 249 1.1 christos tm = ASN1_TIME_adj(NULL, t, 0, 60 * 60); 250 1.1 christos b = BIO_new_fp(stdout, BIO_NOCLOSE); 251 1.1 christos ASN1_TIME_print(b, tm); 252 1.1 christos ASN1_STRING_free(tm); 253 1.1 christos BIO_free(b); 254 1.1 christos 255 1.1 christos Determine if one time is later or sooner than the current time: 256 1.1 christos 257 1.1 christos int day, sec; 258 1.1 christos 259 1.1 christos if (!ASN1_TIME_diff(&day, &sec, NULL, to)) 260 1.1 christos /* Invalid time format */ 261 1.1 christos 262 1.1 christos if (day > 0 || sec > 0) 263 1.1 christos printf("Later\n"); 264 1.1 christos else if (day < 0 || sec < 0) 265 1.1 christos printf("Sooner\n"); 266 1.1 christos else 267 1.1 christos printf("Same\n"); 268 1.1 christos 269 1.1 christos =head1 HISTORY 270 1.1 christos 271 1.1 christos The ASN1_TIME_to_tm() function was added in OpenSSL 1.1.1. 272 1.1 christos The ASN1_TIME_set_string_X509() function was added in OpenSSL 1.1.1. 273 1.1 christos The ASN1_TIME_normalize() function was added in OpenSSL 1.1.1. 274 1.1 christos The ASN1_TIME_cmp_time_t() function was added in OpenSSL 1.1.1. 275 1.1 christos The ASN1_TIME_compare() function was added in OpenSSL 1.1.1. 276 1.1 christos 277 1.1 christos =head1 COPYRIGHT 278 1.1 christos 279 1.3 christos Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved. 280 1.1 christos 281 1.2 christos Licensed under the Apache License 2.0 (the "License"). You may not use 282 1.1 christos this file except in compliance with the License. You can obtain a copy 283 1.1 christos in the file LICENSE in the source distribution or at 284 1.1 christos L<https://www.openssl.org/source/license.html>. 285 1.1 christos 286 1.1 christos =cut 287