summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbeck <>2021-10-27 09:50:57 +0000
committerbeck <>2021-10-27 09:50:57 +0000
commit5b11389ce53ed7fffc2ec4099109bd281776223b (patch)
treee390498509bc0c0f8cb6b474d56f38580879b460
parent9708d91b72dd881ff18329e76e72608f97555822 (diff)
downloadopenbsd-5b11389ce53ed7fffc2ec4099109bd281776223b.tar.gz
openbsd-5b11389ce53ed7fffc2ec4099109bd281776223b.tar.bz2
openbsd-5b11389ce53ed7fffc2ec4099109bd281776223b.zip
Add ASN1_TIME_diff from OpenSSL.
The symbol is not yet exposed and will show up with tb@'s forthcoming bump ok tb@ jsing@
-rw-r--r--src/lib/libcrypto/asn1/a_time.c16
-rw-r--r--src/lib/libcrypto/asn1/asn1.h7
-rw-r--r--src/lib/libcrypto/o_time.c83
-rw-r--r--src/lib/libcrypto/o_time.h5
4 files changed, 106 insertions, 5 deletions
diff --git a/src/lib/libcrypto/asn1/a_time.c b/src/lib/libcrypto/asn1/a_time.c
index 7a3742fd70..9cb41cf885 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.27 2015/10/19 16:32:37 beck Exp $ */ 1/* $OpenBSD: a_time.c,v 1.28 2021/10/27 09:50:57 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 *
@@ -105,3 +105,17 @@ ASN1_TIME_free(ASN1_TIME *a)
105{ 105{
106 ASN1_item_free((ASN1_VALUE *)a, &ASN1_TIME_it); 106 ASN1_item_free((ASN1_VALUE *)a, &ASN1_TIME_it);
107} 107}
108
109int
110ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from,
111 const ASN1_TIME *to)
112{
113 struct tm tm_from, tm_to;
114
115 if (ASN1_time_parse(from->data, from->length, &tm_from, 0) == -1)
116 return 0;
117 if (ASN1_time_parse(from->data, from->length, &tm_to, 0) == -1)
118 return 0;
119
120 return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
121}
diff --git a/src/lib/libcrypto/asn1/asn1.h b/src/lib/libcrypto/asn1/asn1.h
index 76c294ada8..43b038adaa 100644
--- a/src/lib/libcrypto/asn1/asn1.h
+++ b/src/lib/libcrypto/asn1/asn1.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1.h,v 1.54 2020/12/08 15:06:42 tb Exp $ */ 1/* $OpenBSD: asn1.h,v 1.55 2021/10/27 09:50:57 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -773,6 +773,11 @@ ASN1_TIME *d2i_ASN1_TIME(ASN1_TIME **a, const unsigned char **in, long len);
773int i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **out); 773int i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **out);
774extern const ASN1_ITEM ASN1_TIME_it; 774extern const ASN1_ITEM ASN1_TIME_it;
775 775
776#if defined(LIBRESSL_NEW_API)
777int ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from,
778 const ASN1_TIME *to);
779#endif
780
776extern const ASN1_ITEM ASN1_OCTET_STRING_NDEF_it; 781extern const ASN1_ITEM ASN1_OCTET_STRING_NDEF_it;
777 782
778ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t); 783ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
diff --git a/src/lib/libcrypto/o_time.c b/src/lib/libcrypto/o_time.c
index 9b2e7e5b5e..3f164c7fde 100644
--- a/src/lib/libcrypto/o_time.c
+++ b/src/lib/libcrypto/o_time.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: o_time.c,v 1.15 2014/06/12 15:49:27 deraadt Exp $ */ 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 2/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
@@ -72,6 +72,8 @@
72 72
73static long date_to_julian(int y, int m, int d); 73static long date_to_julian(int y, int m, int d);
74static void julian_to_date(long jd, 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);
75 77
76int 78int
77OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) 79OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
@@ -131,6 +133,85 @@ OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
131 133
132} 134}
133 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
134/* Convert date to and from julian day 215/* Convert date to and from julian day
135 * Uses Fliegel & Van Flandern algorithm 216 * Uses Fliegel & Van Flandern algorithm
136 */ 217 */
diff --git a/src/lib/libcrypto/o_time.h b/src/lib/libcrypto/o_time.h
index 8c6301db31..064f2cc237 100644
--- a/src/lib/libcrypto/o_time.h
+++ b/src/lib/libcrypto/o_time.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: o_time.h,v 1.7 2016/12/21 15:49:29 jsing Exp $ */ 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 2/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
@@ -64,7 +64,8 @@
64__BEGIN_HIDDEN_DECLS 64__BEGIN_HIDDEN_DECLS
65 65
66int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec); 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);
67 69
68__END_HIDDEN_DECLS 70__END_HIDDEN_DECLS
69
70#endif 71#endif