summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_x509_expiration_check.c
blob: d0fc0b00b8d3c5e1da768e99778bd6fa49a69474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Secure Component                                                 */
/**                                                                       */
/**    X.509 Digital Certificates                                         */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SECURE_SOURCE_CODE

#include "nx_secure_x509.h"

/* Local helper function. */
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        */
/*                                                                        */
/*    _nx_secure_x509_expiration_check                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks a certificate's validity period against the    */
/*    current time, which is a 32-bit UNIX-epoch format value of GMT.     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    certificate                           Pointer to certificate        */
/*    current_time                          Current GMT value             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Validity of certificate       */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_secure_x509_asn1_time_to_unix_convert                           */
/*                                          Convert ASN.1 time to UNIX    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_secure_tls_remote_certificate_verify                            */
/*                                          Verify the server certificate */
/*                                                                        */
/**************************************************************************/
UINT _nx_secure_x509_expiration_check(NX_SECURE_X509_CERT *certificate, ULONG current_time)
{
ULONG not_before;
ULONG not_after;
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_not_before_validity_format, &not_before);
    if (status != NX_SECURE_X509_SUCCESS)
    {
        return(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_not_after_validity_format, &not_after);
    if (status != NX_SECURE_X509_SUCCESS)
    {
        return(status);
    }

    /* Check if certificate is expired. */
    if (current_time > not_after)
    {
        /* Certificate is expired. */
        return(NX_SECURE_X509_CERTIFICATE_EXPIRED);
    }

    /* Check if certificate is not yet valid. */
    if (current_time < not_before)
    {
        /* Certificate is not valid yet. */
        return(NX_SECURE_X509_CERTIFICATE_NOT_YET_VALID);
    }

    return(NX_SECURE_X509_SUCCESS);
}



/* 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.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function converts ASN.1 time to 32-bit UNIX-epoch format value */
/*    of GMT.                                                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    asn1_time                             String of ASN.1 time          */
/*    asn1_length                           Length of ASN.1 time string   */
/*    format                                Format of UNIX time           */
/*    unix_time                             UNIX time value for output    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_secure_count_leap_years                                         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_secure_x509_expiration_check      Verify expiration of cert     */
/*                                                                        */
/**************************************************************************/
static UINT _nx_secure_x509_asn1_time_to_unix_convert(const UCHAR *asn1_time, USHORT asn1_length,
                                                      USHORT format, ULONG *unix_time)
{
ULONG year, month, day, hour, minute, second;
UINT index;

    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);
        hour = date_2_chars_to_int(asn1_time, 6);
        minute = date_2_chars_to_int(asn1_time, 8);
        second = 0;

        /* Check the next field, can be 'Z' for Zulu time (GMT) or [+/-] for local time offset. */
        index = 10;

        /* 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')
        {
            /* Check for optional local time offset. NOTE: The additions and subtractions here may
             * 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;
                minute -= date_2_chars_to_int(asn1_time, 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);
            }
            else
            {
                /* Not a correct UTC time! */
                return(NX_SECURE_X509_INVALID_DATE_FORMAT);
            }
        }

        /* 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 that have occurred. */
        if (year >= 70)
        {
            /* Year is before 2000. Add 1900 to get actual year. */
            year += 1900;
        }
        else
        {
            /* 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 ((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 if (format == NX_SECURE_ASN_TAG_GENERALIZED_TIME)
    {
        /* 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
    {
        return(NX_SECURE_X509_INVALID_DATE_FORMAT);
    }

    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;
}