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