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