blob: 5e3b9b0188321b099c63871b213223019ed94100 [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 Walkin659a7512004-08-11 07:35:08 +00005#define __USE_BSD /* To enable tm_gmtoff in glibc */
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 Walkin64399722004-08-11 07:17:22 +000013#ifdef WIN32
14#define localtime_r(tlocp, tmp) (*tmp = localtime(&tlocp))
15#warning PLEASE STOP AND READ!
16#warning localtime_r is implemented via localtime(), which is not thread-safe. You must fix the code to insert appropriate locking if you want to use asn_GT2time() or asn_UT2time().
17#warning PLEASE STOP AND READ!
18#endif
19
Lev Walkinf15320b2004-06-03 03:38:44 +000020#ifndef __NO_ASN_TABLE__
21
22/*
23 * GeneralizedTime basic type description.
24 */
25static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = {
26 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2))
27};
28asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
29 "GeneralizedTime",
30 GeneralizedTime_constraint, /* Check validity of time */
31 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkin99006362004-08-07 03:52:26 +000032 GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000033 GeneralizedTime_print,
34 OCTET_STRING_free,
35 0, /* Use generic outmost tag fetcher */
36 asn1_DEF_GeneralizedTime_tags,
37 sizeof(asn1_DEF_GeneralizedTime_tags)
38 / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
39 1, /* Single UNIVERSAL tag may be implicitly overriden */
40 -1, /* Both ways are fine */
Lev Walkind9bd7752004-06-05 08:17:50 +000041 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000042};
43
44#endif /* __NO_ASN_TABLE__ */
45
46/*
47 * Check that the time looks like the time.
48 */
49int
50GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
51 asn_app_consume_bytes_f *app_errlog, void *app_key) {
52 const GeneralizedTime_t *st = sptr;
53 time_t tloc;
54
55 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +000056 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000057 if(tloc == -1 && errno != EPERM) {
58 _ASN_ERRLOG("%s: Invalid time format: %s",
59 td->name, strerror(errno));
60 return -1;
61 }
62
63 return 0;
64}
65
Lev Walkin99006362004-08-07 03:52:26 +000066der_enc_rval_t
67GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
68 int tag_mode, ber_tlv_tag_t tag,
69 asn_app_consume_bytes_f *cb, void *app_key) {
70 GeneralizedTime_t *st = ptr;
71 der_enc_rval_t erval;
72
73 /* If not canonical DER, re-encode into canonical DER. */
74 if(st->size && st->buf[st->size-1] != 'Z') {
75 struct tm tm;
76 time_t tloc;
77
78 errno = EPERM;
79 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
80 if(tloc == -1 && errno != EPERM) {
81 /* Failed to recognize time. Fail completely. */
82 erval.encoded = -1;
83 erval.failed_type = td;
84 erval.structure_ptr = ptr;
85 return erval;
86 }
87 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
88 if(!st) {
89 /* Memory allocation failure. */
90 erval.encoded = -1;
91 erval.failed_type = td;
92 erval.structure_ptr = ptr;
93 return erval;
94 }
95 }
96
97 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
98
99 if(st != ptr) {
100 FREEMEM(st->buf);
101 FREEMEM(st);
102 }
103
104 return erval;
105}
106
Lev Walkinf15320b2004-06-03 03:38:44 +0000107int
108GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
109 asn_app_consume_bytes_f *cb, void *app_key) {
110 const GeneralizedTime_t *st = sptr;
111
Lev Walkind9bd7752004-06-05 08:17:50 +0000112 (void)td; /* Unused argument */
113 (void)ilevel; /* Unused argument */
114
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 if(st && st->buf) {
116 char buf[32];
117 struct tm tm;
118 int ret;
119
120 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000121 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 return cb("<bad-value>", 11, app_key);
123
124 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000125 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
127 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000128 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 return cb(buf, ret, app_key);
130 } else {
131 return cb("<absent>", 8, app_key);
132 }
133}
134
135/*
136 * Where to look for offset from GMT, Phase I.
137 * Several platforms are known.
138 */
Lev Walkin659a7512004-08-11 07:35:08 +0000139#if defined(__FreeBSD__) \
140 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
141 || (defined __GLIBC__ && __GLIBC__ >= 2)
142#undef HAVE_TM_GMTOFF
143#define HAVE_TM_GMTOFF
144#endif /* BSDs and newer glibc */
Lev Walkinf15320b2004-06-03 03:38:44 +0000145
146/*
147 * Where to look for offset from GMT, Phase II.
148 */
Lev Walkin659a7512004-08-11 07:35:08 +0000149#ifdef HAVE_TM_GMTOFF
Lev Walkin99006362004-08-07 03:52:26 +0000150#define GMTOFF(tm) ((tm).tm_gmtoff)
Lev Walkin659a7512004-08-11 07:35:08 +0000151#else /* HAVE_TM_GMTOFF */
Lev Walkin99006362004-08-07 03:52:26 +0000152#define GMTOFF(tm) (-timezone)
Lev Walkin659a7512004-08-11 07:35:08 +0000153#endif /* HAVE_TM_GMTOFF */
Lev Walkinf15320b2004-06-03 03:38:44 +0000154
155time_t
Lev Walkin99006362004-08-07 03:52:26 +0000156asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 struct tm tm_s;
158 uint8_t *buf;
159 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000160 int gmtoff_h = 0;
161 int gmtoff_m = 0;
162 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000163 int offset_specified = 0;
164 time_t tloc;
165
166 if(!st || !st->buf) {
167 errno = EINVAL;
168 return -1;
169 } else {
170 buf = st->buf;
171 end = buf + st->size;
172 }
173
174 if(st->size < 10) {
175 errno = EINVAL;
176 return -1;
177 }
178
179 /*
180 * Decode first 10 bytes: "AAAAMMJJhh"
181 */
182 memset(&tm_s, 0, sizeof(tm_s));
183#undef B2F
184#undef B2T
185#define B2F(var) do { \
186 unsigned ch = *buf; \
187 if(ch < 0x30 && ch > 0x39) { \
188 errno = EINVAL; \
189 return -1; \
190 } else { \
191 var = var * 10 + (ch - 0x30); \
192 buf++; \
193 } \
194 } while(0)
195#define B2T(var) B2F(tm_s.var)
196
197 B2T(tm_year); /* 1: A */
198 B2T(tm_year); /* 2: A */
199 B2T(tm_year); /* 3: A */
200 B2T(tm_year); /* 4: A */
201 B2T(tm_mon); /* 5: M */
202 B2T(tm_mon); /* 6: M */
203 B2T(tm_mday); /* 7: J */
204 B2T(tm_mday); /* 8: J */
205 B2T(tm_hour); /* 9: h */
206 B2T(tm_hour); /* 0: h */
207
208 if(buf == end) goto local_finish;
209
210 /*
211 * Parse [mm[ss[(.|,)ffff]]]
212 * ^^
213 */
214 switch(*buf) {
215 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
216 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
217 tm_s.tm_min = (*buf++) - 0x30;
218 if(buf == end) { errno = EINVAL; return -1; }
219 B2T(tm_min);
220 break;
221 case 0x2B: case 0x2D: /* +, - */
222 goto offset;
223 case 0x5A: /* Z */
224 goto utc_finish;
225 default:
226 errno = EINVAL;
227 return -1;
228 }
229
230 if(buf == end) goto local_finish;
231
232 /*
233 * Parse [mm[ss[(.|,)ffff]]]
234 * ^^
235 */
236 switch(*buf) {
237 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
238 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
239 tm_s.tm_sec = (*buf++) - 0x30;
240 if(buf == end) { errno = EINVAL; return -1; }
241 B2T(tm_sec);
242 break;
243 case 0x2B: case 0x2D: /* +, - */
244 goto offset;
245 case 0x5A: /* Z */
246 goto utc_finish;
247 default:
248 errno = EINVAL;
249 return -1;
250 }
251
252 if(buf == end) goto local_finish;
253
254 /*
255 * Parse [mm[ss[(.|,)ffff]]]
256 * ^ ^
257 */
258 switch(*buf) {
259 case 0x2C: case 0x2E: /* (.|,) */
260 /* Fractions of seconds are not supported
261 * by time_t or struct tm. Skip them */
262 for(buf++; buf < end; buf++) {
263 switch(*buf) {
264 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
265 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
266 continue;
267 default:
268 break;
269 }
270 break;
271 }
272 }
273
274 if(buf == end) goto local_finish;
275
276 switch(*buf) {
277 case 0x2B: case 0x2D: /* +, - */
278 goto offset;
279 case 0x5A: /* Z */
280 goto utc_finish;
281 default:
282 errno = EINVAL;
283 return -1;
284 }
285
286
287offset:
288
289 if(end - buf < 3) {
290 errno = EINVAL;
291 return -1;
292 }
293 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000294 B2F(gmtoff_h);
295 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000296 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000297 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000298 else
Lev Walkin99006362004-08-07 03:52:26 +0000299 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000300
301 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000302 B2F(gmtoff_m);
303 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 } else if(end != buf) {
305 errno = EINVAL;
306 return -1;
307 }
308
Lev Walkin99006362004-08-07 03:52:26 +0000309 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000310
311 /* Fall through */
312utc_finish:
313
314 offset_specified = 1;
315
316 /* Fall through */
317local_finish:
318
319 /*
320 * Validation.
321 */
322 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
323 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
324 || (tm_s.tm_hour > 23)
325 || (tm_s.tm_sec > 60)
326 ) {
327 errno = EINVAL;
328 return -1;
329 }
330
331 /* Canonicalize */
332 tm_s.tm_mon -= 1; /* 0 - 11 */
333 tm_s.tm_year -= 1900;
334 tm_s.tm_isdst = -1;
335
Lev Walkin99006362004-08-07 03:52:26 +0000336 tm_s.tm_sec -= gmtoff;
337
338 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
339
340 if(offset_specified)
341 tloc = timegm(&tm_s);
342 else {
343 /*
344 * Without an offset (or 'Z'),
345 * we can only guess that it is a local zone.
346 * Interpret it in this fashion.
347 */
348 tloc = mktime(&tm_s);
349 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000350 if(tloc == -1) {
351 errno = EINVAL;
352 return -1;
353 }
354
Lev Walkin99006362004-08-07 03:52:26 +0000355 if(ret_tm) {
356 if(as_gmt) {
357 if(offset_specified) {
358 *ret_tm = tm_s;
359 } else {
360 if(gmtime_r(&tloc, ret_tm) == 0) {
361 errno = EINVAL;
362 return -1;
363 }
364 }
365 } else {
366 if(localtime_r(&tloc, ret_tm) == 0) {
367 errno = EINVAL;
368 return -1;
369 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 }
371 }
372
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 return tloc;
374}
375
Lev Walkin99006362004-08-07 03:52:26 +0000376
377GeneralizedTime_t *
378asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
379 struct tm tm_s;
380 long gmtoff;
381 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
382 char *buf;
383 char *p;
384 int size;
385
386 /* Check arguments */
387 if(!tm) {
388 errno = EINVAL;
389 return 0;
390 }
391
392 /* Pre-allocate a buffer of sufficient yet small length */
393 buf = MALLOC(buf_size);
394 if(!buf) return 0;
395
396 gmtoff = GMTOFF(*tm);
397
398 if(force_gmt && gmtoff) {
399 tm_s = *tm;
400 tm_s.tm_sec -= gmtoff;
401 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000402 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000403#ifdef HAVE_TM_GMTOFF
404 assert(!GMTOFF(tm_s)); /* Will fix itself */
405#else
406 gmtoff = 0; /* Intervention required */
407#endif
Lev Walkin99006362004-08-07 03:52:26 +0000408 }
409
410 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
411 tm->tm_year + 1900,
412 tm->tm_mon + 1,
413 tm->tm_mday,
414 tm->tm_hour,
415 tm->tm_min,
416 tm->tm_sec
417 );
418 assert(size == 14);
419
420 p = buf + size;
421 if(force_gmt) {
422 *p++ = 0x5a; /* 'Z' */
423 *p++ = 0;
424 size++;
425 } else {
426 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
427 gmtoff / 3600, gmtoff % 3600);
428 assert(ret >= 5 && ret <= 7);
429 size += ret;
430 }
431
432 if(opt_gt) {
433 if(opt_gt->buf)
434 FREEMEM(opt_gt->buf);
435 } else {
436 opt_gt = CALLOC(1, sizeof *opt_gt);
437 if(!opt_gt) { free(buf); return 0; }
438 }
439
440 opt_gt->buf = buf;
441 opt_gt->size = size;
442
443 return opt_gt;
444}
445
446