Blame


1 f9ab77a8 2023-08-23 op /* $OpenBSD: a_time_tm.c,v 1.15 2018/04/25 11:48:21 tb Exp $ */
2 f9ab77a8 2023-08-23 op /*
3 f9ab77a8 2023-08-23 op * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 f9ab77a8 2023-08-23 op *
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.
8 f9ab77a8 2023-08-23 op *
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.
16 f9ab77a8 2023-08-23 op */
17 f9ab77a8 2023-08-23 op
18 f9ab77a8 2023-08-23 op #include "config.h"
19 f9ab77a8 2023-08-23 op
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>
25 f9ab77a8 2023-08-23 op
26 f9ab77a8 2023-08-23 op #define GENTIME_LENGTH 15
27 f9ab77a8 2023-08-23 op #define UTCTIME_LENGTH 13
28 f9ab77a8 2023-08-23 op
29 ebfc5784 2024-01-07 op #define V_ASN1_UTCTIME 23
30 ebfc5784 2024-01-07 op #define V_ASN1_GENERALIZEDTIME 24
31 f9ab77a8 2023-08-23 op
32 f9ab77a8 2023-08-23 op #ifndef HAVE_ASN1_TIME_TM_CMP
33 f9ab77a8 2023-08-23 op int
34 f9ab77a8 2023-08-23 op ASN1_time_tm_cmp(struct tm *tm1, struct tm *tm2)
35 f9ab77a8 2023-08-23 op {
36 ebfc5784 2024-01-07 op if (tm1->tm_year < tm2->tm_year)
37 ebfc5784 2024-01-07 op return (-1);
38 ebfc5784 2024-01-07 op if (tm1->tm_year > tm2->tm_year)
39 ebfc5784 2024-01-07 op return (1);
40 ebfc5784 2024-01-07 op if (tm1->tm_mon < tm2->tm_mon)
41 ebfc5784 2024-01-07 op return (-1);
42 ebfc5784 2024-01-07 op if (tm1->tm_mon > tm2->tm_mon)
43 ebfc5784 2024-01-07 op return (1);
44 ebfc5784 2024-01-07 op if (tm1->tm_mday < tm2->tm_mday)
45 ebfc5784 2024-01-07 op return (-1);
46 ebfc5784 2024-01-07 op if (tm1->tm_mday > tm2->tm_mday)
47 ebfc5784 2024-01-07 op return (1);
48 ebfc5784 2024-01-07 op if (tm1->tm_hour < tm2->tm_hour)
49 ebfc5784 2024-01-07 op return (-1);
50 ebfc5784 2024-01-07 op if (tm1->tm_hour > tm2->tm_hour)
51 ebfc5784 2024-01-07 op return (1);
52 ebfc5784 2024-01-07 op if (tm1->tm_min < tm2->tm_min)
53 ebfc5784 2024-01-07 op return (-1);
54 ebfc5784 2024-01-07 op if (tm1->tm_min > tm2->tm_min)
55 ebfc5784 2024-01-07 op return (1);
56 ebfc5784 2024-01-07 op if (tm1->tm_sec < tm2->tm_sec)
57 ebfc5784 2024-01-07 op return (-1);
58 ebfc5784 2024-01-07 op if (tm1->tm_sec > tm2->tm_sec)
59 ebfc5784 2024-01-07 op return (1);
60 ebfc5784 2024-01-07 op return 0;
61 f9ab77a8 2023-08-23 op }
62 f9ab77a8 2023-08-23 op #endif
63 f9ab77a8 2023-08-23 op
64 f9ab77a8 2023-08-23 op #ifndef HAVE_ASN1_TIME_TM_CLAMP_NOTAFTER
65 f9ab77a8 2023-08-23 op int
66 f9ab77a8 2023-08-23 op ASN1_time_tm_clamp_notafter(struct tm *tm)
67 f9ab77a8 2023-08-23 op {
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;
71 f9ab77a8 2023-08-23 op
72 ebfc5784 2024-01-07 op if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL)
73 ebfc5784 2024-01-07 op return 0;
74 f9ab77a8 2023-08-23 op
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));
77 f9ab77a8 2023-08-23 op #endif
78 ebfc5784 2024-01-07 op return 1;
79 f9ab77a8 2023-08-23 op }
80 f9ab77a8 2023-08-23 op #endif