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