diff --git a/libclamav/crypto.c b/libclamav/crypto.c index 8b398e402f..61c6d54349 100644 --- a/libclamav/crypto.c +++ b/libclamav/crypto.c @@ -1748,9 +1748,10 @@ X509 *cl_load_cert(const char *certpath) struct tm *cl_ASN1_GetTimeT(ASN1_TIME *timeobj) { - struct tm *t; - char *str; - const char *fmt = NULL; + struct tm *ret = NULL; + struct tm *t = NULL; + char *str = NULL; + const char *data, *fmt = NULL; time_t localt; #ifdef _WIN32 struct tm localtm, *ltm; @@ -1758,18 +1759,21 @@ struct tm *cl_ASN1_GetTimeT(ASN1_TIME *timeobj) struct tm localtm; #endif - if (!(timeobj) || !(timeobj->data)) - return NULL; + if (timeobj == NULL || (data = ASN1_STRING_get0_data(timeobj)) == NULL) + goto done; + + str = CLI_STRNDUP(data, ASN1_STRING_length(timeobj)); + if (NULL == str) + goto done; - str = (char *)(timeobj->data); if (strlen(str) < 12) - return NULL; + goto done; t = (struct tm *)calloc(1, sizeof(struct tm)); if (!(t)) - return NULL; + goto done; - if (timeobj->type == V_ASN1_UTCTIME) { + if (ASN1_STRING_type(timeobj) == V_ASN1_UTCTIME) { /* two digit year */ fmt = "%y%m%d%H%M%S"; if (str[3] == '0') { @@ -1778,7 +1782,7 @@ struct tm *cl_ASN1_GetTimeT(ASN1_TIME *timeobj) } else { str[3]--; } - } else if (timeobj->type == V_ASN1_GENERALIZEDTIME) { + } else if (ASN1_STRING_type(timeobj) == V_ASN1_GENERALIZEDTIME) { /* four digit year */ fmt = "%Y%m%d%H%M%S"; if (str[5] == '0') { @@ -1789,15 +1793,11 @@ struct tm *cl_ASN1_GetTimeT(ASN1_TIME *timeobj) } } - if (!(fmt)) { - free(t); - return NULL; - } + if (!(fmt)) + goto done; - if (!strptime(str, fmt, t)) { - free(t); - return NULL; - } + if (!strptime(str, fmt, t)) + goto done; /* Convert to local time */ localt = time(NULL); @@ -1808,7 +1808,15 @@ struct tm *cl_ASN1_GetTimeT(ASN1_TIME *timeobj) localtime_r(&localt, &localtm); #endif t->tm_isdst = localtm.tm_isdst; - return t; + + ret = t; + t = NULL; + +done: + free(t); + free(str); + + return ret; } X509_CRL *cl_load_crl(const char *file)