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