vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 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 | */ |
| 16 | static ber_tlv_tag_t asn1_DEF_UTCTime_tags[] = { |
| 17 | (ASN_TAG_CLASS_UNIVERSAL | (23 << 2)) |
| 18 | }; |
| 19 | asn1_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 */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 32 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 33 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | #endif /* __NO_ASN_TABLE__ */ |
| 37 | |
| 38 | /* |
| 39 | * Check that the time looks like the time. |
| 40 | */ |
| 41 | int |
| 42 | UTCTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 43 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 44 | const UTCTime_t *st = (const UTCTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | time_t tloc; |
| 46 | |
| 47 | errno = EPERM; /* Just an unlikely error code */ |
vlm | d0c608e | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 48 | tloc = asn_UT2time(st, 0, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | if(tloc == -1 && errno != EPERM) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 50 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 51 | "%s: Invalid time format: %s (%s:%d)", |
| 52 | td->name, strerror(errno), __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int |
| 60 | UTCTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 61 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 62 | const UTCTime_t *st = (const UTCTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 63 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 64 | (void)td; /* Unused argument */ |
| 65 | (void)ilevel; /* Unused argument */ |
| 66 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | if(st && st->buf) { |
| 68 | char buf[32]; |
| 69 | struct tm tm; |
| 70 | int ret; |
| 71 | |
| 72 | errno = EPERM; |
vlm | d0c608e | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 73 | if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | return cb("<bad-value>", 11, app_key); |
| 75 | |
| 76 | ret = snprintf(buf, sizeof(buf), |
vlm | d0c608e | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 77 | "%04d-%02d-%02d %02d:%02d%02d (GMT)", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 78 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 79 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 80 | assert(ret > 0 && ret < (int)sizeof(buf)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | return cb(buf, ret, app_key); |
| 82 | } else { |
| 83 | return cb("<absent>", 8, app_key); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | time_t |
vlm | d0c608e | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 88 | asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 89 | char buf[17+2]; /* "AAMMJJhhmmss+hhmm" = 17, + 2 = 19 */ |
| 90 | GeneralizedTime_t gt; |
| 91 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 92 | if(!st || !st->buf |
| 93 | || st->size < 11 || st->size > ((int)sizeof(buf) - 2)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | errno = EINVAL; |
| 95 | return -1; |
| 96 | } |
| 97 | |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 98 | gt.buf = (unsigned char *)buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | gt.size = st->size + 2; |
| 100 | memcpy(gt.buf + 2, st->buf, st->size); |
| 101 | if(st->buf[0] > 0x35) { |
| 102 | /* 19xx */ |
| 103 | gt.buf[0] = 0x31; |
| 104 | gt.buf[1] = 0x39; |
| 105 | } else { |
| 106 | /* 20xx */ |
| 107 | gt.buf[0] = 0x32; |
| 108 | gt.buf[1] = 0x30; |
| 109 | } |
| 110 | |
vlm | d0c608e | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 111 | return asn_GT2time(>, _tm, as_gmt); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | } |
vlm | d0c608e | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 113 | |
| 114 | UTCTime_t * |
| 115 | asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) { |
| 116 | GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut; |
| 117 | |
| 118 | gt = asn_time2GT(gt, tm, force_gmt); |
| 119 | if(gt == 0) return 0; |
| 120 | |
| 121 | assert(gt->size >= 2); |
| 122 | gt->size -= 2; |
| 123 | memmove(gt->buf, gt->buf + 2, gt->size + 1); |
| 124 | |
| 125 | return (UTCTime_t *)gt; |
| 126 | } |
| 127 | |