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