summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libcrypto/Makefile6
-rw-r--r--src/lib/libcrypto/asn1/a_time.c5
-rw-r--r--src/lib/libcrypto/asn1/a_time_posix.c272
-rw-r--r--src/lib/libcrypto/asn1/a_time_tm.c12
-rw-r--r--src/lib/libcrypto/asn1/asn1_locl.h7
-rw-r--r--src/lib/libcrypto/o_time.c242
-rw-r--r--src/lib/libcrypto/o_time.h71
7 files changed, 289 insertions, 326 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index a3091616a5..d3daf29970 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.79 2022/08/20 09:16:18 tb Exp $ 1# $OpenBSD: Makefile,v 1.80 2022/11/08 12:56:00 beck Exp $
2 2
3LIB= crypto 3LIB= crypto
4LIBREBUILD=y 4LIBREBUILD=y
@@ -53,7 +53,7 @@ SYMBOL_LIST= ${.CURDIR}/Symbols.list
53 53
54# crypto/ 54# crypto/
55SRCS+= cryptlib.c malloc-wrapper.c mem_dbg.c cversion.c ex_data.c cpt_err.c 55SRCS+= cryptlib.c malloc-wrapper.c mem_dbg.c cversion.c ex_data.c cpt_err.c
56SRCS+= o_time.c o_str.c o_init.c o_fips.c 56SRCS+= o_str.c o_init.c o_fips.c
57SRCS+= mem_clr.c crypto_init.c crypto_lock.c 57SRCS+= mem_clr.c crypto_init.c crypto_lock.c
58 58
59# aes/ 59# aes/
@@ -72,7 +72,7 @@ SRCS+= tasn_prn.c ameth_lib.c
72SRCS+= x_pkey.c x_exten.c bio_asn1.c bio_ndef.c asn_mime.c 72SRCS+= x_pkey.c x_exten.c bio_asn1.c bio_ndef.c asn_mime.c
73SRCS+= asn1_gen.c asn1_par.c asn1_old_lib.c asn1_err.c a_strnid.c 73SRCS+= asn1_gen.c asn1_par.c asn1_old_lib.c asn1_err.c a_strnid.c
74SRCS+= p5_pbe.c p5_pbev2.c p8_pkey.c asn_moid.c 74SRCS+= p5_pbe.c p5_pbev2.c p8_pkey.c asn_moid.c
75SRCS+= a_time_tm.c asn1_item.c asn1_old.c asn1_types.c asn1_lib.c 75SRCS+= a_time_tm.c asn1_item.c asn1_old.c asn1_types.c asn1_lib.c a_time_posix.c
76 76
77# bf/ 77# bf/
78SRCS+= bf_skey.c bf_ecb.c bf_cfb64.c bf_ofb64.c 78SRCS+= bf_skey.c bf_ecb.c bf_cfb64.c bf_ofb64.c
diff --git a/src/lib/libcrypto/asn1/a_time.c b/src/lib/libcrypto/asn1/a_time.c
index 03311e1b7f..a951b32049 100644
--- a/src/lib/libcrypto/asn1/a_time.c
+++ b/src/lib/libcrypto/asn1/a_time.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_time.c,v 1.34 2022/06/27 13:54:57 beck Exp $ */ 1/* $OpenBSD: a_time.c,v 1.35 2022/11/08 12:56:00 beck Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -67,7 +67,6 @@
67#include <openssl/asn1t.h> 67#include <openssl/asn1t.h>
68#include <openssl/err.h> 68#include <openssl/err.h>
69 69
70#include "o_time.h"
71#include "asn1_locl.h" 70#include "asn1_locl.h"
72 71
73const ASN1_ITEM ASN1_TIME_it = { 72const ASN1_ITEM ASN1_TIME_it = {
@@ -103,7 +102,7 @@ ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
103 time(&now); 102 time(&now);
104 memset(tm, 0, sizeof(*tm)); 103 memset(tm, 0, sizeof(*tm));
105 104
106 return gmtime_r(&now, tm) != NULL; 105 return asn1_time_time_t_to_tm(&now, tm);
107} 106}
108 107
109int 108int
diff --git a/src/lib/libcrypto/asn1/a_time_posix.c b/src/lib/libcrypto/asn1/a_time_posix.c
new file mode 100644
index 0000000000..a2c9988575
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_time_posix.c
@@ -0,0 +1,272 @@
1/*
2 * Copyright (c) 2022, Google Inc.
3 * Copyright (c) 2022, Bob Beck <beck@obtuse.com>
4 *
5 * Permission to use, copy, modify, and/or 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 ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/*
19 * Time conversion to/from POSIX time_t and struct tm, with no support
20 * for time zones other than UTC
21 */
22
23#include <inttypes.h>
24#include <limits.h>
25#include <string.h>
26#include <time.h>
27
28#define SECS_PER_HOUR (int64_t)(60 * 60)
29#define SECS_PER_DAY (int64_t)(24 * SECS_PER_HOUR)
30
31/*
32 * Is a year/month/day combination valid, in the range from year 0000
33 * to 9999?
34 */
35static int
36is_valid_date(int year, int month, int day)
37{
38 int days_in_month;
39 if (day < 1 || month < 1 || year < 0 || year > 9999)
40 return 0;
41 switch (month) {
42 case 1:
43 case 3:
44 case 5:
45 case 7:
46 case 8:
47 case 10:
48 case 12:
49 days_in_month = 31;
50 break;
51 case 4:
52 case 6:
53 case 9:
54 case 11:
55 days_in_month = 30;
56 break;
57 case 2:
58 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
59 days_in_month = 29;
60 else
61 days_in_month = 28;
62 break;
63 default:
64 return 0;
65 }
66 return day <= days_in_month;
67}
68
69/*
70 * Is a time valid? Leap seconds of 60 are not considered valid, as
71 * the POSIX time in seconds does not include them.
72 */
73static int
74is_valid_time(int hours, int minutes, int seconds)
75{
76 return hours >= 0 && minutes >= 0 && seconds >= 0 && hours <= 23 &&
77 minutes <= 59 && seconds <= 59;
78}
79
80/* Is a int64 time representing a time within our expected range? */
81static int
82is_valid_epoch_time(int64_t time)
83{
84 /* 0000-01-01 00:00:00 UTC to 9999-12-31 23:59:59 UTC */
85 return (int64_t)-62167219200 <= time && time <= (int64_t)253402300799;
86}
87
88/*
89 * Inspired by algorithms presented in
90 * https://howardhinnant.github.io/date_algorithms.html
91 * (Public Domain)
92 */
93static int
94posix_time_from_utc(int year, int month, int day, int hours, int minutes,
95 int seconds, int64_t *out_time)
96{
97 int64_t era, year_of_era, day_of_year, day_of_era, posix_days;
98
99 if (!is_valid_date(year, month, day) ||
100 !is_valid_time(hours, minutes, seconds))
101 return 0;
102 if (month <= 2)
103 year--; /* Start years on Mar 1, so leap days end a year. */
104
105 /* At this point year will be in the range -1 and 9999.*/
106 era = (year >= 0 ? year : year - 399) / 400;
107 year_of_era = year - era * 400;
108 day_of_year = (153 * (month > 2 ? month - 3 : month + 9) + 2) /
109 5 + day - 1;
110 day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era /
111 100 + day_of_year;
112 posix_days = era * 146097 + day_of_era - 719468;
113 *out_time = posix_days * SECS_PER_DAY + hours * SECS_PER_HOUR +
114 minutes * 60 + seconds;
115
116 return 1;
117}
118
119/*
120 * Inspired by algorithms presented in
121 * https://howardhinnant.github.io/date_algorithms.html
122 * (Public Domain)
123 */
124static int
125utc_from_posix_time(int64_t time, int *out_year, int *out_month, int *out_day,
126 int *out_hours, int *out_minutes, int *out_seconds)
127{
128 int64_t days, leftover_seconds, era, day_of_era, year_of_era,
129 day_of_year, month_of_year;
130
131 if (!is_valid_epoch_time(time))
132 return 0;
133
134 days = time / SECS_PER_DAY;
135 leftover_seconds = time % SECS_PER_DAY;
136 if (leftover_seconds < 0) {
137 days--;
138 leftover_seconds += SECS_PER_DAY;
139 }
140 days += 719468; /* Shift to starting epoch of Mar 1 0000. */
141
142 /* At this point, days will be in the range -61 and 3652364. */
143 era = (days > 0 ? days : days - 146096) / 146097;
144 day_of_era = days - era * 146097;
145 year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 -
146 day_of_era / 146096) /
147 365;
148 *out_year = year_of_era + era * 400; /* Year starts on Mar 1 */
149 day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 -
150 year_of_era / 100);
151 month_of_year = (5 * day_of_year + 2) / 153;
152 *out_month = (month_of_year < 10 ? month_of_year + 3 :
153 month_of_year - 9);
154 if (*out_month <= 2)
155 (*out_year)++; /* Adjust year back to Jan 1 start of year. */
156
157 *out_day = day_of_year - (153 * month_of_year + 2) / 5 + 1;
158 *out_hours = leftover_seconds / SECS_PER_HOUR;
159 leftover_seconds %= SECS_PER_HOUR;
160 *out_minutes = leftover_seconds / 60;
161 *out_seconds = leftover_seconds % 60;
162
163 return 1;
164}
165
166static int
167asn1_time_tm_to_posix(const struct tm *tm, int64_t *out)
168{
169 /* Ensure additions below do not overflow */
170 if (tm->tm_year > 9999)
171 return 0;
172 if (tm->tm_mon > 12)
173 return 0;
174
175 return posix_time_from_utc(tm->tm_year + 1900, tm->tm_mon + 1,
176 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, out);
177}
178
179static int
180asn1_time_posix_to_tm(int64_t time, struct tm *out_tm)
181{
182 memset(out_tm, 0, sizeof(struct tm));
183 if (!utc_from_posix_time(time, &out_tm->tm_year, &out_tm->tm_mon,
184 &out_tm->tm_mday, &out_tm->tm_hour, &out_tm->tm_min,
185 &out_tm->tm_sec))
186 return 0;
187
188 out_tm->tm_year -= 1900;
189 out_tm->tm_mon -= 1;
190
191 return 1;
192}
193
194int
195asn1_time_tm_to_time_t(const struct tm *tm, time_t *out)
196{
197 int64_t posix_time;
198
199 if (!asn1_time_tm_to_posix(tm, &posix_time))
200 return 0;
201
202#ifdef SMALL_TIME_T
203 /* For portable. */
204 if (sizeof(time_t) == sizeof(int32_t) &&
205 (posix_time > INT32_MAX || posix_time < INT32_MIN))
206 return 0;
207#endif
208
209 *out = posix_time;
210 return 1;
211}
212
213int
214asn1_time_time_t_to_tm(const time_t *time, struct tm *out_tm)
215{
216 int64_t posix_time = *time;
217
218 return asn1_time_posix_to_tm(posix_time, out_tm);
219}
220
221int
222OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
223{
224 int64_t posix_time;
225
226 /* Ensure additions below do not overflow */
227 if (tm->tm_year > 9999)
228 return 0;
229 if (tm->tm_mon > 12)
230 return 0;
231
232 if (!posix_time_from_utc(tm->tm_year + 1900, tm->tm_mon + 1,
233 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, &posix_time))
234 return 0;
235
236 if (!utc_from_posix_time(posix_time + off_day * SECS_PER_DAY +
237 offset_sec, &tm->tm_year, &tm->tm_mon, &tm->tm_mday, &tm->tm_hour,
238 &tm->tm_min, &tm->tm_sec))
239 return 0;
240
241 tm->tm_year -= 1900;
242 tm->tm_mon -= 1;
243
244 return 1;
245}
246
247int
248OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
249 const struct tm *to)
250{
251 int64_t time_to, time_from, timediff, daydiff;
252
253 if (!posix_time_from_utc(to->tm_year + 1900, to->tm_mon + 1,
254 to->tm_mday, to->tm_hour, to->tm_min, to->tm_sec, &time_to))
255 return 0;
256
257 if (!posix_time_from_utc(from->tm_year + 1900, from->tm_mon + 1,
258 from->tm_mday, from->tm_hour, from->tm_min,
259 from->tm_sec, &time_from))
260 return 0;
261
262 timediff = time_to - time_from;
263 daydiff = timediff / SECS_PER_DAY;
264 timediff %= SECS_PER_DAY;
265 if (daydiff > INT_MAX || daydiff < INT_MIN)
266 return 0;
267
268 *out_secs = timediff;
269 *out_days = daydiff;
270
271 return 1;
272}
diff --git a/src/lib/libcrypto/asn1/a_time_tm.c b/src/lib/libcrypto/asn1/a_time_tm.c
index cb677ae93e..2ae8430a0d 100644
--- a/src/lib/libcrypto/asn1/a_time_tm.c
+++ b/src/lib/libcrypto/asn1/a_time_tm.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_time_tm.c,v 1.24 2022/07/04 14:39:43 tb Exp $ */ 1/* $OpenBSD: a_time_tm.c,v 1.25 2022/11/08 12:56:00 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 * 4 *
@@ -25,7 +25,7 @@
25#include <openssl/err.h> 25#include <openssl/err.h>
26 26
27#include "bytestring.h" 27#include "bytestring.h"
28#include "o_time.h" 28#include "asn1_locl.h"
29 29
30#define RFC5280 0 30#define RFC5280 0
31#define GENTIME_LENGTH 15 31#define GENTIME_LENGTH 15
@@ -68,7 +68,7 @@ ASN1_time_tm_clamp_notafter(struct tm *tm)
68 struct tm broken_os_epoch_tm; 68 struct tm broken_os_epoch_tm;
69 time_t broken_os_epoch_time = INT_MAX; 69 time_t broken_os_epoch_time = INT_MAX;
70 70
71 if (gmtime_r(&broken_os_epoch_time, &broken_os_epoch_tm) == NULL) 71 if (!OPENSSL_gmtime(&broken_os_epoch_time, &broken_os_epoch_tm))
72 return 0; 72 return 0;
73 73
74 if (ASN1_time_tm_cmp(tm, &broken_os_epoch_tm) == 1) 74 if (ASN1_time_tm_cmp(tm, &broken_os_epoch_tm) == 1)
@@ -379,7 +379,7 @@ ASN1_TIME_adj_internal(ASN1_TIME *s, time_t t, int offset_day, long offset_sec,
379{ 379{
380 struct tm tm; 380 struct tm tm;
381 381
382 if (gmtime_r(&t, &tm) == NULL) 382 if (!asn1_time_time_t_to_tm(&t, &tm))
383 return (NULL); 383 return (NULL);
384 384
385 if (offset_day != 0 || offset_sec != 0) { 385 if (offset_day != 0 || offset_sec != 0) {
@@ -410,7 +410,7 @@ ASN1_TIME_set_tm(ASN1_TIME *s, struct tm *tm)
410{ 410{
411 time_t t; 411 time_t t;
412 412
413 if ((t = timegm(tm)) == -1) 413 if (!asn1_time_tm_to_time_t(tm, &t))
414 return NULL; 414 return NULL;
415 return (ASN1_TIME_adj(s, t, 0, 0)); 415 return (ASN1_TIME_adj(s, t, 0, 0));
416} 416}
@@ -475,7 +475,7 @@ ASN1_TIME_cmp_time_t_internal(const ASN1_TIME *s, time_t t2, int mode)
475 if (ASN1_time_parse(s->data, s->length, &tm1, mode) == -1) 475 if (ASN1_time_parse(s->data, s->length, &tm1, mode) == -1)
476 return -2; 476 return -2;
477 477
478 if (gmtime_r(&t2, &tm2) == NULL) 478 if (!asn1_time_time_t_to_tm(&t2, &tm2))
479 return -2; 479 return -2;
480 480
481 return ASN1_time_tm_cmp(&tm1, &tm2); 481 return ASN1_time_tm_cmp(&tm1, &tm2);
diff --git a/src/lib/libcrypto/asn1/asn1_locl.h b/src/lib/libcrypto/asn1/asn1_locl.h
index ca0b7d7d6e..e15b98b7c3 100644
--- a/src/lib/libcrypto/asn1/asn1_locl.h
+++ b/src/lib/libcrypto/asn1/asn1_locl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1_locl.h,v 1.39 2022/09/11 17:22:52 tb Exp $ */ 1/* $OpenBSD: asn1_locl.h,v 1.40 2022/11/08 12:56:00 beck Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006. 3 * project 2006.
4 */ 4 */
@@ -233,5 +233,10 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
233int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp); 233int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp);
234ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, 234ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
235 long length); 235 long length);
236int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec);
237int OPENSSL_gmtime_diff(int *pday, int *psec, const struct tm *from,
238 const struct tm *to);
239int asn1_time_time_t_to_tm(const time_t *time, struct tm *out_tm);
240int asn1_time_tm_to_time_t(const struct tm *tm, time_t *out);
236 241
237__END_HIDDEN_DECLS 242__END_HIDDEN_DECLS
diff --git a/src/lib/libcrypto/o_time.c b/src/lib/libcrypto/o_time.c
deleted file mode 100644
index 3f164c7fde..0000000000
--- a/src/lib/libcrypto/o_time.c
+++ /dev/null
@@ -1,242 +0,0 @@
1/* $OpenBSD: o_time.c,v 1.16 2021/10/27 09:50:56 beck Exp $ */
2/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3 * project 2001.
4 */
5/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 * project 2008.
7 */
8/* ====================================================================
9 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
22 *
23 * 3. All advertising materials mentioning features or use of this
24 * software must display the following acknowledgment:
25 * "This product includes software developed by the OpenSSL Project
26 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
27 *
28 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
29 * endorse or promote products derived from this software without
30 * prior written permission. For written permission, please contact
31 * licensing@OpenSSL.org.
32 *
33 * 5. Products derived from this software may not be called "OpenSSL"
34 * nor may "OpenSSL" appear in their names without prior written
35 * permission of the OpenSSL Project.
36 *
37 * 6. Redistributions of any form whatsoever must retain the following
38 * acknowledgment:
39 * "This product includes software developed by the OpenSSL Project
40 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
43 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
46 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53 * OF THE POSSIBILITY OF SUCH DAMAGE.
54 * ====================================================================
55 *
56 * This product includes cryptographic software written by Eric Young
57 * (eay@cryptsoft.com). This product includes software written by Tim
58 * Hudson (tjh@cryptsoft.com).
59 *
60 */
61
62#include <string.h>
63
64#include "o_time.h"
65
66/* Take a tm structure and add an offset to it. This avoids any OS issues
67 * with restricted date types and overflows which cause the year 2038
68 * problem.
69 */
70
71#define SECS_PER_DAY (24 * 60 * 60)
72
73static long date_to_julian(int y, int m, int d);
74static void julian_to_date(long jd, int *y, int *m, int *d);
75static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
76 long *pday, int *psec);
77
78int
79OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
80{
81 int offset_hms, offset_day;
82 long time_jd;
83 int time_year, time_month, time_day;
84 /* split offset into days and day seconds */
85 offset_day = offset_sec / SECS_PER_DAY;
86 /* Avoid sign issues with % operator */
87 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
88 offset_day += off_day;
89 /* Add current time seconds to offset */
90 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
91 /* Adjust day seconds if overflow */
92 if (offset_hms >= SECS_PER_DAY) {
93 offset_day++;
94 offset_hms -= SECS_PER_DAY;
95 } else if (offset_hms < 0) {
96 offset_day--;
97 offset_hms += SECS_PER_DAY;
98 }
99
100 /* Convert date of time structure into a Julian day number.
101 */
102
103 time_year = tm->tm_year + 1900;
104 time_month = tm->tm_mon + 1;
105 time_day = tm->tm_mday;
106
107 time_jd = date_to_julian(time_year, time_month, time_day);
108
109 /* Work out Julian day of new date */
110 time_jd += offset_day;
111
112 if (time_jd < 0)
113 return 0;
114
115 /* Convert Julian day back to date */
116
117 julian_to_date(time_jd, &time_year, &time_month, &time_day);
118
119 if (time_year < 1900 || time_year > 9999)
120 return 0;
121
122 /* Update tm structure */
123
124 tm->tm_year = time_year - 1900;
125 tm->tm_mon = time_month - 1;
126 tm->tm_mday = time_day;
127
128 tm->tm_hour = offset_hms / 3600;
129 tm->tm_min = (offset_hms / 60) % 60;
130 tm->tm_sec = offset_hms % 60;
131
132 return 1;
133
134}
135
136int
137OPENSSL_gmtime_diff(int *pday, int *psec, const struct tm *from,
138 const struct tm *to)
139{
140 int from_sec, to_sec, diff_sec;
141 long from_jd, to_jd, diff_day;
142
143 if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
144 return 0;
145 if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
146 return 0;
147 diff_day = to_jd - from_jd;
148 diff_sec = to_sec - from_sec;
149 /* Adjust differences so both positive or both negative */
150 if (diff_day > 0 && diff_sec < 0) {
151 diff_day--;
152 diff_sec += SECS_PER_DAY;
153 }
154 if (diff_day < 0 && diff_sec > 0) {
155 diff_day++;
156 diff_sec -= SECS_PER_DAY;
157 }
158
159 if (pday)
160 *pday = (int)diff_day;
161 if (psec)
162 *psec = diff_sec;
163
164 return 1;
165
166}
167
168/* Convert tm structure and offset into julian day and seconds */
169static int
170julian_adj(const struct tm *tm, int off_day, long offset_sec, long *pday,
171 int *psec)
172{
173 int time_year, time_month, time_day;
174 long offset_day, time_jd;
175 int offset_hms;
176
177 /* split offset into days and day seconds */
178 offset_day = offset_sec / SECS_PER_DAY;
179 /* Avoid sign issues with % operator */
180 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
181 offset_day += off_day;
182 /* Add current time seconds to offset */
183 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
184 /* Adjust day seconds if overflow */
185 if (offset_hms >= SECS_PER_DAY) {
186 offset_day++;
187 offset_hms -= SECS_PER_DAY;
188 } else if (offset_hms < 0) {
189 offset_day--;
190 offset_hms += SECS_PER_DAY;
191 }
192
193 /*
194 * Convert date of time structure into a Julian day number.
195 */
196
197 time_year = tm->tm_year + 1900;
198 time_month = tm->tm_mon + 1;
199 time_day = tm->tm_mday;
200
201 time_jd = date_to_julian(time_year, time_month, time_day);
202
203 /* Work out Julian day of new date */
204 time_jd += offset_day;
205
206 if (time_jd < 0)
207 return 0;
208
209 *pday = time_jd;
210 *psec = offset_hms;
211
212 return 1;
213}
214
215/* Convert date to and from julian day
216 * Uses Fliegel & Van Flandern algorithm
217 */
218static long
219date_to_julian(int y, int m, int d)
220{
221 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
222 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
223 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 +
224 d - 32075;
225}
226
227static void
228julian_to_date(long jd, int *y, int *m, int *d)
229{
230 long L = jd + 68569;
231 long n = (4 * L) / 146097;
232 long i, j;
233
234 L = L - (146097 * n + 3) / 4;
235 i = (4000 * (L + 1)) / 1461001;
236 L = L - (1461 * i) / 4 + 31;
237 j = (80 * L) / 2447;
238 *d = L - (2447 * j) / 80;
239 L = j / 11;
240 *m = j + 2 - (12 * L);
241 *y = 100 * (n - 49) + i + L;
242}
diff --git a/src/lib/libcrypto/o_time.h b/src/lib/libcrypto/o_time.h
deleted file mode 100644
index 064f2cc237..0000000000
--- a/src/lib/libcrypto/o_time.h
+++ /dev/null
@@ -1,71 +0,0 @@
1/* $OpenBSD: o_time.h,v 1.8 2021/10/27 09:50:56 beck Exp $ */
2/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3 * project 2001.
4 */
5/* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#ifndef HEADER_O_TIME_H
60#define HEADER_O_TIME_H
61
62#include <time.h>
63
64__BEGIN_HIDDEN_DECLS
65
66int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec);
67int OPENSSL_gmtime_diff(int *pday, int *psec, const struct tm *from,
68 const struct tm *to);
69
70__END_HIDDEN_DECLS
71#endif