blob: f884eebcaa6fe0b0f7bb328c003062b0fd8b8796 [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>
8#include <time.h>
9#include <errno.h>
10#include <assert.h>
11
12#ifndef __NO_ASN_TABLE__
13
14/*
15 * UTCTime basic type description.
16 */
17static ber_tlv_tag_t asn1_DEF_UTCTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +000018 (ASN_TAG_CLASS_UNIVERSAL | (23 << 2)), /* [UNIVERSAL 23] IMPLICIT ...*/
19 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
20 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000021};
22asn1_TYPE_descriptor_t asn1_DEF_UTCTime = {
23 "UTCTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 OCTET_STRING_free,
25 UTCTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 UTCTime_constraint,
27 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
28 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkina9cc46e2004-09-22 16:06:28 +000029 0, /* Not implemented yet */
30 UTCTime_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000031 0, /* Use generic outmost tag fetcher */
32 asn1_DEF_UTCTime_tags,
33 sizeof(asn1_DEF_UTCTime_tags)
Lev Walkin8e8078a2004-09-26 13:10:40 +000034 / sizeof(asn1_DEF_UTCTime_tags[0]) - 2,
35 asn1_DEF_UTCTime_tags,
Lev Walkin188ed2c2004-09-13 08:31:01 +000036 sizeof(asn1_DEF_UTCTime_tags)
37 / sizeof(asn1_DEF_UTCTime_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000038 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000039 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000040};
41
42#endif /* __NO_ASN_TABLE__ */
43
44/*
45 * Check that the time looks like the time.
46 */
47int
48UTCTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
49 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000050 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000051 time_t tloc;
52
53 errno = EPERM; /* Just an unlikely error code */
Lev Walkin0aca3852004-08-07 03:51:43 +000054 tloc = asn_UT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000055 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +000056 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000057 "%s: Invalid time format: %s (%s:%d)",
58 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000059 return -1;
60 }
61
62 return 0;
63}
64
Lev Walkina9cc46e2004-09-22 16:06:28 +000065asn_enc_rval_t
66UTCTime_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
67 int ilevel, enum xer_encoder_flags_e flags,
68 asn_app_consume_bytes_f *cb, void *app_key) {
69 OCTET_STRING_t st;
70
71 if(flags & XER_F_CANONICAL) {
72 char buf[32];
73 struct tm tm;
74 ssize_t ret;
75
76 errno = EPERM;
77 if(asn_UT2time((UTCTime_t *)sptr, &tm, 1) == -1
78 && errno != EPERM)
79 _ASN_ENCODE_FAILED;
80
81 ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
82 tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday,
83 tm.tm_hour, tm.tm_min, tm.tm_sec);
84 assert(ret > 0 && ret < (int)sizeof(buf));
85
86 st.buf = (uint8_t *)buf;
87 st.size = ret;
88 sptr = &st;
89 }
90
91 return OCTET_STRING_encode_xer_ascii(td, sptr, ilevel, flags,
92 cb, app_key);
93}
94
Lev Walkinf15320b2004-06-03 03:38:44 +000095int
96UTCTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
97 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000098 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000099
Lev Walkind9bd7752004-06-05 08:17:50 +0000100 (void)td; /* Unused argument */
101 (void)ilevel; /* Unused argument */
102
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 if(st && st->buf) {
104 char buf[32];
105 struct tm tm;
106 int ret;
107
108 errno = EPERM;
Lev Walkin0aca3852004-08-07 03:51:43 +0000109 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000110 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000111
112 ret = snprintf(buf, sizeof(buf),
Lev Walkin0aca3852004-08-07 03:51:43 +0000113 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
115 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000116 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000117 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000119 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 }
121}
122
123time_t
Lev Walkin0aca3852004-08-07 03:51:43 +0000124asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 char buf[17+2]; /* "AAMMJJhhmmss+hhmm" = 17, + 2 = 19 */
126 GeneralizedTime_t gt;
127
Lev Walkind9bd7752004-06-05 08:17:50 +0000128 if(!st || !st->buf
129 || st->size < 11 || st->size > ((int)sizeof(buf) - 2)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 errno = EINVAL;
131 return -1;
132 }
133
Lev Walkin4d9528c2004-08-11 08:10:13 +0000134 gt.buf = (unsigned char *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 gt.size = st->size + 2;
136 memcpy(gt.buf + 2, st->buf, st->size);
137 if(st->buf[0] > 0x35) {
138 /* 19xx */
139 gt.buf[0] = 0x31;
140 gt.buf[1] = 0x39;
141 } else {
142 /* 20xx */
143 gt.buf[0] = 0x32;
144 gt.buf[1] = 0x30;
145 }
146
Lev Walkin0aca3852004-08-07 03:51:43 +0000147 return asn_GT2time(&gt, _tm, as_gmt);
Lev Walkinf15320b2004-06-03 03:38:44 +0000148}
Lev Walkin0aca3852004-08-07 03:51:43 +0000149
150UTCTime_t *
151asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
152 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
153
154 gt = asn_time2GT(gt, tm, force_gmt);
155 if(gt == 0) return 0;
156
157 assert(gt->size >= 2);
158 gt->size -= 2;
159 memmove(gt->buf, gt->buf + 2, gt->size + 1);
160
161 return (UTCTime_t *)gt;
162}
163