ASN1_TIME_set.3ossl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "ASN1_TIME_SET 3ossl"
  58. .TH ASN1_TIME_SET 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. ASN1_TIME_set, ASN1_UTCTIME_set, ASN1_GENERALIZEDTIME_set,
  65. ASN1_TIME_adj, ASN1_UTCTIME_adj, ASN1_GENERALIZEDTIME_adj,
  66. ASN1_TIME_check, ASN1_UTCTIME_check, ASN1_GENERALIZEDTIME_check,
  67. ASN1_TIME_set_string, ASN1_UTCTIME_set_string, ASN1_GENERALIZEDTIME_set_string,
  68. ASN1_TIME_set_string_X509,
  69. ASN1_TIME_normalize,
  70. ASN1_TIME_to_tm,
  71. ASN1_TIME_print, ASN1_TIME_print_ex, ASN1_UTCTIME_print, ASN1_GENERALIZEDTIME_print,
  72. ASN1_TIME_diff,
  73. ASN1_TIME_cmp_time_t, ASN1_UTCTIME_cmp_time_t,
  74. ASN1_TIME_compare,
  75. ASN1_TIME_to_generalizedtime,
  76. ASN1_TIME_dup, ASN1_UTCTIME_dup, ASN1_GENERALIZEDTIME_dup \- ASN.1 Time functions
  77. .SH SYNOPSIS
  78. .IX Header "SYNOPSIS"
  79. .Vb 4
  80. \& ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
  81. \& ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t);
  82. \& ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
  83. \& time_t t);
  84. \&
  85. \& ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
  86. \& long offset_sec);
  87. \& ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
  88. \& int offset_day, long offset_sec);
  89. \& ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
  90. \& time_t t, int offset_day,
  91. \& long offset_sec);
  92. \&
  93. \& int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
  94. \& int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str);
  95. \& int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
  96. \& int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s,
  97. \& const char *str);
  98. \&
  99. \& int ASN1_TIME_normalize(ASN1_TIME *s);
  100. \&
  101. \& int ASN1_TIME_check(const ASN1_TIME *t);
  102. \& int ASN1_UTCTIME_check(const ASN1_UTCTIME *t);
  103. \& int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *t);
  104. \&
  105. \& int ASN1_TIME_print(BIO *b, const ASN1_TIME *s);
  106. \& int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags);
  107. \& int ASN1_UTCTIME_print(BIO *b, const ASN1_UTCTIME *s);
  108. \& int ASN1_GENERALIZEDTIME_print(BIO *b, const ASN1_GENERALIZEDTIME *s);
  109. \&
  110. \& int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm);
  111. \& int ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from,
  112. \& const ASN1_TIME *to);
  113. \&
  114. \& int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t);
  115. \& int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);
  116. \&
  117. \& int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b);
  118. \&
  119. \& ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
  120. \& ASN1_GENERALIZEDTIME **out);
  121. \&
  122. \& ASN1_TIME *ASN1_TIME_dup(const ASN1_TIME *t);
  123. \& ASN1_UTCTIME *ASN1_UTCTIME_dup(const ASN1_UTCTIME *t);
  124. \& ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_dup(const ASN1_GENERALIZEDTIME *t);
  125. .Ve
  126. .SH DESCRIPTION
  127. .IX Header "DESCRIPTION"
  128. The \fBASN1_TIME_set()\fR, \fBASN1_UTCTIME_set()\fR and \fBASN1_GENERALIZEDTIME_set()\fR
  129. functions set the structure \fIs\fR to the time represented by the time_t
  130. value \fIt\fR. If \fIs\fR is NULL a new time structure is allocated and returned.
  131. .PP
  132. The \fBASN1_TIME_adj()\fR, \fBASN1_UTCTIME_adj()\fR and \fBASN1_GENERALIZEDTIME_adj()\fR
  133. functions set the time structure \fIs\fR to the time represented
  134. by the time \fIoffset_day\fR and \fIoffset_sec\fR after the time_t value \fIt\fR.
  135. The values of \fIoffset_day\fR or \fIoffset_sec\fR can be negative to set a
  136. time before \fIt\fR. The \fIoffset_sec\fR value can also exceed the number of
  137. seconds in a day. If \fIs\fR is NULL a new structure is allocated
  138. and returned.
  139. .PP
  140. The \fBASN1_TIME_set_string()\fR, \fBASN1_UTCTIME_set_string()\fR and
  141. \&\fBASN1_GENERALIZEDTIME_set_string()\fR functions set the time structure \fIs\fR
  142. to the time represented by string \fIstr\fR which must be in appropriate ASN.1
  143. time format (for example YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ). If \fIs\fR is NULL
  144. this function performs a format check on \fIstr\fR only. The string \fIstr\fR
  145. is copied into \fIs\fR.
  146. .PP
  147. \&\fBASN1_TIME_set_string_X509()\fR sets \fBASN1_TIME\fR structure \fIs\fR to the time
  148. represented by string \fIstr\fR which must be in appropriate time format
  149. that RFC 5280 requires, which means it only allows YYMMDDHHMMSSZ and
  150. YYYYMMDDHHMMSSZ (leap second is rejected), all other ASN.1 time format
  151. are not allowed. If \fIs\fR is NULL this function performs a format check
  152. on \fIstr\fR only.
  153. .PP
  154. The \fBASN1_TIME_normalize()\fR function converts an \fBASN1_GENERALIZEDTIME\fR or
  155. \&\fBASN1_UTCTIME\fR into a time value that can be used in a certificate. It
  156. should be used after the \fBASN1_TIME_set_string()\fR functions and before
  157. \&\fBASN1_TIME_print()\fR functions to get consistent (i.e. GMT) results.
  158. .PP
  159. The \fBASN1_TIME_check()\fR, \fBASN1_UTCTIME_check()\fR and \fBASN1_GENERALIZEDTIME_check()\fR
  160. functions check the syntax of the time structure \fIs\fR.
  161. .PP
  162. The \fBASN1_TIME_print()\fR, \fBASN1_UTCTIME_print()\fR and \fBASN1_GENERALIZEDTIME_print()\fR
  163. functions print the time structure \fIs\fR to BIO \fIb\fR in human readable
  164. format. It will be of the format MMM DD HH:MM:SS YYYY [GMT], for example
  165. "Feb 3 00:55:52 2015 GMT", which does not include a newline.
  166. If the time structure has invalid format it prints out "Bad time value" and
  167. returns an error. The output for generalized time may include a fractional part
  168. following the second.
  169. .PP
  170. \&\fBASN1_TIME_print_ex()\fR provides \fIflags\fR to specify the output format of the
  171. datetime. This can be either \fBASN1_DTFLGS_RFC822\fR or \fBASN1_DTFLGS_ISO8601\fR.
  172. .PP
  173. \&\fBASN1_TIME_to_tm()\fR converts the time \fIs\fR to the standard \fItm\fR structure.
  174. If \fIs\fR is NULL, then the current time is converted. The output time is GMT.
  175. The \fItm_sec\fR, \fItm_min\fR, \fItm_hour\fR, \fItm_mday\fR, \fItm_wday\fR, \fItm_yday\fR,
  176. \&\fItm_mon\fR and \fItm_year\fR fields of \fItm\fR structure are set to proper values,
  177. whereas all other fields are set to 0. If \fItm\fR is NULL this function performs
  178. a format check on \fIs\fR only. If \fIs\fR is in Generalized format with fractional
  179. seconds, e.g. YYYYMMDDHHMMSS.SSSZ, the fractional seconds will be lost while
  180. converting \fIs\fR to \fItm\fR structure.
  181. .PP
  182. \&\fBASN1_TIME_diff()\fR sets \fI*pday\fR and \fI*psec\fR to the time difference between
  183. \&\fIfrom\fR and \fIto\fR. If \fIto\fR represents a time later than \fIfrom\fR then
  184. one or both (depending on the time difference) of \fI*pday\fR and \fI*psec\fR
  185. will be positive. If \fIto\fR represents a time earlier than \fIfrom\fR then
  186. one or both of \fI*pday\fR and \fI*psec\fR will be negative. If \fIto\fR and \fIfrom\fR
  187. represent the same time then \fI*pday\fR and \fI*psec\fR will both be zero.
  188. If both \fI*pday\fR and \fI*psec\fR are nonzero they will always have the same
  189. sign. The value of \fI*psec\fR will always be less than the number of seconds
  190. in a day. If \fIfrom\fR or \fIto\fR is NULL the current time is used.
  191. .PP
  192. The \fBASN1_TIME_cmp_time_t()\fR and \fBASN1_UTCTIME_cmp_time_t()\fR functions compare
  193. the two times represented by the time structure \fIs\fR and the time_t \fIt\fR.
  194. .PP
  195. The \fBASN1_TIME_compare()\fR function compares the two times represented by the
  196. time structures \fIa\fR and \fIb\fR.
  197. .PP
  198. The \fBASN1_TIME_to_generalizedtime()\fR function converts an \fBASN1_TIME\fR to an
  199. \&\fBASN1_GENERALIZEDTIME\fR, regardless of year. If either \fIout\fR or
  200. \&\fI*out\fR are NULL, then a new object is allocated and must be freed after use.
  201. .PP
  202. The \fBASN1_TIME_dup()\fR, \fBASN1_UTCTIME_dup()\fR and \fBASN1_GENERALIZEDTIME_dup()\fR functions
  203. duplicate the time structure \fIt\fR and return the duplicated result
  204. correspondingly.
  205. .SH NOTES
  206. .IX Header "NOTES"
  207. The \fBASN1_TIME\fR structure corresponds to the ASN.1 structure \fBTime\fR
  208. defined in RFC5280 et al. The time setting functions obey the rules outlined
  209. in RFC5280: if the date can be represented by UTCTime it is used, else
  210. GeneralizedTime is used.
  211. .PP
  212. The \fBASN1_TIME\fR, \fBASN1_UTCTIME\fR and \fBASN1_GENERALIZEDTIME\fR structures are
  213. represented as an \fBASN1_STRING\fR internally and can be freed up using
  214. \&\fBASN1_STRING_free()\fR.
  215. .PP
  216. The \fBASN1_TIME\fR structure can represent years from 0000 to 9999 but no attempt
  217. is made to correct ancient calendar changes (for example from Julian to
  218. Gregorian calendars).
  219. .PP
  220. \&\fBASN1_UTCTIME\fR is limited to a year range of 1950 through 2049.
  221. .PP
  222. Some applications add offset times directly to a time_t value and pass the
  223. results to \fBASN1_TIME_set()\fR (or equivalent). This can cause problems as the
  224. time_t value can overflow on some systems resulting in unexpected results.
  225. New applications should use \fBASN1_TIME_adj()\fR instead and pass the offset value
  226. in the \fIoffset_sec\fR and \fIoffset_day\fR parameters instead of directly
  227. manipulating a time_t value.
  228. .PP
  229. \&\fBASN1_TIME_adj()\fR may change the type from \fBASN1_GENERALIZEDTIME\fR to
  230. \&\fBASN1_UTCTIME\fR, or vice versa, based on the resulting year.
  231. \&\fBASN1_GENERALIZEDTIME_adj()\fR and \fBASN1_UTCTIME_adj()\fR will not modify the type
  232. of the return structure.
  233. .PP
  234. It is recommended that functions starting with \fBASN1_TIME\fR be used instead of
  235. those starting with \fBASN1_UTCTIME\fR or \fBASN1_GENERALIZEDTIME\fR. The functions
  236. starting with \fBASN1_UTCTIME\fR and \fBASN1_GENERALIZEDTIME\fR act only on that
  237. specific time format. The functions starting with \fBASN1_TIME\fR will operate on
  238. either format.
  239. .SH BUGS
  240. .IX Header "BUGS"
  241. \&\fBASN1_TIME_print()\fR, \fBASN1_UTCTIME_print()\fR and \fBASN1_GENERALIZEDTIME_print()\fR do
  242. not print out the timezone: it either prints out "GMT" or nothing. But all
  243. certificates complying with RFC5280 et al use GMT anyway.
  244. .PP
  245. \&\fBASN1_TIME_print()\fR, \fBASN1_TIME_print_ex()\fR, \fBASN1_UTCTIME_print()\fR and
  246. \&\fBASN1_GENERALIZEDTIME_print()\fR do not distinguish if they fail because
  247. of an I/O error or invalid time format.
  248. .PP
  249. Use the \fBASN1_TIME_normalize()\fR function to normalize the time value before
  250. printing to get GMT results.
  251. .SH "RETURN VALUES"
  252. .IX Header "RETURN VALUES"
  253. \&\fBASN1_TIME_set()\fR, \fBASN1_UTCTIME_set()\fR, \fBASN1_GENERALIZEDTIME_set()\fR,
  254. \&\fBASN1_TIME_adj()\fR, \fBASN1_UTCTIME_adj()\fR and \fBASN1_GENERALIZEDTIME_set()\fR return
  255. a pointer to a time structure or NULL if an error occurred.
  256. .PP
  257. \&\fBASN1_TIME_set_string()\fR, \fBASN1_UTCTIME_set_string()\fR,
  258. \&\fBASN1_GENERALIZEDTIME_set_string()\fR and \fBASN1_TIME_set_string_X509()\fR return
  259. 1 if the time value is successfully set and 0 otherwise.
  260. .PP
  261. \&\fBASN1_TIME_normalize()\fR returns 1 on success, and 0 on error.
  262. .PP
  263. \&\fBASN1_TIME_check()\fR, ASN1_UTCTIME_check and \fBASN1_GENERALIZEDTIME_check()\fR return 1
  264. if the structure is syntactically correct and 0 otherwise.
  265. .PP
  266. \&\fBASN1_TIME_print()\fR, \fBASN1_UTCTIME_print()\fR and \fBASN1_GENERALIZEDTIME_print()\fR
  267. return 1 if the time is successfully printed out and
  268. 0 if an I/O error occurred an error occurred (I/O error or invalid time format).
  269. .PP
  270. \&\fBASN1_TIME_to_tm()\fR returns 1 if the time is successfully parsed and 0 if an
  271. error occurred (invalid time format).
  272. .PP
  273. \&\fBASN1_TIME_diff()\fR returns 1 for success and 0 for failure. It can fail if the
  274. passed-in time structure has invalid syntax, for example.
  275. .PP
  276. \&\fBASN1_TIME_cmp_time_t()\fR and \fBASN1_UTCTIME_cmp_time_t()\fR return \-1 if \fIs\fR is
  277. before \fIt\fR, 0 if \fIs\fR equals \fIt\fR, or 1 if \fIs\fR is after \fIt\fR. \-2 is returned
  278. on error.
  279. .PP
  280. \&\fBASN1_TIME_compare()\fR returns \-1 if \fIa\fR is before \fIb\fR, 0 if \fIa\fR equals \fIb\fR,
  281. or 1 if \fIa\fR is after \fIb\fR. \-2 is returned on error.
  282. .PP
  283. \&\fBASN1_TIME_to_generalizedtime()\fR returns a pointer to the appropriate time
  284. structure on success or NULL if an error occurred.
  285. .PP
  286. \&\fBASN1_TIME_dup()\fR, \fBASN1_UTCTIME_dup()\fR and \fBASN1_GENERALIZEDTIME_dup()\fR return a
  287. pointer to a time structure or NULL if an error occurred.
  288. .SH EXAMPLES
  289. .IX Header "EXAMPLES"
  290. Set a time structure to one hour after the current time and print it out:
  291. .PP
  292. .Vb 2
  293. \& #include <time.h>
  294. \& #include <openssl/asn1.h>
  295. \&
  296. \& ASN1_TIME *tm;
  297. \& time_t t;
  298. \& BIO *b;
  299. \&
  300. \& t = time(NULL);
  301. \& tm = ASN1_TIME_adj(NULL, t, 0, 60 * 60);
  302. \& b = BIO_new_fp(stdout, BIO_NOCLOSE);
  303. \& ASN1_TIME_print(b, tm);
  304. \& ASN1_STRING_free(tm);
  305. \& BIO_free(b);
  306. .Ve
  307. .PP
  308. Determine if one time is later or sooner than the current time:
  309. .PP
  310. .Vb 1
  311. \& int day, sec;
  312. \&
  313. \& if (!ASN1_TIME_diff(&day, &sec, NULL, to))
  314. \& /* Invalid time format */
  315. \&
  316. \& if (day > 0 || sec > 0)
  317. \& printf("Later\en");
  318. \& else if (day < 0 || sec < 0)
  319. \& printf("Sooner\en");
  320. \& else
  321. \& printf("Same\en");
  322. .Ve
  323. .SH HISTORY
  324. .IX Header "HISTORY"
  325. The \fBASN1_TIME_to_tm()\fR function was added in OpenSSL 1.1.1.
  326. The \fBASN1_TIME_set_string_X509()\fR function was added in OpenSSL 1.1.1.
  327. The \fBASN1_TIME_normalize()\fR function was added in OpenSSL 1.1.1.
  328. The \fBASN1_TIME_cmp_time_t()\fR function was added in OpenSSL 1.1.1.
  329. The \fBASN1_TIME_compare()\fR function was added in OpenSSL 1.1.1.
  330. .SH COPYRIGHT
  331. .IX Header "COPYRIGHT"
  332. Copyright 2015\-2021 The OpenSSL Project Authors. All Rights Reserved.
  333. .PP
  334. Licensed under the Apache License 2.0 (the "License"). You may not use
  335. this file except in compliance with the License. You can obtain a copy
  336. in the file LICENSE in the source distribution or at
  337. <https://www.openssl.org/source/license.html>.