blob: 25f2bb6e371e8efe5464b7488320ce4b4cae4888 [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 Walkin64399722004-08-11 07:17:22 +000012#ifdef 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
21static struct tm *localtime_r(time_t *tloc, struct tm *result) {
22 struct tm *tm;
23 if((tm = localtime(tloc)))
24 return memcpy(result, tm, sizeof(struct tm));
25 return 0;
26}
27
28static struct tm *gmtime_r(time_t *tloc, struct tm *result) {
29 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 +000035#define tzset() _tzset()
36static time_t timegm(struct tm *tm) {
37 time_t tloc;
38 char *tz;
Lev Walkine730f2b2004-08-12 07:47:03 +000039 char *buf;
Lev Walkindb424002004-08-12 03:58:25 +000040
41 tz = getenv("TZ");
Lev Walkine730f2b2004-08-12 07:47:03 +000042 _putenv("TZ=UTC");
Lev Walkindb424002004-08-12 03:58:25 +000043 tzset();
44 tloc = mktime(tm);
Lev Walkine730f2b2004-08-12 07:47:03 +000045 if (tz) {
46 buf = alloca(strlen(tz) + 4);
47 sprintf(buf, "TZ=%s", tz);
48 } else {
49 buf = "TZ=";
50 }
51 _putenv(buf);
Lev Walkindb424002004-08-12 03:58:25 +000052 tzset();
53 return tloc;
54}
55
56#if 0 /* Alternate version */
Lev Walkin02a31552004-08-12 03:52:53 +000057/* vlm: I am not sure about validity of this algorithm. */
58static time_t timegm(struct tm *tm) {
59 struct tm tmp;
60 time_t tloc = mktime(tm);
61 localtime_r(&tloc, &tmp); /* Figure out our GMT offset */
62 tloc += tmp.tm_gmtoff;
63 tm->tm_zone = "GMT";
64 tm->tm_gmtoff = 0; /* Simulate GMT */
65 return tloc;
66}
Lev Walkindb424002004-08-12 03:58:25 +000067#endif
Lev Walkin02a31552004-08-12 03:52:53 +000068#endif /* WIN32 */
Lev Walkin64399722004-08-11 07:17:22 +000069
Lev Walkinf15320b2004-06-03 03:38:44 +000070#ifndef __NO_ASN_TABLE__
71
72/*
73 * GeneralizedTime basic type description.
74 */
75static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = {
76 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2))
77};
78asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
79 "GeneralizedTime",
80 GeneralizedTime_constraint, /* Check validity of time */
81 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkin99006362004-08-07 03:52:26 +000082 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000083 GeneralizedTime_print,
84 OCTET_STRING_free,
85 0, /* Use generic outmost tag fetcher */
86 asn1_DEF_GeneralizedTime_tags,
87 sizeof(asn1_DEF_GeneralizedTime_tags)
88 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
89 1, /* Single UNIVERSAL tag may be implicitly overriden */
90 -1, /* Both ways are fine */
Lev Walkin449f8322004-08-20 13:23:42 +000091 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000092 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000093};
94
95#endif /* __NO_ASN_TABLE__ */
96
97/*
98 * Check that the time looks like the time.
99 */
100int
101GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
102 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000103 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 time_t tloc;
105
106 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000107 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000109 _ASN_ERRLOG(app_errlog, app_key,
110 "%s: Invalid time format: %s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 td->name, strerror(errno));
112 return -1;
113 }
114
115 return 0;
116}
117
Lev Walkin99006362004-08-07 03:52:26 +0000118der_enc_rval_t
119GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
120 int tag_mode, ber_tlv_tag_t tag,
121 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000122 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
Lev Walkin99006362004-08-07 03:52:26 +0000123 der_enc_rval_t erval;
124
125 /* If not canonical DER, re-encode into canonical DER. */
126 if(st->size && st->buf[st->size-1] != 'Z') {
127 struct tm tm;
128 time_t tloc;
129
130 errno = EPERM;
131 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
132 if(tloc == -1 && errno != EPERM) {
133 /* Failed to recognize time. Fail completely. */
134 erval.encoded = -1;
135 erval.failed_type = td;
136 erval.structure_ptr = ptr;
137 return erval;
138 }
139 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
140 if(!st) {
141 /* Memory allocation failure. */
142 erval.encoded = -1;
143 erval.failed_type = td;
144 erval.structure_ptr = ptr;
145 return erval;
146 }
147 }
148
149 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
150
151 if(st != ptr) {
152 FREEMEM(st->buf);
153 FREEMEM(st);
154 }
155
156 return erval;
157}
158
Lev Walkinf15320b2004-06-03 03:38:44 +0000159int
160GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
161 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000162 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000163
Lev Walkind9bd7752004-06-05 08:17:50 +0000164 (void)td; /* Unused argument */
165 (void)ilevel; /* Unused argument */
166
Lev Walkinf15320b2004-06-03 03:38:44 +0000167 if(st && st->buf) {
168 char buf[32];
169 struct tm tm;
170 int ret;
171
172 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000173 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 return cb("<bad-value>", 11, app_key);
175
176 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000177 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
179 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000180 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 return cb(buf, ret, app_key);
182 } else {
183 return cb("<absent>", 8, app_key);
184 }
185}
186
187/*
188 * Where to look for offset from GMT, Phase I.
189 * Several platforms are known.
190 */
Lev Walkin659a7512004-08-11 07:35:08 +0000191#if defined(__FreeBSD__) \
192 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
193 || (defined __GLIBC__ && __GLIBC__ >= 2)
194#undef HAVE_TM_GMTOFF
195#define HAVE_TM_GMTOFF
196#endif /* BSDs and newer glibc */
Lev Walkinf15320b2004-06-03 03:38:44 +0000197
198/*
199 * Where to look for offset from GMT, Phase II.
200 */
Lev Walkin659a7512004-08-11 07:35:08 +0000201#ifdef HAVE_TM_GMTOFF
Lev Walkin99006362004-08-07 03:52:26 +0000202#define GMTOFF(tm) ((tm).tm_gmtoff)
Lev Walkin659a7512004-08-11 07:35:08 +0000203#else /* HAVE_TM_GMTOFF */
Lev Walkin99006362004-08-07 03:52:26 +0000204#define GMTOFF(tm) (-timezone)
Lev Walkin659a7512004-08-11 07:35:08 +0000205#endif /* HAVE_TM_GMTOFF */
Lev Walkinf15320b2004-06-03 03:38:44 +0000206
207time_t
Lev Walkin99006362004-08-07 03:52:26 +0000208asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000209 struct tm tm_s;
210 uint8_t *buf;
211 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000212 int gmtoff_h = 0;
213 int gmtoff_m = 0;
214 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000215 int offset_specified = 0;
216 time_t tloc;
217
218 if(!st || !st->buf) {
219 errno = EINVAL;
220 return -1;
221 } else {
222 buf = st->buf;
223 end = buf + st->size;
224 }
225
226 if(st->size < 10) {
227 errno = EINVAL;
228 return -1;
229 }
230
231 /*
232 * Decode first 10 bytes: "AAAAMMJJhh"
233 */
234 memset(&tm_s, 0, sizeof(tm_s));
235#undef B2F
236#undef B2T
237#define B2F(var) do { \
238 unsigned ch = *buf; \
239 if(ch < 0x30 && ch > 0x39) { \
240 errno = EINVAL; \
241 return -1; \
242 } else { \
243 var = var * 10 + (ch - 0x30); \
244 buf++; \
245 } \
246 } while(0)
247#define B2T(var) B2F(tm_s.var)
248
249 B2T(tm_year); /* 1: A */
250 B2T(tm_year); /* 2: A */
251 B2T(tm_year); /* 3: A */
252 B2T(tm_year); /* 4: A */
253 B2T(tm_mon); /* 5: M */
254 B2T(tm_mon); /* 6: M */
255 B2T(tm_mday); /* 7: J */
256 B2T(tm_mday); /* 8: J */
257 B2T(tm_hour); /* 9: h */
258 B2T(tm_hour); /* 0: h */
259
260 if(buf == end) goto local_finish;
261
262 /*
263 * Parse [mm[ss[(.|,)ffff]]]
264 * ^^
265 */
266 switch(*buf) {
267 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
268 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
269 tm_s.tm_min = (*buf++) - 0x30;
270 if(buf == end) { errno = EINVAL; return -1; }
271 B2T(tm_min);
272 break;
273 case 0x2B: case 0x2D: /* +, - */
274 goto offset;
275 case 0x5A: /* Z */
276 goto utc_finish;
277 default:
278 errno = EINVAL;
279 return -1;
280 }
281
282 if(buf == end) goto local_finish;
283
284 /*
285 * Parse [mm[ss[(.|,)ffff]]]
286 * ^^
287 */
288 switch(*buf) {
289 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
290 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
291 tm_s.tm_sec = (*buf++) - 0x30;
292 if(buf == end) { errno = EINVAL; return -1; }
293 B2T(tm_sec);
294 break;
295 case 0x2B: case 0x2D: /* +, - */
296 goto offset;
297 case 0x5A: /* Z */
298 goto utc_finish;
299 default:
300 errno = EINVAL;
301 return -1;
302 }
303
304 if(buf == end) goto local_finish;
305
306 /*
307 * Parse [mm[ss[(.|,)ffff]]]
308 * ^ ^
309 */
310 switch(*buf) {
311 case 0x2C: case 0x2E: /* (.|,) */
312 /* Fractions of seconds are not supported
313 * by time_t or struct tm. Skip them */
314 for(buf++; buf < end; buf++) {
315 switch(*buf) {
316 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
317 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
318 continue;
319 default:
320 break;
321 }
322 break;
323 }
324 }
325
326 if(buf == end) goto local_finish;
327
328 switch(*buf) {
329 case 0x2B: case 0x2D: /* +, - */
330 goto offset;
331 case 0x5A: /* Z */
332 goto utc_finish;
333 default:
334 errno = EINVAL;
335 return -1;
336 }
337
338
339offset:
340
341 if(end - buf < 3) {
342 errno = EINVAL;
343 return -1;
344 }
345 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000346 B2F(gmtoff_h);
347 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000348 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000349 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 else
Lev Walkin99006362004-08-07 03:52:26 +0000351 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000352
353 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000354 B2F(gmtoff_m);
355 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 } else if(end != buf) {
357 errno = EINVAL;
358 return -1;
359 }
360
Lev Walkin99006362004-08-07 03:52:26 +0000361 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000362
363 /* Fall through */
364utc_finish:
365
366 offset_specified = 1;
367
368 /* Fall through */
369local_finish:
370
371 /*
372 * Validation.
373 */
374 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
375 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
376 || (tm_s.tm_hour > 23)
377 || (tm_s.tm_sec > 60)
378 ) {
379 errno = EINVAL;
380 return -1;
381 }
382
383 /* Canonicalize */
384 tm_s.tm_mon -= 1; /* 0 - 11 */
385 tm_s.tm_year -= 1900;
386 tm_s.tm_isdst = -1;
387
Lev Walkin99006362004-08-07 03:52:26 +0000388 tm_s.tm_sec -= gmtoff;
389
390 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
391
Lev Walkin02a31552004-08-12 03:52:53 +0000392 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000393 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000394 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000395 /*
396 * Without an offset (or 'Z'),
397 * we can only guess that it is a local zone.
398 * Interpret it in this fashion.
399 */
400 tloc = mktime(&tm_s);
401 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 if(tloc == -1) {
403 errno = EINVAL;
404 return -1;
405 }
406
Lev Walkin99006362004-08-07 03:52:26 +0000407 if(ret_tm) {
408 if(as_gmt) {
409 if(offset_specified) {
410 *ret_tm = tm_s;
411 } else {
412 if(gmtime_r(&tloc, ret_tm) == 0) {
413 errno = EINVAL;
414 return -1;
415 }
416 }
417 } else {
418 if(localtime_r(&tloc, ret_tm) == 0) {
419 errno = EINVAL;
420 return -1;
421 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000422 }
423 }
424
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 return tloc;
426}
427
Lev Walkin99006362004-08-07 03:52:26 +0000428
429GeneralizedTime_t *
430asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
431 struct tm tm_s;
432 long gmtoff;
433 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
434 char *buf;
435 char *p;
436 int size;
437
438 /* Check arguments */
439 if(!tm) {
440 errno = EINVAL;
441 return 0;
442 }
443
444 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkinc2346572004-08-11 09:07:36 +0000445 (void *)buf = MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000446 if(!buf) return 0;
447
448 gmtoff = GMTOFF(*tm);
449
450 if(force_gmt && gmtoff) {
451 tm_s = *tm;
452 tm_s.tm_sec -= gmtoff;
453 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000454 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000455#ifdef HAVE_TM_GMTOFF
456 assert(!GMTOFF(tm_s)); /* Will fix itself */
457#else
458 gmtoff = 0; /* Intervention required */
459#endif
Lev Walkin99006362004-08-07 03:52:26 +0000460 }
461
462 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
463 tm->tm_year + 1900,
464 tm->tm_mon + 1,
465 tm->tm_mday,
466 tm->tm_hour,
467 tm->tm_min,
468 tm->tm_sec
469 );
470 assert(size == 14);
471
472 p = buf + size;
473 if(force_gmt) {
474 *p++ = 0x5a; /* 'Z' */
475 *p++ = 0;
476 size++;
477 } else {
478 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
479 gmtoff / 3600, gmtoff % 3600);
480 assert(ret >= 5 && ret <= 7);
481 size += ret;
482 }
483
484 if(opt_gt) {
485 if(opt_gt->buf)
486 FREEMEM(opt_gt->buf);
487 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000488 (void *)opt_gt = CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000489 if(!opt_gt) { free(buf); return 0; }
490 }
491
Lev Walkin4d9528c2004-08-11 08:10:13 +0000492 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000493 opt_gt->size = size;
494
495 return opt_gt;
496}
497
498