blob: 161898f3c39e1303e558969c711602a9511bf401 [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[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000113 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
114 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
115 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000116};
117asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
118 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000119 OCTET_STRING_free,
120 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 GeneralizedTime_constraint, /* Check validity of time */
122 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkin99006362004-08-07 03:52:26 +0000123 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000124 0, /* Not implemented yet */
125 GeneralizedTime_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 0, /* Use generic outmost tag fetcher */
127 asn1_DEF_GeneralizedTime_tags,
128 sizeof(asn1_DEF_GeneralizedTime_tags)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000129 / sizeof(asn1_DEF_GeneralizedTime_tags[0]) - 2,
130 asn1_DEF_GeneralizedTime_tags,
Lev Walkin188ed2c2004-09-13 08:31:01 +0000131 sizeof(asn1_DEF_GeneralizedTime_tags)
132 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +0000133 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000134 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000135};
136
137#endif /* __NO_ASN_TABLE__ */
138
139/*
140 * Check that the time looks like the time.
141 */
142int
143GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
144 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000145 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 time_t tloc;
147
148 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000149 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000151 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000152 "%s: Invalid time format: %s (%s:%d)",
153 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 return -1;
155 }
156
157 return 0;
158}
159
Lev Walkina9cc46e2004-09-22 16:06:28 +0000160asn_enc_rval_t
Lev Walkin99006362004-08-07 03:52:26 +0000161GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
162 int tag_mode, ber_tlv_tag_t tag,
163 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000164 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000165 asn_enc_rval_t erval;
Lev Walkin99006362004-08-07 03:52:26 +0000166
167 /* If not canonical DER, re-encode into canonical DER. */
168 if(st->size && st->buf[st->size-1] != 'Z') {
169 struct tm tm;
170 time_t tloc;
171
172 errno = EPERM;
173 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
174 if(tloc == -1 && errno != EPERM) {
175 /* Failed to recognize time. Fail completely. */
176 erval.encoded = -1;
177 erval.failed_type = td;
178 erval.structure_ptr = ptr;
179 return erval;
180 }
181 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
182 if(!st) {
183 /* Memory allocation failure. */
184 erval.encoded = -1;
185 erval.failed_type = td;
186 erval.structure_ptr = ptr;
187 return erval;
188 }
189 }
190
191 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
192
193 if(st != ptr) {
194 FREEMEM(st->buf);
195 FREEMEM(st);
196 }
197
198 return erval;
199}
200
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201asn_enc_rval_t
202GeneralizedTime_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
203 int ilevel, enum xer_encoder_flags_e flags,
204 asn_app_consume_bytes_f *cb, void *app_key) {
205 OCTET_STRING_t st;
206
207 if(flags & XER_F_CANONICAL) {
208 char buf[32];
209 struct tm tm;
210 ssize_t ret;
211
212 errno = EPERM;
213 if(asn_GT2time((GeneralizedTime_t *)sptr, &tm, 1) == -1
214 && errno != EPERM)
215 _ASN_ENCODE_FAILED;
216
217 ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
218 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
219 tm.tm_hour, tm.tm_min, tm.tm_sec);
220 assert(ret > 0 && ret < (int)sizeof(buf));
221
222 st.buf = (uint8_t *)buf;
223 st.size = ret;
224 sptr = &st;
225 }
226
227 return OCTET_STRING_encode_xer_ascii(td, sptr, ilevel, flags,
228 cb, app_key);
229}
230
Lev Walkinf15320b2004-06-03 03:38:44 +0000231int
232GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
233 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000234 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000235
Lev Walkind9bd7752004-06-05 08:17:50 +0000236 (void)td; /* Unused argument */
237 (void)ilevel; /* Unused argument */
238
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 if(st && st->buf) {
240 char buf[32];
241 struct tm tm;
242 int ret;
243
244 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000245 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000246 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000247
248 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000249 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
251 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000252 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000253 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000255 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000256 }
257}
258
Lev Walkinf15320b2004-06-03 03:38:44 +0000259time_t
Lev Walkin99006362004-08-07 03:52:26 +0000260asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000261 struct tm tm_s;
262 uint8_t *buf;
263 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000264 int gmtoff_h = 0;
265 int gmtoff_m = 0;
266 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000267 int offset_specified = 0;
268 time_t tloc;
269
270 if(!st || !st->buf) {
271 errno = EINVAL;
272 return -1;
273 } else {
274 buf = st->buf;
275 end = buf + st->size;
276 }
277
278 if(st->size < 10) {
279 errno = EINVAL;
280 return -1;
281 }
282
283 /*
284 * Decode first 10 bytes: "AAAAMMJJhh"
285 */
286 memset(&tm_s, 0, sizeof(tm_s));
287#undef B2F
288#undef B2T
289#define B2F(var) do { \
290 unsigned ch = *buf; \
291 if(ch < 0x30 && ch > 0x39) { \
292 errno = EINVAL; \
293 return -1; \
294 } else { \
295 var = var * 10 + (ch - 0x30); \
296 buf++; \
297 } \
298 } while(0)
299#define B2T(var) B2F(tm_s.var)
300
301 B2T(tm_year); /* 1: A */
302 B2T(tm_year); /* 2: A */
303 B2T(tm_year); /* 3: A */
304 B2T(tm_year); /* 4: A */
305 B2T(tm_mon); /* 5: M */
306 B2T(tm_mon); /* 6: M */
307 B2T(tm_mday); /* 7: J */
308 B2T(tm_mday); /* 8: J */
309 B2T(tm_hour); /* 9: h */
310 B2T(tm_hour); /* 0: h */
311
312 if(buf == end) goto local_finish;
313
314 /*
315 * Parse [mm[ss[(.|,)ffff]]]
316 * ^^
317 */
318 switch(*buf) {
319 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
320 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
321 tm_s.tm_min = (*buf++) - 0x30;
322 if(buf == end) { errno = EINVAL; return -1; }
323 B2T(tm_min);
324 break;
325 case 0x2B: case 0x2D: /* +, - */
326 goto offset;
327 case 0x5A: /* Z */
328 goto utc_finish;
329 default:
330 errno = EINVAL;
331 return -1;
332 }
333
334 if(buf == end) goto local_finish;
335
336 /*
337 * Parse [mm[ss[(.|,)ffff]]]
338 * ^^
339 */
340 switch(*buf) {
341 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
342 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
343 tm_s.tm_sec = (*buf++) - 0x30;
344 if(buf == end) { errno = EINVAL; return -1; }
345 B2T(tm_sec);
346 break;
347 case 0x2B: case 0x2D: /* +, - */
348 goto offset;
349 case 0x5A: /* Z */
350 goto utc_finish;
351 default:
352 errno = EINVAL;
353 return -1;
354 }
355
356 if(buf == end) goto local_finish;
357
358 /*
359 * Parse [mm[ss[(.|,)ffff]]]
360 * ^ ^
361 */
362 switch(*buf) {
363 case 0x2C: case 0x2E: /* (.|,) */
364 /* Fractions of seconds are not supported
365 * by time_t or struct tm. Skip them */
366 for(buf++; buf < end; buf++) {
367 switch(*buf) {
368 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
369 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
370 continue;
371 default:
372 break;
373 }
374 break;
375 }
376 }
377
378 if(buf == end) goto local_finish;
379
380 switch(*buf) {
381 case 0x2B: case 0x2D: /* +, - */
382 goto offset;
383 case 0x5A: /* Z */
384 goto utc_finish;
385 default:
386 errno = EINVAL;
387 return -1;
388 }
389
390
391offset:
392
393 if(end - buf < 3) {
394 errno = EINVAL;
395 return -1;
396 }
397 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000398 B2F(gmtoff_h);
399 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000401 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 else
Lev Walkin99006362004-08-07 03:52:26 +0000403 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000404
405 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000406 B2F(gmtoff_m);
407 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 } else if(end != buf) {
409 errno = EINVAL;
410 return -1;
411 }
412
Lev Walkin99006362004-08-07 03:52:26 +0000413 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000414
415 /* Fall through */
416utc_finish:
417
418 offset_specified = 1;
419
420 /* Fall through */
421local_finish:
422
423 /*
424 * Validation.
425 */
426 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
427 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
428 || (tm_s.tm_hour > 23)
429 || (tm_s.tm_sec > 60)
430 ) {
431 errno = EINVAL;
432 return -1;
433 }
434
435 /* Canonicalize */
436 tm_s.tm_mon -= 1; /* 0 - 11 */
437 tm_s.tm_year -= 1900;
438 tm_s.tm_isdst = -1;
439
Lev Walkin99006362004-08-07 03:52:26 +0000440 tm_s.tm_sec -= gmtoff;
441
442 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
443
Lev Walkin02a31552004-08-12 03:52:53 +0000444 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000445 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000446 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000447 /*
448 * Without an offset (or 'Z'),
449 * we can only guess that it is a local zone.
450 * Interpret it in this fashion.
451 */
452 tloc = mktime(&tm_s);
453 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000454 if(tloc == -1) {
455 errno = EINVAL;
456 return -1;
457 }
458
Lev Walkin99006362004-08-07 03:52:26 +0000459 if(ret_tm) {
460 if(as_gmt) {
461 if(offset_specified) {
462 *ret_tm = tm_s;
463 } else {
464 if(gmtime_r(&tloc, ret_tm) == 0) {
465 errno = EINVAL;
466 return -1;
467 }
468 }
469 } else {
470 if(localtime_r(&tloc, ret_tm) == 0) {
471 errno = EINVAL;
472 return -1;
473 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000474 }
475 }
476
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 return tloc;
478}
479
Lev Walkin99006362004-08-07 03:52:26 +0000480
481GeneralizedTime_t *
482asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
483 struct tm tm_s;
484 long gmtoff;
485 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
486 char *buf;
487 char *p;
488 int size;
489
490 /* Check arguments */
491 if(!tm) {
492 errno = EINVAL;
493 return 0;
494 }
495
496 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000497 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000498 if(!buf) return 0;
499
500 gmtoff = GMTOFF(*tm);
501
502 if(force_gmt && gmtoff) {
503 tm_s = *tm;
504 tm_s.tm_sec -= gmtoff;
505 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000506 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000507#ifdef HAVE_TM_GMTOFF
508 assert(!GMTOFF(tm_s)); /* Will fix itself */
509#else
510 gmtoff = 0; /* Intervention required */
511#endif
Lev Walkin99006362004-08-07 03:52:26 +0000512 }
513
514 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
515 tm->tm_year + 1900,
516 tm->tm_mon + 1,
517 tm->tm_mday,
518 tm->tm_hour,
519 tm->tm_min,
520 tm->tm_sec
521 );
522 assert(size == 14);
523
524 p = buf + size;
525 if(force_gmt) {
526 *p++ = 0x5a; /* 'Z' */
527 *p++ = 0;
528 size++;
529 } else {
530 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
531 gmtoff / 3600, gmtoff % 3600);
532 assert(ret >= 5 && ret <= 7);
533 size += ret;
534 }
535
536 if(opt_gt) {
537 if(opt_gt->buf)
538 FREEMEM(opt_gt->buf);
539 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000540 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000541 if(!opt_gt) { free(buf); return 0; }
542 }
543
Lev Walkin4d9528c2004-08-11 08:10:13 +0000544 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000545 opt_gt->size = size;
546
547 return opt_gt;
548}
549
550