blob: 029a43e4d464a932802efdac39f9c110d20ccd2e [file] [log] [blame]
vlmfa67ddc2004-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 */
5#include <UTCTime.h>
6#include <GeneralizedTime.h>
7#include <time.h>
8#include <errno.h>
9#include <assert.h>
10
11#ifndef __NO_ASN_TABLE__
12
13/*
14 * UTCTime basic type description.
15 */
16static ber_tlv_tag_t asn1_DEF_UTCTime_tags[] = {
17 (ASN_TAG_CLASS_UNIVERSAL | (23 << 2))
18};
19asn1_TYPE_descriptor_t asn1_DEF_UTCTime = {
20 "UTCTime",
21 UTCTime_constraint,
22 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
23 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
24 UTCTime_print,
25 OCTET_STRING_free,
26 0, /* Use generic outmost tag fetcher */
27 asn1_DEF_UTCTime_tags,
28 sizeof(asn1_DEF_UTCTime_tags)
29 / sizeof(asn1_DEF_UTCTime_tags[0]),
vlm72425de2004-09-13 08:31:01 +000030 asn1_DEF_UTCTime_tags, /* Same as above */
31 sizeof(asn1_DEF_UTCTime_tags)
32 / sizeof(asn1_DEF_UTCTime_tags[0]),
vlmfa67ddc2004-06-03 03:38:44 +000033 -1, /* Both ways are fine */
vlme413c122004-08-20 13:23:42 +000034 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000035 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000036};
37
38#endif /* __NO_ASN_TABLE__ */
39
40/*
41 * Check that the time looks like the time.
42 */
43int
44UTCTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
45 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +000046 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000047 time_t tloc;
48
49 errno = EPERM; /* Just an unlikely error code */
vlmd0c608e2004-08-07 03:51:43 +000050 tloc = asn_UT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +000051 if(tloc == -1 && errno != EPERM) {
vlme3f0f282004-08-11 09:44:13 +000052 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000053 "%s: Invalid time format: %s (%s:%d)",
54 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000055 return -1;
56 }
57
58 return 0;
59}
60
61int
62UTCTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
63 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +000064 const UTCTime_t *st = (const UTCTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000065
vlmb42843a2004-06-05 08:17:50 +000066 (void)td; /* Unused argument */
67 (void)ilevel; /* Unused argument */
68
vlmfa67ddc2004-06-03 03:38:44 +000069 if(st && st->buf) {
70 char buf[32];
71 struct tm tm;
72 int ret;
73
74 errno = EPERM;
vlmd0c608e2004-08-07 03:51:43 +000075 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
vlmfa67ddc2004-06-03 03:38:44 +000076 return cb("<bad-value>", 11, app_key);
77
78 ret = snprintf(buf, sizeof(buf),
vlmd0c608e2004-08-07 03:51:43 +000079 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +000080 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
81 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +000082 assert(ret > 0 && ret < (int)sizeof(buf));
vlmfa67ddc2004-06-03 03:38:44 +000083 return cb(buf, ret, app_key);
84 } else {
85 return cb("<absent>", 8, app_key);
86 }
87}
88
89time_t
vlmd0c608e2004-08-07 03:51:43 +000090asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +000091 char buf[17+2]; /* "AAMMJJhhmmss+hhmm" = 17, + 2 = 19 */
92 GeneralizedTime_t gt;
93
vlmb42843a2004-06-05 08:17:50 +000094 if(!st || !st->buf
95 || st->size < 11 || st->size > ((int)sizeof(buf) - 2)) {
vlmfa67ddc2004-06-03 03:38:44 +000096 errno = EINVAL;
97 return -1;
98 }
99
vlm1ff928d2004-08-11 08:10:13 +0000100 gt.buf = (unsigned char *)buf;
vlmfa67ddc2004-06-03 03:38:44 +0000101 gt.size = st->size + 2;
102 memcpy(gt.buf + 2, st->buf, st->size);
103 if(st->buf[0] > 0x35) {
104 /* 19xx */
105 gt.buf[0] = 0x31;
106 gt.buf[1] = 0x39;
107 } else {
108 /* 20xx */
109 gt.buf[0] = 0x32;
110 gt.buf[1] = 0x30;
111 }
112
vlmd0c608e2004-08-07 03:51:43 +0000113 return asn_GT2time(&gt, _tm, as_gmt);
vlmfa67ddc2004-06-03 03:38:44 +0000114}
vlmd0c608e2004-08-07 03:51:43 +0000115
116UTCTime_t *
117asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
118 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
119
120 gt = asn_time2GT(gt, tm, force_gmt);
121 if(gt == 0) return 0;
122
123 assert(gt->size >= 2);
124 gt->size -= 2;
125 memmove(gt->buf, gt->buf + 2, gt->size + 1);
126
127 return (UTCTime_t *)gt;
128}
129