Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

262 рядки
8.3 KiB

  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 "asn1_locl.h"
  62. int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
  63. {
  64. static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
  65. static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
  66. char *a;
  67. int n, i, l, o;
  68. if (d->type != V_ASN1_GENERALIZEDTIME)
  69. return (0);
  70. l = d->length;
  71. a = (char *)d->data;
  72. o = 0;
  73. /*
  74. * GENERALIZEDTIME is similar to UTCTIME except the year is represented
  75. * as YYYY. This stuff treats everything as a two digit field so make
  76. * first two fields 00 to 99
  77. */
  78. if (l < 13)
  79. goto err;
  80. for (i = 0; i < 7; i++) {
  81. if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
  82. i++;
  83. if (tm)
  84. tm->tm_sec = 0;
  85. break;
  86. }
  87. if ((a[o] < '0') || (a[o] > '9'))
  88. goto err;
  89. n = a[o] - '0';
  90. if (++o > l)
  91. goto err;
  92. if ((a[o] < '0') || (a[o] > '9'))
  93. goto err;
  94. n = (n * 10) + a[o] - '0';
  95. if (++o > l)
  96. goto err;
  97. if ((n < min[i]) || (n > max[i]))
  98. goto err;
  99. if (tm) {
  100. switch (i) {
  101. case 0:
  102. tm->tm_year = n * 100 - 1900;
  103. break;
  104. case 1:
  105. tm->tm_year += n;
  106. break;
  107. case 2:
  108. tm->tm_mon = n - 1;
  109. break;
  110. case 3:
  111. tm->tm_mday = n;
  112. break;
  113. case 4:
  114. tm->tm_hour = n;
  115. break;
  116. case 5:
  117. tm->tm_min = n;
  118. break;
  119. case 6:
  120. tm->tm_sec = n;
  121. break;
  122. }
  123. }
  124. }
  125. /*
  126. * Optional fractional seconds: decimal point followed by one or more
  127. * digits.
  128. */
  129. if (a[o] == '.') {
  130. if (++o > l)
  131. goto err;
  132. i = o;
  133. while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
  134. o++;
  135. /* Must have at least one digit after decimal point */
  136. if (i == o)
  137. goto err;
  138. }
  139. if (a[o] == 'Z')
  140. o++;
  141. else if ((a[o] == '+') || (a[o] == '-')) {
  142. int offsign = a[o] == '-' ? 1 : -1, offset = 0;
  143. o++;
  144. if (o + 4 > l)
  145. goto err;
  146. for (i = 7; i < 9; i++) {
  147. if ((a[o] < '0') || (a[o] > '9'))
  148. goto err;
  149. n = a[o] - '0';
  150. o++;
  151. if ((a[o] < '0') || (a[o] > '9'))
  152. goto err;
  153. n = (n * 10) + a[o] - '0';
  154. if ((n < min[i]) || (n > max[i]))
  155. goto err;
  156. if (tm) {
  157. if (i == 7)
  158. offset = n * 3600;
  159. else if (i == 8)
  160. offset += n * 60;
  161. }
  162. o++;
  163. }
  164. if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
  165. return 0;
  166. } else if (a[o]) {
  167. /* Missing time zone information. */
  168. goto err;
  169. }
  170. return (o == l);
  171. err:
  172. return (0);
  173. }
  174. int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
  175. {
  176. return asn1_generalizedtime_to_tm(NULL, d);
  177. }
  178. int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
  179. {
  180. ASN1_GENERALIZEDTIME t;
  181. t.type = V_ASN1_GENERALIZEDTIME;
  182. t.length = strlen(str);
  183. t.data = (unsigned char *)str;
  184. if (ASN1_GENERALIZEDTIME_check(&t)) {
  185. if (s != NULL) {
  186. if (!ASN1_STRING_set((ASN1_STRING *)s,
  187. (unsigned char *)str, t.length))
  188. return 0;
  189. s->type = V_ASN1_GENERALIZEDTIME;
  190. }
  191. return (1);
  192. } else
  193. return (0);
  194. }
  195. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
  196. time_t t)
  197. {
  198. return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
  199. }
  200. ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
  201. time_t t, int offset_day,
  202. long offset_sec)
  203. {
  204. char *p;
  205. struct tm *ts;
  206. struct tm data;
  207. size_t len = 20;
  208. ASN1_GENERALIZEDTIME *tmps = NULL;
  209. if (s == NULL)
  210. tmps = ASN1_GENERALIZEDTIME_new();
  211. else
  212. tmps = s;
  213. if (tmps == NULL)
  214. return NULL;
  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. p = (char *)tmps->data;
  223. if ((p == NULL) || ((size_t)tmps->length < len)) {
  224. p = OPENSSL_malloc(len);
  225. if (p == NULL) {
  226. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  227. goto err;
  228. }
  229. OPENSSL_free(tmps->data);
  230. tmps->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. tmps->length = strlen(p);
  236. tmps->type = V_ASN1_GENERALIZEDTIME;
  237. return tmps;
  238. err:
  239. if (s == NULL)
  240. ASN1_GENERALIZEDTIME_free(tmps);
  241. return NULL;
  242. }