blob: 88806f91d7c5aa7982e1902f3caac820a324d938 [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!
vlmbed6f812004-09-17 06:46:10 +000014#warning localtime_r is implemented via localtime(), which may be not thread-safe.
15#warning gmtime_r is implemented via gmtime(), which may be not thread-safe.
vlmc48f2132004-08-12 03:52:53 +000016#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
vlmbed6f812004-09-17 06:46:10 +000035#define tzset() _tzset()
36#define _EMULATE_TIMEGM
37
38#endif /* WIN32 */
39
40/*
41 * Where to look for offset from GMT, Phase I.
42 * Several platforms are known.
43 */
44#if defined(__FreeBSD__) \
45 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
46 || (defined __GLIBC__ && __GLIBC__ >= 2)
47#undef HAVE_TM_GMTOFF
48#define HAVE_TM_GMTOFF
49#endif /* BSDs and newer glibc */
50
51/*
52 * Where to look for offset from GMT, Phase II.
53 */
54#ifdef HAVE_TM_GMTOFF
55#define GMTOFF(tm) ((tm).tm_gmtoff)
56#else /* HAVE_TM_GMTOFF */
57#define GMTOFF(tm) (-timezone)
58#endif /* HAVE_TM_GMTOFF */
59
60/*
61 * Override our GMTOFF decision for other known platforms.
62 */
63#ifdef __CYGWIN__
64#undef GMTOFF
65static long GMTOFF(struct tm a){
66 struct tm *lt;
67 time_t local_time, gmt_time;
68 long zone;
69
70 tzset();
71 gmt_time = time (NULL);
72
73 lt = gmtime(&gmt_time);
74
75 local_time = mktime(lt);
76 return (gmt_time - local_time);
77}
78#define _EMULATE_TIMEGM
79
80#endif /* __CYGWIN__ */
81
82#ifdef _EMULATE_TIMEGM
vlm348a37a2004-08-12 03:58:25 +000083static time_t timegm(struct tm *tm) {
84 time_t tloc;
85 char *tz;
vlmd3692282004-08-12 07:47:03 +000086 char *buf;
vlm348a37a2004-08-12 03:58:25 +000087
88 tz = getenv("TZ");
vlmbed6f812004-09-17 06:46:10 +000089 _putenv("TZ=UTC");
90 tzset();
vlm348a37a2004-08-12 03:58:25 +000091 tloc = mktime(tm);
vlmd3692282004-08-12 07:47:03 +000092 if (tz) {
vlmbed6f812004-09-17 06:46:10 +000093 int bufsize = strlen(tz) + 4;
94 buf = alloca(bufsize);
95 snprintf(buf, bufsize, "TZ=%s", tz);
vlmd3692282004-08-12 07:47:03 +000096 } else {
97 buf = "TZ=";
98 }
99 _putenv(buf);
vlmbed6f812004-09-17 06:46:10 +0000100 tzset();
vlm348a37a2004-08-12 03:58:25 +0000101 return tloc;
102}
vlmbed6f812004-09-17 06:46:10 +0000103#endif /* _EMULATE_TIMEGM */
vlm348a37a2004-08-12 03:58:25 +0000104
vlm6e73a042004-08-11 07:17:22 +0000105
vlmfa67ddc2004-06-03 03:38:44 +0000106#ifndef __NO_ASN_TABLE__
107
108/*
109 * GeneralizedTime basic type description.
110 */
111static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = {
112 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2))
113};
114asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
115 "GeneralizedTime",
116 GeneralizedTime_constraint, /* Check validity of time */
117 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
vlm81057a82004-08-07 03:52:26 +0000118 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
vlmfa67ddc2004-06-03 03:38:44 +0000119 GeneralizedTime_print,
120 OCTET_STRING_free,
121 0, /* Use generic outmost tag fetcher */
122 asn1_DEF_GeneralizedTime_tags,
123 sizeof(asn1_DEF_GeneralizedTime_tags)
124 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
vlm72425de2004-09-13 08:31:01 +0000125 asn1_DEF_GeneralizedTime_tags, /* Same as above */
126 sizeof(asn1_DEF_GeneralizedTime_tags)
127 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
vlmfa67ddc2004-06-03 03:38:44 +0000128 -1, /* Both ways are fine */
vlme413c122004-08-20 13:23:42 +0000129 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +0000130 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +0000131};
132
133#endif /* __NO_ASN_TABLE__ */
134
135/*
136 * Check that the time looks like the time.
137 */
138int
139GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
140 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000141 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000142 time_t tloc;
143
144 errno = EPERM; /* Just an unlikely error code */
vlm81057a82004-08-07 03:52:26 +0000145 tloc = asn_GT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000146 if(tloc == -1 && errno != EPERM) {
vlme3f0f282004-08-11 09:44:13 +0000147 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000148 "%s: Invalid time format: %s (%s:%d)",
149 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000150 return -1;
151 }
152
153 return 0;
154}
155
vlm81057a82004-08-07 03:52:26 +0000156der_enc_rval_t
157GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
158 int tag_mode, ber_tlv_tag_t tag,
159 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000160 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
vlm81057a82004-08-07 03:52:26 +0000161 der_enc_rval_t erval;
162
163 /* If not canonical DER, re-encode into canonical DER. */
164 if(st->size && st->buf[st->size-1] != 'Z') {
165 struct tm tm;
166 time_t tloc;
167
168 errno = EPERM;
169 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
170 if(tloc == -1 && errno != EPERM) {
171 /* Failed to recognize time. Fail completely. */
172 erval.encoded = -1;
173 erval.failed_type = td;
174 erval.structure_ptr = ptr;
175 return erval;
176 }
177 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
178 if(!st) {
179 /* Memory allocation failure. */
180 erval.encoded = -1;
181 erval.failed_type = td;
182 erval.structure_ptr = ptr;
183 return erval;
184 }
185 }
186
187 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
188
189 if(st != ptr) {
190 FREEMEM(st->buf);
191 FREEMEM(st);
192 }
193
194 return erval;
195}
196
vlmfa67ddc2004-06-03 03:38:44 +0000197int
198GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
199 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000200 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000201
vlmb42843a2004-06-05 08:17:50 +0000202 (void)td; /* Unused argument */
203 (void)ilevel; /* Unused argument */
204
vlmfa67ddc2004-06-03 03:38:44 +0000205 if(st && st->buf) {
206 char buf[32];
207 struct tm tm;
208 int ret;
209
210 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000211 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlmfa67ddc2004-06-03 03:38:44 +0000212 return cb("<bad-value>", 11, app_key);
213
214 ret = snprintf(buf, sizeof(buf),
vlm81057a82004-08-07 03:52:26 +0000215 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000216 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
217 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000218 assert(ret > 0 && ret < (int)sizeof(buf));
vlmfa67ddc2004-06-03 03:38:44 +0000219 return cb(buf, ret, app_key);
220 } else {
221 return cb("<absent>", 8, app_key);
222 }
223}
224
vlmfa67ddc2004-06-03 03:38:44 +0000225time_t
vlm81057a82004-08-07 03:52:26 +0000226asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +0000227 struct tm tm_s;
228 uint8_t *buf;
229 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000230 int gmtoff_h = 0;
231 int gmtoff_m = 0;
232 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000233 int offset_specified = 0;
234 time_t tloc;
235
236 if(!st || !st->buf) {
237 errno = EINVAL;
238 return -1;
239 } else {
240 buf = st->buf;
241 end = buf + st->size;
242 }
243
244 if(st->size < 10) {
245 errno = EINVAL;
246 return -1;
247 }
248
249 /*
250 * Decode first 10 bytes: "AAAAMMJJhh"
251 */
252 memset(&tm_s, 0, sizeof(tm_s));
253#undef B2F
254#undef B2T
255#define B2F(var) do { \
256 unsigned ch = *buf; \
257 if(ch < 0x30 && ch > 0x39) { \
258 errno = EINVAL; \
259 return -1; \
260 } else { \
261 var = var * 10 + (ch - 0x30); \
262 buf++; \
263 } \
264 } while(0)
265#define B2T(var) B2F(tm_s.var)
266
267 B2T(tm_year); /* 1: A */
268 B2T(tm_year); /* 2: A */
269 B2T(tm_year); /* 3: A */
270 B2T(tm_year); /* 4: A */
271 B2T(tm_mon); /* 5: M */
272 B2T(tm_mon); /* 6: M */
273 B2T(tm_mday); /* 7: J */
274 B2T(tm_mday); /* 8: J */
275 B2T(tm_hour); /* 9: h */
276 B2T(tm_hour); /* 0: h */
277
278 if(buf == end) goto local_finish;
279
280 /*
281 * Parse [mm[ss[(.|,)ffff]]]
282 * ^^
283 */
284 switch(*buf) {
285 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
286 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
287 tm_s.tm_min = (*buf++) - 0x30;
288 if(buf == end) { errno = EINVAL; return -1; }
289 B2T(tm_min);
290 break;
291 case 0x2B: case 0x2D: /* +, - */
292 goto offset;
293 case 0x5A: /* Z */
294 goto utc_finish;
295 default:
296 errno = EINVAL;
297 return -1;
298 }
299
300 if(buf == end) goto local_finish;
301
302 /*
303 * Parse [mm[ss[(.|,)ffff]]]
304 * ^^
305 */
306 switch(*buf) {
307 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
308 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
309 tm_s.tm_sec = (*buf++) - 0x30;
310 if(buf == end) { errno = EINVAL; return -1; }
311 B2T(tm_sec);
312 break;
313 case 0x2B: case 0x2D: /* +, - */
314 goto offset;
315 case 0x5A: /* Z */
316 goto utc_finish;
317 default:
318 errno = EINVAL;
319 return -1;
320 }
321
322 if(buf == end) goto local_finish;
323
324 /*
325 * Parse [mm[ss[(.|,)ffff]]]
326 * ^ ^
327 */
328 switch(*buf) {
329 case 0x2C: case 0x2E: /* (.|,) */
330 /* Fractions of seconds are not supported
331 * by time_t or struct tm. Skip them */
332 for(buf++; buf < end; buf++) {
333 switch(*buf) {
334 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
335 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
336 continue;
337 default:
338 break;
339 }
340 break;
341 }
342 }
343
344 if(buf == end) goto local_finish;
345
346 switch(*buf) {
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
357offset:
358
359 if(end - buf < 3) {
360 errno = EINVAL;
361 return -1;
362 }
363 buf++;
vlm81057a82004-08-07 03:52:26 +0000364 B2F(gmtoff_h);
365 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000366 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000367 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000368 else
vlm81057a82004-08-07 03:52:26 +0000369 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000370
371 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000372 B2F(gmtoff_m);
373 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000374 } else if(end != buf) {
375 errno = EINVAL;
376 return -1;
377 }
378
vlm81057a82004-08-07 03:52:26 +0000379 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000380
381 /* Fall through */
382utc_finish:
383
384 offset_specified = 1;
385
386 /* Fall through */
387local_finish:
388
389 /*
390 * Validation.
391 */
392 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
393 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
394 || (tm_s.tm_hour > 23)
395 || (tm_s.tm_sec > 60)
396 ) {
397 errno = EINVAL;
398 return -1;
399 }
400
401 /* Canonicalize */
402 tm_s.tm_mon -= 1; /* 0 - 11 */
403 tm_s.tm_year -= 1900;
404 tm_s.tm_isdst = -1;
405
vlm81057a82004-08-07 03:52:26 +0000406 tm_s.tm_sec -= gmtoff;
407
408 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
409
vlmc48f2132004-08-12 03:52:53 +0000410 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000411 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000412 } else {
vlm81057a82004-08-07 03:52:26 +0000413 /*
414 * Without an offset (or 'Z'),
415 * we can only guess that it is a local zone.
416 * Interpret it in this fashion.
417 */
418 tloc = mktime(&tm_s);
419 }
vlmfa67ddc2004-06-03 03:38:44 +0000420 if(tloc == -1) {
421 errno = EINVAL;
422 return -1;
423 }
424
vlm81057a82004-08-07 03:52:26 +0000425 if(ret_tm) {
426 if(as_gmt) {
427 if(offset_specified) {
428 *ret_tm = tm_s;
429 } else {
430 if(gmtime_r(&tloc, ret_tm) == 0) {
431 errno = EINVAL;
432 return -1;
433 }
434 }
435 } else {
436 if(localtime_r(&tloc, ret_tm) == 0) {
437 errno = EINVAL;
438 return -1;
439 }
vlmfa67ddc2004-06-03 03:38:44 +0000440 }
441 }
442
vlmfa67ddc2004-06-03 03:38:44 +0000443 return tloc;
444}
445
vlm81057a82004-08-07 03:52:26 +0000446
447GeneralizedTime_t *
448asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
449 struct tm tm_s;
450 long gmtoff;
451 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
452 char *buf;
453 char *p;
454 int size;
455
456 /* Check arguments */
457 if(!tm) {
458 errno = EINVAL;
459 return 0;
460 }
461
462 /* Pre-allocate a buffer of sufficient yet small length */
vlmda674682004-08-11 09:07:36 +0000463 (void *)buf = MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000464 if(!buf) return 0;
465
466 gmtoff = GMTOFF(*tm);
467
468 if(force_gmt && gmtoff) {
469 tm_s = *tm;
470 tm_s.tm_sec -= gmtoff;
471 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000472 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000473#ifdef HAVE_TM_GMTOFF
474 assert(!GMTOFF(tm_s)); /* Will fix itself */
475#else
476 gmtoff = 0; /* Intervention required */
477#endif
vlm81057a82004-08-07 03:52:26 +0000478 }
479
480 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
481 tm->tm_year + 1900,
482 tm->tm_mon + 1,
483 tm->tm_mday,
484 tm->tm_hour,
485 tm->tm_min,
486 tm->tm_sec
487 );
488 assert(size == 14);
489
490 p = buf + size;
491 if(force_gmt) {
492 *p++ = 0x5a; /* 'Z' */
493 *p++ = 0;
494 size++;
495 } else {
496 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
497 gmtoff / 3600, gmtoff % 3600);
498 assert(ret >= 5 && ret <= 7);
499 size += ret;
500 }
501
502 if(opt_gt) {
503 if(opt_gt->buf)
504 FREEMEM(opt_gt->buf);
505 } else {
vlmda674682004-08-11 09:07:36 +0000506 (void *)opt_gt = CALLOC(1, sizeof *opt_gt);
vlm81057a82004-08-07 03:52:26 +0000507 if(!opt_gt) { free(buf); return 0; }
508 }
509
vlm1ff928d2004-08-11 08:10:13 +0000510 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000511 opt_gt->size = size;
512
513 return opt_gt;
514}
515
516