summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_x509_expiration_check.c
diff options
context:
space:
mode:
authorFrédéric Desbiens <[email protected]>2026-06-08 10:03:52 +0200
committerGitHub <[email protected]>2026-06-08 10:03:52 +0200
commit1a0e9e43a03e37ddecfb6b49e9b078d6e1803d05 (patch)
treec60ec41b4ff4fd2d7566c505cbdd423ceddd65f3 /nx_secure/src/nx_secure_x509_expiration_check.c
parent8b6e03ac30ab688bec02c69d42f2304b7f72a202 (diff)
parent4d88828b06d164a5b7e82152395a92254abcd6e4 (diff)
Merge pull request #391 from eclipse-threadx/devv6.5.1.202602_reldev
Merging changes for the v.6.5.1.202602 release
Diffstat (limited to 'nx_secure/src/nx_secure_x509_expiration_check.c')
-rw-r--r--nx_secure/src/nx_secure_x509_expiration_check.c271
1 files changed, 247 insertions, 24 deletions
diff --git a/nx_secure/src/nx_secure_x509_expiration_check.c b/nx_secure/src/nx_secure_x509_expiration_check.c
index e0e46751..d0fc0b00 100644
--- a/nx_secure/src/nx_secure_x509_expiration_check.c
+++ b/nx_secure/src/nx_secure_x509_expiration_check.c
@@ -28,6 +28,8 @@
static UINT _nx_secure_x509_asn1_time_to_unix_convert(const UCHAR *asn1_time, USHORT asn1_length,
USHORT format, ULONG *unix_time);
+static ULONG _nx_secure_count_leap_years(ULONG start_year, ULONG end_year);
+
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
@@ -71,7 +73,7 @@ UINT status;
/* First, convert the X.509 ASN.1 time format into 32-bit UINX-epoch format of the "not before" field. */
status = _nx_secure_x509_asn1_time_to_unix_convert(certificate -> nx_secure_x509_not_before, certificate -> nx_secure_x509_not_before_length,
- certificate -> nx_secure_x509_validity_format, &not_before);
+ certificate -> nx_secure_x509_not_before_validity_format, &not_before);
if (status != NX_SECURE_X509_SUCCESS)
{
return(status);
@@ -79,7 +81,7 @@ UINT status;
/* Convert the "not after" time field. */
status = _nx_secure_x509_asn1_time_to_unix_convert(certificate -> nx_secure_x509_not_after, certificate -> nx_secure_x509_not_after_length,
- certificate -> nx_secure_x509_validity_format, &not_after);
+ certificate -> nx_secure_x509_not_after_validity_format, &not_after);
if (status != NX_SECURE_X509_SUCCESS)
{
return(status);
@@ -107,18 +109,27 @@ UINT status;
/* Helper function to convert the ASN.1 time formats into UNIX epoch time for comparison. */
#define date_2_chars_to_int(buffer, index) (ULONG)(((buffer[index] - '0') * 10) + (buffer[index + 1] - '0'))
+#define date_4_chars_to_int(buffer, index) (ULONG)(((buffer[index] - '0') * 1000) + ((buffer[index + 1] - '0') * 100) + ((buffer[index + 2] - '0') * 10) + (buffer[index + 3] - '0'))
+#define date_char_is_digit(character) (((character) >= '0') && ((character) <= '9'))
+
+/* Helper function to determine if a given year is a leap year */
+
+#define is_leap_year(year) ((((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0))
/* Array indexed on month - 1 gives the total number of days in all previous months (through last day of previous
month). Leap years are handled in the logic below and are not reflected in this array. */
/* J F M A M J J A S O N D */
static const UINT days_before_month[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
+/* Define epoch year for UNIX time */
+static const ULONG unix_epoch = 1970;
+
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_secure_x509_asn1_time_to_unix_convert PORTABLE C */
-/* 6.1.11 */
+/* 6.1.12 */
/* AUTHOR */
/* */
/* Timothy Stapko, Microsoft Corporation */
@@ -141,7 +152,7 @@ static const UINT days_before_month[12] = {0, 31, 59, 90, 120, 151, 181, 212, 24
/* */
/* CALLS */
/* */
-/* None */
+/* _nx_secure_count_leap_years */
/* */
/* CALLED BY */
/* */
@@ -154,16 +165,28 @@ static UINT _nx_secure_x509_asn1_time_to_unix_convert(const UCHAR *asn1_time, US
ULONG year, month, day, hour, minute, second;
UINT index;
- NX_CRYPTO_PARAMETER_NOT_USED(asn1_length);
index = 0;
/* See what format we are using. */
if (format == NX_SECURE_ASN_TAG_UTC_TIME)
{
/* UTCTime is either "YYMMDDhhmm[ss]Z" or "YYMMDDhhmm[ss](+|-)hhmm" */
+ if (asn1_length < 11)
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ for (index = 0; index < 10; index++)
+ {
+ if (!date_char_is_digit(asn1_time[index]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+ }
+
year = date_2_chars_to_int(asn1_time, 0);
month = date_2_chars_to_int(asn1_time, 2);
- day = date_2_chars_to_int(asn1_time, 4) - 1; /* For calculations, day is 0-based. */
+ day = date_2_chars_to_int(asn1_time, 4);
hour = date_2_chars_to_int(asn1_time, 6);
minute = date_2_chars_to_int(asn1_time, 8);
second = 0;
@@ -174,10 +197,26 @@ UINT index;
/* Check for optional seconds. */
if (asn1_time[index] != 'Z' && asn1_time[index] != '+' && asn1_time[index] != '-')
{
+ if (((ULONG)index + 1) >= asn1_length ||
+ !date_char_is_digit(asn1_time[index]) ||
+ !date_char_is_digit(asn1_time[index + 1]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
second = date_2_chars_to_int(asn1_time, index);
index += 2;
}
+ if ((month < 1) || (month > 12) || (day < 1) || (day > 31) ||
+ (hour > 23) || (minute > 59) || (second > 59) || (index >= asn1_length))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ /* For calculations, day is 0-based. */
+ day -= 1;
+
/* Check for GMT time or local time offset. */
if (asn1_time[index] != 'Z')
{
@@ -185,6 +224,15 @@ UINT index;
* result in values > 24 or < 0 but that is OK for the calculations. */
if (asn1_time[index] == '+')
{
+ if (((ULONG)index + 4) >= asn1_length ||
+ !date_char_is_digit(asn1_time[index + 1]) ||
+ !date_char_is_digit(asn1_time[index + 2]) ||
+ !date_char_is_digit(asn1_time[index + 3]) ||
+ !date_char_is_digit(asn1_time[index + 4]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
index++; /* Skip the '+' */
hour -= date_2_chars_to_int(asn1_time, index);
index += 2;
@@ -192,6 +240,15 @@ UINT index;
}
else if (asn1_time[index] == '-')
{
+ if (((ULONG)index + 4) >= asn1_length ||
+ !date_char_is_digit(asn1_time[index + 1]) ||
+ !date_char_is_digit(asn1_time[index + 2]) ||
+ !date_char_is_digit(asn1_time[index + 3]) ||
+ !date_char_is_digit(asn1_time[index + 4]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
index++; /* Skip the '-' */
hour += date_2_chars_to_int(asn1_time, index);
index += 2;
@@ -206,31 +263,28 @@ UINT index;
/* printf("year: %lu, month: %lu, day: %lu, hour: %lu, minute: %lu, second: %lu\n", year, month, day, hour, minute, second);*/
- /* Now we have our time in integers, calculate leap years. We aren't concerned with years outside the UNIX
- time range of 1970-2038 so we can assume every 4 years starting with 1972 is a leap year (years divisible
- by 100 are NOT leap years unless also divisible by 400, which the year 2000 is). Using integer division gives
- us the floor of the number of 4 year periods, so add 1. */
+ /* Now we have our time in integers, calculate leap years that have occurred. */
if (year >= 70)
{
- /* Year is before 2000. Subtract 72 to get duration from first leap year in epoch. */
- year -= 70;
- day += ((year + 2) / 4);
+ /* Year is before 2000. Add 1900 to get actual year. */
+ year += 1900;
}
else
{
- /* Year is 2000 or greater. Add 28 (2000-1972) to get duration from first leap year in epoch. */
- year += 30;
- day += ((year - 2) / 4) + 1;
+ /* Year is 2000 or greater. Add 2000 to get actual year. */
+ year += 2000;
}
+ day += _nx_secure_count_leap_years(unix_epoch, year);
+
/* If it is leap year and month is before March, subtract 1 day. */
- if (((year + 2) % 4 == 0) && (month < 3))
+ if ((is_leap_year(year)) && (month < 3))
{
day -= 1;
}
/* Finally, calculate the number of seconds from the extracted values. */
- day += year * 365;
+ day += (year - unix_epoch) * 365;
day += days_before_month[month - 1];
hour += day * 24;
minute += hour * 60;
@@ -241,12 +295,133 @@ UINT index;
}
else if (format == NX_SECURE_ASN_TAG_GENERALIZED_TIME)
{
- /* Generalized time formats:
- Local time only. ``YYYYMMDDHH[MM[SS[.fff]]]'', where the optional fff is three decimal places (fractions of seconds).
- Universal time (UTC time) only. ``YYYYMMDDHH[MM[SS[.fff]]]Z''. MM, SS, .fff are optional.
- Difference between local and UTC times. ``YYYYMMDDHH[MM[SS[.fff]]]+-HHMM''. +/-HHMM is local time offset. */
- /* TODO: Implement conversion to 32-bit UNIX time. */
- return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ /* GeneralizedTime is parsed as YYYYMMDDHHMM[SS[.fff]](Z|+HHMM|-HHMM). */
+ if (asn1_length < 13)
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ for (index = 0; index < 12; index++)
+ {
+ if (!date_char_is_digit(asn1_time[index]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+ }
+
+ year = date_4_chars_to_int(asn1_time, 0);
+ month = date_2_chars_to_int(asn1_time, 4);
+ day = date_2_chars_to_int(asn1_time, 6);
+ hour = date_2_chars_to_int(asn1_time, 8);
+ minute = date_2_chars_to_int(asn1_time, 10);
+ second = 0;
+ index = 12;
+
+ /* Check for optional seconds. */
+ if (asn1_time[index] != 'Z' && asn1_time[index] != '+' && asn1_time[index] != '-')
+ {
+ if (((ULONG)index + 1) >= asn1_length ||
+ !date_char_is_digit(asn1_time[index]) ||
+ !date_char_is_digit(asn1_time[index + 1]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ second = date_2_chars_to_int(asn1_time, index);
+ index += 2;
+
+ /* Skip optional fractional seconds. */
+ if ((index < asn1_length) && (asn1_time[index] == '.'))
+ {
+ index++;
+ if ((index >= asn1_length) || !date_char_is_digit(asn1_time[index]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ while ((index < asn1_length) && date_char_is_digit(asn1_time[index]))
+ {
+ index++;
+ }
+ }
+ }
+
+ if ((year < unix_epoch) || (month < 1) || (month > 12) || (day < 1) || (day > 31) ||
+ (hour > 23) || (minute > 59) || (second > 59) || (index >= asn1_length))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ /* For calculations, day is 0-based. */
+ day -= 1;
+
+ /* Check for GMT time or local time offset. */
+ if (asn1_time[index] == 'Z')
+ {
+ index++;
+ }
+ else if (asn1_time[index] == '+')
+ {
+ if (((ULONG)index + 4) >= asn1_length ||
+ !date_char_is_digit(asn1_time[index + 1]) ||
+ !date_char_is_digit(asn1_time[index + 2]) ||
+ !date_char_is_digit(asn1_time[index + 3]) ||
+ !date_char_is_digit(asn1_time[index + 4]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ index++; /* Skip the '+' */
+ hour -= date_2_chars_to_int(asn1_time, index);
+ index += 2;
+ minute -= date_2_chars_to_int(asn1_time, index);
+ index += 2;
+ }
+ else if (asn1_time[index] == '-')
+ {
+ if (((ULONG)index + 4) >= asn1_length ||
+ !date_char_is_digit(asn1_time[index + 1]) ||
+ !date_char_is_digit(asn1_time[index + 2]) ||
+ !date_char_is_digit(asn1_time[index + 3]) ||
+ !date_char_is_digit(asn1_time[index + 4]))
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ index++; /* Skip the '-' */
+ hour += date_2_chars_to_int(asn1_time, index);
+ index += 2;
+ minute += date_2_chars_to_int(asn1_time, index);
+ index += 2;
+ }
+ else
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ if (index != asn1_length)
+ {
+ return(NX_SECURE_X509_INVALID_DATE_FORMAT);
+ }
+
+ /* Now we have our time in integers, calculate leap years that have occurred. */
+ day += _nx_secure_count_leap_years(unix_epoch, year);
+
+ /* If it is leap year and month is before March, subtract 1 day. */
+ if (is_leap_year(year) && (month < 3))
+ {
+ day -= 1;
+ }
+
+ /* Finally, calculate the number of seconds from the extracted values. */
+ day += (year - unix_epoch) * 365;
+ day += days_before_month[month - 1];
+ hour += day * 24;
+ minute += hour * 60;
+ second += minute * 60;
+
+ /* Finally, return the converted time. */
+ *unix_time = second;
}
else
{
@@ -256,3 +431,51 @@ UINT index;
return(NX_SECURE_X509_SUCCESS);
}
+
+/**************************************************************************/
+/* */
+/* FUNCTION RELEASE */
+/* */
+/* _nx_secure_count_leap_years PORTABLE C */
+/* 6.4.1 */
+/* AUTHOR */
+/* */
+/* Simon Scurrell, T3S Solutions Ltd */
+/* */
+/* DESCRIPTION */
+/* */
+/* This function calculates the number of leap years that have */
+/* occurred between the given start and end years. */
+/* */
+/* INPUT */
+/* */
+/* start_year 4-digit start year (YYYY) */
+/* end_year 4-digit end year (YYYY) */
+/* */
+/* OUTPUT */
+/* */
+/* count Returns the number of leap years */
+/* */
+/* CALLS */
+/* */
+/* None */
+/* */
+/* CALLED BY */
+/* */
+/* _nx_secure_x509_asn1_time_to_unix_convert ASN.1 time convert */
+/* */
+/**************************************************************************/
+static ULONG _nx_secure_count_leap_years(ULONG start_year, ULONG end_year)
+{
+ ULONG count = 0;
+
+ for (ULONG year = start_year; year <= end_year; year++)
+ {
+ if (is_leap_year(year))
+ {
+ count += 1;
+ }
+ }
+
+ return count;
+}