blob: c66c3145542ad8f7ad65d13b15a88c7e9b097328 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <GeneralizedTime.h>
6#include <time.h>
7#include <errno.h>
8#ifndef __NO_ASSERT_H__
9#include <assert.h>
10#endif /* __NO_ASSERT_H__ */
11
Lev Walkin2a1646d2004-09-04 04:43:28 +000012#if defined(WIN32)
Lev Walkin64399722004-08-11 07:17:22 +000013#warning PLEASE STOP AND READ!
Lev Walkin02a31552004-08-12 03:52:53 +000014#warning localtime_r is implemented via localtime(), which is not thread-safe.
15#warning gmtime_r is implemented via gmtime(), which is not thread-safe.
16#warning
17#warning You must fix the code by inserting appropriate locking
18#warning if you want to use asn_GT2time() or asn_UT2time().
Lev Walkin64399722004-08-11 07:17:22 +000019#warning PLEASE STOP AND READ!
Lev Walkin02a31552004-08-12 03:52:53 +000020
Lev Walkincec43b52004-09-02 05:20:39 +000021static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000022 struct tm *tm;
23 if((tm = localtime(tloc)))
24 return memcpy(result, tm, sizeof(struct tm));
25 return 0;
26}
27
Lev Walkincec43b52004-09-02 05:20:39 +000028static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000029 struct tm *tm;
30 if((tm = gmtime(tloc)))
31 return memcpy(result, tm, sizeof(struct tm));
32 return 0;
33}
34
Lev Walkindb424002004-08-12 03:58:25 +000035static time_t timegm(struct tm *tm) {
36 time_t tloc;
37 char *tz;
Lev Walkine730f2b2004-08-12 07:47:03 +000038 char *buf;
Lev Walkindb424002004-08-12 03:58:25 +000039
40 tz = getenv("TZ");
Lev Walkine730f2b2004-08-12 07:47:03 +000041 _putenv("TZ=UTC");
Lev Walkincec43b52004-09-02 05:20:39 +000042 _tzset();
Lev Walkindb424002004-08-12 03:58:25 +000043 tloc = mktime(tm);
Lev Walkine730f2b2004-08-12 07:47:03 +000044 if (tz) {
45 buf = alloca(strlen(tz) + 4);
46 sprintf(buf, "TZ=%s", tz);
47 } else {
48 buf = "TZ=";
49 }
50 _putenv(buf);
Lev Walkincec43b52004-09-02 05:20:39 +000051 _tzset();
Lev Walkindb424002004-08-12 03:58:25 +000052 return tloc;
53}
54
55#if 0 /* Alternate version */
Lev Walkin02a31552004-08-12 03:52:53 +000056/* vlm: I am not sure about validity of this algorithm. */
57static time_t timegm(struct tm *tm) {
58 struct tm tmp;
59 time_t tloc = mktime(tm);
60 localtime_r(&tloc, &tmp); /* Figure out our GMT offset */
61 tloc += tmp.tm_gmtoff;
62 tm->tm_zone = "GMT";
63 tm->tm_gmtoff = 0; /* Simulate GMT */
64 return tloc;
65}
Lev Walkindb424002004-08-12 03:58:25 +000066#endif
Lev Walkin02a31552004-08-12 03:52:53 +000067#endif /* WIN32 */
Lev Walkin64399722004-08-11 07:17:22 +000068
Lev Walkinf15320b2004-06-03 03:38:44 +000069#ifndef __NO_ASN_TABLE__
70
71/*
72 * GeneralizedTime basic type description.
73 */
74static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = {
75 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2))
76};
77asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
78 "GeneralizedTime",
79 GeneralizedTime_constraint, /* Check validity of time */
80 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkin99006362004-08-07 03:52:26 +000081 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000082 GeneralizedTime_print,
83 OCTET_STRING_free,
84 0, /* Use generic outmost tag fetcher */
85 asn1_DEF_GeneralizedTime_tags,
86 sizeof(asn1_DEF_GeneralizedTime_tags)
87 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
Lev Walkinf15320b2004-06-03 03:38:44 +000088 -1, /* Both ways are fine */
Lev Walkin449f8322004-08-20 13:23:42 +000089 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000090 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000091};
92
93#endif /* __NO_ASN_TABLE__ */
94
95/*
96 * Check that the time looks like the time.
97 */
98int
99GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
100 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000101 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 time_t tloc;
103
104 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000105 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000107 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000108 "%s: Invalid time format: %s (%s:%d)",
109 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 return -1;
111 }
112
113 return 0;
114}
115
Lev Walkin99006362004-08-07 03:52:26 +0000116der_enc_rval_t
117GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
118 int tag_mode, ber_tlv_tag_t tag,
119 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000120 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
Lev Walkin99006362004-08-07 03:52:26 +0000121 der_enc_rval_t erval;
122
123 /* If not canonical DER, re-encode into canonical DER. */
124 if(st->size && st->buf[st->size-1] != 'Z') {
125 struct tm tm;
126 time_t tloc;
127
128 errno = EPERM;
129 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
130 if(tloc == -1 && errno != EPERM) {
131 /* Failed to recognize time. Fail completely. */
132 erval.encoded = -1;
133 erval.failed_type = td;
134 erval.structure_ptr = ptr;
135 return erval;
136 }
137 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
138 if(!st) {
139 /* Memory allocation failure. */
140 erval.encoded = -1;
141 erval.failed_type = td;
142 erval.structure_ptr = ptr;
143 return erval;
144 }
145 }
146
147 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
148
149 if(st != ptr) {
150 FREEMEM(st->buf);
151 FREEMEM(st);
152 }
153
154 return erval;
155}
156
Lev Walkinf15320b2004-06-03 03:38:44 +0000157int
158GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
159 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000160 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000161
Lev Walkind9bd7752004-06-05 08:17:50 +0000162 (void)td; /* Unused argument */
163 (void)ilevel; /* Unused argument */
164
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 if(st && st->buf) {
166 char buf[32];
167 struct tm tm;
168 int ret;
169
170 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000171 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 return cb("<bad-value>", 11, app_key);
173
174 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000175 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
177 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000178 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 return cb(buf, ret, app_key);
180 } else {
181 return cb("<absent>", 8, app_key);
182 }
183}
184
185/*
186 * Where to look for offset from GMT, Phase I.
187 * Several platforms are known.
188 */
Lev Walkin659a7512004-08-11 07:35:08 +0000189#if defined(__FreeBSD__) \
190 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
191 || (defined __GLIBC__ && __GLIBC__ >= 2)
192#undef HAVE_TM_GMTOFF
193#define HAVE_TM_GMTOFF
194#endif /* BSDs and newer glibc */
Lev Walkinf15320b2004-06-03 03:38:44 +0000195
196/*
197 * Where to look for offset from GMT, Phase II.
198 */
Lev Walkin659a7512004-08-11 07:35:08 +0000199#ifdef HAVE_TM_GMTOFF
Lev Walkin99006362004-08-07 03:52:26 +0000200#define GMTOFF(tm) ((tm).tm_gmtoff)
Lev Walkin659a7512004-08-11 07:35:08 +0000201#else /* HAVE_TM_GMTOFF */
Lev Walkin99006362004-08-07 03:52:26 +0000202#define GMTOFF(tm) (-timezone)
Lev Walkin659a7512004-08-11 07:35:08 +0000203#endif /* HAVE_TM_GMTOFF */
Lev Walkinf15320b2004-06-03 03:38:44 +0000204
205time_t
Lev Walkin99006362004-08-07 03:52:26 +0000206asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 struct tm tm_s;
208 uint8_t *buf;
209 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000210 int gmtoff_h = 0;
211 int gmtoff_m = 0;
212 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 int offset_specified = 0;
214 time_t tloc;
215
216 if(!st || !st->buf) {
217 errno = EINVAL;
218 return -1;
219 } else {
220 buf = st->buf;
221 end = buf + st->size;
222 }
223
224 if(st->size < 10) {
225 errno = EINVAL;
226 return -1;
227 }
228
229 /*
230 * Decode first 10 bytes: "AAAAMMJJhh"
231 */
232 memset(&tm_s, 0, sizeof(tm_s));
233#undef B2F
234#undef B2T
235#define B2F(var) do { \
236 unsigned ch = *buf; \
237 if(ch < 0x30 && ch > 0x39) { \
238 errno = EINVAL; \
239 return -1; \
240 } else { \
241 var = var * 10 + (ch - 0x30); \
242 buf++; \
243 } \
244 } while(0)
245#define B2T(var) B2F(tm_s.var)
246
247 B2T(tm_year); /* 1: A */
248 B2T(tm_year); /* 2: A */
249 B2T(tm_year); /* 3: A */
250 B2T(tm_year); /* 4: A */
251 B2T(tm_mon); /* 5: M */
252 B2T(tm_mon); /* 6: M */
253 B2T(tm_mday); /* 7: J */
254 B2T(tm_mday); /* 8: J */
255 B2T(tm_hour); /* 9: h */
256 B2T(tm_hour); /* 0: h */
257
258 if(buf == end) goto local_finish;
259
260 /*
261 * Parse [mm[ss[(.|,)ffff]]]
262 * ^^
263 */
264 switch(*buf) {
265 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
266 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
267 tm_s.tm_min = (*buf++) - 0x30;
268 if(buf == end) { errno = EINVAL; return -1; }
269 B2T(tm_min);
270 break;
271 case 0x2B: case 0x2D: /* +, - */
272 goto offset;
273 case 0x5A: /* Z */
274 goto utc_finish;
275 default:
276 errno = EINVAL;
277 return -1;
278 }
279
280 if(buf == end) goto local_finish;
281
282 /*
283 * Parse [mm[ss[(.|,)ffff]]]
284 * ^^
285 */
286 switch(*buf) {
287 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
288 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
289 tm_s.tm_sec = (*buf++) - 0x30;
290 if(buf == end) { errno = EINVAL; return -1; }
291 B2T(tm_sec);
292 break;
293 case 0x2B: case 0x2D: /* +, - */
294 goto offset;
295 case 0x5A: /* Z */
296 goto utc_finish;
297 default:
298 errno = EINVAL;
299 return -1;
300 }
301
302 if(buf == end) goto local_finish;
303
304 /*
305 * Parse [mm[ss[(.|,)ffff]]]
306 * ^ ^
307 */
308 switch(*buf) {
309 case 0x2C: case 0x2E: /* (.|,) */
310 /* Fractions of seconds are not supported
311 * by time_t or struct tm. Skip them */
312 for(buf++; buf < end; buf++) {
313 switch(*buf) {
314 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
315 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
316 continue;
317 default:
318 break;
319 }
320 break;
321 }
322 }
323
324 if(buf == end) goto local_finish;
325
326 switch(*buf) {
327 case 0x2B: case 0x2D: /* +, - */
328 goto offset;
329 case 0x5A: /* Z */
330 goto utc_finish;
331 default:
332 errno = EINVAL;
333 return -1;
334 }
335
336
337offset:
338
339 if(end - buf < 3) {
340 errno = EINVAL;
341 return -1;
342 }
343 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000344 B2F(gmtoff_h);
345 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000346 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000347 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000348 else
Lev Walkin99006362004-08-07 03:52:26 +0000349 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000350
351 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000352 B2F(gmtoff_m);
353 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 } else if(end != buf) {
355 errno = EINVAL;
356 return -1;
357 }
358
Lev Walkin99006362004-08-07 03:52:26 +0000359 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000360
361 /* Fall through */
362utc_finish:
363
364 offset_specified = 1;
365
366 /* Fall through */
367local_finish:
368
369 /*
370 * Validation.
371 */
372 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
373 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
374 || (tm_s.tm_hour > 23)
375 || (tm_s.tm_sec > 60)
376 ) {
377 errno = EINVAL;
378 return -1;
379 }
380
381 /* Canonicalize */
382 tm_s.tm_mon -= 1; /* 0 - 11 */
383 tm_s.tm_year -= 1900;
384 tm_s.tm_isdst = -1;
385
Lev Walkin99006362004-08-07 03:52:26 +0000386 tm_s.tm_sec -= gmtoff;
387
388 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
389
Lev Walkin02a31552004-08-12 03:52:53 +0000390 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000391 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000392 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000393 /*
394 * Without an offset (or 'Z'),
395 * we can only guess that it is a local zone.
396 * Interpret it in this fashion.
397 */
398 tloc = mktime(&tm_s);
399 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 if(tloc == -1) {
401 errno = EINVAL;
402 return -1;
403 }
404
Lev Walkin99006362004-08-07 03:52:26 +0000405 if(ret_tm) {
406 if(as_gmt) {
407 if(offset_specified) {
408 *ret_tm = tm_s;
409 } else {
410 if(gmtime_r(&tloc, ret_tm) == 0) {
411 errno = EINVAL;
412 return -1;
413 }
414 }
415 } else {
416 if(localtime_r(&tloc, ret_tm) == 0) {
417 errno = EINVAL;
418 return -1;
419 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000420 }
421 }
422
Lev Walkinf15320b2004-06-03 03:38:44 +0000423 return tloc;
424}
425
Lev Walkin99006362004-08-07 03:52:26 +0000426
427GeneralizedTime_t *
428asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
429 struct tm tm_s;
430 long gmtoff;
431 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
432 char *buf;
433 char *p;
434 int size;
435
436 /* Check arguments */
437 if(!tm) {
438 errno = EINVAL;
439 return 0;
440 }
441
442 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkinc2346572004-08-11 09:07:36 +0000443 (void *)buf = MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000444 if(!buf) return 0;
445
446 gmtoff = GMTOFF(*tm);
447
448 if(force_gmt && gmtoff) {
449 tm_s = *tm;
450 tm_s.tm_sec -= gmtoff;
451 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000452 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000453#ifdef HAVE_TM_GMTOFF
454 assert(!GMTOFF(tm_s)); /* Will fix itself */
455#else
456 gmtoff = 0; /* Intervention required */
457#endif
Lev Walkin99006362004-08-07 03:52:26 +0000458 }
459
460 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
461 tm->tm_year + 1900,
462 tm->tm_mon + 1,
463 tm->tm_mday,
464 tm->tm_hour,
465 tm->tm_min,
466 tm->tm_sec
467 );
468 assert(size == 14);
469
470 p = buf + size;
471 if(force_gmt) {
472 *p++ = 0x5a; /* 'Z' */
473 *p++ = 0;
474 size++;
475 } else {
476 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
477 gmtoff / 3600, gmtoff % 3600);
478 assert(ret >= 5 && ret <= 7);
479 size += ret;
480 }
481
482 if(opt_gt) {
483 if(opt_gt->buf)
484 FREEMEM(opt_gt->buf);
485 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000486 (void *)opt_gt = CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000487 if(!opt_gt) { free(buf); return 0; }
488 }
489
Lev Walkin4d9528c2004-08-11 08:10:13 +0000490 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000491 opt_gt->size = size;
492
493 return opt_gt;
494}
495
496