blob: 8f8fff21e6e171b90d82a82903e7cef47abfb4c4 [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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <GeneralizedTime.h>
7#include <time.h>
8#include <errno.h>
9#ifndef __NO_ASSERT_H__
10#include <assert.h>
11#endif /* __NO_ASSERT_H__ */
12
Lev Walkin2a1646d2004-09-04 04:43:28 +000013#if defined(WIN32)
Lev Walkin64399722004-08-11 07:17:22 +000014#warning PLEASE STOP AND READ!
Lev Walkin93e9fe32004-09-17 06:46:10 +000015#warning localtime_r is implemented via localtime(), which may be not thread-safe.
16#warning gmtime_r is implemented via gmtime(), which may be not thread-safe.
Lev Walkin02a31552004-08-12 03:52:53 +000017#warning
18#warning You must fix the code by inserting appropriate locking
19#warning if you want to use asn_GT2time() or asn_UT2time().
Lev Walkin64399722004-08-11 07:17:22 +000020#warning PLEASE STOP AND READ!
Lev Walkin02a31552004-08-12 03:52:53 +000021
Lev Walkincec43b52004-09-02 05:20:39 +000022static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000023 struct tm *tm;
24 if((tm = localtime(tloc)))
25 return memcpy(result, tm, sizeof(struct tm));
26 return 0;
27}
28
Lev Walkincec43b52004-09-02 05:20:39 +000029static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000030 struct tm *tm;
31 if((tm = gmtime(tloc)))
32 return memcpy(result, tm, sizeof(struct tm));
33 return 0;
34}
35
Lev Walkin93e9fe32004-09-17 06:46:10 +000036#define tzset() _tzset()
37#define _EMULATE_TIMEGM
38
39#endif /* WIN32 */
40
41/*
42 * Where to look for offset from GMT, Phase I.
43 * Several platforms are known.
44 */
45#if defined(__FreeBSD__) \
46 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
47 || (defined __GLIBC__ && __GLIBC__ >= 2)
48#undef HAVE_TM_GMTOFF
49#define HAVE_TM_GMTOFF
50#endif /* BSDs and newer glibc */
51
52/*
53 * Where to look for offset from GMT, Phase II.
54 */
55#ifdef HAVE_TM_GMTOFF
56#define GMTOFF(tm) ((tm).tm_gmtoff)
57#else /* HAVE_TM_GMTOFF */
58#define GMTOFF(tm) (-timezone)
59#endif /* HAVE_TM_GMTOFF */
60
61/*
62 * Override our GMTOFF decision for other known platforms.
63 */
64#ifdef __CYGWIN__
65#undef GMTOFF
66static long GMTOFF(struct tm a){
67 struct tm *lt;
68 time_t local_time, gmt_time;
69 long zone;
70
71 tzset();
72 gmt_time = time (NULL);
73
74 lt = gmtime(&gmt_time);
75
76 local_time = mktime(lt);
77 return (gmt_time - local_time);
78}
79#define _EMULATE_TIMEGM
80
81#endif /* __CYGWIN__ */
82
83#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +000084static time_t timegm(struct tm *tm) {
85 time_t tloc;
86 char *tz;
Lev Walkine730f2b2004-08-12 07:47:03 +000087 char *buf;
Lev Walkindb424002004-08-12 03:58:25 +000088
89 tz = getenv("TZ");
Lev Walkin93e9fe32004-09-17 06:46:10 +000090 _putenv("TZ=UTC");
91 tzset();
Lev Walkindb424002004-08-12 03:58:25 +000092 tloc = mktime(tm);
Lev Walkine730f2b2004-08-12 07:47:03 +000093 if (tz) {
Lev Walkin93e9fe32004-09-17 06:46:10 +000094 int bufsize = strlen(tz) + 4;
95 buf = alloca(bufsize);
96 snprintf(buf, bufsize, "TZ=%s", tz);
Lev Walkine730f2b2004-08-12 07:47:03 +000097 } else {
98 buf = "TZ=";
99 }
100 _putenv(buf);
Lev Walkin93e9fe32004-09-17 06:46:10 +0000101 tzset();
Lev Walkindb424002004-08-12 03:58:25 +0000102 return tloc;
103}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000104#endif /* _EMULATE_TIMEGM */
Lev Walkindb424002004-08-12 03:58:25 +0000105
Lev Walkin64399722004-08-11 07:17:22 +0000106
Lev Walkinf15320b2004-06-03 03:38:44 +0000107#ifndef __NO_ASN_TABLE__
108
109/*
110 * GeneralizedTime basic type description.
111 */
112static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = {
113 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2))
114};
115asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
116 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000117 OCTET_STRING_free,
118 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000119 GeneralizedTime_constraint, /* Check validity of time */
120 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkin99006362004-08-07 03:52:26 +0000121 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000122 0, /* Not implemented yet */
123 GeneralizedTime_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 0, /* Use generic outmost tag fetcher */
125 asn1_DEF_GeneralizedTime_tags,
126 sizeof(asn1_DEF_GeneralizedTime_tags)
127 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
Lev Walkin188ed2c2004-09-13 08:31:01 +0000128 asn1_DEF_GeneralizedTime_tags, /* Same as above */
129 sizeof(asn1_DEF_GeneralizedTime_tags)
130 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
Lev Walkinf15320b2004-06-03 03:38:44 +0000131 -1, /* Both ways are fine */
Lev Walkin449f8322004-08-20 13:23:42 +0000132 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000133 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000134};
135
136#endif /* __NO_ASN_TABLE__ */
137
138/*
139 * Check that the time looks like the time.
140 */
141int
142GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
143 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000144 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 time_t tloc;
146
147 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000148 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000150 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000151 "%s: Invalid time format: %s (%s:%d)",
152 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 return -1;
154 }
155
156 return 0;
157}
158
Lev Walkina9cc46e2004-09-22 16:06:28 +0000159asn_enc_rval_t
Lev Walkin99006362004-08-07 03:52:26 +0000160GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
161 int tag_mode, ber_tlv_tag_t tag,
162 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000163 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000164 asn_enc_rval_t erval;
Lev Walkin99006362004-08-07 03:52:26 +0000165
166 /* If not canonical DER, re-encode into canonical DER. */
167 if(st->size && st->buf[st->size-1] != 'Z') {
168 struct tm tm;
169 time_t tloc;
170
171 errno = EPERM;
172 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
173 if(tloc == -1 && errno != EPERM) {
174 /* Failed to recognize time. Fail completely. */
175 erval.encoded = -1;
176 erval.failed_type = td;
177 erval.structure_ptr = ptr;
178 return erval;
179 }
180 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
181 if(!st) {
182 /* Memory allocation failure. */
183 erval.encoded = -1;
184 erval.failed_type = td;
185 erval.structure_ptr = ptr;
186 return erval;
187 }
188 }
189
190 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
191
192 if(st != ptr) {
193 FREEMEM(st->buf);
194 FREEMEM(st);
195 }
196
197 return erval;
198}
199
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200asn_enc_rval_t
201GeneralizedTime_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
202 int ilevel, enum xer_encoder_flags_e flags,
203 asn_app_consume_bytes_f *cb, void *app_key) {
204 OCTET_STRING_t st;
205
206 if(flags & XER_F_CANONICAL) {
207 char buf[32];
208 struct tm tm;
209 ssize_t ret;
210
211 errno = EPERM;
212 if(asn_GT2time((GeneralizedTime_t *)sptr, &tm, 1) == -1
213 && errno != EPERM)
214 _ASN_ENCODE_FAILED;
215
216 ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
217 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
218 tm.tm_hour, tm.tm_min, tm.tm_sec);
219 assert(ret > 0 && ret < (int)sizeof(buf));
220
221 st.buf = (uint8_t *)buf;
222 st.size = ret;
223 sptr = &st;
224 }
225
226 return OCTET_STRING_encode_xer_ascii(td, sptr, ilevel, flags,
227 cb, app_key);
228}
229
Lev Walkinf15320b2004-06-03 03:38:44 +0000230int
231GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
232 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000233 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000234
Lev Walkind9bd7752004-06-05 08:17:50 +0000235 (void)td; /* Unused argument */
236 (void)ilevel; /* Unused argument */
237
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 if(st && st->buf) {
239 char buf[32];
240 struct tm tm;
241 int ret;
242
243 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000244 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 return cb("<bad-value>", 11, app_key);
246
247 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000248 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
250 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000251 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 return cb(buf, ret, app_key);
253 } else {
254 return cb("<absent>", 8, app_key);
255 }
256}
257
Lev Walkinf15320b2004-06-03 03:38:44 +0000258time_t
Lev Walkin99006362004-08-07 03:52:26 +0000259asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 struct tm tm_s;
261 uint8_t *buf;
262 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000263 int gmtoff_h = 0;
264 int gmtoff_m = 0;
265 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 int offset_specified = 0;
267 time_t tloc;
268
269 if(!st || !st->buf) {
270 errno = EINVAL;
271 return -1;
272 } else {
273 buf = st->buf;
274 end = buf + st->size;
275 }
276
277 if(st->size < 10) {
278 errno = EINVAL;
279 return -1;
280 }
281
282 /*
283 * Decode first 10 bytes: "AAAAMMJJhh"
284 */
285 memset(&tm_s, 0, sizeof(tm_s));
286#undef B2F
287#undef B2T
288#define B2F(var) do { \
289 unsigned ch = *buf; \
290 if(ch < 0x30 && ch > 0x39) { \
291 errno = EINVAL; \
292 return -1; \
293 } else { \
294 var = var * 10 + (ch - 0x30); \
295 buf++; \
296 } \
297 } while(0)
298#define B2T(var) B2F(tm_s.var)
299
300 B2T(tm_year); /* 1: A */
301 B2T(tm_year); /* 2: A */
302 B2T(tm_year); /* 3: A */
303 B2T(tm_year); /* 4: A */
304 B2T(tm_mon); /* 5: M */
305 B2T(tm_mon); /* 6: M */
306 B2T(tm_mday); /* 7: J */
307 B2T(tm_mday); /* 8: J */
308 B2T(tm_hour); /* 9: h */
309 B2T(tm_hour); /* 0: h */
310
311 if(buf == end) goto local_finish;
312
313 /*
314 * Parse [mm[ss[(.|,)ffff]]]
315 * ^^
316 */
317 switch(*buf) {
318 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
319 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
320 tm_s.tm_min = (*buf++) - 0x30;
321 if(buf == end) { errno = EINVAL; return -1; }
322 B2T(tm_min);
323 break;
324 case 0x2B: case 0x2D: /* +, - */
325 goto offset;
326 case 0x5A: /* Z */
327 goto utc_finish;
328 default:
329 errno = EINVAL;
330 return -1;
331 }
332
333 if(buf == end) goto local_finish;
334
335 /*
336 * Parse [mm[ss[(.|,)ffff]]]
337 * ^^
338 */
339 switch(*buf) {
340 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
341 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
342 tm_s.tm_sec = (*buf++) - 0x30;
343 if(buf == end) { errno = EINVAL; return -1; }
344 B2T(tm_sec);
345 break;
346 case 0x2B: case 0x2D: /* +, - */
347 goto offset;
348 case 0x5A: /* Z */
349 goto utc_finish;
350 default:
351 errno = EINVAL;
352 return -1;
353 }
354
355 if(buf == end) goto local_finish;
356
357 /*
358 * Parse [mm[ss[(.|,)ffff]]]
359 * ^ ^
360 */
361 switch(*buf) {
362 case 0x2C: case 0x2E: /* (.|,) */
363 /* Fractions of seconds are not supported
364 * by time_t or struct tm. Skip them */
365 for(buf++; buf < end; buf++) {
366 switch(*buf) {
367 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
368 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
369 continue;
370 default:
371 break;
372 }
373 break;
374 }
375 }
376
377 if(buf == end) goto local_finish;
378
379 switch(*buf) {
380 case 0x2B: case 0x2D: /* +, - */
381 goto offset;
382 case 0x5A: /* Z */
383 goto utc_finish;
384 default:
385 errno = EINVAL;
386 return -1;
387 }
388
389
390offset:
391
392 if(end - buf < 3) {
393 errno = EINVAL;
394 return -1;
395 }
396 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000397 B2F(gmtoff_h);
398 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000399 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000400 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 else
Lev Walkin99006362004-08-07 03:52:26 +0000402 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000403
404 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000405 B2F(gmtoff_m);
406 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 } else if(end != buf) {
408 errno = EINVAL;
409 return -1;
410 }
411
Lev Walkin99006362004-08-07 03:52:26 +0000412 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000413
414 /* Fall through */
415utc_finish:
416
417 offset_specified = 1;
418
419 /* Fall through */
420local_finish:
421
422 /*
423 * Validation.
424 */
425 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
426 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
427 || (tm_s.tm_hour > 23)
428 || (tm_s.tm_sec > 60)
429 ) {
430 errno = EINVAL;
431 return -1;
432 }
433
434 /* Canonicalize */
435 tm_s.tm_mon -= 1; /* 0 - 11 */
436 tm_s.tm_year -= 1900;
437 tm_s.tm_isdst = -1;
438
Lev Walkin99006362004-08-07 03:52:26 +0000439 tm_s.tm_sec -= gmtoff;
440
441 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
442
Lev Walkin02a31552004-08-12 03:52:53 +0000443 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000444 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000445 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000446 /*
447 * Without an offset (or 'Z'),
448 * we can only guess that it is a local zone.
449 * Interpret it in this fashion.
450 */
451 tloc = mktime(&tm_s);
452 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000453 if(tloc == -1) {
454 errno = EINVAL;
455 return -1;
456 }
457
Lev Walkin99006362004-08-07 03:52:26 +0000458 if(ret_tm) {
459 if(as_gmt) {
460 if(offset_specified) {
461 *ret_tm = tm_s;
462 } else {
463 if(gmtime_r(&tloc, ret_tm) == 0) {
464 errno = EINVAL;
465 return -1;
466 }
467 }
468 } else {
469 if(localtime_r(&tloc, ret_tm) == 0) {
470 errno = EINVAL;
471 return -1;
472 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 }
474 }
475
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 return tloc;
477}
478
Lev Walkin99006362004-08-07 03:52:26 +0000479
480GeneralizedTime_t *
481asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
482 struct tm tm_s;
483 long gmtoff;
484 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
485 char *buf;
486 char *p;
487 int size;
488
489 /* Check arguments */
490 if(!tm) {
491 errno = EINVAL;
492 return 0;
493 }
494
495 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkinc2346572004-08-11 09:07:36 +0000496 (void *)buf = MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000497 if(!buf) return 0;
498
499 gmtoff = GMTOFF(*tm);
500
501 if(force_gmt && gmtoff) {
502 tm_s = *tm;
503 tm_s.tm_sec -= gmtoff;
504 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000505 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000506#ifdef HAVE_TM_GMTOFF
507 assert(!GMTOFF(tm_s)); /* Will fix itself */
508#else
509 gmtoff = 0; /* Intervention required */
510#endif
Lev Walkin99006362004-08-07 03:52:26 +0000511 }
512
513 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
514 tm->tm_year + 1900,
515 tm->tm_mon + 1,
516 tm->tm_mday,
517 tm->tm_hour,
518 tm->tm_min,
519 tm->tm_sec
520 );
521 assert(size == 14);
522
523 p = buf + size;
524 if(force_gmt) {
525 *p++ = 0x5a; /* 'Z' */
526 *p++ = 0;
527 size++;
528 } else {
529 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
530 gmtoff / 3600, gmtoff % 3600);
531 assert(ret >= 5 && ret <= 7);
532 size += ret;
533 }
534
535 if(opt_gt) {
536 if(opt_gt->buf)
537 FREEMEM(opt_gt->buf);
538 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000539 (void *)opt_gt = CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000540 if(!opt_gt) { free(buf); return 0; }
541 }
542
Lev Walkin4d9528c2004-08-11 08:10:13 +0000543 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000544 opt_gt->size = size;
545
546 return opt_gt;
547}
548
549