blob: b2d06034b00bfa6b3fcf00edb30080dd0b9a480e [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkina5972be2017-09-29 23:15:58 -07002 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina460ba32004-10-20 15:40:04 +00005#define _POSIX_PTHREAD_SEMANTICS /* for Sun */
6#define _REENTRANT /* for Sun */
Lev Walkin9d6ba1c2017-10-06 16:38:07 -07007#define __EXTENSIONS__ /* for Sun */
Vasil Velichkovadf65942016-08-01 16:52:02 +03008#ifndef _BSD_SOURCE
Lev Walkin7623d8c2016-03-14 03:55:21 -07009#define _BSD_SOURCE /* for timegm(3) */
Vasil Velichkovadf65942016-08-01 16:52:02 +030010#endif
Lev Walkina9cc46e2004-09-22 16:06:28 +000011#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000012#include <GeneralizedTime.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000013
Lev Walkin223000a2006-09-13 00:21:58 +000014#ifdef __CYGWIN__
15#include "/usr/include/time.h"
16#else
17#include <time.h>
18#endif /* __CYGWIN__ */
19
Lev Walkin7623d8c2016-03-14 03:55:21 -070020#include <stdio.h>
21#include <errno.h>
22
Lev Walkin93659562010-11-20 09:47:13 -080023#if defined(_WIN32)
Lev Walkina57953d2005-06-15 19:01:45 +000024#pragma message( "PLEASE STOP AND READ!")
25#pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.")
26#pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.")
27#pragma message( " ")
28#pragma message( " You must fix the code by inserting appropriate locking")
29#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
Lev Walkinc8c2cb52006-07-13 13:20:19 +000030#pragma message( "PLEASE STOP AND READ!")
Lev Walkin02a31552004-08-12 03:52:53 +000031
Lev Walkincec43b52004-09-02 05:20:39 +000032static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000033 struct tm *tm;
34 if((tm = localtime(tloc)))
35 return memcpy(result, tm, sizeof(struct tm));
36 return 0;
37}
38
Lev Walkincec43b52004-09-02 05:20:39 +000039static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000040 struct tm *tm;
41 if((tm = gmtime(tloc)))
42 return memcpy(result, tm, sizeof(struct tm));
43 return 0;
44}
45
Lev Walkin93e9fe32004-09-17 06:46:10 +000046#define tzset() _tzset()
Lev Walkina57953d2005-06-15 19:01:45 +000047#define putenv(c) _putenv(c)
Lev Walkin93e9fe32004-09-17 06:46:10 +000048#define _EMULATE_TIMEGM
49
Lev Walkin93659562010-11-20 09:47:13 -080050#endif /* _WIN32 */
Lev Walkin93e9fe32004-09-17 06:46:10 +000051
Lev Walkin9d6ba1c2017-10-06 16:38:07 -070052#if defined(sun) || defined(__sun) || defined(__solaris__)
Lev Walkina460ba32004-10-20 15:40:04 +000053#define _EMULATE_TIMEGM
54#endif
55
Lev Walkin93e9fe32004-09-17 06:46:10 +000056/*
57 * Where to look for offset from GMT, Phase I.
58 * Several platforms are known.
59 */
60#if defined(__FreeBSD__) \
61 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
62 || (defined __GLIBC__ && __GLIBC__ >= 2)
63#undef HAVE_TM_GMTOFF
64#define HAVE_TM_GMTOFF
65#endif /* BSDs and newer glibc */
66
67/*
68 * Where to look for offset from GMT, Phase II.
69 */
70#ifdef HAVE_TM_GMTOFF
71#define GMTOFF(tm) ((tm).tm_gmtoff)
72#else /* HAVE_TM_GMTOFF */
73#define GMTOFF(tm) (-timezone)
74#endif /* HAVE_TM_GMTOFF */
75
Frank Morgner917a81d2013-05-21 09:52:19 +020076#if defined(_WIN32)
77#pragma message( "PLEASE STOP AND READ!")
78#pragma message( " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe.")
79#pragma message( " ")
80#pragma message( " You must fix the code by inserting appropriate locking")
81#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
82#pragma message( "PLEASE STOP AND READ!")
83#else
Lev Walkinc8c2cb52006-07-13 13:20:19 +000084#if (defined(_EMULATE_TIMEGM) || !defined(HAVE_TM_GMTOFF))
85#warning "PLEASE STOP AND READ!"
86#warning " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe."
87#warning " "
88#warning " You must fix the code by inserting appropriate locking"
89#warning " if you want to use asn_GT2time() or asn_UT2time()."
90#warning "PLEASE STOP AND READ!"
91#endif /* _EMULATE_TIMEGM */
Frank Morgner917a81d2013-05-21 09:52:19 +020092#endif
Lev Walkinc8c2cb52006-07-13 13:20:19 +000093
Lev Walkin93e9fe32004-09-17 06:46:10 +000094/*
95 * Override our GMTOFF decision for other known platforms.
96 */
97#ifdef __CYGWIN__
98#undef GMTOFF
99static long GMTOFF(struct tm a){
100 struct tm *lt;
101 time_t local_time, gmt_time;
102 long zone;
103
104 tzset();
105 gmt_time = time (NULL);
106
107 lt = gmtime(&gmt_time);
108
109 local_time = mktime(lt);
110 return (gmt_time - local_time);
111}
112#define _EMULATE_TIMEGM
113
114#endif /* __CYGWIN__ */
115
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000116#define ATZVARS do { \
117 char tzoldbuf[64]; \
118 char *tzold
119#define ATZSAVETZ do { \
120 tzold = getenv("TZ"); \
121 if(tzold) { \
122 size_t tzlen = strlen(tzold); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000123 if(tzlen < sizeof(tzoldbuf)) { \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000124 tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000125 } else { \
Lev Walkinb9671d12007-10-02 11:23:24 +0000126 char *dupptr = tzold; \
Lev Walkin2fe66ca2007-10-02 11:03:02 +0000127 tzold = MALLOC(tzlen + 1); \
128 if(tzold) memcpy(tzold, dupptr, tzlen + 1); \
129 } \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000130 setenv("TZ", "UTC", 1); \
131 } \
132 tzset(); \
133} while(0)
134#define ATZOLDTZ do { \
135 if (tzold) { \
136 setenv("TZ", tzold, 1); \
137 *tzoldbuf = 0; \
138 if(tzold != tzoldbuf) \
Lev Walkin419f6752006-09-13 04:02:00 +0000139 FREEMEM(tzold); \
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000140 } else { \
141 unsetenv("TZ"); \
142 } \
143 tzset(); \
144} while(0); } while(0);
145
Lev Walkin04fbe622014-09-17 02:27:01 -0700146#ifndef HAVE_TIMEGM
Lev Walkin93e9fe32004-09-17 06:46:10 +0000147#ifdef _EMULATE_TIMEGM
Lev Walkin9d6ba1c2017-10-06 16:38:07 -0700148#include <stdlib.h>
Lev Walkindb424002004-08-12 03:58:25 +0000149static time_t timegm(struct tm *tm) {
150 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000151 ATZVARS;
152 ATZSAVETZ;
Lev Walkindb424002004-08-12 03:58:25 +0000153 tloc = mktime(tm);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000154 ATZOLDTZ;
Lev Walkindb424002004-08-12 03:58:25 +0000155 return tloc;
156}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000157#endif /* _EMULATE_TIMEGM */
Lev Walkin04fbe622014-09-17 02:27:01 -0700158#endif
Lev Walkindb424002004-08-12 03:58:25 +0000159
Lev Walkin64399722004-08-11 07:17:22 +0000160
Lev Walkin7c1dc052016-03-14 03:08:15 -0700161#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkinf15320b2004-06-03 03:38:44 +0000162
163/*
164 * GeneralizedTime basic type description.
165 */
Wim Lewis18c2ec92014-07-29 11:30:10 -0700166static const ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000167 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
168 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
169 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000170};
Lev Walkina5972be2017-09-29 23:15:58 -0700171static asn_per_constraints_t asn_DEF_GeneralizedTime_per_constraints = {
Lev Walkin725883b2006-10-09 12:07:58 +0000172 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
173 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
174 0, 0
175};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800176asn_TYPE_operation_t asn_OP_GeneralizedTime = {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000177 OCTET_STRING_free,
178 GeneralizedTime_print,
Lev Walkindd01a5e2017-10-14 01:45:26 -0700179 GeneralizedTime_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000181 GeneralizedTime_encode_der,
182 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183 GeneralizedTime_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -0700184#ifdef ASN_DISABLE_OER_SUPPORT
185 0,
186 0,
187#else
Lev Walkindd01a5e2017-10-14 01:45:26 -0700188 OCTET_STRING_decode_oer,
189 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -0700190#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +0400191#ifdef ASN_DISABLE_PER_SUPPORT
192 0,
193 0,
194#else
195 OCTET_STRING_decode_uper,
196 OCTET_STRING_encode_uper,
197#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -0700198 GeneralizedTime_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800199 0 /* Use generic outmost tag fetcher */
200};
201asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
202 "GeneralizedTime",
203 "GeneralizedTime",
204 &asn_OP_GeneralizedTime,
Lev Walkin5e033762004-09-29 13:26:15 +0000205 asn_DEF_GeneralizedTime_tags,
206 sizeof(asn_DEF_GeneralizedTime_tags)
207 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
208 asn_DEF_GeneralizedTime_tags,
209 sizeof(asn_DEF_GeneralizedTime_tags)
210 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -0700211 { 0, &asn_DEF_GeneralizedTime_per_constraints, GeneralizedTime_constraint },
Lev Walkin449f8322004-08-20 13:23:42 +0000212 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000213 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000214};
215
Lev Walkin7c1dc052016-03-14 03:08:15 -0700216#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkinf15320b2004-06-03 03:38:44 +0000217
218/*
219 * Check that the time looks like the time.
220 */
221int
Lev Walkin20696a42017-10-17 21:27:33 -0700222GeneralizedTime_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
223 asn_app_constraint_failed_f *ctfailcb,
224 void *app_key) {
225 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 time_t tloc;
227
228 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000229 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 if(tloc == -1 && errno != EPERM) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700231 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000232 "%s: Invalid time format: %s (%s:%d)",
233 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 return -1;
235 }
236
237 return 0;
238}
239
Lev Walkina9cc46e2004-09-22 16:06:28 +0000240asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700241GeneralizedTime_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
242 int tag_mode, ber_tlv_tag_t tag,
243 asn_app_consume_bytes_f *cb, void *app_key) {
244 GeneralizedTime_t *st;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000245 asn_enc_rval_t erval;
Lev Walkin3a522782005-07-04 12:21:51 +0000246 int fv, fd; /* seconds fraction value and number of digits */
Lev Walkin535612a2005-07-03 05:32:40 +0000247 struct tm tm;
248 time_t tloc;
Lev Walkin99006362004-08-07 03:52:26 +0000249
Lev Walkin535612a2005-07-03 05:32:40 +0000250 /*
251 * Encode as a canonical DER.
252 */
Lev Walkin20696a42017-10-17 21:27:33 -0700253 errno = EPERM;
254 tloc = asn_GT2time_frac((const GeneralizedTime_t *)sptr, &fv, &fd, &tm,
255 1); /* Recognize time */
256 if(tloc == -1 && errno != EPERM) {
257 /* Failed to recognize time. Fail completely. */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700258 ASN__ENCODE_FAILED;
Lev Walkin20696a42017-10-17 21:27:33 -0700259 }
Lev Walkin99006362004-08-07 03:52:26 +0000260
Lev Walkin20696a42017-10-17 21:27:33 -0700261 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
262 if(!st) ASN__ENCODE_FAILED; /* Memory allocation failure. */
Lev Walkin99006362004-08-07 03:52:26 +0000263
Lev Walkin20696a42017-10-17 21:27:33 -0700264 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
Lev Walkin99006362004-08-07 03:52:26 +0000265
Lev Walkin20696a42017-10-17 21:27:33 -0700266 ASN_STRUCT_FREE(*td, st);
Lev Walkin99006362004-08-07 03:52:26 +0000267
Lev Walkin20696a42017-10-17 21:27:33 -0700268 return erval;
Lev Walkin99006362004-08-07 03:52:26 +0000269}
270
Lev Walkin7c1dc052016-03-14 03:08:15 -0700271#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkin535612a2005-07-03 05:32:40 +0000272
Lev Walkina9cc46e2004-09-22 16:06:28 +0000273asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700274GeneralizedTime_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
275 int ilevel, enum xer_encoder_flags_e flags,
276 asn_app_consume_bytes_f *cb, void *app_key) {
277 if(flags & XER_F_CANONICAL) {
Lev Walkin535612a2005-07-03 05:32:40 +0000278 GeneralizedTime_t *gt;
279 asn_enc_rval_t rv;
Lev Walkin3a522782005-07-04 12:21:51 +0000280 int fv, fd; /* fractional parts */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000281 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000282
283 errno = EPERM;
Lev Walkin20696a42017-10-17 21:27:33 -0700284 if(asn_GT2time_frac((const GeneralizedTime_t *)sptr,
Lev Walkin3a522782005-07-04 12:21:51 +0000285 &fv, &fd, &tm, 1) == -1
Lev Walkina9cc46e2004-09-22 16:06:28 +0000286 && errno != EPERM)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700287 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000288
Lev Walkin3a522782005-07-04 12:21:51 +0000289 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700290 if(!gt) ASN__ENCODE_FAILED;
Lev Walkin535612a2005-07-03 05:32:40 +0000291
292 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
293 cb, app_key);
Lev Walkinadcb5862006-03-17 02:11:12 +0000294 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkin535612a2005-07-03 05:32:40 +0000295 return rv;
296 } else {
297 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
298 cb, app_key);
299 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000300}
301
Lev Walkin7c1dc052016-03-14 03:08:15 -0700302#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkin535612a2005-07-03 05:32:40 +0000303
Lev Walkinf15320b2004-06-03 03:38:44 +0000304int
Lev Walkin20696a42017-10-17 21:27:33 -0700305GeneralizedTime_print(const asn_TYPE_descriptor_t *td, const void *sptr,
306 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
307 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000308
Lev Walkind9bd7752004-06-05 08:17:50 +0000309 (void)td; /* Unused argument */
310 (void)ilevel; /* Unused argument */
311
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 if(st && st->buf) {
313 char buf[32];
314 struct tm tm;
315 int ret;
316
317 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000318 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000319 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000320
321 ret = snprintf(buf, sizeof(buf),
Lev Walkin535612a2005-07-03 05:32:40 +0000322 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000323 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
324 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000325 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000326 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000328 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000329 }
330}
331
Lev Walkinf15320b2004-06-03 03:38:44 +0000332time_t
Lev Walkin99006362004-08-07 03:52:26 +0000333asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000334 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
335}
336
337time_t
Lev Walkin3a522782005-07-04 12:21:51 +0000338asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
339 time_t tloc;
340 int fv, fd = 0;
341
342 if(frac_value)
343 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
344 else
345 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
346 if(fd == 0 || frac_digits <= 0) {
347 *frac_value = 0;
348 } else {
349 while(fd > frac_digits)
350 fv /= 10, fd--;
351 while(fd < frac_digits) {
Lev Walkin27126182012-09-03 01:09:54 -0700352 if(fv < INT_MAX / 10) {
353 fv *= 10;
354 fd++;
355 } else {
Lev Walkin3a522782005-07-04 12:21:51 +0000356 /* Too long precision request */
357 fv = 0;
358 break;
359 }
Lev Walkin3a522782005-07-04 12:21:51 +0000360 }
361
362 *frac_value = fv;
363 }
364
365 return tloc;
366}
367
368time_t
369asn_GT2time_frac(const GeneralizedTime_t *st, int *frac_value, int *frac_digits, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 struct tm tm_s;
371 uint8_t *buf;
372 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000373 int gmtoff_h = 0;
374 int gmtoff_m = 0;
375 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000376 int offset_specified = 0;
Lev Walkin3a522782005-07-04 12:21:51 +0000377 int fvalue = 0;
378 int fdigits = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 time_t tloc;
380
381 if(!st || !st->buf) {
382 errno = EINVAL;
383 return -1;
384 } else {
385 buf = st->buf;
386 end = buf + st->size;
387 }
388
389 if(st->size < 10) {
390 errno = EINVAL;
391 return -1;
392 }
393
394 /*
395 * Decode first 10 bytes: "AAAAMMJJhh"
396 */
397 memset(&tm_s, 0, sizeof(tm_s));
398#undef B2F
399#undef B2T
400#define B2F(var) do { \
401 unsigned ch = *buf; \
Lev Walkinc81dab12006-01-10 08:08:14 +0000402 if(ch < 0x30 || ch > 0x39) { \
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 errno = EINVAL; \
404 return -1; \
405 } else { \
406 var = var * 10 + (ch - 0x30); \
407 buf++; \
408 } \
409 } while(0)
410#define B2T(var) B2F(tm_s.var)
411
412 B2T(tm_year); /* 1: A */
413 B2T(tm_year); /* 2: A */
414 B2T(tm_year); /* 3: A */
415 B2T(tm_year); /* 4: A */
416 B2T(tm_mon); /* 5: M */
417 B2T(tm_mon); /* 6: M */
418 B2T(tm_mday); /* 7: J */
419 B2T(tm_mday); /* 8: J */
420 B2T(tm_hour); /* 9: h */
421 B2T(tm_hour); /* 0: h */
422
423 if(buf == end) goto local_finish;
424
425 /*
426 * Parse [mm[ss[(.|,)ffff]]]
427 * ^^
428 */
429 switch(*buf) {
430 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
431 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
432 tm_s.tm_min = (*buf++) - 0x30;
433 if(buf == end) { errno = EINVAL; return -1; }
434 B2T(tm_min);
435 break;
436 case 0x2B: case 0x2D: /* +, - */
437 goto offset;
438 case 0x5A: /* Z */
439 goto utc_finish;
440 default:
441 errno = EINVAL;
442 return -1;
443 }
444
445 if(buf == end) goto local_finish;
446
447 /*
448 * Parse [mm[ss[(.|,)ffff]]]
449 * ^^
450 */
451 switch(*buf) {
452 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
453 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
454 tm_s.tm_sec = (*buf++) - 0x30;
455 if(buf == end) { errno = EINVAL; return -1; }
456 B2T(tm_sec);
457 break;
458 case 0x2B: case 0x2D: /* +, - */
459 goto offset;
460 case 0x5A: /* Z */
461 goto utc_finish;
462 default:
463 errno = EINVAL;
464 return -1;
465 }
466
467 if(buf == end) goto local_finish;
468
469 /*
470 * Parse [mm[ss[(.|,)ffff]]]
471 * ^ ^
472 */
473 switch(*buf) {
Lev Walkin535612a2005-07-03 05:32:40 +0000474 case 0x2C: case 0x2E: /* (.|,) */
475 /*
476 * Process fractions of seconds.
477 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 for(buf++; buf < end; buf++) {
Lev Walkin535612a2005-07-03 05:32:40 +0000479 int v = *buf;
Lev Walkin9a338f82012-01-22 16:38:37 -0800480 /* GCC 4.x is being too smart without volatile */
Lev Walkin535612a2005-07-03 05:32:40 +0000481 switch(v) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
483 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkin27126182012-09-03 01:09:54 -0700484 if(fvalue < INT_MAX/10) {
485 fvalue = fvalue * 10 + (v - 0x30);
Lev Walkin3a522782005-07-04 12:21:51 +0000486 fdigits++;
Lev Walkin27126182012-09-03 01:09:54 -0700487 } else {
488 /* Not enough precision, ignore */
Lev Walkin535612a2005-07-03 05:32:40 +0000489 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000490 continue;
491 default:
492 break;
493 }
494 break;
495 }
496 }
497
498 if(buf == end) goto local_finish;
499
500 switch(*buf) {
501 case 0x2B: case 0x2D: /* +, - */
502 goto offset;
503 case 0x5A: /* Z */
504 goto utc_finish;
505 default:
506 errno = EINVAL;
507 return -1;
508 }
509
510
511offset:
512
513 if(end - buf < 3) {
514 errno = EINVAL;
515 return -1;
516 }
517 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000518 B2F(gmtoff_h);
519 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000521 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000522 else
Lev Walkin99006362004-08-07 03:52:26 +0000523 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000524
525 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000526 B2F(gmtoff_m);
527 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000528 } else if(end != buf) {
529 errno = EINVAL;
530 return -1;
531 }
532
Lev Walkin99006362004-08-07 03:52:26 +0000533 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000534
535 /* Fall through */
536utc_finish:
537
538 offset_specified = 1;
539
540 /* Fall through */
541local_finish:
542
543 /*
544 * Validation.
545 */
546 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
547 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
548 || (tm_s.tm_hour > 23)
549 || (tm_s.tm_sec > 60)
550 ) {
551 errno = EINVAL;
552 return -1;
553 }
554
555 /* Canonicalize */
556 tm_s.tm_mon -= 1; /* 0 - 11 */
557 tm_s.tm_year -= 1900;
558 tm_s.tm_isdst = -1;
559
Lev Walkin99006362004-08-07 03:52:26 +0000560 tm_s.tm_sec -= gmtoff;
561
562 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
563
Lev Walkin02a31552004-08-12 03:52:53 +0000564 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000565 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000566 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000567 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000568 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000569 * we can only guess that it is a local zone.
570 * Interpret it in this fashion.
571 */
572 tloc = mktime(&tm_s);
573 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 if(tloc == -1) {
575 errno = EINVAL;
576 return -1;
577 }
578
Lev Walkin99006362004-08-07 03:52:26 +0000579 if(ret_tm) {
580 if(as_gmt) {
581 if(offset_specified) {
582 *ret_tm = tm_s;
583 } else {
584 if(gmtime_r(&tloc, ret_tm) == 0) {
585 errno = EINVAL;
586 return -1;
587 }
588 }
589 } else {
590 if(localtime_r(&tloc, ret_tm) == 0) {
591 errno = EINVAL;
592 return -1;
593 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000594 }
595 }
596
Lev Walkin535612a2005-07-03 05:32:40 +0000597 /* Fractions of seconds */
598 if(frac_value) *frac_value = fvalue;
Lev Walkin3a522782005-07-04 12:21:51 +0000599 if(frac_digits) *frac_digits = fdigits;
Lev Walkin535612a2005-07-03 05:32:40 +0000600
Lev Walkinf15320b2004-06-03 03:38:44 +0000601 return tloc;
602}
603
Lev Walkin99006362004-08-07 03:52:26 +0000604GeneralizedTime_t *
605asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
Lev Walkin535612a2005-07-03 05:32:40 +0000606 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
607}
608
609GeneralizedTime_t *
Lev Walkin3a522782005-07-04 12:21:51 +0000610asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, int frac_value, int frac_digits, int force_gmt) {
Lev Walkin99006362004-08-07 03:52:26 +0000611 struct tm tm_s;
612 long gmtoff;
Lev Walkin535612a2005-07-03 05:32:40 +0000613 const unsigned int buf_size =
614 4 + 2 + 2 /* yyyymmdd */
615 + 2 + 2 + 2 /* hhmmss */
616 + 1 + 6 /* .ffffff */
617 + 1 + 4 /* +hhmm */
618 + 1 /* '\0' */
619 ;
Lev Walkin99006362004-08-07 03:52:26 +0000620 char *buf;
621 char *p;
622 int size;
623
624 /* Check arguments */
625 if(!tm) {
626 errno = EINVAL;
627 return 0;
628 }
629
630 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000631 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000632 if(!buf) return 0;
633
634 gmtoff = GMTOFF(*tm);
635
636 if(force_gmt && gmtoff) {
637 tm_s = *tm;
638 tm_s.tm_sec -= gmtoff;
639 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000640 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000641#ifdef HAVE_TM_GMTOFF
642 assert(!GMTOFF(tm_s)); /* Will fix itself */
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000643#else /* !HAVE_TM_GMTOFF */
644 gmtoff = 0;
Lev Walkin659a7512004-08-11 07:35:08 +0000645#endif
Lev Walkin99006362004-08-07 03:52:26 +0000646 }
647
648 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
649 tm->tm_year + 1900,
650 tm->tm_mon + 1,
651 tm->tm_mday,
652 tm->tm_hour,
653 tm->tm_min,
654 tm->tm_sec
655 );
Lev Walkin535612a2005-07-03 05:32:40 +0000656 if(size != 14) {
657 /* Could be assert(size == 14); */
658 FREEMEM(buf);
659 errno = EINVAL;
660 return 0;
661 }
Lev Walkin99006362004-08-07 03:52:26 +0000662
663 p = buf + size;
Lev Walkin535612a2005-07-03 05:32:40 +0000664
665 /*
666 * Deal with fractions.
667 */
Lev Walkin3a522782005-07-04 12:21:51 +0000668 if(frac_value > 0 && frac_digits > 0) {
Lev Walkin535612a2005-07-03 05:32:40 +0000669 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
Lev Walkin4b4c1462005-07-04 01:44:01 +0000670 char *z = p;
Lev Walkin3a522782005-07-04 12:21:51 +0000671 long fbase;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000672 *z++ = '.';
Lev Walkin3a522782005-07-04 12:21:51 +0000673
674 /* Place bounds on precision */
675 while(frac_digits-- > 6)
676 frac_value /= 10;
677
678 /* emulate fbase = pow(10, frac_digits) */
679 for(fbase = 1; frac_digits--;)
680 fbase *= 10;
681
Lev Walkin535612a2005-07-03 05:32:40 +0000682 do {
Lev Walkin3a522782005-07-04 12:21:51 +0000683 int digit = frac_value / fbase;
Lev Walkin88e0a772005-07-04 02:29:36 +0000684 if(digit > 9) { z = 0; break; }
Lev Walkin4b4c1462005-07-04 01:44:01 +0000685 *z++ = digit + 0x30;
Lev Walkin3a522782005-07-04 12:21:51 +0000686 frac_value %= fbase;
687 fbase /= 10;
688 } while(fbase > 0 && frac_value > 0 && z < end);
689 if(z) {
Lev Walkin88e0a772005-07-04 02:29:36 +0000690 for(--z; *z == 0x30; --z); /* Strip zeroes */
691 p = z + (*z != '.');
692 size = p - buf;
693 }
Lev Walkin535612a2005-07-03 05:32:40 +0000694 }
695
Lev Walkin99006362004-08-07 03:52:26 +0000696 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000697 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000698 *p++ = 0;
699 size++;
700 } else {
Lev Walkin535612a2005-07-03 05:32:40 +0000701 int ret;
702 gmtoff %= 86400;
703 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
Lev Walkina4347cc2010-03-10 23:05:56 +0000704 gmtoff / 3600, labs(gmtoff % 3600) / 60);
Lev Walkin535612a2005-07-03 05:32:40 +0000705 if(ret != 5) {
706 FREEMEM(buf);
707 errno = EINVAL;
708 return 0;
709 }
Lev Walkin99006362004-08-07 03:52:26 +0000710 size += ret;
711 }
712
713 if(opt_gt) {
714 if(opt_gt->buf)
715 FREEMEM(opt_gt->buf);
716 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000717 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin419f6752006-09-13 04:02:00 +0000718 if(!opt_gt) { FREEMEM(buf); return 0; }
Lev Walkin99006362004-08-07 03:52:26 +0000719 }
720
Lev Walkin4d9528c2004-08-11 08:10:13 +0000721 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000722 opt_gt->size = size;
723
724 return opt_gt;
725}
726
Lev Walkina5972be2017-09-29 23:15:58 -0700727asn_random_fill_result_t
728GeneralizedTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
729 const asn_encoding_constraints_t *constraints,
730 size_t max_length) {
731 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
732 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
733 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
734 static const char *values[] = {
735 "19700101000000", "19700101000000-0000", "19700101000000+0000",
736 "19700101000000Z", "19700101000000.3Z", "19821106210623.3",
737 "19821106210629.3Z", "19691106210827.3-0500", "19821106210629.456",
738 };
739 size_t rnd = asn_random_between(0, sizeof(values)/sizeof(values[0])-1);
Lev Walkin99006362004-08-07 03:52:26 +0000740
Lev Walkina5972be2017-09-29 23:15:58 -0700741 (void)constraints;
742
Lev Walkindd01a5e2017-10-14 01:45:26 -0700743 if(max_length < sizeof("yyyymmddhhmmss") && !*sptr) {
Lev Walkina5972be2017-09-29 23:15:58 -0700744 return result_skipped;
745 }
746
747 if(*sptr) {
748 if(OCTET_STRING_fromBuf(*sptr, values[rnd], -1) != 0) {
749 if(!sptr) return result_failed;
750 }
751 } else {
752 *sptr = OCTET_STRING_new_fromBuf(td, values[rnd], -1);
753 if(!sptr) return result_failed;
754 }
755
756 return result_ok;
757}
Lev Walkindd01a5e2017-10-14 01:45:26 -0700758
759int
760GeneralizedTime_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
761 const void *bptr) {
762 const GeneralizedTime_t *a = aptr;
763 const GeneralizedTime_t *b = bptr;
764
765 (void)td;
766
767 if(a && b) {
768 int afrac_value, afrac_digits;
769 int bfrac_value, bfrac_digits;
Lev Walkine4a16252017-10-16 15:09:04 -0700770 int aerr, berr;
771 time_t at, bt;
772
773 errno = EPERM;
774 at = asn_GT2time_frac(a, &afrac_value, &afrac_digits, 0, 0);
775 aerr = errno;
776 errno = EPERM;
777 bt = asn_GT2time_frac(b, &bfrac_value, &bfrac_digits, 0, 0);
778 berr = errno;
779
780 if(at == -1 && aerr != EPERM) {
781 if(bt == -1 && berr != EPERM) {
782 return OCTET_STRING_compare(td, aptr, bptr);
783 } else {
784 return -1;
785 }
786 } else if(bt == -1 && berr != EPERM) {
787 return 1;
788 } else {
789 /* Both values are valid. */
790 }
791
Lev Walkindd01a5e2017-10-14 01:45:26 -0700792 if(at < bt) {
793 return -1;
794 } else if(at > bt) {
795 return 1;
796 } else if(afrac_digits == bfrac_digits) {
797 if(afrac_value == bfrac_value) {
798 return 0;
799 }
800 if(afrac_value < bfrac_value) {
801 return -1;
802 } else {
803 return 1;
804 }
805 } else if(afrac_digits == 0) {
806 return -1;
807 } else if(bfrac_digits == 0) {
808 return 1;
809 } else {
810 double afrac = (double)afrac_value / afrac_digits;
811 double bfrac = (double)bfrac_value / bfrac_digits;
812 if(afrac < bfrac) {
813 return -1;
814 } else if(afrac > bfrac) {
815 return 1;
816 } else {
817 return 0;
818 }
819 }
820 } else if(!a && !b) {
821 return 0;
822 } else if(!a) {
823 return -1;
824 } else {
825 return 1;
826 }
827
828}
829