ASN1_TIME_set.pod 12 KB

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