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