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 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 12 | #ifdef WIN32 |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 13 | #warning PLEASE STOP AND READ! |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 14 | #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(). |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 19 | #warning PLEASE STOP AND READ! |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 20 | |
| 21 | static 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 | |
| 28 | static 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 | |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 35 | #define tzset() _tzset() |
| 36 | static time_t timegm(struct tm *tm) { |
| 37 | time_t tloc; |
| 38 | char *tz; |
vlm | d369228 | 2004-08-12 07:47:03 +0000 | [diff] [blame] | 39 | char *buf; |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 40 | |
| 41 | tz = getenv("TZ"); |
vlm | d369228 | 2004-08-12 07:47:03 +0000 | [diff] [blame] | 42 | _putenv("TZ=UTC"); |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 43 | tzset(); |
| 44 | tloc = mktime(tm); |
vlm | d369228 | 2004-08-12 07:47:03 +0000 | [diff] [blame] | 45 | if (tz) { |
| 46 | buf = alloca(strlen(tz) + 4); |
| 47 | sprintf(buf, "TZ=%s", tz); |
| 48 | } else { |
| 49 | buf = "TZ="; |
| 50 | } |
| 51 | _putenv(buf); |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 52 | tzset(); |
| 53 | return tloc; |
| 54 | } |
| 55 | |
| 56 | #if 0 /* Alternate version */ |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 57 | /* vlm: I am not sure about validity of this algorithm. */ |
| 58 | static 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 | } |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame] | 67 | #endif |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 68 | #endif /* WIN32 */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 69 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | #ifndef __NO_ASN_TABLE__ |
| 71 | |
| 72 | /* |
| 73 | * GeneralizedTime basic type description. |
| 74 | */ |
| 75 | static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = { |
| 76 | (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)) |
| 77 | }; |
| 78 | asn1_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 */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 82 | GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 83 | 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 */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 91 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 92 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | #endif /* __NO_ASN_TABLE__ */ |
| 96 | |
| 97 | /* |
| 98 | * Check that the time looks like the time. |
| 99 | */ |
| 100 | int |
| 101 | GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 102 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 103 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | time_t tloc; |
| 105 | |
| 106 | errno = EPERM; /* Just an unlikely error code */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 107 | tloc = asn_GT2time(st, 0, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 108 | if(tloc == -1 && errno != EPERM) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 109 | _ASN_ERRLOG(app_errlog, app_key, |
| 110 | "%s: Invalid time format: %s", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | td->name, strerror(errno)); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 118 | der_enc_rval_t |
| 119 | GeneralizedTime_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) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 122 | GeneralizedTime_t *st = (GeneralizedTime_t *)ptr; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 123 | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 159 | int |
| 160 | GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 161 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 162 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 163 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 164 | (void)td; /* Unused argument */ |
| 165 | (void)ilevel; /* Unused argument */ |
| 166 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 167 | if(st && st->buf) { |
| 168 | char buf[32]; |
| 169 | struct tm tm; |
| 170 | int ret; |
| 171 | |
| 172 | errno = EPERM; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 173 | if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | return cb("<bad-value>", 11, app_key); |
| 175 | |
| 176 | ret = snprintf(buf, sizeof(buf), |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 177 | "%04d-%02d-%02d %02d:%02d%02d (GMT)", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 179 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 180 | assert(ret > 0 && ret < (int)sizeof(buf)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 181 | 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 | */ |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 191 | #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 */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Where to look for offset from GMT, Phase II. |
| 200 | */ |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 201 | #ifdef HAVE_TM_GMTOFF |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 202 | #define GMTOFF(tm) ((tm).tm_gmtoff) |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 203 | #else /* HAVE_TM_GMTOFF */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 204 | #define GMTOFF(tm) (-timezone) |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 205 | #endif /* HAVE_TM_GMTOFF */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | |
| 207 | time_t |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 208 | asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 209 | struct tm tm_s; |
| 210 | uint8_t *buf; |
| 211 | uint8_t *end; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 212 | int gmtoff_h = 0; |
| 213 | int gmtoff_m = 0; |
| 214 | int gmtoff = 0; /* h + m */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 215 | 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 | |
| 339 | offset: |
| 340 | |
| 341 | if(end - buf < 3) { |
| 342 | errno = EINVAL; |
| 343 | return -1; |
| 344 | } |
| 345 | buf++; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 346 | B2F(gmtoff_h); |
| 347 | B2F(gmtoff_h); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 348 | if(buf[-3] == 0x2D) /* Negative */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 349 | gmtoff = -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 350 | else |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 351 | gmtoff = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | |
| 353 | if((end - buf) == 2) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 354 | B2F(gmtoff_m); |
| 355 | B2F(gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 356 | } else if(end != buf) { |
| 357 | errno = EINVAL; |
| 358 | return -1; |
| 359 | } |
| 360 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 361 | gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 362 | |
| 363 | /* Fall through */ |
| 364 | utc_finish: |
| 365 | |
| 366 | offset_specified = 1; |
| 367 | |
| 368 | /* Fall through */ |
| 369 | local_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 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 388 | tm_s.tm_sec -= gmtoff; |
| 389 | |
| 390 | /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/ |
| 391 | |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 392 | if(offset_specified) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 393 | tloc = timegm(&tm_s); |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 394 | } else { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 395 | /* |
| 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 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 402 | if(tloc == -1) { |
| 403 | errno = EINVAL; |
| 404 | return -1; |
| 405 | } |
| 406 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 407 | 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 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 425 | return tloc; |
| 426 | } |
| 427 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 428 | |
| 429 | GeneralizedTime_t * |
| 430 | asn_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 */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 445 | (void *)buf = MALLOC(buf_size); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 446 | 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 */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 454 | tm = &tm_s; |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 455 | #ifdef HAVE_TM_GMTOFF |
| 456 | assert(!GMTOFF(tm_s)); /* Will fix itself */ |
| 457 | #else |
| 458 | gmtoff = 0; /* Intervention required */ |
| 459 | #endif |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 460 | } |
| 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 { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 488 | (void *)opt_gt = CALLOC(1, sizeof *opt_gt); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 489 | if(!opt_gt) { free(buf); return 0; } |
| 490 | } |
| 491 | |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 492 | opt_gt->buf = (unsigned char *)buf; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 493 | opt_gt->size = size; |
| 494 | |
| 495 | return opt_gt; |
| 496 | } |
| 497 | |
| 498 | |