mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-30 18:57:27 +02:00
Refactor wolfSSL_ASN1_TIME_adj to use GetUnformattedTimeString (new API)
This commit is contained in:
65
src/ssl.c
65
src/ssl.c
@ -45308,18 +45308,11 @@ WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_adj(WOLFSSL_ASN1_TIME *s, time_t t,
|
||||
int offset_day, long offset_sec)
|
||||
{
|
||||
const time_t sec_per_day = 24*60*60;
|
||||
struct tm* ts = NULL;
|
||||
struct tm* tmpTime;
|
||||
time_t t_adj = 0;
|
||||
time_t offset_day_sec = 0;
|
||||
#if defined(NEED_TMP_TIME)
|
||||
struct tm tmpTimeStorage;
|
||||
|
||||
tmpTime = &tmpTimeStorage;
|
||||
#else
|
||||
tmpTime = NULL;
|
||||
#endif
|
||||
(void)tmpTime;
|
||||
char utc_str_buf[MAX_TIME_STRING_SZ] = {0};
|
||||
char* utc_str = utc_str_buf;
|
||||
int time_get;
|
||||
|
||||
WOLFSSL_ENTER("wolfSSL_ASN1_TIME_adj");
|
||||
|
||||
@ -45333,54 +45326,18 @@ WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_adj(WOLFSSL_ASN1_TIME *s, time_t t,
|
||||
/* compute GMT time with offset */
|
||||
offset_day_sec = offset_day * sec_per_day;
|
||||
t_adj = t + offset_day_sec + offset_sec;
|
||||
ts = (struct tm *)XGMTIME(&t_adj, tmpTime);
|
||||
if (ts == NULL){
|
||||
WOLFSSL_MSG("failed to get time data.");
|
||||
|
||||
/* Get UTC Time */
|
||||
time_get = GetUnformattedTimeString(&t_adj, (byte*) utc_str,
|
||||
sizeof(utc_str_buf));
|
||||
if (time_get <= 0) {
|
||||
wolfSSL_ASN1_TIME_free(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* create ASN1 time notation */
|
||||
/* UTC Time */
|
||||
if (ts->tm_year >= 50 && ts->tm_year < 150){
|
||||
char utc_str[ASN_UTC_TIME_SIZE];
|
||||
int utc_year = 0,utc_mon,utc_day,utc_hour,utc_min,utc_sec;
|
||||
|
||||
if (ts->tm_year >= 50 && ts->tm_year < 100){
|
||||
utc_year = ts->tm_year;
|
||||
} else if (ts->tm_year >= 100 && ts->tm_year < 150){
|
||||
utc_year = ts->tm_year - 100;
|
||||
}
|
||||
utc_mon = ts->tm_mon + 1;
|
||||
utc_day = ts->tm_mday;
|
||||
utc_hour = ts->tm_hour;
|
||||
utc_min = ts->tm_min;
|
||||
utc_sec = ts->tm_sec;
|
||||
XSNPRINTF((char *)utc_str, sizeof(utc_str),
|
||||
"%02d%02d%02d%02d%02d%02dZ",
|
||||
utc_year, utc_mon, utc_day, utc_hour, utc_min, utc_sec);
|
||||
if (wolfSSL_ASN1_TIME_set_string(s, utc_str) != WOLFSSL_SUCCESS) {
|
||||
wolfSSL_ASN1_TIME_free(s);
|
||||
return NULL;
|
||||
}
|
||||
/* GeneralizedTime */
|
||||
} else {
|
||||
char gt_str[ASN_GENERALIZED_TIME_MAX];
|
||||
int gt_year,gt_mon,gt_day,gt_hour,gt_min,gt_sec;
|
||||
|
||||
gt_year = ts->tm_year + 1900;
|
||||
gt_mon = ts->tm_mon + 1;
|
||||
gt_day = ts->tm_mday;
|
||||
gt_hour = ts->tm_hour;
|
||||
gt_min = ts->tm_min;
|
||||
gt_sec = ts->tm_sec;
|
||||
XSNPRINTF((char *)gt_str, sizeof(gt_str),
|
||||
"%4d%02d%02d%02d%02d%02dZ",
|
||||
gt_year, gt_mon, gt_day, gt_hour, gt_min,gt_sec);
|
||||
if (wolfSSL_ASN1_TIME_set_string(s, gt_str) != WOLFSSL_SUCCESS) {
|
||||
wolfSSL_ASN1_TIME_free(s);
|
||||
return NULL;
|
||||
}
|
||||
if (wolfSSL_ASN1_TIME_set_string(s, utc_str) != WOLFSSL_SUCCESS) {
|
||||
wolfSSL_ASN1_TIME_free(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return s;
|
||||
|
@ -12192,19 +12192,60 @@ int GetTimeString(byte* date, int format, char* buf, int len)
|
||||
#endif /* OPENSSL_ALL || WOLFSSL_MYSQL_COMPATIBLE || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
|
||||
|
||||
|
||||
#if !defined(NO_ASN_TIME) && defined(HAVE_PKCS7)
|
||||
|
||||
#if !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
|
||||
!defined(TIME_OVERRIDES) && (defined(OPENSSL_EXTRA) || defined(HAVE_PKCS7))
|
||||
/* Set current time string, either UTC or GeneralizedTime.
|
||||
* (void*) tm should be a pointer to time_t, output is placed in buf.
|
||||
*
|
||||
* Return time string length placed in buf on success, negative on error */
|
||||
int GetAsnTimeString(void* currTime, byte* buf, word32 len)
|
||||
{
|
||||
byte* data_ptr = buf;
|
||||
byte uf_time[ASN_GENERALIZED_TIME_SIZE] = {0};
|
||||
word32 data_len = 0;
|
||||
|
||||
WOLFSSL_ENTER("GetAsnTimeString");
|
||||
|
||||
if (buf == NULL || len == 0)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
data_len = GetUnformattedTimeString(currTime, uf_time, len);
|
||||
/* ensure room to add 2 bytes (ASN type and length) before proceeding */
|
||||
if (len < data_len + 2)
|
||||
return BUFFER_E;
|
||||
if(data_len <= 0)
|
||||
return ASN_TIME_E;
|
||||
|
||||
/* Increment by 1 for ASN type */
|
||||
data_len++;
|
||||
|
||||
if (data_len == ASN_UTC_TIME_SIZE) {
|
||||
/* increment data_len for ASN length byte after adding the data_ptr */
|
||||
*data_ptr = (byte) ASN_UTC_TIME; data_ptr++; data_len++;
|
||||
/* -1 below excludes null terminator */
|
||||
*data_ptr = (byte) ASN_UTC_TIME_SIZE - 1; data_ptr++;
|
||||
XMEMCPY(data_ptr,(byte *)uf_time, ASN_UTC_TIME_SIZE - 1);
|
||||
} else if (data_len == ASN_GENERALIZED_TIME_SIZE) {
|
||||
/* increment data_len for ASN length byte after adding the data_ptr */
|
||||
*data_ptr = (byte) ASN_GENERALIZED_TIME; data_ptr++; data_len++;
|
||||
/* -1 below excludes null terminator */
|
||||
*data_ptr = (byte) ASN_GENERALIZED_TIME_SIZE - 1; data_ptr++;
|
||||
XMEMCPY(data_ptr,(byte *)uf_time, ASN_GENERALIZED_TIME_SIZE - 1);
|
||||
} else {
|
||||
WOLFSSL_MSG("Invalid time size returned");
|
||||
return ASN_TIME_E;
|
||||
}
|
||||
|
||||
return data_len;
|
||||
}
|
||||
|
||||
/* return just the raw time string */
|
||||
int GetUnformattedTimeString(void* currTime, byte* buf, word32 len)
|
||||
{
|
||||
struct tm* ts = NULL;
|
||||
struct tm* tmpTime = NULL;
|
||||
byte* data_ptr = buf;
|
||||
word32 data_len = 0;
|
||||
int year, mon, day, hour, mini, sec;
|
||||
int ret_bytes_len = 0;
|
||||
#if defined(NEED_TMP_TIME)
|
||||
struct tm tmpTimeStorage;
|
||||
tmpTime = &tmpTimeStorage;
|
||||
@ -12212,7 +12253,7 @@ int GetAsnTimeString(void* currTime, byte* buf, word32 len)
|
||||
(void)tmpTime;
|
||||
#endif
|
||||
|
||||
WOLFSSL_ENTER("SetAsnTimeString");
|
||||
WOLFSSL_ENTER("GetTimeString");
|
||||
|
||||
if (buf == NULL || len == 0)
|
||||
return BAD_FUNC_ARG;
|
||||
@ -12228,12 +12269,6 @@ int GetAsnTimeString(void* currTime, byte* buf, word32 len)
|
||||
|
||||
if (ts->tm_year >= 50 && ts->tm_year < 150) {
|
||||
/* UTC Time */
|
||||
char utc_str[ASN_UTC_TIME_SIZE];
|
||||
data_len = ASN_UTC_TIME_SIZE - 1 + 2;
|
||||
|
||||
if (len < data_len)
|
||||
return BUFFER_E;
|
||||
|
||||
if (ts->tm_year >= 50 && ts->tm_year < 100) {
|
||||
year = ts->tm_year;
|
||||
} else if (ts->tm_year >= 100 && ts->tm_year < 150) {
|
||||
@ -12248,40 +12283,27 @@ int GetAsnTimeString(void* currTime, byte* buf, word32 len)
|
||||
hour = ts->tm_hour;
|
||||
mini = ts->tm_min;
|
||||
sec = ts->tm_sec;
|
||||
XSNPRINTF((char *)utc_str, ASN_UTC_TIME_SIZE,
|
||||
"%02d%02d%02d%02d%02d%02dZ", year, mon, day, hour, mini, sec);
|
||||
*data_ptr = (byte) ASN_UTC_TIME; data_ptr++;
|
||||
/* -1 below excludes null terminator */
|
||||
*data_ptr = (byte) ASN_UTC_TIME_SIZE - 1; data_ptr++;
|
||||
XMEMCPY(data_ptr,(byte *)utc_str, ASN_UTC_TIME_SIZE - 1);
|
||||
|
||||
ret_bytes_len = XSNPRINTF((char *)buf, len,
|
||||
"%02d%02d%02d%02d%02d%02dZ", year, mon, day,
|
||||
hour, mini, sec);
|
||||
} else {
|
||||
/* GeneralizedTime */
|
||||
char gt_str[ASN_GENERALIZED_TIME_SIZE];
|
||||
data_len = ASN_GENERALIZED_TIME_SIZE - 1 + 2;
|
||||
|
||||
if (len < data_len)
|
||||
return BUFFER_E;
|
||||
|
||||
year = ts->tm_year + 1900;
|
||||
mon = ts->tm_mon + 1;
|
||||
day = ts->tm_mday;
|
||||
hour = ts->tm_hour;
|
||||
mini = ts->tm_min;
|
||||
sec = ts->tm_sec;
|
||||
XSNPRINTF((char *)gt_str, ASN_GENERALIZED_TIME_SIZE,
|
||||
"%4d%02d%02d%02d%02d%02dZ", year, mon, day, hour, mini, sec);
|
||||
*data_ptr = (byte) ASN_GENERALIZED_TIME; data_ptr++;
|
||||
/* -1 below excludes null terminator */
|
||||
*data_ptr = (byte) ASN_GENERALIZED_TIME_SIZE - 1; data_ptr++;
|
||||
XMEMCPY(data_ptr,(byte *)gt_str, ASN_GENERALIZED_TIME_SIZE - 1);
|
||||
ret_bytes_len = XSNPRINTF((char *)buf, len,
|
||||
"%4d%02d%02d%02d%02d%02dZ", year, mon, day,
|
||||
hour, mini, sec);
|
||||
}
|
||||
|
||||
return data_len;
|
||||
return ret_bytes_len;
|
||||
}
|
||||
|
||||
#endif /* !NO_ASN_TIME && HAVE_PKCS7 */
|
||||
|
||||
#endif /* !NO_ASN_TIME && !USER_TIME && !TIME_OVERRIDES &&
|
||||
* (OPENSSL_EXTRA || HAVE_PKCS7) */
|
||||
|
||||
#if defined(USE_WOLF_VALIDDATE)
|
||||
|
||||
|
@ -1894,7 +1894,9 @@ typedef struct tm wolfssl_tm;
|
||||
defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
|
||||
WOLFSSL_LOCAL int GetTimeString(byte* date, int format, char* buf, int len);
|
||||
#endif
|
||||
#if !defined(NO_ASN_TIME) && defined(HAVE_PKCS7)
|
||||
#if !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
|
||||
!defined(TIME_OVERRIDES) && (defined(OPENSSL_EXTRA) || defined(HAVE_PKCS7))
|
||||
WOLFSSL_LOCAL int GetUnformattedTimeString(void* currTime, byte* buf, word32 len);
|
||||
WOLFSSL_LOCAL int GetAsnTimeString(void* currTime, byte* buf, word32 len);
|
||||
#endif
|
||||
WOLFSSL_LOCAL int ExtractDate(const unsigned char* date, unsigned char format,
|
||||
|
Reference in New Issue
Block a user