ASN1_TIME_set.pod 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. =pod
  2. =head1 NAME
  3. ASN1_TIME_set, ASN1_TIME_adj, ASN1_TIME_check, ASN1_TIME_set_string,
  4. ASN1_TIME_print, ASN1_TIME_diff - ASN.1 Time functions.
  5. =head1 SYNOPSIS
  6. ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
  7. ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
  8. int offset_day, long offset_sec);
  9. int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
  10. int ASN1_TIME_check(const ASN1_TIME *t);
  11. int ASN1_TIME_print(BIO *b, const ASN1_TIME *s);
  12. int ASN1_TIME_diff(int *pday, int *psec,
  13. const ASN1_TIME *from, const ASN1_TIME *to);
  14. =head1 DESCRIPTION
  15. The function ASN1_TIME_set() sets the ASN1_TIME structure B<s> to the
  16. time represented by the time_t value B<t>. If B<s> is NULL a new ASN1_TIME
  17. structure is allocated and returned.
  18. ASN1_TIME_adj() sets the ASN1_TIME structure B<s> to the time represented
  19. by the time B<offset_day> and B<offset_sec> after the time_t value B<t>.
  20. The values of B<offset_day> or B<offset_sec> can be negative to set a
  21. time before B<t>. The B<offset_sec> value can also exceed the number of
  22. seconds in a day. If B<s> is NULL a new ASN1_TIME structure is allocated
  23. and returned.
  24. ASN1_TIME_set_string() sets ASN1_TIME structure B<s> to the time
  25. represented by string B<str> which must be in appropriate ASN.1 time
  26. format (for example YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ).
  27. ASN1_TIME_check() checks the syntax of ASN1_TIME structure B<s>.
  28. ASN1_TIME_print() prints out the time B<s> to BIO B<b> in human readable
  29. format. It will be of the format MMM DD HH:MM:SS YYYY [GMT], for example
  30. "Feb 3 00:55:52 2015 GMT" it does not include a newline. If the time
  31. structure has invalid format it prints out "Bad time value" and returns
  32. an error.
  33. ASN1_TIME_diff() sets B<*pday> and B<*psec> to the time difference between
  34. B<from> and B<to>. If B<to> represents a time later than B<from> then
  35. one or both (depending on the time difference) of B<*pday> and B<*psec>
  36. will be positive. If B<to> represents a time earlier than B<from> then
  37. one or both of B<*pday> and B<*psec> will be negative. If B<to> and B<from>
  38. represent the same time then B<*pday> and B<*psec> will both be zero.
  39. If both B<*pday> and B<*psec> are non-zero they will always have the same
  40. sign. The value of B<*psec> will always be less than the number of seconds
  41. in a day. If B<from> or B<to> is NULL the current time is used.
  42. =head1 NOTES
  43. The ASN1_TIME structure corresponds to the ASN.1 structure B<Time>
  44. defined in RFC5280 et al. The time setting functions obey the rules outlined
  45. in RFC5280: if the date can be represented by UTCTime it is used, else
  46. GeneralizedTime is used.
  47. The ASN1_TIME structure is represented as an ASN1_STRING internally and can
  48. be freed up using ASN1_STRING_free().
  49. The ASN1_TIME structure can represent years from 0000 to 9999 but no attempt
  50. is made to correct ancient calendar changes (for example from Julian to
  51. Gregorian calendars).
  52. Some applications add offset times directly to a time_t value and pass the
  53. results to ASN1_TIME_set() (or equivalent). This can cause problems as the
  54. time_t value can overflow on some systems resulting in unexpected results.
  55. New applications should use ASN1_TIME_adj() instead and pass the offset value
  56. in the B<offset_sec> and B<offset_day> parameters instead of directly
  57. manipulating a time_t value.
  58. =head1 BUGS
  59. ASN1_TIME_print() currently does not print out the time zone: it either prints
  60. out "GMT" or nothing. But all certificates complying with RFC5280 et al use GMT
  61. anyway.
  62. =head1 EXAMPLES
  63. Set a time structure to one hour after the current time and print it out:
  64. #include <time.h>
  65. #include <openssl/asn1.h>
  66. ASN1_TIME *tm;
  67. time_t t;
  68. BIO *b;
  69. t = time(NULL);
  70. tm = ASN1_TIME_adj(NULL, t, 0, 60 * 60);
  71. b = BIO_new_fp(stdout, BIO_NOCLOSE);
  72. ASN1_TIME_print(b, tm);
  73. ASN1_STRING_free(tm);
  74. BIO_free(b);
  75. Determine if one time is later or sooner than the current time:
  76. int day, sec;
  77. if (!ASN1_TIME_diff(&day, &sec, NULL, to))
  78. /* Invalid time format */
  79. if (day > 0 || sec > 0)
  80. printf("Later\n");
  81. else if (day < 0 || sec < 0)
  82. printf("Sooner\n");
  83. else
  84. printf("Same\n");
  85. =head1 RETURN VALUES
  86. ASN1_TIME_set() and ASN1_TIME_adj() return a pointer to an ASN1_TIME structure
  87. or NULL if an error occurred.
  88. ASN1_TIME_set_string() returns 1 if the time value is successfully set and
  89. 0 otherwise.
  90. ASN1_TIME_check() returns 1 if the structure is syntactically correct and 0
  91. otherwise.
  92. ASN1_TIME_print() returns 1 if the time is successfully printed out and 0 if
  93. an error occurred (I/O error or invalid time format).
  94. ASN1_TIME_diff() returns 1 for sucess and 0 for failure. It can fail if the
  95. pass ASN1_TIME structure has invalid syntax for example.
  96. =cut