]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
ssl: expose the get time function internally
authorPauli <pauli@openssl.org>
Tue, 10 May 2022 03:28:40 +0000 (13:28 +1000)
committerPauli <pauli@openssl.org>
Wed, 22 Jun 2022 03:05:40 +0000 (13:05 +1000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/18274)

include/internal/time.h [new file with mode: 0644]
ssl/build.info
ssl/d1_lib.c
ssl/time.c [new file with mode: 0644]

diff --git a/include/internal/time.h b/include/internal/time.h
new file mode 100644 (file)
index 0000000..b413eca
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#ifndef OSSL_INTERNAL_TIME_H
+# define OSSL_INTERNAL_TIME_H
+# pragma once
+
+# include <openssl/e_os2.h>     /* uint64_t */
+# include "internal/e_os.h"     /* for struct timeval */
+# include "internal/safe_math.h"
+
+/* The precision of times allows this many values per second */
+# define OSSL_TIME_SECOND 1000000000
+
+/* Macro representing the most distant future time */
+# define OSSL_TIME_INFINITY (~(OSSL_TIME)0)
+
+/*
+ * Internal type defining a time.
+ * The time datum is Unix's 1970 and at nanosecond precision, this gives
+ * a range of 584 years roughly.
+ */
+typedef uint64_t OSSL_TIME;
+
+/* Get current time */
+OSSL_TIME ossl_time_now(void);
+
+/* Convert time to timeval */
+static ossl_unused ossl_inline
+void ossl_time_time_to_timeval(OSSL_TIME t, struct timeval *out)
+{
+#ifdef _WIN32
+    out->tv_sec = (long int)(t / OSSL_TIME_SECOND);
+#else
+    out->tv_sec = (time_t)(t / OSSL_TIME_SECOND);
+#endif
+    out->tv_usec = (t % OSSL_TIME_SECOND) / (OSSL_TIME_SECOND / 1000000);
+}
+
+/* Compare two time values, return -1 if less, 1 if greater and 0 if equal */
+static ossl_unused ossl_inline
+int ossl_time_compare(OSSL_TIME a, OSSL_TIME b)
+{
+    if (a > b)
+        return 1;
+    if (a < b)
+        return -1;
+    return 0;
+}
+
+/*
+ * Arithmetic operations on times.
+ * These operations are saturating, in that an overflow or underflow returns
+ * the largest or smallest value respectively.
+ */
+OSSL_SAFE_MATH_UNSIGNED(time, OSSL_TIME)
+
+static ossl_unused ossl_inline
+OSSL_TIME ossl_time_add(OSSL_TIME a, OSSL_TIME b)
+{
+    OSSL_TIME r;
+    int err = 0;
+
+    r = safe_add_time(a, b, &err);
+    return err ? OSSL_TIME_INFINITY : r;
+}
+
+static ossl_unused ossl_inline
+OSSL_TIME ossl_time_subtract(OSSL_TIME a, OSSL_TIME b)
+{
+    OSSL_TIME r;
+    int err = 0;
+
+    r = safe_sub_time(a, b, &err);
+    return err ? 0 : r;
+}
+
+#endif
index 5c7ed7d43e013ffcd9810173910f66c818b113e4..6b6692de2fce2dfbf21190c04ae1b68a58827986 100644 (file)
@@ -20,7 +20,7 @@ IF[{- !$disabled{ktls} -}]
 ENDIF
 
 SOURCE[../libssl]=\
-        pqueue.c \
+        pqueue.c time.c \
         statem/statem_srvr.c statem/statem_clnt.c  s3_lib.c  s3_enc.c record/rec_layer_s3.c \
         statem/statem_lib.c statem/extensions.c statem/extensions_srvr.c \
         statem/extensions_clnt.c statem/extensions_cust.c s3_msg.c \
index 871c187a9dae85f2bc7d0312cf3777dc139be3de..17bcf39888239b9d0dfd02cccf23d06577e1d5a8 100644 (file)
@@ -12,6 +12,7 @@
 #include <openssl/objects.h>
 #include <openssl/rand.h>
 #include "ssl_local.h"
+#include "internal/time.h"
 
 static void get_current_time(struct timeval *t);
 static int dtls1_handshake_write(SSL *s);
@@ -409,28 +410,7 @@ int dtls1_handle_timeout(SSL *s)
 
 static void get_current_time(struct timeval *t)
 {
-#if defined(_WIN32)
-    SYSTEMTIME st;
-    union {
-        unsigned __int64 ul;
-        FILETIME ft;
-    } now;
-
-    GetSystemTime(&st);
-    SystemTimeToFileTime(&st, &now.ft);
-    /* re-bias to 1/1/1970 */
-# ifdef  __MINGW32__
-    now.ul -= 116444736000000000ULL;
-# else
-    /* *INDENT-OFF* */
-    now.ul -= 116444736000000000UI64;
-    /* *INDENT-ON* */
-# endif
-    t->tv_sec = (long)(now.ul / 10000000);
-    t->tv_usec = ((int)(now.ul % 10000000)) / 10;
-#else
-    gettimeofday(t, NULL);
-#endif
+    ossl_time_time_to_timeval(ossl_time_now(), t);
 }
 
 #define LISTEN_SUCCESS              2
diff --git a/ssl/time.c b/ssl/time.c
new file mode 100644 (file)
index 0000000..ecdd6ea
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <errno.h>
+#include <openssl/err.h>
+#include "internal/time.h"
+
+OSSL_TIME ossl_time_now(void)
+{
+#if defined(_WIN32)
+    SYSTEMTIME st;
+    union {
+        unsigned __int64 ul;
+        FILETIME ft;
+    } now;
+
+    GetSystemTime(&st);
+    SystemTimeToFileTime(&st, &now.ft);
+    /* re-bias to 1/1/1970 */
+# ifdef  __MINGW32__
+    now.ul -= 116444736000000000ULL;
+# else
+    now.ul -= 116444736000000000UI64;
+# endif
+    return ((uint64_t)now.ul) * (OSSL_TIME_SECOND / 10000000);
+#else
+    struct timeval t;
+
+    if (gettimeofday(&t, NULL) < 0) {
+        ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
+                       "calling gettimeofday()");
+        return 0;
+    }
+    if (t.tv_sec <= 0)
+        return t.tv_usec <= 0 ? 0 : t.tv_usec * (OSSL_TIME_SECOND / 1000000);
+    return ((uint64_t)t.tv_sec * 1000000 + t.tv_usec)
+           * (OSSL_TIME_SECOND / 1000000);
+#endif
+}
+