From 231a0bbb848fe255974e236f4765703979527e42 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Thu, 20 Jan 2022 16:07:16 +0100 Subject: [PATCH] dtls-srtp: no ekm cross check on single threaded/no pthread conf --- examples/client/client.c | 21 +++++++++++++++------ examples/server/server.c | 23 +++++++++++++++-------- tests/suites.c | 7 ++++--- wolfssl/test.h | 19 +++++-------------- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index 8927c6c4d..7278b3d4c 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -1770,11 +1770,18 @@ static void Usage(void) * calls srtp_helper_get_ekm() to wait and then get the ekm computed by the * server, then check if it matches the one computed by itself. */ -static int client_srtp_test(WOLFSSL *ssl, srtp_test_helper *srtp_helper) +static int client_srtp_test(WOLFSSL *ssl, func_args *args) { - byte *srtp_secret, *other_secret = NULL, *p; - size_t srtp_secret_length, other_size = 0; + size_t srtp_secret_length; + byte *srtp_secret, *p; int ret; +#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) + srtp_test_helper *srtp_helper = args->srtp_helper; + byte *other_secret = NULL; + size_t other_size = 0; +#else + (void)args; +#endif ret = wolfSSL_export_dtls_srtp_keying_material(ssl, NULL, &srtp_secret_length); @@ -1797,11 +1804,12 @@ static int client_srtp_test(WOLFSSL *ssl, srtp_test_helper *srtp_helper) return ret; } - printf("DTLS SRTP: Exported key material:\n"); + printf("DTLS SRTP: Exported key material: "); for (p = srtp_secret; p < srtp_secret + srtp_secret_length; p++) printf("%02X", *p); printf("\n"); +#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) if (srtp_helper != NULL) { srtp_helper_get_ekm(srtp_helper, &other_secret, &other_size); @@ -1817,6 +1825,7 @@ static int client_srtp_test(WOLFSSL *ssl, srtp_test_helper *srtp_helper) /* we are delegated from server to free this buffer */ XFREE(other_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -3976,7 +3985,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) #ifdef WOLFSSL_SRTP if (dtlsSrtpProfiles != NULL) { - err = client_srtp_test(ssl, ((func_args*)args)->srtp_helper); + err = client_srtp_test(ssl, (func_args*)args); if (err != 0) { if (exitWithRet) { ((func_args*)args)->return_code = err; @@ -4341,7 +4350,7 @@ exit: StartTCP(); -#ifdef WOLFSSL_SRTP +#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) args.srtp_helper = NULL; #endif args.argc = argc; diff --git a/examples/server/server.c b/examples/server/server.c index 8bec45332..2d08083ab 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -1290,11 +1290,16 @@ static void Usage(void) * calls srtp_helper_set_ekm() to wake the client and share the ekm with * him. The client will check that the ekm matches the one computed by itself. */ -static int server_srtp_test(WOLFSSL *ssl, srtp_test_helper *srtp_helper) +static int server_srtp_test(WOLFSSL *ssl, func_args *args) { size_t srtp_secret_length; byte *srtp_secret, *p; int ret; +#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) + srtp_test_helper *srtp_helper = args->srtp_helper; +#else + (void)args; +#endif ret = wolfSSL_export_dtls_srtp_keying_material(ssl, NULL, &srtp_secret_length); @@ -1317,20 +1322,22 @@ static int server_srtp_test(WOLFSSL *ssl, srtp_test_helper *srtp_helper) return ret; } - printf("DTLS SRTP: Exported key material:\n"); + printf("DTLS SRTP: Exported key material: "); for (p = srtp_secret; p < srtp_secret + srtp_secret_length; p++) printf("%02X", *p); printf("\n"); - if (srtp_helper != NULL) { +#if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) + if (srtp_helper != NULL) { srtp_helper_set_ekm(srtp_helper, srtp_secret, srtp_secret_length); + /* client code will free srtp_secret buffer after checking for correctness */ + return 0; } - else { - XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } +#endif /* _POSIX_THREADS */ + XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER); return 0; } #endif @@ -3146,7 +3153,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) #ifdef WOLFSSL_SRTP if (dtlsSrtpProfiles != NULL) { - err = server_srtp_test(ssl, ((func_args*)args)->srtp_helper); + err = server_srtp_test(ssl, (func_args*)args); if (err != 0) { if (exitWithRet) { ((func_args*)args)->return_code = err; @@ -3423,7 +3430,7 @@ exit: args.argv = argv; args.signal = &ready; args.return_code = 0; -#ifdef WOLFSSL_SRTP +#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) args.srtp_helper = NULL; #endif InitTcpReady(&ready); diff --git a/tests/suites.c b/tests/suites.c index 39764ff97..d42040ada 100644 --- a/tests/suites.c +++ b/tests/suites.c @@ -321,7 +321,7 @@ static int execute_test_case(int svr_argc, char** svr_argv, int reqClientCert; #endif -#ifdef WOLFSSL_SRTP +#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) srtp_test_helper srtp_helper; #endif /* Is Valid Cipher and Version Checks */ @@ -452,11 +452,12 @@ static int execute_test_case(int svr_argc, char** svr_argv, InitTcpReady(&ready); -#ifdef WOLFSSL_SRTP +#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) srtp_helper_init(&srtp_helper); cliArgs.srtp_helper = &srtp_helper; svrArgs.srtp_helper = &srtp_helper; #endif + #ifdef WOLFSSL_TIRTOS fdOpenSession(Task_self()); #endif @@ -570,7 +571,7 @@ static int execute_test_case(int svr_argc, char** svr_argv, #endif FreeTcpReady(&ready); -#ifdef WOLFSSL_SRTP +#if defined (WOLFSSL_SRTP) &&!defined(SINGLE_THREADED) && defined(_POSIX_THREADS) srtp_helper_free(&srtp_helper); #endif diff --git a/wolfssl/test.h b/wolfssl/test.h index 885dd2b01..33de44eb6 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -520,12 +520,10 @@ typedef struct callback_functions { unsigned char loadToSSL:1; } callback_functions; -#ifdef WOLFSSL_SRTP +#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) typedef struct srtp_test_helper { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) pthread_mutex_t mutex; pthread_cond_t cond; -#endif uint8_t* server_srtp_ekm; size_t server_srtp_ekm_size; } srtp_test_helper; @@ -537,7 +535,7 @@ typedef struct func_args { int return_code; tcp_ready* signal; callback_functions *callbacks; -#ifdef WOLFSSL_SRTP +#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) srtp_test_helper* srtp_helper; #endif } func_args; @@ -643,16 +641,15 @@ err_sys_with_errno(const char* msg) extern int myoptind; extern char* myoptarg; -#ifdef WOLFSSL_SRTP +#if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS) static WC_INLINE void srtp_helper_init(srtp_test_helper *srtp) { srtp->server_srtp_ekm_size = 0; srtp->server_srtp_ekm = NULL; -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) + pthread_mutex_init(&srtp->mutex, 0); pthread_cond_init(&srtp->cond, 0); -#endif } /** @@ -667,7 +664,6 @@ static WC_INLINE void srtp_helper_init(srtp_test_helper *srtp) static WC_INLINE void srtp_helper_get_ekm(srtp_test_helper *srtp, uint8_t **ekm, size_t *size) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) pthread_mutex_lock(&srtp->mutex); if (srtp->server_srtp_ekm == NULL) @@ -681,7 +677,6 @@ static WC_INLINE void srtp_helper_get_ekm(srtp_test_helper *srtp, srtp->server_srtp_ekm_size = 0; pthread_mutex_unlock(&srtp->mutex); -#endif } /** @@ -698,7 +693,6 @@ static WC_INLINE void srtp_helper_get_ekm(srtp_test_helper *srtp, static WC_INLINE void srtp_helper_set_ekm(srtp_test_helper *srtp, uint8_t *ekm, size_t size) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) pthread_mutex_lock(&srtp->mutex); srtp->server_srtp_ekm_size = size; @@ -706,18 +700,15 @@ static WC_INLINE void srtp_helper_set_ekm(srtp_test_helper *srtp, pthread_cond_signal(&srtp->cond); pthread_mutex_unlock(&srtp->mutex); -#endif } static WC_INLINE void srtp_helper_free(srtp_test_helper *srtp) { -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) pthread_mutex_destroy(&srtp->mutex); pthread_cond_destroy(&srtp->cond); -#endif } -#endif /* WOLFSSL_SRTP */ +#endif /* WOLFSSL_SRTP && !SINGLE_THREADED && POSIX_THREADS */ /** *