Skip to content

Commit 97480f6

Browse files
spelling fixes, cast on sizeof return, macro guard for certificate use, less verbose auth demos
1 parent 91368f0 commit 97480f6

6 files changed

Lines changed: 31 additions & 21 deletions

File tree

port/posix/posix_auth.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ typedef struct whAuthBase_User {
4545
} whAuthBase_User;
4646
static whAuthBase_User users[WH_AUTH_BASE_MAX_USERS];
4747

48+
#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO)
4849
#include <wolfssl/ssl.h>
4950
#include <wolfssl/wolfcrypt/asn.h>
51+
#endif
5052

5153
int posixAuth_Init(void* context, const void* config)
5254
{
@@ -85,7 +87,7 @@ static whAuthBase_User* posixAuth_CheckPin(const char* username, const void* aut
8587
return NULL;
8688
}
8789

88-
90+
#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO)
8991
static int posixAuth_VerifyCertificate(whAuthBase_User* found_user,
9092
const uint8_t* certificate,
9193
uint16_t certificate_len)
@@ -128,6 +130,7 @@ static whAuthBase_User* posixAuth_CheckCertificate(const char* username,
128130
}
129131
return NULL;
130132
}
133+
#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */
131134

132135
int posixAuth_Login(void* context, uint8_t client_id, whAuthMethod method,
133136
const char* username, const void* auth_data,
@@ -148,9 +151,11 @@ int posixAuth_Login(void* context, uint8_t client_id, whAuthMethod method,
148151
case WH_AUTH_METHOD_PIN:
149152
current_user = posixAuth_CheckPin(username, auth_data, auth_data_len);
150153
break;
154+
#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO)
151155
case WH_AUTH_METHOD_CERTIFICATE:
152156
current_user = posixAuth_CheckCertificate(username, auth_data, auth_data_len);
153157
break;
158+
#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */
154159
default:
155160
return WH_ERROR_BADARGS;
156161
}
@@ -300,8 +305,11 @@ int posixAuth_UserAdd(void* context, const char* username,
300305

301306
/* Validate method is supported if credentials are provided */
302307
if (credentials != NULL && credentials_len > 0) {
303-
if (method != WH_AUTH_METHOD_PIN &&
304-
method != WH_AUTH_METHOD_CERTIFICATE) {
308+
if (method != WH_AUTH_METHOD_PIN
309+
#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO)
310+
&& method != WH_AUTH_METHOD_CERTIFICATE
311+
#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */
312+
) {
305313
return WH_ERROR_BADARGS;
306314
}
307315
}
@@ -315,7 +323,7 @@ int posixAuth_UserAdd(void* context, const char* username,
315323
if (i >= WH_AUTH_BASE_MAX_USERS) {
316324
return WH_ERROR_BUFFER_SIZE;
317325
}
318-
userId = i + 1; /* save 0 fron WH_USER_ID_INVALID */
326+
userId = i + 1; /* save 0 for WH_USER_ID_INVALID */
319327
new_user = &users[i];
320328

321329
memset(new_user, 0, sizeof(whAuthBase_User));
@@ -358,7 +366,7 @@ int posixAuth_UserDelete(void* context, uint16_t current_user_id,
358366
{
359367
whAuthBase_User* user;
360368

361-
if (user_id == WH_USER_ID_INVALID || user_id >= WH_AUTH_BASE_MAX_USERS) {
369+
if (user_id == WH_USER_ID_INVALID || user_id > WH_AUTH_BASE_MAX_USERS) {
362370
return WH_ERROR_NOTFOUND;
363371
}
364372

@@ -379,7 +387,7 @@ int posixAuth_UserSetPermissions(void* context, uint16_t current_user_id,
379387
{
380388
whAuthBase_User* user;
381389

382-
if (user_id == WH_USER_ID_INVALID || user_id >= WH_AUTH_BASE_MAX_USERS) {
390+
if (user_id == WH_USER_ID_INVALID || user_id > WH_AUTH_BASE_MAX_USERS) {
383391
return WH_ERROR_NOTFOUND;
384392
}
385393

@@ -432,12 +440,16 @@ int posixAuth_UserSetCredentials(void* context, uint16_t user_id,
432440
whAuthBase_User* user;
433441
int rc = WH_ERROR_OK;
434442

435-
if (user_id == WH_USER_ID_INVALID || user_id >= WH_AUTH_BASE_MAX_USERS) {
443+
if (user_id == WH_USER_ID_INVALID || user_id > WH_AUTH_BASE_MAX_USERS) {
436444
return WH_ERROR_BADARGS;
437445
}
438446

439447
/* Validate method is supported */
440-
if (method != WH_AUTH_METHOD_PIN && method != WH_AUTH_METHOD_CERTIFICATE) {
448+
if (method != WH_AUTH_METHOD_PIN
449+
#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO)
450+
&& method != WH_AUTH_METHOD_CERTIFICATE
451+
#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */
452+
) {
441453
return WH_ERROR_BADARGS;
442454
}
443455

src/wh_client_auth.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ int wh_Client_AuthUserAddResponse(whClientContext* c, int32_t* out_rc,
292292
/* Validate response */
293293
if ((resp_group != WH_MESSAGE_GROUP_AUTH) ||
294294
(resp_action != WH_MESSAGE_AUTH_ACTION_USER_ADD) ||
295-
(resp_size < hdr_len) || (resp_size > sizeof(buffer)) ||
296-
(resp_size - hdr_len > sizeof(whMessageAuth_UserAddResponse))) {
295+
(resp_size < hdr_len) || (resp_size > (uint16_t)sizeof(buffer)) ||
296+
(resp_size - hdr_len > (uint16_t)sizeof(whMessageAuth_UserAddResponse))) {
297297
/* Invalid message */
298298
rc = WH_ERROR_ABORTED;
299299
}
@@ -415,7 +415,7 @@ int wh_Client_AuthUserGetRequest(whClientContext* c, const char* username)
415415
return WH_ERROR_BADARGS;
416416
}
417417

418-
strncpy(msg.username, username, sizeof(msg.username));
418+
strncpy(msg.username, username, sizeof(msg.username) - 1);
419419
return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_AUTH,
420420
WH_MESSAGE_AUTH_ACTION_USER_GET, sizeof(msg),
421421
&msg);

src/wh_message_auth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int wh_MessageAuth_FlattenPermissions(whAuthPermissions* permissions,
120120
uint32_t keyId;
121121

122122
if (permissions == NULL || buffer == NULL ||
123-
buffer_len < WH_FLAT_PERRMISIONS_LEN) {
123+
buffer_len < WH_FLAT_PERMISSIONS_LEN) {
124124
return WH_ERROR_BADARGS;
125125
}
126126

@@ -169,7 +169,7 @@ int wh_MessageAuth_UnflattenPermissions(uint8_t* buffer, uint16_t buffer_len,
169169
uint32_t keyId;
170170

171171
if (buffer == NULL || permissions == NULL ||
172-
buffer_len < WH_FLAT_PERRMISIONS_LEN) {
172+
buffer_len < WH_FLAT_PERMISSIONS_LEN) {
173173
return WH_ERROR_BADARGS;
174174
}
175175

test/wh_test_auth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ static int _whTest_Auth_BadArgs(void)
414414
rc = wh_Auth_UserSetCredentials(&ctx, 1, WH_AUTH_METHOD_PIN, "pin", 3,
415415
"new", 3);
416416
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
417-
rc = wh_Auth_Logout(NULL, 999); /* This test may be troublesum if the port
417+
rc = wh_Auth_Logout(NULL, 999); /* This test may be troublesome if the port
418418
* supports 999 users */
419419
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
420420

wolfhsm/wh_client.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,13 +1934,12 @@ int wh_Client_AuthLoginRequest(whClientContext* c, whAuthMethod method,
19341934
*
19351935
* This function attempts to process an authentication response message from the
19361936
* server. It validates the response and extracts the return code, user ID,
1937-
* session ID, and permissions. This function does not block; it returns
1937+
* and permissions. This function does not block; it returns
19381938
* WH_ERROR_NOTREADY if a response has not been received.
19391939
*
19401940
* @param[in] c Pointer to the client context.
19411941
* @param[out] out_rc Pointer to store the return code from the server.
19421942
* @param[out] out_user_id Pointer to store the authenticated user ID.
1943-
* @param[out] out_session_id Pointer to store the session ID.
19441943
* @param[out] out_permissions Pointer to store the user permissions.
19451944
* @return int Returns 0 on success, WH_ERROR_NOTREADY if no response is
19461945
* available, or a negative error code on failure.
@@ -1965,7 +1964,6 @@ int wh_Client_AuthLoginResponse(whClientContext* c, int32_t* out_rc,
19651964
* @param[in] auth_data_len Length of the authentication data.
19661965
* @param[out] out_rc Pointer to store the return code from the server.
19671966
* @param[out] out_user_id Pointer to store the authenticated user ID.
1968-
* @param[out] out_session_id Pointer to store the session ID.
19691967
* @param[out] out_permissions Pointer to store the user permissions.
19701968
* @return int Returns 0 on success, or a negative error code on failure.
19711969
*/

wolfhsm/wh_message_auth.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int wh_MessageAuth_TranslateLogoutRequest(
136136
* uint16_t (groupPermissions) + uint16_t[WH_NUMBER_OF_GROUPS]
137137
* (actionPermissions) + uint16_t (keyIdCount) + uint32_t[WH_AUTH_MAX_KEY_IDS]
138138
* (keyIds) */
139-
#define WH_FLAT_PERRMISIONS_LEN \
139+
#define WH_FLAT_PERMISSIONS_LEN \
140140
(2 + (2 * WH_NUMBER_OF_GROUPS) + 2 + (4 * WH_AUTH_MAX_KEY_IDS))
141141

142142
/**
@@ -165,7 +165,7 @@ int wh_MessageAuth_UnflattenPermissions(uint8_t* buffer, uint16_t buffer_len,
165165
/** User Add Request */
166166
typedef struct {
167167
char username[WH_MESSAGE_AUTH_MAX_USERNAME_LEN];
168-
uint8_t permissions[WH_FLAT_PERRMISIONS_LEN];
168+
uint8_t permissions[WH_FLAT_PERMISSIONS_LEN];
169169
uint16_t method;
170170
uint16_t credentials_len;
171171
/* credentials follow */
@@ -247,7 +247,7 @@ int wh_MessageAuth_TranslateUserGetRequest(
247247
typedef struct {
248248
int32_t rc;
249249
uint16_t user_id;
250-
uint8_t permissions[WH_FLAT_PERRMISIONS_LEN];
250+
uint8_t permissions[WH_FLAT_PERMISSIONS_LEN];
251251
} whMessageAuth_UserGetResponse;
252252

253253
/**
@@ -265,7 +265,7 @@ int wh_MessageAuth_TranslateUserGetResponse(
265265
/** User Set Permissions Request */
266266
typedef struct {
267267
uint16_t user_id;
268-
uint8_t permissions[WH_FLAT_PERRMISIONS_LEN];
268+
uint8_t permissions[WH_FLAT_PERMISSIONS_LEN];
269269
} whMessageAuth_UserSetPermissionsRequest;
270270

271271
/**

0 commit comments

Comments
 (0)