blob: 98a4c15ba929d538c7b4ccfb16f116f251e5c879 [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
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 <asn_internal.h>
6#include <UTCTime.h>
7#include <GeneralizedTime.h>
8#include <errno.h>
9
10#ifdef __CYGWIN__
11#include "/usr/include/time.h"
12#else
13#include <time.h>
14#endif /* __CYGWIN__ */
15
16#ifndef __ASN_INTERNAL_TEST_MODE__
17
18/*
19 * UTCTime basic type description.
20 */
Harald Welte41b85d52015-08-31 08:56:53 +020021static const ber_tlv_tag_t asn_DEF_UTCTime_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020022 (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 */
25};
Harald Welteec0e2172010-07-20 00:03:44 +020026static 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};
Harald Welte92c45f32010-06-12 18:59:38 +020031asn_TYPE_descriptor_t asn_DEF_UTCTime = {
32 "UTCTime",
33 "UTCTime",
34 OCTET_STRING_free,
35 UTCTime_print,
36 UTCTime_constraint,
37 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
38 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
39 OCTET_STRING_decode_xer_utf8,
40 UTCTime_encode_xer,
Harald Welteec0e2172010-07-20 00:03:44 +020041 OCTET_STRING_decode_uper,
42 OCTET_STRING_encode_uper,
Harald Welte41b85d52015-08-31 08:56:53 +020043 OCTET_STRING_decode_aper,
44 OCTET_STRING_encode_aper,
Harald Welte92c45f32010-06-12 18:59:38 +020045 0, /* Use generic outmost tag fetcher */
46 asn_DEF_UTCTime_tags,
47 sizeof(asn_DEF_UTCTime_tags)
48 / sizeof(asn_DEF_UTCTime_tags[0]) - 2,
49 asn_DEF_UTCTime_tags,
50 sizeof(asn_DEF_UTCTime_tags)
51 / sizeof(asn_DEF_UTCTime_tags[0]),
Harald Welteec0e2172010-07-20 00:03:44 +020052 &asn_DEF_UTCTime_constraints,
Harald Welte92c45f32010-06-12 18:59:38 +020053 0, 0, /* No members */
54 0 /* No specifics */
55};
56
57#endif /* __ASN_INTERNAL_TEST_MODE__ */
58
59/*
60 * Check that the time looks like the time.
61 */
62int
63UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
64 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
65 const UTCTime_t *st = (const UTCTime_t *)sptr;
66 time_t tloc;
67
68 errno = EPERM; /* Just an unlikely error code */
69 tloc = asn_UT2time(st, 0, 0);
70 if(tloc == -1 && errno != EPERM) {
Harald Welteec0e2172010-07-20 00:03:44 +020071 _ASN_CTFAIL(app_key, td, sptr,
Harald Welte92c45f32010-06-12 18:59:38 +020072 "%s: Invalid time format: %s (%s:%d)",
73 td->name, strerror(errno), __FILE__, __LINE__);
74 return -1;
75 }
76
77 return 0;
78}
79
80#ifndef __ASN_INTERNAL_TEST_MODE__
81
82asn_enc_rval_t
83UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
84 int ilevel, enum xer_encoder_flags_e flags,
85 asn_app_consume_bytes_f *cb, void *app_key) {
86
87 if(flags & XER_F_CANONICAL) {
88 asn_enc_rval_t rv;
89 UTCTime_t *ut;
90 struct tm tm;
91
92 errno = EPERM;
93 if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1
94 && errno != EPERM)
95 _ASN_ENCODE_FAILED;
96
97 /* Fractions are not allowed in UTCTime */
98 ut = asn_time2GT(0, 0, 1);
99 if(!ut) _ASN_ENCODE_FAILED;
100
101 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
102 cb, app_key);
103 OCTET_STRING_free(&asn_DEF_UTCTime, ut, 0);
104 return rv;
105 } else {
106 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
107 cb, app_key);
108 }
109}
110
111#endif /* __ASN_INTERNAL_TEST_MODE__ */
112
113int
114UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
115 asn_app_consume_bytes_f *cb, void *app_key) {
116 const UTCTime_t *st = (const UTCTime_t *)sptr;
117
118 (void)td; /* Unused argument */
119 (void)ilevel; /* Unused argument */
120
121 if(st && st->buf) {
122 char buf[32];
123 struct tm tm;
124 int ret;
125
126 errno = EPERM;
127 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
128 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
129
130 ret = snprintf(buf, sizeof(buf),
131 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
132 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
133 tm.tm_hour, tm.tm_min, tm.tm_sec);
134 assert(ret > 0 && ret < (int)sizeof(buf));
135 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
136 } else {
137 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
138 }
139}
140
141time_t
142asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
143 char buf[24]; /* "AAMMJJhhmmss+hhmm" + cushion */
144 GeneralizedTime_t gt;
145
146 if(!st || !st->buf
147 || st->size < 11 || st->size >= ((int)sizeof(buf) - 2)) {
148 errno = EINVAL;
149 return -1;
150 }
151
152 gt.buf = (unsigned char *)buf;
153 gt.size = st->size + 2;
154 memcpy(gt.buf + 2, st->buf, st->size);
155 if(st->buf[0] > 0x35) {
156 /* 19xx */
157 gt.buf[0] = 0x31;
158 gt.buf[1] = 0x39;
159 } else {
160 /* 20xx */
161 gt.buf[0] = 0x32;
162 gt.buf[1] = 0x30;
163 }
164
165 return asn_GT2time(&gt, _tm, as_gmt);
166}
167
168UTCTime_t *
169asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
170 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
171
172 gt = asn_time2GT(gt, tm, force_gmt);
173 if(gt == 0) return 0;
174
175 assert(gt->size >= 2);
176 gt->size -= 2;
177 memmove(gt->buf, gt->buf + 2, gt->size + 1);
178
179 return (UTCTime_t *)gt;
180}
181