xref: /netbsd-src/crypto/external/bsd/openssl/dist/doc/man3/ASN1_TIME_set.pod (revision b0d1725196a7921d003d2c66a14f186abda4176b)
113d40330Schristos=pod
213d40330Schristos
313d40330Schristos=head1 NAME
413d40330Schristos
513d40330SchristosASN1_TIME_set, ASN1_UTCTIME_set, ASN1_GENERALIZEDTIME_set,
613d40330SchristosASN1_TIME_adj, ASN1_UTCTIME_adj, ASN1_GENERALIZEDTIME_adj,
713d40330SchristosASN1_TIME_check, ASN1_UTCTIME_check, ASN1_GENERALIZEDTIME_check,
813d40330SchristosASN1_TIME_set_string, ASN1_UTCTIME_set_string, ASN1_GENERALIZEDTIME_set_string,
913d40330SchristosASN1_TIME_set_string_X509,
1013d40330SchristosASN1_TIME_normalize,
1113d40330SchristosASN1_TIME_to_tm,
12*b0d17251SchristosASN1_TIME_print, ASN1_TIME_print_ex, ASN1_UTCTIME_print, ASN1_GENERALIZEDTIME_print,
1313d40330SchristosASN1_TIME_diff,
1413d40330SchristosASN1_TIME_cmp_time_t, ASN1_UTCTIME_cmp_time_t,
1513d40330SchristosASN1_TIME_compare,
16*b0d17251SchristosASN1_TIME_to_generalizedtime,
17*b0d17251SchristosASN1_TIME_dup, ASN1_UTCTIME_dup, ASN1_GENERALIZEDTIME_dup - ASN.1 Time functions
1813d40330Schristos
1913d40330Schristos=head1 SYNOPSIS
2013d40330Schristos
2113d40330Schristos ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
2213d40330Schristos ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t);
2313d40330Schristos ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
2413d40330Schristos                                                time_t t);
2513d40330Schristos
2613d40330Schristos ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
2713d40330Schristos                          long offset_sec);
2813d40330Schristos ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
2913d40330Schristos                                int offset_day, long offset_sec);
3013d40330Schristos ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
3113d40330Schristos                                                time_t t, int offset_day,
3213d40330Schristos                                                long offset_sec);
3313d40330Schristos
3413d40330Schristos int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
3513d40330Schristos int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str);
3613d40330Schristos int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
3713d40330Schristos int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s,
3813d40330Schristos                                     const char *str);
3913d40330Schristos
4013d40330Schristos int ASN1_TIME_normalize(ASN1_TIME *s);
4113d40330Schristos
4213d40330Schristos int ASN1_TIME_check(const ASN1_TIME *t);
4313d40330Schristos int ASN1_UTCTIME_check(const ASN1_UTCTIME *t);
4413d40330Schristos int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *t);
4513d40330Schristos
4613d40330Schristos int ASN1_TIME_print(BIO *b, const ASN1_TIME *s);
47*b0d17251Schristos int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags);
4813d40330Schristos int ASN1_UTCTIME_print(BIO *b, const ASN1_UTCTIME *s);
4913d40330Schristos int ASN1_GENERALIZEDTIME_print(BIO *b, const ASN1_GENERALIZEDTIME *s);
5013d40330Schristos
5113d40330Schristos int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm);
5213d40330Schristos int ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from,
5313d40330Schristos                    const ASN1_TIME *to);
5413d40330Schristos
5513d40330Schristos int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t);
5613d40330Schristos int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
5713d40330Schristos
5813d40330Schristos int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b);
5913d40330Schristos
6013d40330Schristos ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
6113d40330Schristos                                                    ASN1_GENERALIZEDTIME **out);
6213d40330Schristos
63*b0d17251Schristos ASN1_TIME *ASN1_TIME_dup(const ASN1_TIME *t);
64*b0d17251Schristos ASN1_UTCTIME *ASN1_UTCTIME_dup(const ASN1_UTCTIME *t);
65*b0d17251Schristos ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_dup(const ASN1_GENERALIZEDTIME *t);
66*b0d17251Schristos
6713d40330Schristos=head1 DESCRIPTION
6813d40330Schristos
6913d40330SchristosThe ASN1_TIME_set(), ASN1_UTCTIME_set() and ASN1_GENERALIZEDTIME_set()
70*b0d17251Schristosfunctions set the structure I<s> to the time represented by the time_t
71*b0d17251Schristosvalue I<t>. If I<s> is NULL a new time structure is allocated and returned.
7213d40330Schristos
7313d40330SchristosThe ASN1_TIME_adj(), ASN1_UTCTIME_adj() and ASN1_GENERALIZEDTIME_adj()
74*b0d17251Schristosfunctions set the time structure I<s> to the time represented
75*b0d17251Schristosby the time I<offset_day> and I<offset_sec> after the time_t value I<t>.
76*b0d17251SchristosThe values of I<offset_day> or I<offset_sec> can be negative to set a
77*b0d17251Schristostime before I<t>. The I<offset_sec> value can also exceed the number of
78*b0d17251Schristosseconds in a day. If I<s> is NULL a new structure is allocated
7913d40330Schristosand returned.
8013d40330Schristos
8113d40330SchristosThe ASN1_TIME_set_string(), ASN1_UTCTIME_set_string() and
82*b0d17251SchristosASN1_GENERALIZEDTIME_set_string() functions set the time structure I<s>
83*b0d17251Schristosto the time represented by string I<str> which must be in appropriate ASN.1
84*b0d17251Schristostime format (for example YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ). If I<s> is NULL
85*b0d17251Schristosthis function performs a format check on I<str> only. The string I<str>
86*b0d17251Schristosis copied into I<s>.
8713d40330Schristos
88*b0d17251SchristosASN1_TIME_set_string_X509() sets B<ASN1_TIME> structure I<s> to the time
89*b0d17251Schristosrepresented by string I<str> which must be in appropriate time format
9013d40330Schristosthat RFC 5280 requires, which means it only allows YYMMDDHHMMSSZ and
9113d40330SchristosYYYYMMDDHHMMSSZ (leap second is rejected), all other ASN.1 time format
92*b0d17251Schristosare not allowed. If I<s> is NULL this function performs a format check
93*b0d17251Schristoson I<str> only.
9413d40330Schristos
95*b0d17251SchristosThe ASN1_TIME_normalize() function converts an B<ASN1_GENERALIZEDTIME> or
96*b0d17251SchristosB<ASN1_UTCTIME> into a time value that can be used in a certificate. It
9713d40330Schristosshould be used after the ASN1_TIME_set_string() functions and before
9813d40330SchristosASN1_TIME_print() functions to get consistent (i.e. GMT) results.
9913d40330Schristos
10013d40330SchristosThe ASN1_TIME_check(), ASN1_UTCTIME_check() and ASN1_GENERALIZEDTIME_check()
101*b0d17251Schristosfunctions check the syntax of the time structure I<s>.
10213d40330Schristos
10313d40330SchristosThe ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print()
104*b0d17251Schristosfunctions print the time structure I<s> to BIO I<b> in human readable
10513d40330Schristosformat. It will be of the format MMM DD HH:MM:SS YYYY [GMT], for example
106*b0d17251Schristos"Feb  3 00:55:52 2015 GMT", which does not include a newline.
107*b0d17251SchristosIf the time structure has invalid format it prints out "Bad time value" and
108*b0d17251Schristosreturns an error. The output for generalized time may include a fractional part
10913d40330Schristosfollowing the second.
11013d40330Schristos
111*b0d17251SchristosASN1_TIME_print_ex() provides I<flags> to specify the output format of the
112*b0d17251Schristosdatetime. This can be either B<ASN1_DTFLGS_RFC822> or B<ASN1_DTFLGS_ISO8601>.
11313d40330Schristos
114*b0d17251SchristosASN1_TIME_to_tm() converts the time I<s> to the standard I<tm> structure.
115*b0d17251SchristosIf I<s> is NULL, then the current time is converted. The output time is GMT.
116*b0d17251SchristosThe I<tm_sec>, I<tm_min>, I<tm_hour>, I<tm_mday>, I<tm_wday>, I<tm_yday>,
117*b0d17251SchristosI<tm_mon> and I<tm_year> fields of I<tm> structure are set to proper values,
118*b0d17251Schristoswhereas all other fields are set to 0. If I<tm> is NULL this function performs
119*b0d17251Schristosa format check on I<s> only. If I<s> is in Generalized format with fractional
120*b0d17251Schristosseconds, e.g. YYYYMMDDHHMMSS.SSSZ, the fractional seconds will be lost while
121*b0d17251Schristosconverting I<s> to I<tm> structure.
122*b0d17251Schristos
123*b0d17251SchristosASN1_TIME_diff() sets I<*pday> and I<*psec> to the time difference between
124*b0d17251SchristosI<from> and I<to>. If I<to> represents a time later than I<from> then
125*b0d17251Schristosone or both (depending on the time difference) of I<*pday> and I<*psec>
126*b0d17251Schristoswill be positive. If I<to> represents a time earlier than I<from> then
127*b0d17251Schristosone or both of I<*pday> and I<*psec> will be negative. If I<to> and I<from>
128*b0d17251Schristosrepresent the same time then I<*pday> and I<*psec> will both be zero.
129*b0d17251SchristosIf both I<*pday> and I<*psec> are nonzero they will always have the same
130*b0d17251Schristossign. The value of I<*psec> will always be less than the number of seconds
131*b0d17251Schristosin a day. If I<from> or I<to> is NULL the current time is used.
13213d40330Schristos
13313d40330SchristosThe ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() functions compare
134*b0d17251Schristosthe two times represented by the time structure I<s> and the time_t I<t>.
13513d40330Schristos
13613d40330SchristosThe ASN1_TIME_compare() function compares the two times represented by the
137*b0d17251Schristostime structures I<a> and I<b>.
13813d40330Schristos
139*b0d17251SchristosThe ASN1_TIME_to_generalizedtime() function converts an B<ASN1_TIME> to an
140*b0d17251SchristosB<ASN1_GENERALIZEDTIME>, regardless of year. If either I<out> or
141*b0d17251SchristosI<*out> are NULL, then a new object is allocated and must be freed after use.
142*b0d17251Schristos
143*b0d17251SchristosThe ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() functions
144*b0d17251Schristosduplicate the time structure I<t> and return the duplicated result
145*b0d17251Schristoscorrespondingly.
14613d40330Schristos
14713d40330Schristos=head1 NOTES
14813d40330Schristos
149*b0d17251SchristosThe B<ASN1_TIME> structure corresponds to the ASN.1 structure B<Time>
15013d40330Schristosdefined in RFC5280 et al. The time setting functions obey the rules outlined
15113d40330Schristosin RFC5280: if the date can be represented by UTCTime it is used, else
15213d40330SchristosGeneralizedTime is used.
15313d40330Schristos
154*b0d17251SchristosThe B<ASN1_TIME>, B<ASN1_UTCTIME> and B<ASN1_GENERALIZEDTIME> structures are
155*b0d17251Schristosrepresented as an B<ASN1_STRING> internally and can be freed up using
156*b0d17251SchristosASN1_STRING_free().
15713d40330Schristos
158*b0d17251SchristosThe B<ASN1_TIME> structure can represent years from 0000 to 9999 but no attempt
15913d40330Schristosis made to correct ancient calendar changes (for example from Julian to
16013d40330SchristosGregorian calendars).
16113d40330Schristos
162*b0d17251SchristosB<ASN1_UTCTIME> is limited to a year range of 1950 through 2049.
16313d40330Schristos
16413d40330SchristosSome applications add offset times directly to a time_t value and pass the
16513d40330Schristosresults to ASN1_TIME_set() (or equivalent). This can cause problems as the
16613d40330Schristostime_t value can overflow on some systems resulting in unexpected results.
16713d40330SchristosNew applications should use ASN1_TIME_adj() instead and pass the offset value
168*b0d17251Schristosin the I<offset_sec> and I<offset_day> parameters instead of directly
16913d40330Schristosmanipulating a time_t value.
17013d40330Schristos
171*b0d17251SchristosASN1_TIME_adj() may change the type from B<ASN1_GENERALIZEDTIME> to
172*b0d17251SchristosB<ASN1_UTCTIME>, or vice versa, based on the resulting year.
173*b0d17251SchristosASN1_GENERALIZEDTIME_adj() and ASN1_UTCTIME_adj() will not modify the type
174*b0d17251Schristosof the return structure.
17513d40330Schristos
176*b0d17251SchristosIt is recommended that functions starting with B<ASN1_TIME> be used instead of
177*b0d17251Schristosthose starting with B<ASN1_UTCTIME> or B<ASN1_GENERALIZEDTIME>. The functions
178*b0d17251Schristosstarting with B<ASN1_UTCTIME> and B<ASN1_GENERALIZEDTIME> act only on that
179*b0d17251Schristosspecific time format. The functions starting with B<ASN1_TIME> will operate on
180*b0d17251Schristoseither format.
18113d40330Schristos
18213d40330Schristos=head1 BUGS
18313d40330Schristos
184*b0d17251SchristosASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() do
185*b0d17251Schristosnot print out the timezone: it either prints out "GMT" or nothing. But all
18613d40330Schristoscertificates complying with RFC5280 et al use GMT anyway.
18713d40330Schristos
188*b0d17251SchristosASN1_TIME_print(), ASN1_TIME_print_ex(), ASN1_UTCTIME_print() and
189*b0d17251SchristosASN1_GENERALIZEDTIME_print() do not distinguish if they fail because
190*b0d17251Schristosof an I/O error or invalid time format.
191*b0d17251Schristos
19213d40330SchristosUse the ASN1_TIME_normalize() function to normalize the time value before
19313d40330Schristosprinting to get GMT results.
19413d40330Schristos
19513d40330Schristos=head1 RETURN VALUES
19613d40330Schristos
197*b0d17251SchristosASN1_TIME_set(), ASN1_UTCTIME_set(), ASN1_GENERALIZEDTIME_set(),
198*b0d17251SchristosASN1_TIME_adj(), ASN1_UTCTIME_adj() and ASN1_GENERALIZEDTIME_set() return
199*b0d17251Schristosa pointer to a time structure or NULL if an error occurred.
20013d40330Schristos
201*b0d17251SchristosASN1_TIME_set_string(), ASN1_UTCTIME_set_string(),
202*b0d17251SchristosASN1_GENERALIZEDTIME_set_string() and ASN1_TIME_set_string_X509() return
203*b0d17251Schristos1 if the time value is successfully set and 0 otherwise.
20413d40330Schristos
20513d40330SchristosASN1_TIME_normalize() returns 1 on success, and 0 on error.
20613d40330Schristos
20713d40330SchristosASN1_TIME_check(), ASN1_UTCTIME_check and ASN1_GENERALIZEDTIME_check() return 1
20813d40330Schristosif the structure is syntactically correct and 0 otherwise.
20913d40330Schristos
210*b0d17251SchristosASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print()
211*b0d17251Schristosreturn 1 if the time is successfully printed out and
212*b0d17251Schristos0 if an I/O error occurred an error occurred (I/O error or invalid time format).
21313d40330Schristos
21413d40330SchristosASN1_TIME_to_tm() returns 1 if the time is successfully parsed and 0 if an
21513d40330Schristoserror occurred (invalid time format).
21613d40330Schristos
21713d40330SchristosASN1_TIME_diff() returns 1 for success and 0 for failure. It can fail if the
21813d40330Schristospassed-in time structure has invalid syntax, for example.
21913d40330Schristos
220*b0d17251SchristosASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() return -1 if I<s> is
221*b0d17251Schristosbefore I<t>, 0 if I<s> equals I<t>, or 1 if I<s> is after I<t>. -2 is returned
22213d40330Schristoson error.
22313d40330Schristos
224*b0d17251SchristosASN1_TIME_compare() returns -1 if I<a> is before I<b>, 0 if I<a> equals I<b>,
225*b0d17251Schristosor 1 if I<a> is after I<b>. -2 is returned on error.
22613d40330Schristos
227*b0d17251SchristosASN1_TIME_to_generalizedtime() returns a pointer to the appropriate time
228*b0d17251Schristosstructure on success or NULL if an error occurred.
229*b0d17251Schristos
230*b0d17251SchristosASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() return a
231*b0d17251Schristospointer to a time structure or NULL if an error occurred.
23213d40330Schristos
233a3b08d93Schristos=head1 EXAMPLES
234a3b08d93Schristos
235a3b08d93SchristosSet a time structure to one hour after the current time and print it out:
236a3b08d93Schristos
237a3b08d93Schristos #include <time.h>
238a3b08d93Schristos #include <openssl/asn1.h>
239a3b08d93Schristos
240a3b08d93Schristos ASN1_TIME *tm;
241a3b08d93Schristos time_t t;
242a3b08d93Schristos BIO *b;
243a3b08d93Schristos
244a3b08d93Schristos t = time(NULL);
245a3b08d93Schristos tm = ASN1_TIME_adj(NULL, t, 0, 60 * 60);
246a3b08d93Schristos b = BIO_new_fp(stdout, BIO_NOCLOSE);
247a3b08d93Schristos ASN1_TIME_print(b, tm);
248a3b08d93Schristos ASN1_STRING_free(tm);
249a3b08d93Schristos BIO_free(b);
250a3b08d93Schristos
251a3b08d93SchristosDetermine if one time is later or sooner than the current time:
252a3b08d93Schristos
253a3b08d93Schristos int day, sec;
254a3b08d93Schristos
255a3b08d93Schristos if (!ASN1_TIME_diff(&day, &sec, NULL, to))
256a3b08d93Schristos     /* Invalid time format */
257a3b08d93Schristos
258a3b08d93Schristos if (day > 0 || sec > 0)
259a3b08d93Schristos     printf("Later\n");
260a3b08d93Schristos else if (day < 0 || sec < 0)
261a3b08d93Schristos     printf("Sooner\n");
262a3b08d93Schristos else
263a3b08d93Schristos     printf("Same\n");
264a3b08d93Schristos
26513d40330Schristos=head1 HISTORY
26613d40330Schristos
26713d40330SchristosThe ASN1_TIME_to_tm() function was added in OpenSSL 1.1.1.
26813d40330SchristosThe ASN1_TIME_set_string_X509() function was added in OpenSSL 1.1.1.
26913d40330SchristosThe ASN1_TIME_normalize() function was added in OpenSSL 1.1.1.
27013d40330SchristosThe ASN1_TIME_cmp_time_t() function was added in OpenSSL 1.1.1.
27113d40330SchristosThe ASN1_TIME_compare() function was added in OpenSSL 1.1.1.
27213d40330Schristos
27313d40330Schristos=head1 COPYRIGHT
27413d40330Schristos
275*b0d17251SchristosCopyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
27613d40330Schristos
277*b0d17251SchristosLicensed under the Apache License 2.0 (the "License").  You may not use
27813d40330Schristosthis file except in compliance with the License.  You can obtain a copy
27913d40330Schristosin the file LICENSE in the source distribution or at
28013d40330SchristosL<https://www.openssl.org/source/license.html>.
28113d40330Schristos
28213d40330Schristos=cut
283