Blob


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