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