blob: 61caf2ead74bed1409b2fb59150cf95f1c4cc340 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
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 Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <UTCTime.h>
7#include <GeneralizedTime.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <errno.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009
Lev Walkin223000a2006-09-13 00:21:58 +000010#ifdef __CYGWIN__
11#include "/usr/include/time.h"
12#else
13#include <time.h>
14#endif /* __CYGWIN__ */
15
Lev Walkin7c1dc052016-03-14 03:08:15 -070016#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkinf15320b2004-06-03 03:38:44 +000017
18/*
19 * UTCTime basic type description.
20 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070021static const ber_tlv_tag_t asn_DEF_UTCTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +000022 (ASN_TAG_CLASS_UNIVERSAL | (23 << 2)), /* [UNIVERSAL 23] IMPLICIT ...*/
23 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
24 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000025};
Lev Walkin725883b2006-10-09 12:07:58 +000026static asn_per_constraints_t asn_DEF_UTCTime_constraints = {
27 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
28 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
29 0, 0
30};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080031asn_TYPE_operation_t asn_OP_UTCTime = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000032 OCTET_STRING_free,
33 UTCTime_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070034 OCTET_STRING_compare, /* Does not deal with time zones. */
Lev Walkinf15320b2004-06-03 03:38:44 +000035 UTCTime_constraint,
36 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
37 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000038 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +000039 UTCTime_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070040#ifdef ASN_DISABLE_OER_SUPPORT
41 0,
42 0,
43#else
44 0,
45 0,
46#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040047#ifdef ASN_DISABLE_PER_SUPPORT
48 0,
49 0,
50#else
51 OCTET_STRING_decode_uper,
52 OCTET_STRING_encode_uper,
53#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080054 0 /* Use generic outmost tag fetcher */
55};
56asn_TYPE_descriptor_t asn_DEF_UTCTime = {
57 "UTCTime",
58 "UTCTime",
59 &asn_OP_UTCTime,
60 UTCTime_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000061 asn_DEF_UTCTime_tags,
62 sizeof(asn_DEF_UTCTime_tags)
63 / sizeof(asn_DEF_UTCTime_tags[0]) - 2,
64 asn_DEF_UTCTime_tags,
65 sizeof(asn_DEF_UTCTime_tags)
66 / sizeof(asn_DEF_UTCTime_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070067 0, /* No OER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +000068 &asn_DEF_UTCTime_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +000069 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000070 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000071};
72
Lev Walkin7c1dc052016-03-14 03:08:15 -070073#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkinf15320b2004-06-03 03:38:44 +000074
75/*
76 * Check that the time looks like the time.
77 */
78int
Lev Walkin5e033762004-09-29 13:26:15 +000079UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000080 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000081 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000082 time_t tloc;
83
84 errno = EPERM; /* Just an unlikely error code */
Lev Walkin0aca3852004-08-07 03:51:43 +000085 tloc = asn_UT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000086 if(tloc == -1 && errno != EPERM) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070087 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000088 "%s: Invalid time format: %s (%s:%d)",
89 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000090 return -1;
91 }
92
93 return 0;
94}
95
Lev Walkin7c1dc052016-03-14 03:08:15 -070096#ifndef ASN___INTERNAL_TEST_MODE
Lev Walkin7b3713a2005-07-03 05:29:36 +000097
Lev Walkina9cc46e2004-09-22 16:06:28 +000098asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000099UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000100 int ilevel, enum xer_encoder_flags_e flags,
101 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000102
103 if(flags & XER_F_CANONICAL) {
Lev Walkin7b3713a2005-07-03 05:29:36 +0000104 asn_enc_rval_t rv;
105 UTCTime_t *ut;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000106 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000107
108 errno = EPERM;
109 if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1
110 && errno != EPERM)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700111 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000112
Lev Walkin7b3713a2005-07-03 05:29:36 +0000113 /* Fractions are not allowed in UTCTime */
114 ut = asn_time2GT(0, 0, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700115 if(!ut) ASN__ENCODE_FAILED;
Lev Walkin7b3713a2005-07-03 05:29:36 +0000116
117 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
118 cb, app_key);
119 OCTET_STRING_free(&asn_DEF_UTCTime, ut, 0);
120 return rv;
121 } else {
122 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
123 cb, app_key);
124 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000125}
126
Lev Walkin7c1dc052016-03-14 03:08:15 -0700127#endif /* ASN___INTERNAL_TEST_MODE */
Lev Walkin7b3713a2005-07-03 05:29:36 +0000128
Lev Walkinf15320b2004-06-03 03:38:44 +0000129int
Lev Walkin5e033762004-09-29 13:26:15 +0000130UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000131 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000132 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000133
Lev Walkind9bd7752004-06-05 08:17:50 +0000134 (void)td; /* Unused argument */
135 (void)ilevel; /* Unused argument */
136
Lev Walkinf15320b2004-06-03 03:38:44 +0000137 if(st && st->buf) {
138 char buf[32];
139 struct tm tm;
140 int ret;
141
142 errno = EPERM;
Lev Walkin0aca3852004-08-07 03:51:43 +0000143 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000144 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000145
146 ret = snprintf(buf, sizeof(buf),
Lev Walkin7b3713a2005-07-03 05:29:36 +0000147 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
149 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000150 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000151 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000153 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 }
155}
156
157time_t
Lev Walkin0aca3852004-08-07 03:51:43 +0000158asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
Lev Walkin7b3713a2005-07-03 05:29:36 +0000159 char buf[24]; /* "AAMMJJhhmmss+hhmm" + cushion */
Lev Walkinf15320b2004-06-03 03:38:44 +0000160 GeneralizedTime_t gt;
161
Lev Walkind9bd7752004-06-05 08:17:50 +0000162 if(!st || !st->buf
Lev Walkin7b3713a2005-07-03 05:29:36 +0000163 || st->size < 11 || st->size >= ((int)sizeof(buf) - 2)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 errno = EINVAL;
165 return -1;
166 }
167
Lev Walkin4d9528c2004-08-11 08:10:13 +0000168 gt.buf = (unsigned char *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 gt.size = st->size + 2;
170 memcpy(gt.buf + 2, st->buf, st->size);
171 if(st->buf[0] > 0x35) {
172 /* 19xx */
173 gt.buf[0] = 0x31;
174 gt.buf[1] = 0x39;
175 } else {
176 /* 20xx */
177 gt.buf[0] = 0x32;
178 gt.buf[1] = 0x30;
179 }
180
Lev Walkin0aca3852004-08-07 03:51:43 +0000181 return asn_GT2time(&gt, _tm, as_gmt);
Lev Walkinf15320b2004-06-03 03:38:44 +0000182}
Lev Walkin0aca3852004-08-07 03:51:43 +0000183
184UTCTime_t *
185asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
186 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
187
188 gt = asn_time2GT(gt, tm, force_gmt);
189 if(gt == 0) return 0;
190
191 assert(gt->size >= 2);
192 gt->size -= 2;
193 memmove(gt->buf, gt->buf + 2, gt->size + 1);
194
195 return (UTCTime_t *)gt;
196}
197