a_utctm.c 7.9 KB

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