a_utctm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <string.h>
  58. #include <time.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/time_support.h>
  62. #if 0
  63. int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a, unsigned char **pp)
  64. {
  65. return (i2d_ASN1_bytes((ASN1_STRING *)a, pp,
  66. V_ASN1_UTCTIME, V_ASN1_UNIVERSAL));
  67. }
  68. ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **a, unsigned char **pp,
  69. long length)
  70. {
  71. ASN1_UTCTIME *ret = NULL;
  72. ret = (ASN1_UTCTIME *)d2i_ASN1_bytes((ASN1_STRING **)a, pp, length,
  73. V_ASN1_UTCTIME, V_ASN1_UNIVERSAL);
  74. if (ret == NULL) {
  75. OPENSSL_PUT_ERROR(ASN1, ERR_R_NESTED_ASN1_ERROR);
  76. return (NULL);
  77. }
  78. if (!ASN1_UTCTIME_check(ret)) {
  79. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT);
  80. goto err;
  81. }
  82. return (ret);
  83. err:
  84. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  85. M_ASN1_UTCTIME_free(ret);
  86. return (NULL);
  87. }
  88. #endif
  89. int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
  90. {
  91. static const int min[8] = { 0, 1, 1, 0, 0, 0, 0, 0 };
  92. static const int max[8] = { 99, 12, 31, 23, 59, 59, 12, 59 };
  93. char *a;
  94. int n, i, l, o;
  95. if (d->type != V_ASN1_UTCTIME)
  96. return (0);
  97. l = d->length;
  98. a = (char *)d->data;
  99. o = 0;
  100. if (l < 11)
  101. goto err;
  102. for (i = 0; i < 6; i++) {
  103. if ((i == 5) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
  104. i++;
  105. if (tm)
  106. tm->tm_sec = 0;
  107. break;
  108. }
  109. if ((a[o] < '0') || (a[o] > '9'))
  110. goto err;
  111. n = a[o] - '0';
  112. if (++o > l)
  113. goto err;
  114. if ((a[o] < '0') || (a[o] > '9'))
  115. goto err;
  116. n = (n * 10) + a[o] - '0';
  117. if (++o > l)
  118. goto err;
  119. if ((n < min[i]) || (n > max[i]))
  120. goto err;
  121. if (tm) {
  122. switch (i) {
  123. case 0:
  124. tm->tm_year = n < 50 ? n + 100 : n;
  125. break;
  126. case 1:
  127. tm->tm_mon = n - 1;
  128. break;
  129. case 2:
  130. tm->tm_mday = n;
  131. break;
  132. case 3:
  133. tm->tm_hour = n;
  134. break;
  135. case 4:
  136. tm->tm_min = n;
  137. break;
  138. case 5:
  139. tm->tm_sec = n;
  140. break;
  141. }
  142. }
  143. }
  144. if (a[o] == 'Z')
  145. o++;
  146. else if ((a[o] == '+') || (a[o] == '-')) {
  147. int offsign = a[o] == '-' ? -1 : 1, offset = 0;
  148. o++;
  149. if (o + 4 > l)
  150. goto err;
  151. for (i = 6; i < 8; i++) {
  152. if ((a[o] < '0') || (a[o] > '9'))
  153. goto err;
  154. n = a[o] - '0';
  155. o++;
  156. if ((a[o] < '0') || (a[o] > '9'))
  157. goto err;
  158. n = (n * 10) + a[o] - '0';
  159. if ((n < min[i]) || (n > max[i]))
  160. goto err;
  161. if (tm) {
  162. if (i == 6)
  163. offset = n * 3600;
  164. else if (i == 7)
  165. offset += n * 60;
  166. }
  167. o++;
  168. }
  169. if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
  170. return 0;
  171. }
  172. return o == l;
  173. err:
  174. return 0;
  175. }
  176. int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
  177. {
  178. return asn1_utctime_to_tm(NULL, d);
  179. }
  180. int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
  181. {
  182. ASN1_UTCTIME t;
  183. t.type = V_ASN1_UTCTIME;
  184. t.length = strlen(str);
  185. t.data = (unsigned char *)str;
  186. if (ASN1_UTCTIME_check(&t)) {
  187. if (s != NULL) {
  188. if (!ASN1_STRING_set((ASN1_STRING *)s,
  189. (unsigned char *)str, t.length))
  190. return 0;
  191. s->type = V_ASN1_UTCTIME;
  192. }
  193. return (1);
  194. } else
  195. return (0);
  196. }
  197. ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
  198. {
  199. return ASN1_UTCTIME_adj(s, t, 0, 0);
  200. }
  201. ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
  202. int offset_day, long offset_sec)
  203. {
  204. char *p;
  205. struct tm *ts;
  206. struct tm data;
  207. size_t len = 20;
  208. int free_s = 0;
  209. if (s == NULL) {
  210. free_s = 1;
  211. s = M_ASN1_UTCTIME_new();
  212. }
  213. if (s == NULL)
  214. goto err;
  215. ts = OPENSSL_gmtime(&t, &data);
  216. if (ts == NULL)
  217. goto err;
  218. if (offset_day || offset_sec) {
  219. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  220. goto err;
  221. }
  222. if ((ts->tm_year < 50) || (ts->tm_year >= 150))
  223. goto err;
  224. p = (char *)s->data;
  225. if ((p == NULL) || ((size_t)s->length < len)) {
  226. p = OPENSSL_malloc(len);
  227. if (p == NULL) {
  228. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  229. goto err;
  230. }
  231. if (s->data != NULL)
  232. OPENSSL_free(s->data);
  233. s->data = (unsigned char *)p;
  234. }
  235. BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", ts->tm_year % 100,
  236. ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
  237. ts->tm_sec);
  238. s->length = strlen(p);
  239. s->type = V_ASN1_UTCTIME;
  240. return (s);
  241. err:
  242. if (free_s && s)
  243. M_ASN1_UTCTIME_free(s);
  244. return NULL;
  245. }
  246. int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
  247. {
  248. struct tm stm, ttm;
  249. int day, sec;
  250. if (!asn1_utctime_to_tm(&stm, s))
  251. return -2;
  252. if (!OPENSSL_gmtime(&t, &ttm))
  253. return -2;
  254. if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
  255. return -2;
  256. if (day > 0)
  257. return 1;
  258. if (day < 0)
  259. return -1;
  260. if (sec > 0)
  261. return 1;
  262. if (sec < 0)
  263. return -1;
  264. return 0;
  265. }
  266. #if 0
  267. time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s)
  268. {
  269. struct tm tm;
  270. int offset;
  271. memset(&tm, '\0', sizeof tm);
  272. # define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
  273. tm.tm_year = g2(s->data);
  274. if (tm.tm_year < 50)
  275. tm.tm_year += 100;
  276. tm.tm_mon = g2(s->data + 2) - 1;
  277. tm.tm_mday = g2(s->data + 4);
  278. tm.tm_hour = g2(s->data + 6);
  279. tm.tm_min = g2(s->data + 8);
  280. tm.tm_sec = g2(s->data + 10);
  281. if (s->data[12] == 'Z')
  282. offset = 0;
  283. else {
  284. offset = g2(s->data + 13) * 60 + g2(s->data + 15);
  285. if (s->data[12] == '-')
  286. offset = -offset;
  287. }
  288. # undef g2
  289. return mktime(&tm) - offset * 60; /* FIXME: mktime assumes the current
  290. * timezone instead of UTC, and unless
  291. * we rewrite OpenSSL in Lisp we cannot
  292. * locally change the timezone without
  293. * possibly interfering with other
  294. * parts of the program. timegm, which
  295. * uses UTC, is non-standard. Also
  296. * time_t is inappropriate for general
  297. * UTC times because it may a 32 bit
  298. * type. */
  299. }
  300. #endif