blob: b31991739a9b6932348798b5019f636b8960979f [file] [log] [blame]
vlmfa67ddc2004-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
vlmad171462004-08-26 06:22:36 +000012#if defined(WIN32) || defined(__CYGWIN__)
vlm6e73a042004-08-11 07:17:22 +000013#warning PLEASE STOP AND READ!
vlmc48f2132004-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().
vlm6e73a042004-08-11 07:17:22 +000019#warning PLEASE STOP AND READ!
vlmc48f2132004-08-12 03:52:53 +000020
vlm17909e92004-09-02 05:20:39 +000021static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
vlmc48f2132004-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
vlm17909e92004-09-02 05:20:39 +000028static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
vlmc48f2132004-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
vlm348a37a2004-08-12 03:58:25 +000035static time_t timegm(struct tm *tm) {
36 time_t tloc;
37 char *tz;
vlmd3692282004-08-12 07:47:03 +000038 char *buf;
vlm348a37a2004-08-12 03:58:25 +000039
40 tz = getenv("TZ");
vlmd3692282004-08-12 07:47:03 +000041 _putenv("TZ=UTC");
vlm17909e92004-09-02 05:20:39 +000042 _tzset();
vlm348a37a2004-08-12 03:58:25 +000043 tloc = mktime(tm);
vlmd3692282004-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);
vlm17909e92004-09-02 05:20:39 +000051 _tzset();
vlm348a37a2004-08-12 03:58:25 +000052 return tloc;
53}
54
55#if 0 /* Alternate version */
vlmc48f2132004-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}
vlm348a37a2004-08-12 03:58:25 +000066#endif
vlmc48f2132004-08-12 03:52:53 +000067#endif /* WIN32 */
vlm6e73a042004-08-11 07:17:22 +000068
vlmfa67ddc2004-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 */
vlm81057a82004-08-07 03:52:26 +000081 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
vlmfa67ddc2004-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]),
88 1, /* Single UNIVERSAL tag may be implicitly overriden */
89 -1, /* Both ways are fine */
vlme413c122004-08-20 13:23:42 +000090 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000091 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000092};
93
94#endif /* __NO_ASN_TABLE__ */
95
96/*
97 * Check that the time looks like the time.
98 */
99int
100GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
101 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000102 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000103 time_t tloc;
104
105 errno = EPERM; /* Just an unlikely error code */
vlm81057a82004-08-07 03:52:26 +0000106 tloc = asn_GT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000107 if(tloc == -1 && errno != EPERM) {
vlme3f0f282004-08-11 09:44:13 +0000108 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000109 "%s: Invalid time format: %s (%s:%d)",
110 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000111 return -1;
112 }
113
114 return 0;
115}
116
vlm81057a82004-08-07 03:52:26 +0000117der_enc_rval_t
118GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
119 int tag_mode, ber_tlv_tag_t tag,
120 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000121 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
vlm81057a82004-08-07 03:52:26 +0000122 der_enc_rval_t erval;
123
124 /* If not canonical DER, re-encode into canonical DER. */
125 if(st->size && st->buf[st->size-1] != 'Z') {
126 struct tm tm;
127 time_t tloc;
128
129 errno = EPERM;
130 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
131 if(tloc == -1 && errno != EPERM) {
132 /* Failed to recognize time. Fail completely. */
133 erval.encoded = -1;
134 erval.failed_type = td;
135 erval.structure_ptr = ptr;
136 return erval;
137 }
138 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
139 if(!st) {
140 /* Memory allocation failure. */
141 erval.encoded = -1;
142 erval.failed_type = td;
143 erval.structure_ptr = ptr;
144 return erval;
145 }
146 }
147
148 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
149
150 if(st != ptr) {
151 FREEMEM(st->buf);
152 FREEMEM(st);
153 }
154
155 return erval;
156}
157
vlmfa67ddc2004-06-03 03:38:44 +0000158int
159GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
160 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000161 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000162
vlmb42843a2004-06-05 08:17:50 +0000163 (void)td; /* Unused argument */
164 (void)ilevel; /* Unused argument */
165
vlmfa67ddc2004-06-03 03:38:44 +0000166 if(st && st->buf) {
167 char buf[32];
168 struct tm tm;
169 int ret;
170
171 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000172 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlmfa67ddc2004-06-03 03:38:44 +0000173 return cb("<bad-value>", 11, app_key);
174
175 ret = snprintf(buf, sizeof(buf),
vlm81057a82004-08-07 03:52:26 +0000176 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000177 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
178 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000179 assert(ret > 0 && ret < (int)sizeof(buf));
vlmfa67ddc2004-06-03 03:38:44 +0000180 return cb(buf, ret, app_key);
181 } else {
182 return cb("<absent>", 8, app_key);
183 }
184}
185
186/*
187 * Where to look for offset from GMT, Phase I.
188 * Several platforms are known.
189 */
vlm65efbd02004-08-11 07:35:08 +0000190#if defined(__FreeBSD__) \
191 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
192 || (defined __GLIBC__ && __GLIBC__ >= 2)
193#undef HAVE_TM_GMTOFF
194#define HAVE_TM_GMTOFF
195#endif /* BSDs and newer glibc */
vlmfa67ddc2004-06-03 03:38:44 +0000196
197/*
198 * Where to look for offset from GMT, Phase II.
199 */
vlm65efbd02004-08-11 07:35:08 +0000200#ifdef HAVE_TM_GMTOFF
vlm81057a82004-08-07 03:52:26 +0000201#define GMTOFF(tm) ((tm).tm_gmtoff)
vlm65efbd02004-08-11 07:35:08 +0000202#else /* HAVE_TM_GMTOFF */
vlm81057a82004-08-07 03:52:26 +0000203#define GMTOFF(tm) (-timezone)
vlm65efbd02004-08-11 07:35:08 +0000204#endif /* HAVE_TM_GMTOFF */
vlmfa67ddc2004-06-03 03:38:44 +0000205
206time_t
vlm81057a82004-08-07 03:52:26 +0000207asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +0000208 struct tm tm_s;
209 uint8_t *buf;
210 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000211 int gmtoff_h = 0;
212 int gmtoff_m = 0;
213 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000214 int offset_specified = 0;
215 time_t tloc;
216
217 if(!st || !st->buf) {
218 errno = EINVAL;
219 return -1;
220 } else {
221 buf = st->buf;
222 end = buf + st->size;
223 }
224
225 if(st->size < 10) {
226 errno = EINVAL;
227 return -1;
228 }
229
230 /*
231 * Decode first 10 bytes: "AAAAMMJJhh"
232 */
233 memset(&tm_s, 0, sizeof(tm_s));
234#undef B2F
235#undef B2T
236#define B2F(var) do { \
237 unsigned ch = *buf; \
238 if(ch < 0x30 && ch > 0x39) { \
239 errno = EINVAL; \
240 return -1; \
241 } else { \
242 var = var * 10 + (ch - 0x30); \
243 buf++; \
244 } \
245 } while(0)
246#define B2T(var) B2F(tm_s.var)
247
248 B2T(tm_year); /* 1: A */
249 B2T(tm_year); /* 2: A */
250 B2T(tm_year); /* 3: A */
251 B2T(tm_year); /* 4: A */
252 B2T(tm_mon); /* 5: M */
253 B2T(tm_mon); /* 6: M */
254 B2T(tm_mday); /* 7: J */
255 B2T(tm_mday); /* 8: J */
256 B2T(tm_hour); /* 9: h */
257 B2T(tm_hour); /* 0: h */
258
259 if(buf == end) goto local_finish;
260
261 /*
262 * Parse [mm[ss[(.|,)ffff]]]
263 * ^^
264 */
265 switch(*buf) {
266 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
267 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
268 tm_s.tm_min = (*buf++) - 0x30;
269 if(buf == end) { errno = EINVAL; return -1; }
270 B2T(tm_min);
271 break;
272 case 0x2B: case 0x2D: /* +, - */
273 goto offset;
274 case 0x5A: /* Z */
275 goto utc_finish;
276 default:
277 errno = EINVAL;
278 return -1;
279 }
280
281 if(buf == end) goto local_finish;
282
283 /*
284 * Parse [mm[ss[(.|,)ffff]]]
285 * ^^
286 */
287 switch(*buf) {
288 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
289 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
290 tm_s.tm_sec = (*buf++) - 0x30;
291 if(buf == end) { errno = EINVAL; return -1; }
292 B2T(tm_sec);
293 break;
294 case 0x2B: case 0x2D: /* +, - */
295 goto offset;
296 case 0x5A: /* Z */
297 goto utc_finish;
298 default:
299 errno = EINVAL;
300 return -1;
301 }
302
303 if(buf == end) goto local_finish;
304
305 /*
306 * Parse [mm[ss[(.|,)ffff]]]
307 * ^ ^
308 */
309 switch(*buf) {
310 case 0x2C: case 0x2E: /* (.|,) */
311 /* Fractions of seconds are not supported
312 * by time_t or struct tm. Skip them */
313 for(buf++; buf < end; buf++) {
314 switch(*buf) {
315 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
316 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
317 continue;
318 default:
319 break;
320 }
321 break;
322 }
323 }
324
325 if(buf == end) goto local_finish;
326
327 switch(*buf) {
328 case 0x2B: case 0x2D: /* +, - */
329 goto offset;
330 case 0x5A: /* Z */
331 goto utc_finish;
332 default:
333 errno = EINVAL;
334 return -1;
335 }
336
337
338offset:
339
340 if(end - buf < 3) {
341 errno = EINVAL;
342 return -1;
343 }
344 buf++;
vlm81057a82004-08-07 03:52:26 +0000345 B2F(gmtoff_h);
346 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000347 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000348 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000349 else
vlm81057a82004-08-07 03:52:26 +0000350 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000351
352 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000353 B2F(gmtoff_m);
354 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000355 } else if(end != buf) {
356 errno = EINVAL;
357 return -1;
358 }
359
vlm81057a82004-08-07 03:52:26 +0000360 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000361
362 /* Fall through */
363utc_finish:
364
365 offset_specified = 1;
366
367 /* Fall through */
368local_finish:
369
370 /*
371 * Validation.
372 */
373 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
374 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
375 || (tm_s.tm_hour > 23)
376 || (tm_s.tm_sec > 60)
377 ) {
378 errno = EINVAL;
379 return -1;
380 }
381
382 /* Canonicalize */
383 tm_s.tm_mon -= 1; /* 0 - 11 */
384 tm_s.tm_year -= 1900;
385 tm_s.tm_isdst = -1;
386
vlm81057a82004-08-07 03:52:26 +0000387 tm_s.tm_sec -= gmtoff;
388
389 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
390
vlmc48f2132004-08-12 03:52:53 +0000391 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000392 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000393 } else {
vlm81057a82004-08-07 03:52:26 +0000394 /*
395 * Without an offset (or 'Z'),
396 * we can only guess that it is a local zone.
397 * Interpret it in this fashion.
398 */
399 tloc = mktime(&tm_s);
400 }
vlmfa67ddc2004-06-03 03:38:44 +0000401 if(tloc == -1) {
402 errno = EINVAL;
403 return -1;
404 }
405
vlm81057a82004-08-07 03:52:26 +0000406 if(ret_tm) {
407 if(as_gmt) {
408 if(offset_specified) {
409 *ret_tm = tm_s;
410 } else {
411 if(gmtime_r(&tloc, ret_tm) == 0) {
412 errno = EINVAL;
413 return -1;
414 }
415 }
416 } else {
417 if(localtime_r(&tloc, ret_tm) == 0) {
418 errno = EINVAL;
419 return -1;
420 }
vlmfa67ddc2004-06-03 03:38:44 +0000421 }
422 }
423
vlmfa67ddc2004-06-03 03:38:44 +0000424 return tloc;
425}
426
vlm81057a82004-08-07 03:52:26 +0000427
428GeneralizedTime_t *
429asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
430 struct tm tm_s;
431 long gmtoff;
432 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
433 char *buf;
434 char *p;
435 int size;
436
437 /* Check arguments */
438 if(!tm) {
439 errno = EINVAL;
440 return 0;
441 }
442
443 /* Pre-allocate a buffer of sufficient yet small length */
vlmda674682004-08-11 09:07:36 +0000444 (void *)buf = MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000445 if(!buf) return 0;
446
447 gmtoff = GMTOFF(*tm);
448
449 if(force_gmt && gmtoff) {
450 tm_s = *tm;
451 tm_s.tm_sec -= gmtoff;
452 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000453 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000454#ifdef HAVE_TM_GMTOFF
455 assert(!GMTOFF(tm_s)); /* Will fix itself */
456#else
457 gmtoff = 0; /* Intervention required */
458#endif
vlm81057a82004-08-07 03:52:26 +0000459 }
460
461 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
462 tm->tm_year + 1900,
463 tm->tm_mon + 1,
464 tm->tm_mday,
465 tm->tm_hour,
466 tm->tm_min,
467 tm->tm_sec
468 );
469 assert(size == 14);
470
471 p = buf + size;
472 if(force_gmt) {
473 *p++ = 0x5a; /* 'Z' */
474 *p++ = 0;
475 size++;
476 } else {
477 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
478 gmtoff / 3600, gmtoff % 3600);
479 assert(ret >= 5 && ret <= 7);
480 size += ret;
481 }
482
483 if(opt_gt) {
484 if(opt_gt->buf)
485 FREEMEM(opt_gt->buf);
486 } else {
vlmda674682004-08-11 09:07:36 +0000487 (void *)opt_gt = CALLOC(1, sizeof *opt_gt);
vlm81057a82004-08-07 03:52:26 +0000488 if(!opt_gt) { free(buf); return 0; }
489 }
490
vlm1ff928d2004-08-11 08:10:13 +0000491 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000492 opt_gt->size = size;
493
494 return opt_gt;
495}
496
497