blob: 2a27718b1d3782e6dd256360dbd42fb0c40bf026 [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 Walkin7b3713a2005-07-03 05:29:36 +000016#ifndef __ASN_INTERNAL_TEST_MODE__
Lev Walkinf15320b2004-06-03 03:38:44 +000017
18/*
19 * UTCTime basic type description.
20 */
Lev Walkin5e033762004-09-29 13:26:15 +000021static 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 Walkin5e033762004-09-29 13:26:15 +000026asn_TYPE_descriptor_t asn_DEF_UTCTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +000027 "UTCTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +000028 "UTCTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +000029 OCTET_STRING_free,
30 UTCTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000031 UTCTime_constraint,
32 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
33 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000034 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +000035 UTCTime_encode_xer,
Lev Walkin523de9e2006-08-18 01:34:18 +000036 0, 0,
Lev Walkinf15320b2004-06-03 03:38:44 +000037 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000038 asn_DEF_UTCTime_tags,
39 sizeof(asn_DEF_UTCTime_tags)
40 / sizeof(asn_DEF_UTCTime_tags[0]) - 2,
41 asn_DEF_UTCTime_tags,
42 sizeof(asn_DEF_UTCTime_tags)
43 / sizeof(asn_DEF_UTCTime_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000044 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000045 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000046 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000047};
48
Lev Walkin7b3713a2005-07-03 05:29:36 +000049#endif /* __ASN_INTERNAL_TEST_MODE__ */
Lev Walkinf15320b2004-06-03 03:38:44 +000050
51/*
52 * Check that the time looks like the time.
53 */
54int
Lev Walkin5e033762004-09-29 13:26:15 +000055UTCTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000056 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000057 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000058 time_t tloc;
59
60 errno = EPERM; /* Just an unlikely error code */
Lev Walkin0aca3852004-08-07 03:51:43 +000061 tloc = asn_UT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000062 if(tloc == -1 && errno != EPERM) {
Lev Walkin1eded352006-07-13 11:19:01 +000063 _ASN_CTFAIL(app_key, td,
Lev Walkin16835b62004-08-22 13:47:59 +000064 "%s: Invalid time format: %s (%s:%d)",
65 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000066 return -1;
67 }
68
69 return 0;
70}
71
Lev Walkin7b3713a2005-07-03 05:29:36 +000072#ifndef __ASN_INTERNAL_TEST_MODE__
73
Lev Walkina9cc46e2004-09-22 16:06:28 +000074asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000075UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +000076 int ilevel, enum xer_encoder_flags_e flags,
77 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +000078
79 if(flags & XER_F_CANONICAL) {
Lev Walkin7b3713a2005-07-03 05:29:36 +000080 asn_enc_rval_t rv;
81 UTCTime_t *ut;
Lev Walkina9cc46e2004-09-22 16:06:28 +000082 struct tm tm;
Lev Walkina9cc46e2004-09-22 16:06:28 +000083
84 errno = EPERM;
85 if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1
86 && errno != EPERM)
87 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +000088
Lev Walkin7b3713a2005-07-03 05:29:36 +000089 /* Fractions are not allowed in UTCTime */
90 ut = asn_time2GT(0, 0, 1);
91 if(!ut) _ASN_ENCODE_FAILED;
92
93 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
94 cb, app_key);
95 OCTET_STRING_free(&asn_DEF_UTCTime, ut, 0);
96 return rv;
97 } else {
98 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
99 cb, app_key);
100 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000101}
102
Lev Walkin7b3713a2005-07-03 05:29:36 +0000103#endif /* __ASN_INTERNAL_TEST_MODE__ */
104
Lev Walkinf15320b2004-06-03 03:38:44 +0000105int
Lev Walkin5e033762004-09-29 13:26:15 +0000106UTCTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000108 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000109
Lev Walkind9bd7752004-06-05 08:17:50 +0000110 (void)td; /* Unused argument */
111 (void)ilevel; /* Unused argument */
112
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 if(st && st->buf) {
114 char buf[32];
115 struct tm tm;
116 int ret;
117
118 errno = EPERM;
Lev Walkin0aca3852004-08-07 03:51:43 +0000119 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000120 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000121
122 ret = snprintf(buf, sizeof(buf),
Lev Walkin7b3713a2005-07-03 05:29:36 +0000123 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
125 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000126 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000127 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000129 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 }
131}
132
133time_t
Lev Walkin0aca3852004-08-07 03:51:43 +0000134asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
Lev Walkin7b3713a2005-07-03 05:29:36 +0000135 char buf[24]; /* "AAMMJJhhmmss+hhmm" + cushion */
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 GeneralizedTime_t gt;
137
Lev Walkind9bd7752004-06-05 08:17:50 +0000138 if(!st || !st->buf
Lev Walkin7b3713a2005-07-03 05:29:36 +0000139 || st->size < 11 || st->size >= ((int)sizeof(buf) - 2)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 errno = EINVAL;
141 return -1;
142 }
143
Lev Walkin4d9528c2004-08-11 08:10:13 +0000144 gt.buf = (unsigned char *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 gt.size = st->size + 2;
146 memcpy(gt.buf + 2, st->buf, st->size);
147 if(st->buf[0] > 0x35) {
148 /* 19xx */
149 gt.buf[0] = 0x31;
150 gt.buf[1] = 0x39;
151 } else {
152 /* 20xx */
153 gt.buf[0] = 0x32;
154 gt.buf[1] = 0x30;
155 }
156
Lev Walkin0aca3852004-08-07 03:51:43 +0000157 return asn_GT2time(&gt, _tm, as_gmt);
Lev Walkinf15320b2004-06-03 03:38:44 +0000158}
Lev Walkin0aca3852004-08-07 03:51:43 +0000159
160UTCTime_t *
161asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
162 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
163
164 gt = asn_time2GT(gt, tm, force_gmt);
165 if(gt == 0) return 0;
166
167 assert(gt->size >= 2);
168 gt->size -= 2;
169 memmove(gt->buf, gt->buf + 2, gt->size + 1);
170
171 return (UTCTime_t *)gt;
172}
173