Blame doc/man3/ASN1_TIME_set.pod

Packit c4476c
=pod
Packit c4476c
Packit c4476c
=head1 NAME
Packit c4476c
Packit c4476c
ASN1_TIME_set, ASN1_UTCTIME_set, ASN1_GENERALIZEDTIME_set,
Packit c4476c
ASN1_TIME_adj, ASN1_UTCTIME_adj, ASN1_GENERALIZEDTIME_adj,
Packit c4476c
ASN1_TIME_check, ASN1_UTCTIME_check, ASN1_GENERALIZEDTIME_check,
Packit c4476c
ASN1_TIME_set_string, ASN1_UTCTIME_set_string, ASN1_GENERALIZEDTIME_set_string,
Packit c4476c
ASN1_TIME_set_string_X509,
Packit c4476c
ASN1_TIME_normalize,
Packit c4476c
ASN1_TIME_to_tm,
Packit c4476c
ASN1_TIME_print, ASN1_UTCTIME_print, ASN1_GENERALIZEDTIME_print,
Packit c4476c
ASN1_TIME_diff,
Packit c4476c
ASN1_TIME_cmp_time_t, ASN1_UTCTIME_cmp_time_t,
Packit c4476c
ASN1_TIME_compare,
Packit c4476c
ASN1_TIME_to_generalizedtime - ASN.1 Time functions
Packit c4476c
Packit c4476c
=head1 SYNOPSIS
Packit c4476c
Packit c4476c
 ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
Packit c4476c
 ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t);
Packit c4476c
 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
Packit c4476c
                                                time_t t);
Packit c4476c
Packit c4476c
 ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
Packit c4476c
                          long offset_sec);
Packit c4476c
 ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
Packit c4476c
                                int offset_day, long offset_sec);
Packit c4476c
 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
Packit c4476c
                                                time_t t, int offset_day,
Packit c4476c
                                                long offset_sec);
Packit c4476c
Packit c4476c
 int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
Packit c4476c
 int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str);
Packit c4476c
 int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
Packit c4476c
 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s,
Packit c4476c
                                     const char *str);
Packit c4476c
Packit c4476c
 int ASN1_TIME_normalize(ASN1_TIME *s);
Packit c4476c
Packit c4476c
 int ASN1_TIME_check(const ASN1_TIME *t);
Packit c4476c
 int ASN1_UTCTIME_check(const ASN1_UTCTIME *t);
Packit c4476c
 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *t);
Packit c4476c
Packit c4476c
 int ASN1_TIME_print(BIO *b, const ASN1_TIME *s);
Packit c4476c
 int ASN1_UTCTIME_print(BIO *b, const ASN1_UTCTIME *s);
Packit c4476c
 int ASN1_GENERALIZEDTIME_print(BIO *b, const ASN1_GENERALIZEDTIME *s);
Packit c4476c
Packit c4476c
 int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm);
Packit c4476c
 int ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from,
Packit c4476c
                    const ASN1_TIME *to);
Packit c4476c
Packit c4476c
 int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t);
Packit c4476c
 int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
Packit c4476c
Packit c4476c
 int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b);
Packit c4476c
Packit c4476c
 ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
Packit c4476c
                                                    ASN1_GENERALIZEDTIME **out);
Packit c4476c
Packit c4476c
=head1 DESCRIPTION
Packit c4476c
Packit c4476c
The ASN1_TIME_set(), ASN1_UTCTIME_set() and ASN1_GENERALIZEDTIME_set()
Packit c4476c
functions set the structure B<s> to the time represented by the time_t
Packit c4476c
value B<t>. If B<s> is NULL a new time structure is allocated and returned.
Packit c4476c
Packit c4476c
The ASN1_TIME_adj(), ASN1_UTCTIME_adj() and ASN1_GENERALIZEDTIME_adj()
Packit c4476c
functions set the time structure B<s> to the time represented
Packit c4476c
by the time B<offset_day> and B<offset_sec> after the time_t value B<t>.
Packit c4476c
The values of B<offset_day> or B<offset_sec> can be negative to set a
Packit c4476c
time before B<t>. The B<offset_sec> value can also exceed the number of
Packit c4476c
seconds in a day. If B<s> is NULL a new structure is allocated
Packit c4476c
and returned.
Packit c4476c
Packit c4476c
The ASN1_TIME_set_string(), ASN1_UTCTIME_set_string() and
Packit c4476c
ASN1_GENERALIZEDTIME_set_string() functions set the time structure B<s>
Packit c4476c
to the time represented by string B<str> which must be in appropriate ASN.1
Packit c4476c
time format (for example YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ). If B<s> is NULL
Packit c4476c
this function performs a format check on B<str> only. The string B<str>
Packit c4476c
is copied into B<s>.
Packit c4476c
Packit c4476c
ASN1_TIME_set_string_X509() sets ASN1_TIME structure B<s> to the time
Packit c4476c
represented by string B<str> which must be in appropriate time format
Packit c4476c
that RFC 5280 requires, which means it only allows YYMMDDHHMMSSZ and
Packit c4476c
YYYYMMDDHHMMSSZ (leap second is rejected), all other ASN.1 time format
Packit c4476c
are not allowed. If B<s> is NULL this function performs a format check
Packit c4476c
on B<str> only.
Packit c4476c
Packit c4476c
The ASN1_TIME_normalize() function converts an ASN1_GENERALIZEDTIME or
Packit c4476c
ASN1_UTCTIME into a time value that can be used in a certificate. It
Packit c4476c
should be used after the ASN1_TIME_set_string() functions and before
Packit c4476c
ASN1_TIME_print() functions to get consistent (i.e. GMT) results.
Packit c4476c
Packit c4476c
The ASN1_TIME_check(), ASN1_UTCTIME_check() and ASN1_GENERALIZEDTIME_check()
Packit c4476c
functions check the syntax of the time structure B<s>.
Packit c4476c
Packit c4476c
The ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print()
Packit c4476c
functions print the time structure B<s> to BIO B in human readable
Packit c4476c
format. It will be of the format MMM DD HH:MM:SS YYYY [GMT], for example
Packit c4476c
"Feb  3 00:55:52 2015 GMT" it does not include a newline. If the time
Packit c4476c
structure has invalid format it prints out "Bad time value" and returns
Packit c4476c
an error. The output for generalized time may include a fractional part
Packit c4476c
following the second.
Packit c4476c
Packit c4476c
ASN1_TIME_to_tm() converts the time B<s> to the standard B<tm> structure.
Packit c4476c
If B<s> is NULL, then the current time is converted. The output time is GMT.
Packit c4476c
The B<tm_sec>, B<tm_min>, B<tm_hour>, B<tm_mday>, B<tm_wday>, B<tm_yday>,
Packit c4476c
B<tm_mon> and B<tm_year> fields of B<tm> structure are set to proper values,
Packit c4476c
whereas all other fields are set to 0. If B<tm> is NULL this function performs
Packit c4476c
a format check on B<s> only. If B<s> is in Generalized format with fractional
Packit c4476c
seconds, e.g. YYYYMMDDHHMMSS.SSSZ, the fractional seconds will be lost while
Packit c4476c
converting B<s> to B<tm> structure.
Packit c4476c
Packit c4476c
ASN1_TIME_diff() sets B<*pday> and B<*psec> to the time difference between
Packit c4476c
B<from> and B<to>. If B<to> represents a time later than B<from> then
Packit c4476c
one or both (depending on the time difference) of B<*pday> and B<*psec>
Packit c4476c
will be positive. If B<to> represents a time earlier than B<from> then
Packit c4476c
one or both of B<*pday> and B<*psec> will be negative. If B<to> and B<from>
Packit c4476c
represent the same time then B<*pday> and B<*psec> will both be zero.
Packit c4476c
If both B<*pday> and B<*psec> are non-zero they will always have the same
Packit c4476c
sign. The value of B<*psec> will always be less than the number of seconds
Packit c4476c
in a day. If B<from> or B<to> is NULL the current time is used.
Packit c4476c
Packit c4476c
The ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() functions compare
Packit c4476c
the two times represented by the time structure B<s> and the time_t B<t>.
Packit c4476c
Packit c4476c
The ASN1_TIME_compare() function compares the two times represented by the
Packit c4476c
time structures B and B.
Packit c4476c
Packit c4476c
The ASN1_TIME_to_generalizedtime() function converts an ASN1_TIME to an
Packit c4476c
ASN1_GENERALIZEDTIME, regardless of year. If either B<out> or
Packit c4476c
B<*out> are NULL, then a new object is allocated and must be freed after use.
Packit c4476c
Packit c4476c
=head1 NOTES
Packit c4476c
Packit c4476c
The ASN1_TIME structure corresponds to the ASN.1 structure B<Time>
Packit c4476c
defined in RFC5280 et al. The time setting functions obey the rules outlined
Packit c4476c
in RFC5280: if the date can be represented by UTCTime it is used, else
Packit c4476c
GeneralizedTime is used.
Packit c4476c
Packit c4476c
The ASN1_TIME, ASN1_UTCTIME and ASN1_GENERALIZEDTIME structures are represented
Packit c4476c
as an ASN1_STRING internally and can be freed up using ASN1_STRING_free().
Packit c4476c
Packit c4476c
The ASN1_TIME structure can represent years from 0000 to 9999 but no attempt
Packit c4476c
is made to correct ancient calendar changes (for example from Julian to
Packit c4476c
Gregorian calendars).
Packit c4476c
Packit c4476c
ASN1_UTCTIME is limited to a year range of 1950 through 2049.
Packit c4476c
Packit c4476c
Some applications add offset times directly to a time_t value and pass the
Packit c4476c
results to ASN1_TIME_set() (or equivalent). This can cause problems as the
Packit c4476c
time_t value can overflow on some systems resulting in unexpected results.
Packit c4476c
New applications should use ASN1_TIME_adj() instead and pass the offset value
Packit c4476c
in the B<offset_sec> and B<offset_day> parameters instead of directly
Packit c4476c
manipulating a time_t value.
Packit c4476c
Packit c4476c
ASN1_TIME_adj() may change the type from ASN1_GENERALIZEDTIME to ASN1_UTCTIME,
Packit c4476c
or vice versa, based on the resulting year. The ASN1_GENERALIZEDTIME_adj() and
Packit c4476c
ASN1_UTCTIME_adj() functions will not modify the type of the return structure.
Packit c4476c
Packit c4476c
It is recommended that functions starting with ASN1_TIME be used instead of
Packit c4476c
those starting with ASN1_UTCTIME or ASN1_GENERALIZEDTIME. The functions
Packit c4476c
starting with ASN1_UTCTIME and ASN1_GENERALIZEDTIME act only on that specific
Packit c4476c
time format. The functions starting with ASN1_TIME will operate on either
Packit c4476c
format.
Packit c4476c
Packit c4476c
=head1 BUGS
Packit c4476c
Packit c4476c
ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print()
Packit c4476c
do not print out the time zone: it either prints out "GMT" or nothing. But all
Packit c4476c
certificates complying with RFC5280 et al use GMT anyway.
Packit c4476c
Packit c4476c
Use the ASN1_TIME_normalize() function to normalize the time value before
Packit c4476c
printing to get GMT results.
Packit c4476c
Packit c4476c
=head1 RETURN VALUES
Packit c4476c
Packit c4476c
ASN1_TIME_set(), ASN1_UTCTIME_set(), ASN1_GENERALIZEDTIME_set(), ASN1_TIME_adj(),
Packit c4476c
ASN1_UTCTIME_adj and ASN1_GENERALIZEDTIME_set return a pointer to a time structure
Packit c4476c
or NULL if an error occurred.
Packit c4476c
Packit c4476c
ASN1_TIME_set_string(), ASN1_UTCTIME_set_string(), ASN1_GENERALIZEDTIME_set_string()
Packit c4476c
ASN1_TIME_set_string_X509() return 1 if the time value is successfully set and 0 otherwise.
Packit c4476c
Packit c4476c
ASN1_TIME_normalize() returns 1 on success, and 0 on error.
Packit c4476c
Packit c4476c
ASN1_TIME_check(), ASN1_UTCTIME_check and ASN1_GENERALIZEDTIME_check() return 1
Packit c4476c
if the structure is syntactically correct and 0 otherwise.
Packit c4476c
Packit c4476c
ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() return 1
Packit c4476c
if the time is successfully printed out and 0 if an error occurred (I/O error or
Packit c4476c
invalid time format).
Packit c4476c
Packit c4476c
ASN1_TIME_to_tm() returns 1 if the time is successfully parsed and 0 if an
Packit c4476c
error occurred (invalid time format).
Packit c4476c
Packit c4476c
ASN1_TIME_diff() returns 1 for success and 0 for failure. It can fail if the
Packit c4476c
passed-in time structure has invalid syntax, for example.
Packit c4476c
Packit c4476c
ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() return -1 if B<s> is
Packit c4476c
before B<t>, 0 if B<s> equals B<t>, or 1 if B<s> is after B<t>. -2 is returned
Packit c4476c
on error.
Packit c4476c
Packit c4476c
ASN1_TIME_compare() returns -1 if B is before B, 0 if B equals B, or 1 if B is after B. -2 is returned on error.
Packit c4476c
Packit c4476c
ASN1_TIME_to_generalizedtime() returns a pointer to
Packit c4476c
the appropriate time structure on success or NULL if an error occurred.
Packit c4476c
Packit c4476c
=head1 EXAMPLES
Packit c4476c
Packit c4476c
Set a time structure to one hour after the current time and print it out:
Packit c4476c
Packit c4476c
 #include <time.h>
Packit c4476c
 #include <openssl/asn1.h>
Packit c4476c
Packit c4476c
 ASN1_TIME *tm;
Packit c4476c
 time_t t;
Packit c4476c
 BIO *b;
Packit c4476c
Packit c4476c
 t = time(NULL);
Packit c4476c
 tm = ASN1_TIME_adj(NULL, t, 0, 60 * 60);
Packit c4476c
 b = BIO_new_fp(stdout, BIO_NOCLOSE);
Packit c4476c
 ASN1_TIME_print(b, tm);
Packit c4476c
 ASN1_STRING_free(tm);
Packit c4476c
 BIO_free(b);
Packit c4476c
Packit c4476c
Determine if one time is later or sooner than the current time:
Packit c4476c
Packit c4476c
 int day, sec;
Packit c4476c
Packit c4476c
 if (!ASN1_TIME_diff(&day, &sec, NULL, to))
Packit c4476c
     /* Invalid time format */
Packit c4476c
Packit c4476c
 if (day > 0 || sec > 0)
Packit c4476c
     printf("Later\n");
Packit c4476c
 else if (day < 0 || sec < 0)
Packit c4476c
     printf("Sooner\n");
Packit c4476c
 else
Packit c4476c
     printf("Same\n");
Packit c4476c
Packit c4476c
=head1 HISTORY
Packit c4476c
Packit c4476c
The ASN1_TIME_to_tm() function was added in OpenSSL 1.1.1.
Packit c4476c
The ASN1_TIME_set_string_X509() function was added in OpenSSL 1.1.1.
Packit c4476c
The ASN1_TIME_normalize() function was added in OpenSSL 1.1.1.
Packit c4476c
The ASN1_TIME_cmp_time_t() function was added in OpenSSL 1.1.1.
Packit c4476c
The ASN1_TIME_compare() function was added in OpenSSL 1.1.1.
Packit c4476c
Packit c4476c
=head1 COPYRIGHT
Packit c4476c
Packit c4476c
Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
Packit c4476c
Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
this file except in compliance with the License.  You can obtain a copy
Packit c4476c
in the file LICENSE in the source distribution or at
Packit c4476c
L<https://www.openssl.org/source/license.html>.
Packit c4476c
Packit c4476c
=cut