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