@@ -62,9 +62,22 @@ int wh_Auth_Init(whAuthContext* context, const whAuthConfig* config)
6262 context -> context = config -> context ;
6363 memset (& context -> user , 0 , sizeof (whAuthUser ));
6464
65+ #ifdef WOLFHSM_CFG_THREADSAFE
66+ /* Initialize the lock for thread-safe auth operations */
67+ rc = wh_Lock_Init (& context -> lock , config -> lockConfig );
68+ if (rc != WH_ERROR_OK ) {
69+ context -> cb = NULL ;
70+ context -> context = NULL ;
71+ return rc ;
72+ }
73+ #endif /* WOLFHSM_CFG_THREADSAFE */
74+
6575 if (context -> cb != NULL && context -> cb -> Init != NULL ) {
6676 rc = context -> cb -> Init (context -> context , config -> config );
6777 if (rc != WH_ERROR_OK ) {
78+ #ifdef WOLFHSM_CFG_THREADSAFE
79+ (void )wh_Lock_Cleanup (& context -> lock );
80+ #endif
6881 context -> cb = NULL ;
6982 context -> context = NULL ;
7083 }
@@ -76,14 +89,24 @@ int wh_Auth_Init(whAuthContext* context, const whAuthConfig* config)
7689
7790int wh_Auth_Cleanup (whAuthContext * context )
7891{
92+ int rc = WH_ERROR_OK ;
93+
7994 if ((context == NULL ) || (context -> cb == NULL )) {
8095 return WH_ERROR_BADARGS ;
8196 }
8297
8398 if (context -> cb -> Cleanup == NULL ) {
8499 return WH_ERROR_ABORTED ;
85100 }
86- return context -> cb -> Cleanup (context -> context );
101+
102+ rc = context -> cb -> Cleanup (context -> context );
103+
104+ #ifdef WOLFHSM_CFG_THREADSAFE
105+ /* Cleanup the lock for thread-safe auth operations */
106+ (void )wh_Lock_Cleanup (& context -> lock );
107+ #endif /* WOLFHSM_CFG_THREADSAFE */
108+
109+ return rc ;
87110}
88111
89112
@@ -109,6 +132,11 @@ int wh_Auth_Login(whAuthContext* context, uint8_t client_id,
109132 return WH_ERROR_BADARGS ;
110133 }
111134
135+ rc = WH_AUTH_LOCK (context );
136+ if (rc != WH_ERROR_OK ) {
137+ return rc ;
138+ }
139+
112140 /* allowing only one user logged in to an open connection at a time */
113141 if (context -> user .user_id != WH_USER_ID_INVALID ) {
114142 * loggedIn = 0 ;
@@ -125,6 +153,7 @@ int wh_Auth_Login(whAuthContext* context, uint8_t client_id,
125153 }
126154 }
127155
156+ (void )WH_AUTH_UNLOCK (context );
128157 return rc ;
129158}
130159
@@ -138,14 +167,19 @@ int wh_Auth_Logout(whAuthContext* context, whUserId user_id)
138167 return WH_ERROR_BADARGS ;
139168 }
140169
141- rc = context -> cb -> Logout (context -> context , context -> user . user_id , user_id );
170+ rc = WH_AUTH_LOCK (context );
142171 if (rc != WH_ERROR_OK ) {
143172 return rc ;
144173 }
145174
146- /* Clear the user context */
147- memset (& context -> user , 0 , sizeof (whAuthUser ));
148- return WH_ERROR_OK ;
175+ rc = context -> cb -> Logout (context -> context , context -> user .user_id , user_id );
176+ if (rc == WH_ERROR_OK ) {
177+ /* Clear the user context */
178+ memset (& context -> user , 0 , sizeof (whAuthUser ));
179+ }
180+
181+ (void )WH_AUTH_UNLOCK (context );
182+ return rc ;
149183}
150184
151185
@@ -271,51 +305,91 @@ int wh_Auth_UserAdd(whAuthContext* context, const char* username,
271305 whAuthMethod method , const void * credentials ,
272306 uint16_t credentials_len )
273307{
308+ int rc ;
309+
274310 if ((context == NULL ) || (context -> cb == NULL ) ||
275311 (context -> cb -> UserAdd == NULL )) {
276312 return WH_ERROR_BADARGS ;
277313 }
278314
279- return context -> cb -> UserAdd (context -> context , username , out_user_id ,
280- permissions , method , credentials ,
281- credentials_len );
315+ rc = WH_AUTH_LOCK (context );
316+ if (rc != WH_ERROR_OK ) {
317+ return rc ;
318+ }
319+
320+ rc = context -> cb -> UserAdd (context -> context , username , out_user_id ,
321+ permissions , method , credentials ,
322+ credentials_len );
323+
324+ (void )WH_AUTH_UNLOCK (context );
325+ return rc ;
282326}
283327
284328
285329int wh_Auth_UserDelete (whAuthContext * context , whUserId user_id )
286330{
331+ int rc ;
332+
287333 if ((context == NULL ) || (context -> cb == NULL ) ||
288334 (context -> cb -> UserDelete == NULL )) {
289335 return WH_ERROR_BADARGS ;
290336 }
291337
292- return context -> cb -> UserDelete (context -> context , context -> user .user_id ,
293- user_id );
338+ rc = WH_AUTH_LOCK (context );
339+ if (rc != WH_ERROR_OK ) {
340+ return rc ;
341+ }
342+
343+ rc = context -> cb -> UserDelete (context -> context , context -> user .user_id ,
344+ user_id );
345+
346+ (void )WH_AUTH_UNLOCK (context );
347+ return rc ;
294348}
295349
296350
297351int wh_Auth_UserSetPermissions (whAuthContext * context , whUserId user_id ,
298352 whAuthPermissions permissions )
299353{
354+ int rc ;
355+
300356 if ((context == NULL ) || (context -> cb == NULL ) ||
301357 (context -> cb -> UserSetPermissions == NULL )) {
302358 return WH_ERROR_BADARGS ;
303359 }
304360
305- return context -> cb -> UserSetPermissions (
361+ rc = WH_AUTH_LOCK (context );
362+ if (rc != WH_ERROR_OK ) {
363+ return rc ;
364+ }
365+
366+ rc = context -> cb -> UserSetPermissions (
306367 context -> context , context -> user .user_id , user_id , permissions );
368+
369+ (void )WH_AUTH_UNLOCK (context );
370+ return rc ;
307371}
308372
309373int wh_Auth_UserGet (whAuthContext * context , const char * username ,
310374 whUserId * out_user_id , whAuthPermissions * out_permissions )
311375{
376+ int rc ;
377+
312378 if ((context == NULL ) || (context -> cb == NULL ) ||
313379 (context -> cb -> UserGet == NULL )) {
314380 return WH_ERROR_BADARGS ;
315381 }
316382
317- return context -> cb -> UserGet (context -> context , username , out_user_id ,
318- out_permissions );
383+ rc = WH_AUTH_LOCK (context );
384+ if (rc != WH_ERROR_OK ) {
385+ return rc ;
386+ }
387+
388+ rc = context -> cb -> UserGet (context -> context , username , out_user_id ,
389+ out_permissions );
390+
391+ (void )WH_AUTH_UNLOCK (context );
392+ return rc ;
319393}
320394
321395int wh_Auth_UserSetCredentials (whAuthContext * context , whUserId user_id ,
@@ -325,12 +399,44 @@ int wh_Auth_UserSetCredentials(whAuthContext* context, whUserId user_id,
325399 const void * new_credentials ,
326400 uint16_t new_credentials_len )
327401{
402+ int rc ;
403+
328404 if ((context == NULL ) || (context -> cb == NULL ) ||
329405 (context -> cb -> UserSetCredentials == NULL )) {
330406 return WH_ERROR_BADARGS ;
331407 }
332408
333- return context -> cb -> UserSetCredentials (
409+ rc = WH_AUTH_LOCK (context );
410+ if (rc != WH_ERROR_OK ) {
411+ return rc ;
412+ }
413+
414+ rc = context -> cb -> UserSetCredentials (
334415 context -> context , user_id , method , current_credentials ,
335416 current_credentials_len , new_credentials , new_credentials_len );
417+
418+ (void )WH_AUTH_UNLOCK (context );
419+ return rc ;
420+ }
421+
422+
423+ /********** Lock/Unlock Functions for Thread Safety *************************/
424+
425+ #ifdef WOLFHSM_CFG_THREADSAFE
426+ int wh_Auth_Lock (whAuthContext * auth )
427+ {
428+ if (auth == NULL ) {
429+ return WH_ERROR_BADARGS ;
430+ }
431+ return wh_Lock_Acquire (& auth -> lock );
432+ }
433+
434+
435+ int wh_Auth_Unlock (whAuthContext * auth )
436+ {
437+ if (auth == NULL ) {
438+ return WH_ERROR_BADARGS ;
439+ }
440+ return wh_Lock_Release (& auth -> lock );
336441}
442+ #endif /* WOLFHSM_CFG_THREADSAFE */
0 commit comments