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 */ |
| 32 | }; |
| 33 | |
| 34 | #endif /* __NO_ASN_TABLE__ */ |
| 35 | |
| 36 | /* |
| 37 | * Check that the time looks like the time. |
| 38 | */ |
| 39 | int |
| 40 | UTCTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 41 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
| 42 | const UTCTime_t *st = sptr; |
| 43 | time_t tloc; |
| 44 | |
| 45 | errno = EPERM; /* Just an unlikely error code */ |
| 46 | tloc = asn_UT2time(st, 0); |
| 47 | if(tloc == -1 && errno != EPERM) { |
| 48 | _ASN_ERRLOG("%s: Invalid time format: %s", |
| 49 | td->name, strerror(errno)); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | int |
| 57 | UTCTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 58 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 59 | const UTCTime_t *st = sptr; |
| 60 | |
| 61 | if(st && st->buf) { |
| 62 | char buf[32]; |
| 63 | struct tm tm; |
| 64 | int ret; |
| 65 | |
| 66 | errno = EPERM; |
| 67 | if(asn_UT2time(st, &tm) == -1 && errno != EPERM) |
| 68 | return cb("<bad-value>", 11, app_key); |
| 69 | |
| 70 | ret = snprintf(buf, sizeof(buf), |
| 71 | "%04d-%02d-%02d %02d:%02d%02d", |
| 72 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 73 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 74 | assert(ret > 0 && ret < sizeof(buf)); |
| 75 | return cb(buf, ret, app_key); |
| 76 | } else { |
| 77 | return cb("<absent>", 8, app_key); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | time_t |
| 82 | asn_UT2time(const UTCTime_t *st, struct tm *_tm) { |
| 83 | char buf[17+2]; /* "AAMMJJhhmmss+hhmm" = 17, + 2 = 19 */ |
| 84 | GeneralizedTime_t gt; |
| 85 | |
| 86 | if(!st || !st->buf || st->size < 11 || st->size > (sizeof(buf) - 2)) { |
| 87 | errno = EINVAL; |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | gt.buf = buf; |
| 92 | gt.size = st->size + 2; |
| 93 | memcpy(gt.buf + 2, st->buf, st->size); |
| 94 | if(st->buf[0] > 0x35) { |
| 95 | /* 19xx */ |
| 96 | gt.buf[0] = 0x31; |
| 97 | gt.buf[1] = 0x39; |
| 98 | } else { |
| 99 | /* 20xx */ |
| 100 | gt.buf[0] = 0x32; |
| 101 | gt.buf[1] = 0x30; |
| 102 | } |
| 103 | |
| 104 | return asn_GT2time(>, _tm); |
| 105 | } |