Adapt ClientHello writing to case of single hardcoded ciphersuite
This commit modifies the ClientHello writing routine ssl_write_client_hello
in ssl_cli.c to switch between
(a) listing all runtime configured ciphersuites
(in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is not defined)
(b) listing just the single hardcoded ciphersuite
(in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is defined)
The approach taken is to introduce a pair of helper macros
MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info )
MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE
which when delimiting a block of code lead to that block of
code being run once for each ciphersuite that's enabled in the
context `ssl` and version `ver`, referenced through the (fresh)
`info` variable. Internally, this is implemented either through
a plain `for` loop traversing the runtime configured ciphersuite
list (if MBEDTLS_SSL_SINGLE_CIPHERSUITE is disabled) or by just
hardcoding `info` to the single enabled ciphersuite (if
MBEDTLS_SSL_SINGLE_CIPHERSUITE is enabled).
These helper macros will prove useful whereever previous code
traversed the runtime configured ciphersuite list, but adaptations
of those occasions outside ClientHello writing are left for later
commits.
diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h
index b2382fa..bca249d 100644
--- a/include/mbedtls/ssl_internal.h
+++ b/include/mbedtls/ssl_internal.h
@@ -1430,4 +1430,41 @@
}
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
+/*
+ * Macros for the traversal of the list of all enabled ciphersuites.
+ * This is implemented as a plain loop in case we have a runtime
+ * configurable list of ciphersuites, and as a simple variable
+ * instantiation in case a single ciphersuite is enabled at
+ * compile-time.
+ */
+#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE)
+
+#define MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) \
+ { \
+ int const *__id_ptr; \
+ for( __id_ptr=(ssl)->conf->ciphersuite_list[ (ver) ]; \
+ *__id_ptr != 0; __id_ptr++ ) \
+ { \
+ const int __id = *__id_ptr; \
+ mbedtls_ssl_ciphersuite_handle_t info; \
+ info = mbedtls_ssl_ciphersuite_from_id( __id ); \
+ if( info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) \
+ continue;
+
+#define MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE \
+ } \
+ }
+
+#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */
+
+#define MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) \
+ { \
+ const mbedtls_ssl_ciphersuite_handle_t info = \
+ MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE;
+
+#define MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE \
+ }
+
+#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */
+
#endif /* ssl_internal.h */