Add x509parse_time_future()
diff --git a/library/x509parse.c b/library/x509parse.c
index d3174c3..8de0d98 100644
--- a/library/x509parse.c
+++ b/library/x509parse.c
@@ -3078,22 +3078,19 @@
/*
* Return 0 if the x509_time is still valid, or 1 otherwise.
*/
-int x509parse_time_expired( const x509_time *to )
+static void x509_get_current_time( x509_time *now )
{
- int year, mon, day;
- int hour, min, sec;
-
#if defined(_WIN32)
SYSTEMTIME st;
GetLocalTime(&st);
- year = st.wYear;
- mon = st.wMonth;
- day = st.wDay;
- hour = st.wHour;
- min = st.wMinute;
- sec = st.wSecond;
+ now->year = st.wYear;
+ now->mon = st.wMonth;
+ now->day = st.wDay;
+ now->hour = st.wHour;
+ now->min = st.wMinute;
+ now->sec = st.wSecond;
#else
struct tm *lt;
time_t tt;
@@ -3101,50 +3098,74 @@
tt = time( NULL );
lt = localtime( &tt );
- year = lt->tm_year + 1900;
- mon = lt->tm_mon + 1;
- day = lt->tm_mday;
- hour = lt->tm_hour;
- min = lt->tm_min;
- sec = lt->tm_sec;
+ now->year = lt->tm_year + 1900;
+ now->mon = lt->tm_mon + 1;
+ now->day = lt->tm_mday;
+ now->hour = lt->tm_hour;
+ now->min = lt->tm_min;
+ now->sec = lt->tm_sec;
#endif
+}
- if( year > to->year )
+/*
+ * Return 0 if before <= after, 1 otherwise
+ */
+static int x509_check_time( const x509_time *before, const x509_time *after )
+{
+ if( before->year > after->year )
return( 1 );
- if( year == to->year &&
- mon > to->mon )
+ if( before->year == after->year &&
+ before->mon > after->mon )
return( 1 );
- if( year == to->year &&
- mon == to->mon &&
- day > to->day )
+ if( before->year == after->year &&
+ before->mon == after->mon &&
+ before->day > after->day )
return( 1 );
- if( year == to->year &&
- mon == to->mon &&
- day == to->day &&
- hour > to->hour )
+ if( before->year == after->year &&
+ before->mon == after->mon &&
+ before->day == after->day &&
+ before->hour > after->hour )
return( 1 );
- if( year == to->year &&
- mon == to->mon &&
- day == to->day &&
- hour == to->hour &&
- min > to->min )
+ if( before->year == after->year &&
+ before->mon == after->mon &&
+ before->day == after->day &&
+ before->hour == after->hour &&
+ before->min > after->min )
return( 1 );
- if( year == to->year &&
- mon == to->mon &&
- day == to->day &&
- hour == to->hour &&
- min == to->min &&
- sec > to->sec )
+ if( before->year == after->year &&
+ before->mon == after->mon &&
+ before->day == after->day &&
+ before->hour == after->hour &&
+ before->min == after->min &&
+ before->sec > after->sec )
return( 1 );
return( 0 );
}
+int x509parse_time_expired( const x509_time *to )
+{
+ x509_time now;
+
+ x509_get_current_time( &now );
+
+ return( x509_check_time( &now, to ) );
+}
+
+int x509parse_time_future( const x509_time *from )
+{
+ x509_time now;
+
+ x509_get_current_time( &now );
+
+ return( x509_check_time( from, &now ) );
+}
+
/*
* Return 1 if the certificate is revoked, or 0 otherwise.
*/