Allow compile-time configuration of timer callbacks
Introduces
- MBEDTLS_SSL_CONF_SET_TIMER
- MBEDTLS_SSL_CONF_GET_TIMER
which allows to configure timer callbacks at compile-time.
Impact on code-size:
| | GCC 8.2.1 | ARMC5 5.06 | ARMC6 6.12 |
| --- | --- | --- | --- |
| `libmbedtls.a` before | 23379 | 23981 | 26941 |
| `libmbedtls.a` after | 23351 | 23953 | 26869 |
| gain in Bytes | 28 | 28 | 72 |
diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h
index 29d9906..496f7e1 100644
--- a/include/mbedtls/check_config.h
+++ b/include/mbedtls/check_config.h
@@ -683,6 +683,13 @@
#define "MBEDTLS_SSL_CONF_SEND/RECV/RECV_TIMEOUT must be defined simultaneously"
#endif
+#if ( defined(MBEDTLS_SSL_CONF_GET_TIMER) && \
+ !defined(MBEDTLS_SSL_CONF_SET_TIMER) ) || \
+ ( !defined(MBEDTLS_SSL_CONF_GET_TIMER) && \
+ defined(MBEDTLS_SSL_CONF_SET_TIMER) )
+#define "MBEDTLS_SSL_CONF_GET_TIMER and MBEDTLS_SSL_CONF_SET_TIMER must be defined together."
+#endif
+
#if defined(MBEDTLS_SSL_TICKET_C) && !defined(MBEDTLS_CIPHER_C)
#error "MBEDTLS_SSL_TICKET_C defined, but not all prerequisites"
#endif
diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
index a767cb2..da1ae42 100644
--- a/include/mbedtls/config.h
+++ b/include/mbedtls/config.h
@@ -3601,6 +3601,18 @@
//#define MBEDTLS_SSL_CONF_CID_LEN 0
//#define MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID MBEDTLS_SSL_UNEXPECTED_CID_IGNORE
+/* The timer callbacks to use by the SSL module.
+ * If defined,
+ * - MBEDTLS_SSL_CONF_SET_TIMER must evaluate to the name of an externally
+ * defined function with signature
+ * void (*f_set_timer)( void* , uint32_t, uint32_t ),
+ * * MBEDTLS_SSL_CONF_SEND must evaluate to the name of an externally
+ * defined function with signature
+ * int (*f_get_timer)( void* ).
+ */
+//#define MBEDTLS_SSL_CONF_GET_TIMER mbedtls_timing_get_delay
+//#define MBEDTLS_SSL_CONF_SET_TIMER mbedtls_timing_set_delay
+
/* The send and receive callbacks to use by the SSL module.
* If defined,
* - MBEDTLS_SSL_CONF_RECV must evaluate to the name of an externally
diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h
index 671d107..f422d44 100644
--- a/include/mbedtls/ssl.h
+++ b/include/mbedtls/ssl.h
@@ -1183,8 +1183,12 @@
*/
void *p_timer; /*!< context for the timer callbacks */
+#if !defined(MBEDTLS_SSL_CONF_SET_TIMER)
mbedtls_ssl_set_timer_t *f_set_timer; /*!< set timer callback */
+#endif /* !MBEDTLS_SSL_CONF_SET_TIMER */
+#if !defined(MBEDTLS_SSL_CONF_GET_TIMER)
mbedtls_ssl_get_timer_t *f_get_timer; /*!< get timer callback */
+#endif /* !MBEDTLS_SSL_CONF_GET_TIMER */
/*
* Record layer (incoming data)
@@ -1779,6 +1783,8 @@
void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout );
#endif /* !MBEDTLS_SSL_CONF_READ_TIMEOUT */
+#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
+ !defined(MBEDTLS_SSL_CONF_GET_TIMER)
/**
* \brief Set the timer callbacks (Mandatory for DTLS.)
*
@@ -1796,6 +1802,12 @@
* \c mbedtls_timing_get_delay() that are suitable for using
* here, except if using an event-driven style.
*
+ * \note On constrained systems, the timer callbacks \p f_set_timer
+ * and \p f_get_timer may also be configured at compile-time
+ * via MBEDTLS_SSL_CONF_GET_TIMER and MBEDTLS_SSL_CONF_SET_TIMER.
+ * In this case, the corresponding arguments to this function
+ * are ignored.
+ *
* \note See also the "DTLS tutorial" article in our knowledge base.
* https://tls.mbed.org/kb/how-to/dtls-tutorial
*/
@@ -1803,6 +1815,18 @@
void *p_timer,
mbedtls_ssl_set_timer_t *f_set_timer,
mbedtls_ssl_get_timer_t *f_get_timer );
+#else
+/**
+ * \brief Set the context to be passed to the timer callbacks
+ * (Mandatory for DTLS.)
+ *
+ * \param ssl The SSL context to configure.
+ * \param p_timer The context to be passed to the timer callbacks.
+ *
+ */
+void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
+ void *p_timer );
+#endif
/**
* \brief Callback type: generate and write session ticket
diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h
index 8d5170c..4b85781 100644
--- a/include/mbedtls/ssl_internal.h
+++ b/include/mbedtls/ssl_internal.h
@@ -1291,6 +1291,44 @@
typedef int (*mbedtls_frng_t)( void*, unsigned char*, size_t );
+#if !defined(MBEDTLS_SSL_CONF_SET_TIMER)
+static inline mbedtls_ssl_set_timer_t* mbedtls_ssl_get_set_timer(
+ mbedtls_ssl_context const *ssl )
+{
+ return( ssl->f_set_timer );
+}
+#else /* !MBEDTLS_SSL_CONF_SET_TIMER */
+
+#define mbedtls_ssl_conf_set_timer_func MBEDTLS_SSL_CONF_SET_TIMER
+extern void mbedtls_ssl_conf_set_timer_func( void*, uint32_t, uint32_t );
+
+static inline mbedtls_ssl_set_timer_t* mbedtls_ssl_get_set_timer(
+ mbedtls_ssl_context const *ssl )
+{
+ ((void) ssl);
+ return ((mbedtls_ssl_set_timer_t*) mbedtls_ssl_conf_set_timer_func);
+}
+#endif /* MBEDTLS_SSL_CONF_SET_TIMER */
+
+#if !defined(MBEDTLS_SSL_CONF_GET_TIMER)
+static inline mbedtls_ssl_get_timer_t* mbedtls_ssl_get_get_timer(
+ mbedtls_ssl_context const *ssl )
+{
+ return( ssl->f_get_timer );
+}
+#else /* !MBEDTLS_SSL_CONF_GET_TIMER */
+
+#define mbedtls_ssl_conf_get_timer_func MBEDTLS_SSL_CONF_GET_TIMER
+extern int mbedtls_ssl_conf_get_timer_func( void* );
+
+static inline mbedtls_ssl_get_timer_t* mbedtls_ssl_get_get_timer(
+ mbedtls_ssl_context const *ssl )
+{
+ ((void) ssl);
+ return ((mbedtls_ssl_get_timer_t*) mbedtls_ssl_conf_get_timer_func);
+}
+#endif /* MBEDTLS_SSL_CONF_GET_TIMER */
+
#if !defined(MBEDTLS_SSL_CONF_RECV)
static inline mbedtls_ssl_recv_t* mbedtls_ssl_get_recv(
mbedtls_ssl_context const *ssl )