blob: 6a89b2d649d5d3970e211651abfb82fdb9d454d9 [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
vlmc12c2102004-09-04 04:43:28 +000012#if defined(WIN32)
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]),
vlm72425de2004-09-13 08:31:01 +000088 asn1_DEF_GeneralizedTime_tags, /* Same as above */
89 sizeof(asn1_DEF_GeneralizedTime_tags)
90 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
vlmfa67ddc2004-06-03 03:38:44 +000091 -1, /* Both ways are fine */
vlme413c122004-08-20 13:23:42 +000092 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000093 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000094};
95
96#endif /* __NO_ASN_TABLE__ */
97
98/*
99 * Check that the time looks like the time.
100 */
101int
102GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
103 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000104 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000105 time_t tloc;
106
107 errno = EPERM; /* Just an unlikely error code */
vlm81057a82004-08-07 03:52:26 +0000108 tloc = asn_GT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000109 if(tloc == -1 && errno != EPERM) {
vlme3f0f282004-08-11 09:44:13 +0000110 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000111 "%s: Invalid time format: %s (%s:%d)",
112 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000113 return -1;
114 }
115
116 return 0;
117}
118
vlm81057a82004-08-07 03:52:26 +0000119der_enc_rval_t
120GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
121 int tag_mode, ber_tlv_tag_t tag,
122 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000123 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
vlm81057a82004-08-07 03:52:26 +0000124 der_enc_rval_t erval;
125
126 /* If not canonical DER, re-encode into canonical DER. */
127 if(st->size && st->buf[st->size-1] != 'Z') {
128 struct tm tm;
129 time_t tloc;
130
131 errno = EPERM;
132 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
133 if(tloc == -1 && errno != EPERM) {
134 /* Failed to recognize time. Fail completely. */
135 erval.encoded = -1;
136 erval.failed_type = td;
137 erval.structure_ptr = ptr;
138 return erval;
139 }
140 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
141 if(!st) {
142 /* Memory allocation failure. */
143 erval.encoded = -1;
144 erval.failed_type = td;
145 erval.structure_ptr = ptr;
146 return erval;
147 }
148 }
149
150 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
151
152 if(st != ptr) {
153 FREEMEM(st->buf);
154 FREEMEM(st);
155 }
156
157 return erval;
158}
159
vlmfa67ddc2004-06-03 03:38:44 +0000160int
161GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
162 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000163 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000164
vlmb42843a2004-06-05 08:17:50 +0000165 (void)td; /* Unused argument */
166 (void)ilevel; /* Unused argument */
167
vlmfa67ddc2004-06-03 03:38:44 +0000168 if(st && st->buf) {
169 char buf[32];
170 struct tm tm;
171 int ret;
172
173 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000174 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlmfa67ddc2004-06-03 03:38:44 +0000175 return cb("<bad-value>", 11, app_key);
176
177 ret = snprintf(buf, sizeof(buf),
vlm81057a82004-08-07 03:52:26 +0000178 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000179 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
180 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000181 assert(ret > 0 && ret < (int)sizeof(buf));
vlmfa67ddc2004-06-03 03:38:44 +0000182 return cb(buf, ret, app_key);
183 } else {
184 return cb("<absent>", 8, app_key);
185 }
186}
187
188/*
189 * Where to look for offset from GMT, Phase I.
190 * Several platforms are known.
191 */
vlm65efbd02004-08-11 07:35:08 +0000192#if defined(__FreeBSD__) \
193 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
194 || (defined __GLIBC__ && __GLIBC__ >= 2)
195#undef HAVE_TM_GMTOFF
196#define HAVE_TM_GMTOFF
197#endif /* BSDs and newer glibc */
vlmfa67ddc2004-06-03 03:38:44 +0000198
199/*
200 * Where to look for offset from GMT, Phase II.
201 */
vlm65efbd02004-08-11 07:35:08 +0000202#ifdef HAVE_TM_GMTOFF
vlm81057a82004-08-07 03:52:26 +0000203#define GMTOFF(tm) ((tm).tm_gmtoff)
vlm65efbd02004-08-11 07:35:08 +0000204#else /* HAVE_TM_GMTOFF */
vlm81057a82004-08-07 03:52:26 +0000205#define GMTOFF(tm) (-timezone)
vlm65efbd02004-08-11 07:35:08 +0000206#endif /* HAVE_TM_GMTOFF */
vlmfa67ddc2004-06-03 03:38:44 +0000207
208time_t
vlm81057a82004-08-07 03:52:26 +0000209asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +0000210 struct tm tm_s;
211 uint8_t *buf;
212 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000213 int gmtoff_h = 0;
214 int gmtoff_m = 0;
215 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000216 int offset_specified = 0;
217 time_t tloc;
218
219 if(!st || !st->buf) {
220 errno = EINVAL;
221 return -1;
222 } else {
223 buf = st->buf;
224 end = buf + st->size;
225 }
226
227 if(st->size < 10) {
228 errno = EINVAL;
229 return -1;
230 }
231
232 /*
233 * Decode first 10 bytes: "AAAAMMJJhh"
234 */
235 memset(&tm_s, 0, sizeof(tm_s));
236#undef B2F
237#undef B2T
238#define B2F(var) do { \
239 unsigned ch = *buf; \
240 if(ch < 0x30 && ch > 0x39) { \
241 errno = EINVAL; \
242 return -1; \
243 } else { \
244 var = var * 10 + (ch - 0x30); \
245 buf++; \
246 } \
247 } while(0)
248#define B2T(var) B2F(tm_s.var)
249
250 B2T(tm_year); /* 1: A */
251 B2T(tm_year); /* 2: A */
252 B2T(tm_year); /* 3: A */
253 B2T(tm_year); /* 4: A */
254 B2T(tm_mon); /* 5: M */
255 B2T(tm_mon); /* 6: M */
256 B2T(tm_mday); /* 7: J */
257 B2T(tm_mday); /* 8: J */
258 B2T(tm_hour); /* 9: h */
259 B2T(tm_hour); /* 0: h */
260
261 if(buf == end) goto local_finish;
262
263 /*
264 * Parse [mm[ss[(.|,)ffff]]]
265 * ^^
266 */
267 switch(*buf) {
268 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
269 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
270 tm_s.tm_min = (*buf++) - 0x30;
271 if(buf == end) { errno = EINVAL; return -1; }
272 B2T(tm_min);
273 break;
274 case 0x2B: case 0x2D: /* +, - */
275 goto offset;
276 case 0x5A: /* Z */
277 goto utc_finish;
278 default:
279 errno = EINVAL;
280 return -1;
281 }
282
283 if(buf == end) goto local_finish;
284
285 /*
286 * Parse [mm[ss[(.|,)ffff]]]
287 * ^^
288 */
289 switch(*buf) {
290 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
291 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
292 tm_s.tm_sec = (*buf++) - 0x30;
293 if(buf == end) { errno = EINVAL; return -1; }
294 B2T(tm_sec);
295 break;
296 case 0x2B: case 0x2D: /* +, - */
297 goto offset;
298 case 0x5A: /* Z */
299 goto utc_finish;
300 default:
301 errno = EINVAL;
302 return -1;
303 }
304
305 if(buf == end) goto local_finish;
306
307 /*
308 * Parse [mm[ss[(.|,)ffff]]]
309 * ^ ^
310 */
311 switch(*buf) {
312 case 0x2C: case 0x2E: /* (.|,) */
313 /* Fractions of seconds are not supported
314 * by time_t or struct tm. Skip them */
315 for(buf++; buf < end; buf++) {
316 switch(*buf) {
317 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
318 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
319 continue;
320 default:
321 break;
322 }
323 break;
324 }
325 }
326
327 if(buf == end) goto local_finish;
328
329 switch(*buf) {
330 case 0x2B: case 0x2D: /* +, - */
331 goto offset;
332 case 0x5A: /* Z */
333 goto utc_finish;
334 default:
335 errno = EINVAL;
336 return -1;
337 }
338
339
340offset:
341
342 if(end - buf < 3) {
343 errno = EINVAL;
344 return -1;
345 }
346 buf++;
vlm81057a82004-08-07 03:52:26 +0000347 B2F(gmtoff_h);
348 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000349 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000350 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000351 else
vlm81057a82004-08-07 03:52:26 +0000352 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000353
354 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000355 B2F(gmtoff_m);
356 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000357 } else if(end != buf) {
358 errno = EINVAL;
359 return -1;
360 }
361
vlm81057a82004-08-07 03:52:26 +0000362 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000363
364 /* Fall through */
365utc_finish:
366
367 offset_specified = 1;
368
369 /* Fall through */
370local_finish:
371
372 /*
373 * Validation.
374 */
375 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
376 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
377 || (tm_s.tm_hour > 23)
378 || (tm_s.tm_sec > 60)
379 ) {
380 errno = EINVAL;
381 return -1;
382 }
383
384 /* Canonicalize */
385 tm_s.tm_mon -= 1; /* 0 - 11 */
386 tm_s.tm_year -= 1900;
387 tm_s.tm_isdst = -1;
388
vlm81057a82004-08-07 03:52:26 +0000389 tm_s.tm_sec -= gmtoff;
390
391 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
392
vlmc48f2132004-08-12 03:52:53 +0000393 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000394 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000395 } else {
vlm81057a82004-08-07 03:52:26 +0000396 /*
397 * Without an offset (or 'Z'),
398 * we can only guess that it is a local zone.
399 * Interpret it in this fashion.
400 */
401 tloc = mktime(&tm_s);
402 }
vlmfa67ddc2004-06-03 03:38:44 +0000403 if(tloc == -1) {
404 errno = EINVAL;
405 return -1;
406 }
407
vlm81057a82004-08-07 03:52:26 +0000408 if(ret_tm) {
409 if(as_gmt) {
410 if(offset_specified) {
411 *ret_tm = tm_s;
412 } else {
413 if(gmtime_r(&tloc, ret_tm) == 0) {
414 errno = EINVAL;
415 return -1;
416 }
417 }
418 } else {
419 if(localtime_r(&tloc, ret_tm) == 0) {
420 errno = EINVAL;
421 return -1;
422 }
vlmfa67ddc2004-06-03 03:38:44 +0000423 }
424 }
425
vlmfa67ddc2004-06-03 03:38:44 +0000426 return tloc;
427}
428
vlm81057a82004-08-07 03:52:26 +0000429
430GeneralizedTime_t *
431asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
432 struct tm tm_s;
433 long gmtoff;
434 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
435 char *buf;
436 char *p;
437 int size;
438
439 /* Check arguments */
440 if(!tm) {
441 errno = EINVAL;
442 return 0;
443 }
444
445 /* Pre-allocate a buffer of sufficient yet small length */
vlmda674682004-08-11 09:07:36 +0000446 (void *)buf = MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000447 if(!buf) return 0;
448
449 gmtoff = GMTOFF(*tm);
450
451 if(force_gmt && gmtoff) {
452 tm_s = *tm;
453 tm_s.tm_sec -= gmtoff;
454 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000455 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000456#ifdef HAVE_TM_GMTOFF
457 assert(!GMTOFF(tm_s)); /* Will fix itself */
458#else
459 gmtoff = 0; /* Intervention required */
460#endif
vlm81057a82004-08-07 03:52:26 +0000461 }
462
463 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
464 tm->tm_year + 1900,
465 tm->tm_mon + 1,
466 tm->tm_mday,
467 tm->tm_hour,
468 tm->tm_min,
469 tm->tm_sec
470 );
471 assert(size == 14);
472
473 p = buf + size;
474 if(force_gmt) {
475 *p++ = 0x5a; /* 'Z' */
476 *p++ = 0;
477 size++;
478 } else {
479 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
480 gmtoff / 3600, gmtoff % 3600);
481 assert(ret >= 5 && ret <= 7);
482 size += ret;
483 }
484
485 if(opt_gt) {
486 if(opt_gt->buf)
487 FREEMEM(opt_gt->buf);
488 } else {
vlmda674682004-08-11 09:07:36 +0000489 (void *)opt_gt = CALLOC(1, sizeof *opt_gt);
vlm81057a82004-08-07 03:52:26 +0000490 if(!opt_gt) { free(buf); return 0; }
491 }
492
vlm1ff928d2004-08-11 08:10:13 +0000493 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000494 opt_gt->size = size;
495
496 return opt_gt;
497}
498
499