3131#include "wolfhsm/wh_error.h"
3232
3333#include "wolfhsm/wh_message.h"
34+ #include "wolfhsm/wh_message_auth.h"
3435#include "wolfhsm/wh_auth_base.h"
3536
3637/* simple base user list */
@@ -49,10 +50,20 @@ static whAuthBase_User users[WH_AUTH_BASE_MAX_USERS];
4950
5051int wh_AuthBase_Init (void * context , const void * config )
5152{
53+ whAuthPermissions permissions ;
54+ int rc ;
55+ uint16_t out_user_id ;
56+
5257 /* TODO: Initialize auth manager context */
5358 (void )context ;
5459 (void )config ;
55- return WH_ERROR_OK ;
60+
61+ memset (& permissions , 0xFF , sizeof (whAuthPermissions ));
62+ /* add a demo user with admin permissions */
63+ rc = wh_AuthBase_UserAdd (context , "admin" , & out_user_id , permissions ,
64+ WH_AUTH_METHOD_PIN , "1234" , 4 );
65+ printf ("Admin user added with ID: %d\n" , out_user_id );
66+ return rc ;
5667}
5768
5869int wh_AuthBase_Cleanup (void * context )
@@ -169,7 +180,8 @@ int wh_AuthBase_Login(void* context, uint8_t client_id,
169180 return WH_ERROR_OK ;
170181}
171182
172- int wh_AuthBase_Logout (void * context , uint16_t user_id )
183+ int wh_AuthBase_Logout (void * context , uint16_t current_user_id ,
184+ uint16_t user_id )
173185{
174186 whAuthBase_User * user ;
175187
@@ -182,6 +194,7 @@ int wh_AuthBase_Logout(void* context, uint16_t user_id)
182194 }
183195
184196 /* @TODO there likely should be restrictions here on who can logout who */
197+ (void )current_user_id ;
185198
186199 user = & users [user_id - 1 ];
187200 user -> user .is_active = false;
@@ -191,77 +204,90 @@ int wh_AuthBase_Logout(void* context, uint16_t user_id)
191204
192205
193206int wh_AuthBase_CheckRequestAuthorization (void * context ,
194- uint8_t client_id , uint16_t group , uint16_t action )
207+ uint16_t user_id , uint16_t group , uint16_t action )
195208{
196209 int rc ;
197- whAuthContext * auth_context = (whAuthContext * )context ;
198210
211+ printf ("In authorization check: User ID: %d, Group: %d, Action: %d\n" ,
212+ user_id , group , action );
199213
200- printf ("In authorization check: Client ID: %d, Group: %d, Action: %d\n" ,
201- client_id , group , action );
202-
203- if (auth_context == NULL ) {
204- printf ("This likely should be fail case when no authorization context is set\n" );
205- return WH_ERROR_OK ;
206- }
207-
208- if (auth_context -> user .user_id == WH_USER_ID_INVALID ) {
214+ if (user_id == WH_USER_ID_INVALID ) {
209215 /* allow user login request attempt */
210- if (group == WH_MESSAGE_GROUP_AUTH &&
211- action == WH_AUTH_ACTION_LOGIN ) {
212- rc = WH_ERROR_OK ;
216+ if (group == WH_MESSAGE_GROUP_AUTH ) {
217+ if (action == WH_MESSAGE_AUTH_ACTION_LOGIN ) {
218+ rc = WH_ERROR_OK ;
219+ }
220+ else {
221+ printf ("User does not have permissions for the action" );
222+ rc = WH_ERROR_ACCESS ;
223+ }
213224 }
214225 else {
215226 printf ("No user associated with session" );
216- rc = WH_ERROR_ACCESS ;
227+ rc = WH_ERROR_OK ; /*rc = WH_ERROR_ACCESS;*/
217228 }
218229 }
219230 else {
220231 int groupIndex = (group >> 8 ) & 0xFF ;
232+ whAuthBase_User * user = & users [user_id - 1 ];
221233
222234 /* check if user has permissions for the group and action */
223- if (auth_context -> user .permissions .groupPermissions & group ) {
224- if (auth_context -> user .permissions .actionPermissions [groupIndex ] & action ) {
225- rc = WH_ERROR_OK ;
235+
236+ /* some operations a user logged in should by default have access to;
237+ * - logging out
238+ * - updating own credentials */
239+ if (group == WH_MESSAGE_GROUP_AUTH &&
240+ (action == WH_MESSAGE_AUTH_ACTION_LOGOUT ||
241+ action == WH_MESSAGE_AUTH_ACTION_USER_SET_CREDENTIALS )) {
242+ rc = WH_ERROR_OK ;
243+ }
244+ else {
245+ if (user -> user .permissions .groupPermissions & group ) {
246+ if (user -> user .permissions .actionPermissions [groupIndex ] & action ) {
247+ rc = WH_ERROR_OK ;
248+ }
249+ else {
250+ printf ("User does not have permissions for the action" );
251+ rc = WH_ERROR_ACCESS ;
252+ }
226253 }
227254 else {
228- printf ("User does not have permissions for the action " );
255+ printf ("User does not have permissions for the group " );
229256 rc = WH_ERROR_ACCESS ;
230257 }
231258 }
232- else {
233- printf ("User does not have permissions for the group" );
234- rc = WH_ERROR_ACCESS ;
235- }
236259 }
237260
261+ (void )context ;
238262 return rc ;
239263}
240264
241265/* authorization check on key usage after the request has been parsed and before
242266 * the action is done */
243- int wh_AuthBase_CheckKeyAuthorization (void * context , uint8_t client_id ,
267+ int wh_AuthBase_CheckKeyAuthorization (void * context , uint16_t user_id ,
244268 uint32_t key_id , uint16_t action )
245269{
246- int rc ;
247- whAuthContext * auth_context = (whAuthContext * )context ;
270+ int rc = WH_ERROR_OK ;
248271
249- printf ("In key authorization check: Client ID: %d, Key ID: %d, Action: %d\n" ,
250- client_id , key_id , action );
272+ printf ("In key authorization check: User ID: %d, Key ID: %d, Action: %d\n" ,
273+ user_id , key_id , action );
251274
252- if (auth_context -> user . user_id == WH_USER_ID_INVALID ) {
275+ if (user_id == WH_USER_ID_INVALID ) {
253276 rc = WH_ERROR_ACCESS ;
254277 }
255278 else {
279+ /*
256280 if (auth_context->user.permissions.keyId == key_id) {
257281 rc = WH_ERROR_OK;
258282 }
259283 else {
260284 printf("User does not have access to the key");
261285 rc = WH_ERROR_ACCESS;
262286 }
287+ */
263288 }
264289
290+ (void )context ;
265291 return rc ;
266292}
267293
@@ -289,7 +315,6 @@ int wh_AuthBase_UserAdd(void* context, const char* username,
289315 }
290316
291317 if (i >= WH_AUTH_BASE_MAX_USERS ) {
292- printf ("User list is full" );
293318 return WH_ERROR_BUFFER_SIZE ;
294319 }
295320 userId = i + 1 ; /* save 0 fron WH_USER_ID_INVALID */
@@ -320,42 +345,42 @@ int wh_AuthBase_UserAdd(void* context, const char* username,
320345
321346int wh_AuthBase_UserDelete (void * context , uint16_t user_id )
322347{
323- whAuthContext * auth_context = (whAuthContext * )context ;
324348 whAuthBase_User * user = & users [user_id ];
325349 if (user -> user .user_id == WH_USER_ID_INVALID ) {
326350 return WH_ERROR_NOTFOUND ;
327351 }
328352 memset (user , 0 , sizeof (whAuthBase_User ));
329- (void )auth_context ;
353+ (void )context ;
330354 return WH_ERROR_OK ;
331355}
332356
333357int wh_AuthBase_UserSetPermissions (void * context , uint16_t user_id ,
334358 whAuthPermissions permissions )
335359{
336- whAuthContext * auth_context = (whAuthContext * )context ;
337360 whAuthBase_User * user = & users [user_id ];
338361 if (user -> user .user_id == WH_USER_ID_INVALID ) {
339362 return WH_ERROR_NOTFOUND ;
340363 }
341364 user -> user .permissions = permissions ;
342- (void )auth_context ;
365+ (void )context ;
343366 return WH_ERROR_OK ;
344367}
345368
346- int wh_AuthBase_UserGet (void * context , uint16_t user_id ,
347- whAuthUser * out_user )
369+
370+ int wh_AuthBase_UserGet (void * context , const char * username , uint16_t * out_user_id ,
371+ whAuthPermissions * out_permissions )
348372{
349- whAuthContext * auth_context = (whAuthContext * )context ;
350- whAuthBase_User * user = & users [user_id ];
351- if (user -> user .user_id == WH_USER_ID_INVALID ) {
373+ whAuthBase_User * user = FindUser (username );
374+ if (user == NULL ) {
352375 return WH_ERROR_NOTFOUND ;
353376 }
354- memcpy (out_user , & user -> user , sizeof (whAuthUser ));
355- (void )auth_context ;
377+ * out_user_id = user -> user .user_id ;
378+ * out_permissions = user -> user .permissions ;
379+ (void )context ;
356380 return WH_ERROR_OK ;
357381}
358382
383+
359384int wh_AuthBase_UserSetCredentials (void * context , uint16_t user_id ,
360385 whAuthMethod method ,
361386 const void * current_credentials , uint16_t current_credentials_len ,
0 commit comments