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