aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2014-02-05 18:06:15 +0100
committerTobias Brunner <tobias@strongswan.org>2014-02-12 13:54:05 +0100
commitebc665be4d3dfc6aab71caedb052e635d590c674 (patch)
tree36c2e2656e25d7d0bc87011bb6b5fff32845fd06
parentaddc34d5f03962da12f12692b7e326f53c505474 (diff)
downloadstrongswan-ebc665be4d3dfc6aab71caedb052e635d590c674.tar.bz2
strongswan-ebc665be4d3dfc6aab71caedb052e635d590c674.tar.xz
asn1: Support dates before 1970-01-01 (i.e. when time_t gets negative)
On x86 we allow "overflows" around 1969/1970 but not for other dates. Fixes #509.
-rw-r--r--src/libstrongswan/asn1/asn1.c20
-rw-r--r--src/libstrongswan/asn1/asn1.h7
-rw-r--r--src/libstrongswan/tests/suites/test_asn1.c6
3 files changed, 29 insertions, 4 deletions
diff --git a/src/libstrongswan/asn1/asn1.c b/src/libstrongswan/asn1/asn1.c
index 21cf1e9e6..38a6ad688 100644
--- a/src/libstrongswan/asn1/asn1.c
+++ b/src/libstrongswan/asn1/asn1.c
@@ -393,8 +393,8 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
tm_year += (tm_year < 50) ? 2000 : 1900;
}
- /* prevent large 32 bit integer overflows */
- if (sizeof(time_t) == 4 && tm_year > 2038)
+ /* prevent obvious 32 bit integer overflows */
+ if (sizeof(time_t) == 4 && (tm_year > 2038 || tm_year < 1901))
{
return TIME_32_BIT_SIGNED_MAX;
}
@@ -435,8 +435,20 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
tm_days = 365 * (tm_year - 1970) + days[tm_mon] + tm_day + tm_leap;
tm_secs = 60 * (60 * (24 * tm_days + tm_hour) + tm_min) + tm_sec - tz_offset;
- /* has a 32 bit signed integer overflow occurred? */
- return (tm_secs < 0) ? TIME_32_BIT_SIGNED_MAX : tm_secs;
+ if (sizeof(time_t) == 4)
+ { /* has a 32 bit signed integer overflow occurred? */
+ if (tm_year > 1970 && tm_secs < 0)
+ { /* depending on the time zone, the first days in 1970 may result in
+ * a negative value, but dates after 1970 never will */
+ return TIME_32_BIT_SIGNED_MAX;
+ }
+ if (tm_year < 1969 && tm_secs > 0)
+ { /* similarly, tm_secs is not positive for dates before 1970, except
+ * for the last days in 1969, depending on the time zone */
+ return TIME_32_BIT_SIGNED_MAX;
+ }
+ }
+ return tm_secs;
}
/**
diff --git a/src/libstrongswan/asn1/asn1.h b/src/libstrongswan/asn1/asn1.h
index a1d625380..7a48292af 100644
--- a/src/libstrongswan/asn1/asn1.h
+++ b/src/libstrongswan/asn1/asn1.h
@@ -191,6 +191,13 @@ void asn1_debug_simple_object(chunk_t object, asn1_t type, bool private);
/**
* Converts an ASN.1 UTCTIME or GENERALIZEDTIME string to time_t
*
+ * On systems where sizeof(time_t) == 4 there will be an overflow
+ * for dates
+ * > Tue, 19 Jan 2038 03:14:07 UTC (0x7fffffff)
+ * and
+ * < Fri, 13 Dec 1901 20:45:52 UTC (0x80000000)
+ * in both cases TIME_32_BIT_SIGNED_MAX is returned.
+ *
* @param utctime body of an ASN.1 coded time object
* @param type ASN1_UTCTIME or ASN1_GENERALIZEDTIME
* @return time_t in UTC
diff --git a/src/libstrongswan/tests/suites/test_asn1.c b/src/libstrongswan/tests/suites/test_asn1.c
index 099dbcd81..d0cd7e6e4 100644
--- a/src/libstrongswan/tests/suites/test_asn1.c
+++ b/src/libstrongswan/tests/suites/test_asn1.c
@@ -490,6 +490,12 @@ START_TEST(test_asn1_to_time)
{ 0, 0x17, "7001010260Z" },
{ 0, 0x17, "7001010203-1Z" },
{ 0, 0x17, "700101020361Z" },
+ { -631152000, 0x17, "500101000000Z" }, /* UTCTime min */
+ { 59, 0x17, "691231235959-0001" },
+ { -1, 0x17, "691231235959Z" },
+ { 0, 0x17, "700101000000Z" },
+ { -60, 0x17, "700101000000+0001" },
+ { 2524607999UL, 0x17, "491231235959Z" }, /* UTCTime max */
{ 5097600, 0x17, "7003010000Z" },
{ 68256000, 0x17, "7203010000Z" },
{ 951868800, 0x17, "0003010000Z" },