1 f9ab77a8 2023-08-23 op /* $OpenBSD: a_time_tm.c,v 1.15 2018/04/25 11:48:21 tb Exp $ */
3 f9ab77a8 2023-08-23 op * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
5 f9ab77a8 2023-08-23 op * Permission to use, copy, modify, and distribute this software for any
6 f9ab77a8 2023-08-23 op * purpose with or without fee is hereby granted, provided that the above
7 f9ab77a8 2023-08-23 op * copyright notice and this permission notice appear in all copies.
9 f9ab77a8 2023-08-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 f9ab77a8 2023-08-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 f9ab77a8 2023-08-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 f9ab77a8 2023-08-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 f9ab77a8 2023-08-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 f9ab77a8 2023-08-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 f9ab77a8 2023-08-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f9ab77a8 2023-08-23 op #include "config.h"
20 f9ab77a8 2023-08-23 op #include <ctype.h>
21 f9ab77a8 2023-08-23 op #include <limits.h>
22 f9ab77a8 2023-08-23 op #include <stdio.h>
23 f9ab77a8 2023-08-23 op #include <string.h>
24 f9ab77a8 2023-08-23 op #include <time.h>
26 f9ab77a8 2023-08-23 op #define GENTIME_LENGTH 15
27 f9ab77a8 2023-08-23 op #define UTCTIME_LENGTH 13
29 ebfc5784 2024-01-07 op #define V_ASN1_UTCTIME 23
30 ebfc5784 2024-01-07 op #define V_ASN1_GENERALIZEDTIME 24
32 f9ab77a8 2023-08-23 op #ifndef HAVE_ASN1_TIME_TM_CMP
34 f9ab77a8 2023-08-23 op ASN1_time_tm_cmp(struct tm *tm1, struct tm *tm2)
36 ebfc5784 2024-01-07 op if (tm1->tm_year < tm2->tm_year)
38 ebfc5784 2024-01-07 op if (tm1->tm_year > tm2->tm_year)
40 ebfc5784 2024-01-07 op if (tm1->tm_mon < tm2->tm_mon)
42 ebfc5784 2024-01-07 op if (tm1->tm_mon > tm2->tm_mon)
44 ebfc5784 2024-01-07 op if (tm1->tm_mday < tm2->tm_mday)
46 ebfc5784 2024-01-07 op if (tm1->tm_mday > tm2->tm_mday)
48 ebfc5784 2024-01-07 op if (tm1->tm_hour < tm2->tm_hour)
50 ebfc5784 2024-01-07 op if (tm1->tm_hour > tm2->tm_hour)
52 ebfc5784 2024-01-07 op if (tm1->tm_min < tm2->tm_min)
54 ebfc5784 2024-01-07 op if (tm1->tm_min > tm2->tm_min)
56 ebfc5784 2024-01-07 op if (tm1->tm_sec < tm2->tm_sec)
58 ebfc5784 2024-01-07 op if (tm1->tm_sec > tm2->tm_sec)
64 f9ab77a8 2023-08-23 op #ifndef HAVE_ASN1_TIME_TM_CLAMP_NOTAFTER
66 f9ab77a8 2023-08-23 op ASN1_time_tm_clamp_notafter(struct tm *tm)
68 f9ab77a8 2023-08-23 op #ifdef SMALL_TIME_T
69 ebfc5784 2024-01-07 op struct tm broken_os_epoch_tm;
70 ebfc5784 2024-01-07 op time_t broken_os_epoch_time = INT_MAX;
72 ebfc5784 2024-01-07 op if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL)
75 ebfc5784 2024-01-07 op if (ASN1_time_tm_cmp(tm, &broken_os_epoch_tm) == 1)
76 ebfc5784 2024-01-07 op memcpy(tm, &broken_os_epoch_tm, sizeof(*tm));