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