a_gentm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. */
  57. /*
  58. * GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME
  59. */
  60. #include <stdio.h>
  61. #include <time.h>
  62. #include "internal/cryptlib.h"
  63. #include <openssl/asn1.h>
  64. #include "asn1_locl.h"
  65. int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
  66. {
  67. static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
  68. static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
  69. char *a;
  70. int n, i, l, o;
  71. if (d->type != V_ASN1_GENERALIZEDTIME)
  72. return (0);
  73. l = d->length;
  74. a = (char *)d->data;
  75. o = 0;
  76. /*
  77. * GENERALIZEDTIME is similar to UTCTIME except the year is represented
  78. * as YYYY. This stuff treats everything as a two digit field so make
  79. * first two fields 00 to 99
  80. */
  81. if (l < 13)
  82. goto err;
  83. for (i = 0; i < 7; i++) {
  84. if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
  85. i++;
  86. if (tm)
  87. tm->tm_sec = 0;
  88. break;
  89. }
  90. if ((a[o] < '0') || (a[o] > '9'))
  91. goto err;
  92. n = a[o] - '0';
  93. if (++o > l)
  94. goto err;
  95. if ((a[o] < '0') || (a[o] > '9'))
  96. goto err;
  97. n = (n * 10) + a[o] - '0';
  98. if (++o > l)
  99. goto err;
  100. if ((n < min[i]) || (n > max[i]))
  101. goto err;
  102. if (tm) {
  103. switch (i) {
  104. case 0:
  105. tm->tm_year = n * 100 - 1900;
  106. break;
  107. case 1:
  108. tm->tm_year += n;
  109. break;
  110. case 2:
  111. tm->tm_mon = n - 1;
  112. break;
  113. case 3:
  114. tm->tm_mday = n;
  115. break;
  116. case 4:
  117. tm->tm_hour = n;
  118. break;
  119. case 5:
  120. tm->tm_min = n;
  121. break;
  122. case 6:
  123. tm->tm_sec = n;
  124. break;
  125. }
  126. }
  127. }
  128. /*
  129. * Optional fractional seconds: decimal point followed by one or more
  130. * digits.
  131. */
  132. if (a[o] == '.') {
  133. if (++o > l)
  134. goto err;
  135. i = o;
  136. while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
  137. o++;
  138. /* Must have at least one digit after decimal point */
  139. if (i == o)
  140. goto err;
  141. }
  142. if (a[o] == 'Z')
  143. o++;
  144. else if ((a[o] == '+') || (a[o] == '-')) {
  145. int offsign = a[o] == '-' ? -1 : 1, offset = 0;
  146. o++;
  147. if (o + 4 > l)
  148. goto err;
  149. for (i = 7; i < 9; i++) {
  150. if ((a[o] < '0') || (a[o] > '9'))
  151. goto err;
  152. n = a[o] - '0';
  153. o++;
  154. if ((a[o] < '0') || (a[o] > '9'))
  155. goto err;
  156. n = (n * 10) + a[o] - '0';
  157. if ((n < min[i]) || (n > max[i]))
  158. goto err;
  159. if (tm) {
  160. if (i == 7)
  161. offset = n * 3600;
  162. else if (i == 8)
  163. offset += n * 60;
  164. }
  165. o++;
  166. }
  167. if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
  168. return 0;
  169. } else if (a[o]) {
  170. /* Missing time zone information. */
  171. goto err;
  172. }
  173. return (o == l);
  174. err:
  175. return (0);
  176. }
  177. int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
  178. {
  179. return asn1_generalizedtime_to_tm(NULL, d);
  180. }
  181. int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
  182. {
  183. ASN1_GENERALIZEDTIME t;
  184. t.type = V_ASN1_GENERALIZEDTIME;
  185. t.length = strlen(str);
  186. t.data = (unsigned char *)str;
  187. if (ASN1_GENERALIZEDTIME_check(&t)) {
  188. if (s != NULL) {
  189. if (!ASN1_STRING_set((ASN1_STRING *)s,
  190. (unsigned char *)str, t.length))
  191. return 0;
  192. s->type = V_ASN1_GENERALIZEDTIME;
  193. }
  194. return (1);
  195. } else
  196. return (0);
  197. }
  198. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
  199. time_t t)
  200. {
  201. return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
  202. }
  203. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
  204. time_t t, int offset_day,
  205. long offset_sec)
  206. {
  207. char *p;
  208. struct tm *ts;
  209. struct tm data;
  210. size_t len = 20;
  211. if (s == NULL)
  212. s = ASN1_GENERALIZEDTIME_new();
  213. if (s == NULL)
  214. return (NULL);
  215. ts = OPENSSL_gmtime(&t, &data);
  216. if (ts == NULL)
  217. return (NULL);
  218. if (offset_day || offset_sec) {
  219. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  220. return NULL;
  221. }
  222. p = (char *)s->data;
  223. if ((p == NULL) || ((size_t)s->length < len)) {
  224. p = OPENSSL_malloc(len);
  225. if (p == NULL) {
  226. ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
  227. return (NULL);
  228. }
  229. OPENSSL_free(s->data);
  230. s->data = (unsigned char *)p;
  231. }
  232. BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
  233. ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
  234. ts->tm_sec);
  235. s->length = strlen(p);
  236. s->type = V_ASN1_GENERALIZEDTIME;
  237. #ifdef CHARSET_EBCDIC_not
  238. ebcdic2ascii(s->data, s->data, s->length);
  239. #endif
  240. return (s);
  241. }
  242. const char *_asn1_mon[12] = {
  243. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  244. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  245. };
  246. int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
  247. {
  248. char *v;
  249. int gmt = 0;
  250. int i;
  251. int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
  252. char *f = NULL;
  253. int f_len = 0;
  254. i = tm->length;
  255. v = (char *)tm->data;
  256. if (i < 12)
  257. goto err;
  258. if (v[i - 1] == 'Z')
  259. gmt = 1;
  260. for (i = 0; i < 12; i++)
  261. if ((v[i] > '9') || (v[i] < '0'))
  262. goto err;
  263. y = (v[0] - '0') * 1000 + (v[1] - '0') * 100
  264. + (v[2] - '0') * 10 + (v[3] - '0');
  265. M = (v[4] - '0') * 10 + (v[5] - '0');
  266. if ((M > 12) || (M < 1))
  267. goto err;
  268. d = (v[6] - '0') * 10 + (v[7] - '0');
  269. h = (v[8] - '0') * 10 + (v[9] - '0');
  270. m = (v[10] - '0') * 10 + (v[11] - '0');
  271. if (tm->length >= 14 &&
  272. (v[12] >= '0') && (v[12] <= '9') &&
  273. (v[13] >= '0') && (v[13] <= '9')) {
  274. s = (v[12] - '0') * 10 + (v[13] - '0');
  275. /* Check for fractions of seconds. */
  276. if (tm->length >= 15 && v[14] == '.') {
  277. int l = tm->length;
  278. f = &v[14]; /* The decimal point. */
  279. f_len = 1;
  280. while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
  281. ++f_len;
  282. }
  283. }
  284. if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
  285. _asn1_mon[M - 1], d, h, m, s, f_len, f, y,
  286. (gmt) ? " GMT" : "") <= 0)
  287. return (0);
  288. else
  289. return (1);
  290. err:
  291. BIO_write(bp, "Bad time value", 14);
  292. return (0);
  293. }