asn1: Make sure the first argument to sscanf() is null-terminated

This commit is contained in:
Tobias Brunner
2017-05-23 18:29:12 +02:00
parent 9c42126297
commit 411bda6836
+9 -7
View File
@@ -350,13 +350,15 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
int tm_leap_4, tm_leap_100, tm_leap_400, tm_leap; int tm_leap_4, tm_leap_100, tm_leap_400, tm_leap;
int tz_hour, tz_min, tz_offset; int tz_hour, tz_min, tz_offset;
time_t tm_days, tm_secs; time_t tm_days, tm_secs;
u_char *eot = NULL; char buf[BUF_LEN], *eot = NULL;
if ((eot = memchr(utctime->ptr, 'Z', utctime->len)) != NULL) snprintf(buf, sizeof(buf), "%.*s", (int)utctime->len, utctime->ptr);
if ((eot = strchr(buf, 'Z')) != NULL)
{ {
tz_offset = 0; /* Zulu time with a zero time zone offset */ tz_offset = 0; /* Zulu time with a zero time zone offset */
} }
else if ((eot = memchr(utctime->ptr, '+', utctime->len)) != NULL) else if ((eot = strchr(buf, '+')) != NULL)
{ {
if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2) if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2)
{ {
@@ -364,7 +366,7 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
} }
tz_offset = 3600*tz_hour + 60*tz_min; /* positive time zone offset */ tz_offset = 3600*tz_hour + 60*tz_min; /* positive time zone offset */
} }
else if ((eot = memchr(utctime->ptr, '-', utctime->len)) != NULL) else if ((eot = strchr(buf, '-')) != NULL)
{ {
if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2) if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2)
{ {
@@ -382,15 +384,15 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
const char* format = (type == ASN1_UTCTIME)? "%2d%2d%2d%2d%2d": const char* format = (type == ASN1_UTCTIME)? "%2d%2d%2d%2d%2d":
"%4d%2d%2d%2d%2d"; "%4d%2d%2d%2d%2d";
if (sscanf(utctime->ptr, format, &tm_year, &tm_mon, &tm_day, if (sscanf(buf, format, &tm_year, &tm_mon, &tm_day,
&tm_hour, &tm_min) != 5) &tm_hour, &tm_min) != 5)
{ {
return 0; /* error in [yy]yymmddhhmm time format */ return 0; /* error in [yy]yymmddhhmm time format */
} }
} }
/* is there a seconds field? */ /* is there a seconds field? */
if ((eot - utctime->ptr) == ((type == ASN1_UTCTIME)?12:14)) if ((eot - buf) == ((type == ASN1_UTCTIME)?12:14))
{ {
if (sscanf(eot-2, "%2d", &tm_sec) != 1) if (sscanf(eot-2, "%2d", &tm_sec) != 1)
{ {