blob: 7d9711732572ff8005cabbd03dfea2634329bce1 [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 */
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]),
30 1, /* Single UNIVERSAL tag may be implicitly overriden */
31 -1, /* Both ways are fine */
Lev Walkind9bd7752004-06-05 08:17:50 +000032 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000033};
34
35#endif /* __NO_ASN_TABLE__ */
36
37/*
38 * Check that the time looks like the time.
39 */
40int
41UTCTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
42 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000043 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000044 time_t tloc;
45
46 errno = EPERM; /* Just an unlikely error code */
Lev Walkin0aca3852004-08-07 03:51:43 +000047 tloc = asn_UT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000048 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +000049 _ASN_ERRLOG(app_errlog, app_key,
50 "%s: Invalid time format: %s",
Lev Walkinf15320b2004-06-03 03:38:44 +000051 td->name, strerror(errno));
52 return -1;
53 }
54
55 return 0;
56}
57
58int
59UTCTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
60 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000061 const UTCTime_t *st = (const UTCTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000062
Lev Walkind9bd7752004-06-05 08:17:50 +000063 (void)td; /* Unused argument */
64 (void)ilevel; /* Unused argument */
65
Lev Walkinf15320b2004-06-03 03:38:44 +000066 if(st && st->buf) {
67 char buf[32];
68 struct tm tm;
69 int ret;
70
71 errno = EPERM;
Lev Walkin0aca3852004-08-07 03:51:43 +000072 if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkinf15320b2004-06-03 03:38:44 +000073 return cb("<bad-value>", 11, app_key);
74
75 ret = snprintf(buf, sizeof(buf),
Lev Walkin0aca3852004-08-07 03:51:43 +000076 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +000077 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
78 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +000079 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkinf15320b2004-06-03 03:38:44 +000080 return cb(buf, ret, app_key);
81 } else {
82 return cb("<absent>", 8, app_key);
83 }
84}
85
86time_t
Lev Walkin0aca3852004-08-07 03:51:43 +000087asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +000088 char buf[17+2]; /* "AAMMJJhhmmss+hhmm" = 17, + 2 = 19 */
89 GeneralizedTime_t gt;
90
Lev Walkind9bd7752004-06-05 08:17:50 +000091 if(!st || !st->buf
92 || st->size < 11 || st->size > ((int)sizeof(buf) - 2)) {
Lev Walkinf15320b2004-06-03 03:38:44 +000093 errno = EINVAL;
94 return -1;
95 }
96
Lev Walkin4d9528c2004-08-11 08:10:13 +000097 gt.buf = (unsigned char *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +000098 gt.size = st->size + 2;
99 memcpy(gt.buf + 2, st->buf, st->size);
100 if(st->buf[0] > 0x35) {
101 /* 19xx */
102 gt.buf[0] = 0x31;
103 gt.buf[1] = 0x39;
104 } else {
105 /* 20xx */
106 gt.buf[0] = 0x32;
107 gt.buf[1] = 0x30;
108 }
109
Lev Walkin0aca3852004-08-07 03:51:43 +0000110 return asn_GT2time(&gt, _tm, as_gmt);
Lev Walkinf15320b2004-06-03 03:38:44 +0000111}
Lev Walkin0aca3852004-08-07 03:51:43 +0000112
113UTCTime_t *
114asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
115 GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
116
117 gt = asn_time2GT(gt, tm, force_gmt);
118 if(gt == 0) return 0;
119
120 assert(gt->size >= 2);
121 gt->size -= 2;
122 memmove(gt->buf, gt->buf + 2, gt->size + 1);
123
124 return (UTCTime_t *)gt;
125}
126