2323
2424#include "wolfkeymgr/mod_etsi.h"
2525
26+ #include <signal.h>
27+
2628struct EtsiClientCtx {
2729 WOLFSSL_CTX * sslCtx ;
2830 WOLFSSL * ssl ;
2931 EtsiClientType type ;
32+ wolfSSL_Mutex lock ;
3033};
3134
35+
3236EtsiClientCtx * wolfEtsiClientNew (void )
3337{
3438 EtsiClientCtx * client = (EtsiClientCtx * )malloc (sizeof (EtsiClientCtx ));
3539 if (client ) {
3640 memset (client , 0 , sizeof (EtsiClientCtx ));
41+ wc_InitMutex (& client -> lock );
3742 client -> sslCtx = wolfTlsClientNew ();
3843 if (client -> sslCtx == NULL ) {
3944 XLOG (WOLFKM_LOG_ERROR , "Error creating TLS client!\n" );
@@ -47,10 +52,14 @@ EtsiClientCtx* wolfEtsiClientNew(void)
4752int wolfEtsiClientSetKey (EtsiClientCtx * client , const char * keyFile ,
4853 const char * keyPassword , const char * certFile , int fileType )
4954{
55+ int ret ;
5056 if (client == NULL ) {
5157 return WOLFKM_BAD_ARGS ;
5258 }
53- return wolfTlsSetKey (client -> sslCtx , keyFile , keyPassword , certFile , fileType );
59+ wc_LockMutex (& client -> lock );
60+ ret = wolfTlsSetKey (client -> sslCtx , keyFile , keyPassword , certFile , fileType );
61+ wc_UnLockMutex (& client -> lock );
62+ return ret ;
5463}
5564
5665int wolfEtsiClientAddCA (EtsiClientCtx * client , const char * caFile )
@@ -60,7 +69,9 @@ int wolfEtsiClientAddCA(EtsiClientCtx* client, const char* caFile)
6069 return WOLFKM_BAD_ARGS ;
6170 }
6271
72+ wc_LockMutex (& client -> lock );
6373 ret = wolfTlsAddCA (client -> sslCtx , caFile );
74+ wc_UnLockMutex (& client -> lock );
6475 return ret ;
6576}
6677
@@ -73,13 +84,15 @@ int wolfEtsiClientConnect(EtsiClientCtx* client, const char* host,
7384 return WOLFKM_BAD_ARGS ;
7485 }
7586
87+ wc_LockMutex (& client -> lock );
7688 ret = wolfTlsConnect (client -> sslCtx , & client -> ssl , host , port , timeoutSec );
7789 if (ret == 0 ) {
7890 XLOG (WOLFKM_LOG_INFO , "Connected to ETSI service\n" );
7991 }
8092 else {
8193 XLOG (WOLFKM_LOG_ERROR , "Failure connecting to ETSI service %d\n" , ret );
8294 }
95+ wc_UnLockMutex (& client -> lock );
8396
8497 return ret ;
8598}
@@ -125,12 +138,14 @@ int wolfEtsiClientGet(EtsiClientCtx* client,
125138 return WOLFKM_BAD_ARGS ;
126139 }
127140
141+ wc_LockMutex (& client -> lock );
142+
128143 /* only send request if we need to */
129144 if (type != ETSI_CLIENT_PUSH || client -> type != type ) {
130145 ret = EtsiClientMakeRequest (type , fingerprint , request , & requestSz );
131146 if (ret != 0 ) {
132147 XLOG (WOLFKM_LOG_INFO , "EtsiClientMakeRequest failed: %d\n" , ret );
133- return ret ;
148+ goto exit ;
134149 }
135150
136151 /* send key request */
@@ -141,7 +156,7 @@ int wolfEtsiClientGet(EtsiClientCtx* client,
141156 if (ret < 0 ) {
142157 XLOG (WOLFKM_LOG_INFO , "DoClientSend failed: %d (%s)\n" , ret ,
143158 wolfSSL_ERR_reason_error_string (ret ));
144- return ret ;
159+ goto exit ;
145160 }
146161 pos += ret ;
147162 }
@@ -181,6 +196,9 @@ int wolfEtsiClientGet(EtsiClientCtx* client,
181196 XLOG (WOLFKM_LOG_INFO , "Got ETSI response (%d bytes)\n" , * responseSz );
182197 }
183198
199+ exit :
200+ wc_UnLockMutex (& client -> lock );
201+
184202 return ret ;
185203}
186204
@@ -204,15 +222,18 @@ int wolfEtsiClientClose(EtsiClientCtx* client)
204222 int ret = 0 ;
205223 if (client && client -> ssl ) {
206224 /* send shutdown */
225+ wc_LockMutex (& client -> lock );
207226 ret = wolfTlsClose (client -> ssl , 1 );
208227 client -> ssl = NULL ;
228+ wc_UnLockMutex (& client -> lock );
209229 }
210230 return ret ;
211231}
212232
213233void wolfEtsiClientFree (EtsiClientCtx * client )
214234{
215235 if (client ) {
236+ wc_LockMutex (& client -> lock );
216237 if (client -> ssl ) {
217238 wolfTlsClose (client -> ssl , 0 );
218239 client -> ssl = NULL ;
@@ -221,12 +242,17 @@ void wolfEtsiClientFree(EtsiClientCtx* client)
221242 wolfTlsFree (client -> sslCtx );
222243 client -> sslCtx = NULL ;
223244 }
245+ wc_UnLockMutex (& client -> lock );
246+ wc_FreeMutex (& client -> lock );
224247 free (client );
225248 }
226249}
227250
228251int wolfEtsiClientInit (void )
229252{
253+ /* Ignore SIGPIPE */
254+ wolfSigIgnore (SIGPIPE );
255+
230256#if 0
231257 wolfSSL_Debugging_ON ();
232258#endif
0 commit comments