a_time.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* ====================================================================
  2. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * licensing@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com).
  52. *
  53. */
  54. /*-
  55. * This is an implementation of the ASN1 Time structure which is:
  56. * Time ::= CHOICE {
  57. * utcTime UTCTime,
  58. * generalTime GeneralizedTime }
  59. * written by Steve Henson.
  60. */
  61. #include <stdio.h>
  62. #include <time.h>
  63. #include "internal/cryptlib.h"
  64. #include <openssl/asn1t.h>
  65. #include "asn1_locl.h"
  66. IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
  67. IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
  68. ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
  69. {
  70. return ASN1_TIME_adj(s, t, 0, 0);
  71. }
  72. ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
  73. int offset_day, long offset_sec)
  74. {
  75. struct tm *ts;
  76. struct tm data;
  77. ts = OPENSSL_gmtime(&t, &data);
  78. if (ts == NULL) {
  79. ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
  80. return NULL;
  81. }
  82. if (offset_day || offset_sec) {
  83. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  84. return NULL;
  85. }
  86. if ((ts->tm_year >= 50) && (ts->tm_year < 150))
  87. return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
  88. return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
  89. }
  90. int ASN1_TIME_check(const ASN1_TIME *t)
  91. {
  92. if (t->type == V_ASN1_GENERALIZEDTIME)
  93. return ASN1_GENERALIZEDTIME_check(t);
  94. else if (t->type == V_ASN1_UTCTIME)
  95. return ASN1_UTCTIME_check(t);
  96. return 0;
  97. }
  98. /* Convert an ASN1_TIME structure to GeneralizedTime */
  99. ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
  100. ASN1_GENERALIZEDTIME **out)
  101. {
  102. ASN1_GENERALIZEDTIME *ret;
  103. char *str;
  104. int newlen;
  105. if (!ASN1_TIME_check(t))
  106. return NULL;
  107. if (out == NULL || *out == NULL) {
  108. if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL)
  109. return NULL;
  110. if (out)
  111. *out = ret;
  112. } else
  113. ret = *out;
  114. /* If already GeneralizedTime just copy across */
  115. if (t->type == V_ASN1_GENERALIZEDTIME) {
  116. if (!ASN1_STRING_set(ret, t->data, t->length))
  117. return NULL;
  118. return ret;
  119. }
  120. /* grow the string */
  121. if (!ASN1_STRING_set(ret, NULL, t->length + 2))
  122. return NULL;
  123. /* ASN1_STRING_set() allocated 'len + 1' bytes. */
  124. newlen = t->length + 2 + 1;
  125. str = (char *)ret->data;
  126. /* Work out the century and prepend */
  127. if (t->data[0] >= '5')
  128. OPENSSL_strlcpy(str, "19", newlen);
  129. else
  130. OPENSSL_strlcpy(str, "20", newlen);
  131. OPENSSL_strlcat(str, (char *)t->data, newlen);
  132. return ret;
  133. }
  134. int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
  135. {
  136. ASN1_TIME t;
  137. t.length = strlen(str);
  138. t.data = (unsigned char *)str;
  139. t.flags = 0;
  140. t.type = V_ASN1_UTCTIME;
  141. if (!ASN1_TIME_check(&t)) {
  142. t.type = V_ASN1_GENERALIZEDTIME;
  143. if (!ASN1_TIME_check(&t))
  144. return 0;
  145. }
  146. if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
  147. return 0;
  148. return 1;
  149. }
  150. static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t)
  151. {
  152. if (t == NULL) {
  153. time_t now_t;
  154. time(&now_t);
  155. if (OPENSSL_gmtime(&now_t, tm))
  156. return 1;
  157. return 0;
  158. }
  159. if (t->type == V_ASN1_UTCTIME)
  160. return asn1_utctime_to_tm(tm, t);
  161. else if (t->type == V_ASN1_GENERALIZEDTIME)
  162. return asn1_generalizedtime_to_tm(tm, t);
  163. return 0;
  164. }
  165. int ASN1_TIME_diff(int *pday, int *psec,
  166. const ASN1_TIME *from, const ASN1_TIME *to)
  167. {
  168. struct tm tm_from, tm_to;
  169. if (!asn1_time_to_tm(&tm_from, from))
  170. return 0;
  171. if (!asn1_time_to_tm(&tm_to, to))
  172. return 0;
  173. return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
  174. }
  175. int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
  176. {
  177. if (tm->type == V_ASN1_UTCTIME)
  178. return ASN1_UTCTIME_print(bp, tm);
  179. if (tm->type == V_ASN1_GENERALIZEDTIME)
  180. return ASN1_GENERALIZEDTIME_print(bp, tm);
  181. BIO_write(bp, "Bad time value", 14);
  182. return (0);
  183. }