blob: f17b43934ffdefd56346fc868ce5fee6a57b1ac0 [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;
39
40 tz = getenv("TZ");
41 setenv("TZ", "", 1);
42 tzset();
43 tloc = mktime(tm);
44 if (tz)
45 setenv("TZ", tz, 1);
46 else
47 unsetenv("TZ");
48 tzset();
49 return tloc;
50}
51
52#if 0 /* Alternate version */
Lev Walkin02a31552004-08-12 03:52:53 +000053/* vlm: I am not sure about validity of this algorithm. */
54static time_t timegm(struct tm *tm) {
55 struct tm tmp;
56 time_t tloc = mktime(tm);
57 localtime_r(&tloc, &tmp); /* Figure out our GMT offset */
58 tloc += tmp.tm_gmtoff;
59 tm->tm_zone = "GMT";
60 tm->tm_gmtoff = 0; /* Simulate GMT */
61 return tloc;
62}
Lev Walkindb424002004-08-12 03:58:25 +000063#endif
Lev Walkin02a31552004-08-12 03:52:53 +000064#endif /* WIN32 */
Lev Walkin64399722004-08-11 07:17:22 +000065
Lev Walkinf15320b2004-06-03 03:38:44 +000066#ifndef __NO_ASN_TABLE__
67
68/*
69 * GeneralizedTime basic type description.
70 */
71static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = {
72 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2))
73};
74asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
75 "GeneralizedTime",
76 GeneralizedTime_constraint, /* Check validity of time */
77 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkin99006362004-08-07 03:52:26 +000078 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000079 GeneralizedTime_print,
80 OCTET_STRING_free,
81 0, /* Use generic outmost tag fetcher */
82 asn1_DEF_GeneralizedTime_tags,
83 sizeof(asn1_DEF_GeneralizedTime_tags)
84 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
85 1, /* Single UNIVERSAL tag may be implicitly overriden */
86 -1, /* Both ways are fine */
Lev Walkind9bd7752004-06-05 08:17:50 +000087 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000088};
89
90#endif /* __NO_ASN_TABLE__ */
91
92/*
93 * Check that the time looks like the time.
94 */
95int
96GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
97 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000098 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000099 time_t tloc;
100
101 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000102 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000104 _ASN_ERRLOG(app_errlog, app_key,
105 "%s: Invalid time format: %s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 td->name, strerror(errno));
107 return -1;
108 }
109
110 return 0;
111}
112
Lev Walkin99006362004-08-07 03:52:26 +0000113der_enc_rval_t
114GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
115 int tag_mode, ber_tlv_tag_t tag,
116 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000117 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
Lev Walkin99006362004-08-07 03:52:26 +0000118 der_enc_rval_t erval;
119
120 /* If not canonical DER, re-encode into canonical DER. */
121 if(st->size && st->buf[st->size-1] != 'Z') {
122 struct tm tm;
123 time_t tloc;
124
125 errno = EPERM;
126 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
127 if(tloc == -1 && errno != EPERM) {
128 /* Failed to recognize time. Fail completely. */
129 erval.encoded = -1;
130 erval.failed_type = td;
131 erval.structure_ptr = ptr;
132 return erval;
133 }
134 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
135 if(!st) {
136 /* Memory allocation failure. */
137 erval.encoded = -1;
138 erval.failed_type = td;
139 erval.structure_ptr = ptr;
140 return erval;
141 }
142 }
143
144 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
145
146 if(st != ptr) {
147 FREEMEM(st->buf);
148 FREEMEM(st);
149 }
150
151 return erval;
152}
153
Lev Walkinf15320b2004-06-03 03:38:44 +0000154int
155GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
156 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000157 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000158
Lev Walkind9bd7752004-06-05 08:17:50 +0000159 (void)td; /* Unused argument */
160 (void)ilevel; /* Unused argument */
161
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 if(st && st->buf) {
163 char buf[32];
164 struct tm tm;
165 int ret;
166
167 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000168 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 return cb("<bad-value>", 11, app_key);
170
171 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000172 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
174 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000175 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 return cb(buf, ret, app_key);
177 } else {
178 return cb("<absent>", 8, app_key);
179 }
180}
181
182/*
183 * Where to look for offset from GMT, Phase I.
184 * Several platforms are known.
185 */
Lev Walkin659a7512004-08-11 07:35:08 +0000186#if defined(__FreeBSD__) \
187 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
188 || (defined __GLIBC__ && __GLIBC__ >= 2)
189#undef HAVE_TM_GMTOFF
190#define HAVE_TM_GMTOFF
191#endif /* BSDs and newer glibc */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192
193/*
194 * Where to look for offset from GMT, Phase II.
195 */
Lev Walkin659a7512004-08-11 07:35:08 +0000196#ifdef HAVE_TM_GMTOFF
Lev Walkin99006362004-08-07 03:52:26 +0000197#define GMTOFF(tm) ((tm).tm_gmtoff)
Lev Walkin659a7512004-08-11 07:35:08 +0000198#else /* HAVE_TM_GMTOFF */
Lev Walkin99006362004-08-07 03:52:26 +0000199#define GMTOFF(tm) (-timezone)
Lev Walkin659a7512004-08-11 07:35:08 +0000200#endif /* HAVE_TM_GMTOFF */
Lev Walkinf15320b2004-06-03 03:38:44 +0000201
202time_t
Lev Walkin99006362004-08-07 03:52:26 +0000203asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 struct tm tm_s;
205 uint8_t *buf;
206 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000207 int gmtoff_h = 0;
208 int gmtoff_m = 0;
209 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 int offset_specified = 0;
211 time_t tloc;
212
213 if(!st || !st->buf) {
214 errno = EINVAL;
215 return -1;
216 } else {
217 buf = st->buf;
218 end = buf + st->size;
219 }
220
221 if(st->size < 10) {
222 errno = EINVAL;
223 return -1;
224 }
225
226 /*
227 * Decode first 10 bytes: "AAAAMMJJhh"
228 */
229 memset(&tm_s, 0, sizeof(tm_s));
230#undef B2F
231#undef B2T
232#define B2F(var) do { \
233 unsigned ch = *buf; \
234 if(ch < 0x30 && ch > 0x39) { \
235 errno = EINVAL; \
236 return -1; \
237 } else { \
238 var = var * 10 + (ch - 0x30); \
239 buf++; \
240 } \
241 } while(0)
242#define B2T(var) B2F(tm_s.var)
243
244 B2T(tm_year); /* 1: A */
245 B2T(tm_year); /* 2: A */
246 B2T(tm_year); /* 3: A */
247 B2T(tm_year); /* 4: A */
248 B2T(tm_mon); /* 5: M */
249 B2T(tm_mon); /* 6: M */
250 B2T(tm_mday); /* 7: J */
251 B2T(tm_mday); /* 8: J */
252 B2T(tm_hour); /* 9: h */
253 B2T(tm_hour); /* 0: h */
254
255 if(buf == end) goto local_finish;
256
257 /*
258 * Parse [mm[ss[(.|,)ffff]]]
259 * ^^
260 */
261 switch(*buf) {
262 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
263 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
264 tm_s.tm_min = (*buf++) - 0x30;
265 if(buf == end) { errno = EINVAL; return -1; }
266 B2T(tm_min);
267 break;
268 case 0x2B: case 0x2D: /* +, - */
269 goto offset;
270 case 0x5A: /* Z */
271 goto utc_finish;
272 default:
273 errno = EINVAL;
274 return -1;
275 }
276
277 if(buf == end) goto local_finish;
278
279 /*
280 * Parse [mm[ss[(.|,)ffff]]]
281 * ^^
282 */
283 switch(*buf) {
284 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
285 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
286 tm_s.tm_sec = (*buf++) - 0x30;
287 if(buf == end) { errno = EINVAL; return -1; }
288 B2T(tm_sec);
289 break;
290 case 0x2B: case 0x2D: /* +, - */
291 goto offset;
292 case 0x5A: /* Z */
293 goto utc_finish;
294 default:
295 errno = EINVAL;
296 return -1;
297 }
298
299 if(buf == end) goto local_finish;
300
301 /*
302 * Parse [mm[ss[(.|,)ffff]]]
303 * ^ ^
304 */
305 switch(*buf) {
306 case 0x2C: case 0x2E: /* (.|,) */
307 /* Fractions of seconds are not supported
308 * by time_t or struct tm. Skip them */
309 for(buf++; buf < end; buf++) {
310 switch(*buf) {
311 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
312 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
313 continue;
314 default:
315 break;
316 }
317 break;
318 }
319 }
320
321 if(buf == end) goto local_finish;
322
323 switch(*buf) {
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
334offset:
335
336 if(end - buf < 3) {
337 errno = EINVAL;
338 return -1;
339 }
340 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000341 B2F(gmtoff_h);
342 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000344 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000345 else
Lev Walkin99006362004-08-07 03:52:26 +0000346 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000347
348 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000349 B2F(gmtoff_m);
350 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000351 } else if(end != buf) {
352 errno = EINVAL;
353 return -1;
354 }
355
Lev Walkin99006362004-08-07 03:52:26 +0000356 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000357
358 /* Fall through */
359utc_finish:
360
361 offset_specified = 1;
362
363 /* Fall through */
364local_finish:
365
366 /*
367 * Validation.
368 */
369 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
370 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
371 || (tm_s.tm_hour > 23)
372 || (tm_s.tm_sec > 60)
373 ) {
374 errno = EINVAL;
375 return -1;
376 }
377
378 /* Canonicalize */
379 tm_s.tm_mon -= 1; /* 0 - 11 */
380 tm_s.tm_year -= 1900;
381 tm_s.tm_isdst = -1;
382
Lev Walkin99006362004-08-07 03:52:26 +0000383 tm_s.tm_sec -= gmtoff;
384
385 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
386
Lev Walkin02a31552004-08-12 03:52:53 +0000387 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000388 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000389 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000390 /*
391 * Without an offset (or 'Z'),
392 * we can only guess that it is a local zone.
393 * Interpret it in this fashion.
394 */
395 tloc = mktime(&tm_s);
396 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000397 if(tloc == -1) {
398 errno = EINVAL;
399 return -1;
400 }
401
Lev Walkin99006362004-08-07 03:52:26 +0000402 if(ret_tm) {
403 if(as_gmt) {
404 if(offset_specified) {
405 *ret_tm = tm_s;
406 } else {
407 if(gmtime_r(&tloc, ret_tm) == 0) {
408 errno = EINVAL;
409 return -1;
410 }
411 }
412 } else {
413 if(localtime_r(&tloc, ret_tm) == 0) {
414 errno = EINVAL;
415 return -1;
416 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 }
418 }
419
Lev Walkinf15320b2004-06-03 03:38:44 +0000420 return tloc;
421}
422
Lev Walkin99006362004-08-07 03:52:26 +0000423
424GeneralizedTime_t *
425asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
426 struct tm tm_s;
427 long gmtoff;
428 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
429 char *buf;
430 char *p;
431 int size;
432
433 /* Check arguments */
434 if(!tm) {
435 errno = EINVAL;
436 return 0;
437 }
438
439 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkinc2346572004-08-11 09:07:36 +0000440 (void *)buf = MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000441 if(!buf) return 0;
442
443 gmtoff = GMTOFF(*tm);
444
445 if(force_gmt && gmtoff) {
446 tm_s = *tm;
447 tm_s.tm_sec -= gmtoff;
448 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000449 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000450#ifdef HAVE_TM_GMTOFF
451 assert(!GMTOFF(tm_s)); /* Will fix itself */
452#else
453 gmtoff = 0; /* Intervention required */
454#endif
Lev Walkin99006362004-08-07 03:52:26 +0000455 }
456
457 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
458 tm->tm_year + 1900,
459 tm->tm_mon + 1,
460 tm->tm_mday,
461 tm->tm_hour,
462 tm->tm_min,
463 tm->tm_sec
464 );
465 assert(size == 14);
466
467 p = buf + size;
468 if(force_gmt) {
469 *p++ = 0x5a; /* 'Z' */
470 *p++ = 0;
471 size++;
472 } else {
473 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
474 gmtoff / 3600, gmtoff % 3600);
475 assert(ret >= 5 && ret <= 7);
476 size += ret;
477 }
478
479 if(opt_gt) {
480 if(opt_gt->buf)
481 FREEMEM(opt_gt->buf);
482 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000483 (void *)opt_gt = CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000484 if(!opt_gt) { free(buf); return 0; }
485 }
486
Lev Walkin4d9528c2004-08-11 08:10:13 +0000487 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000488 opt_gt->size = size;
489
490 return opt_gt;
491}
492
493