Skip to content

Commit 0c79e67

Browse files
sanity checks on input and adjust return value
1 parent efac20f commit 0c79e67

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

src/wh_auth.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ int wh_Auth_Logout(whAuthContext* context, whUserId user_id)
170170
rc = context->cb->Logout(context->context, context->user.user_id,
171171
user_id);
172172
if (rc == WH_ERROR_OK) {
173-
/* Clear the user context */
174-
memset(&context->user, 0, sizeof(whAuthUser));
173+
/* Clear local session only when logging out the current user */
174+
if (user_id == context->user.user_id) {
175+
memset(&context->user, 0, sizeof(whAuthUser));
176+
}
175177
}
176178

177179
(void)WH_AUTH_UNLOCK(context);

src/wh_auth_base.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ int wh_Auth_BaseUserAdd(void* context, const char* username,
263263
int i;
264264
int userId = WH_USER_ID_INVALID;
265265

266+
if (username == NULL || out_user_id == NULL) {
267+
return WH_ERROR_BADARGS;
268+
}
269+
266270
/* Validate method is supported if credentials are provided */
267271
if (credentials != NULL && credentials_len > 0) {
268272
if (method != WH_AUTH_METHOD_PIN

src/wh_message_auth.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int wh_MessageAuth_TranslateLoginRequest(
5959
}
6060

6161
if (src_size < header_size) {
62-
return WH_ERROR_BADARGS;
62+
return WH_ERROR_BUFFER_SIZE;
6363
}
6464

6565
src_header = (const whMessageAuth_LoginRequest*)src_packet;
@@ -76,7 +76,7 @@ int wh_MessageAuth_TranslateLoginRequest(
7676
expected_size = (uint16_t)(header_size + dest_header->auth_data_len);
7777
if (dest_header->auth_data_len > WH_MESSAGE_AUTH_LOGIN_MAX_AUTH_DATA_LEN ||
7878
src_size < expected_size) {
79-
return WH_ERROR_BADARGS;
79+
return WH_ERROR_BUFFER_SIZE;
8080
}
8181

8282
return 0;
@@ -230,7 +230,7 @@ int wh_MessageAuth_TranslateUserAddRequest(
230230
}
231231

232232
if (src_size < header_size) {
233-
return WH_ERROR_BADARGS;
233+
return WH_ERROR_BUFFER_SIZE;
234234
}
235235

236236
src_header = (const whMessageAuth_UserAddRequest*)src_packet;
@@ -339,7 +339,7 @@ int wh_MessageAuth_TranslateUserSetCredentialsRequest(
339339
}
340340

341341
if (src_size < header_size) {
342-
return WH_ERROR_BADARGS;
342+
return WH_ERROR_BUFFER_SIZE;
343343
}
344344

345345
src_header = (const whMessageAuth_UserSetCredentialsRequest*)src_packet;
@@ -363,7 +363,7 @@ int wh_MessageAuth_TranslateUserSetCredentialsRequest(
363363
expected_size = header_size + dest_header->current_credentials_len +
364364
dest_header->new_credentials_len;
365365
if (src_size < expected_size) {
366-
return WH_ERROR_BADARGS;
366+
return WH_ERROR_BUFFER_SIZE;
367367
}
368368

369369
return 0;

test/wh_test_auth.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static whAuthCb default_auth_cb = {
131131
static whAuthContext auth_ctx = {0};
132132

133133
#ifndef WOLFHSM_CFG_NO_CRYPTO
134-
static whServerCryptoContext crypto[1] = {{.devId = INVALID_DEVID}};
134+
static whServerCryptoContext crypto[1] = {0};
135135
#endif
136136

137137
/* Setup helper for memory transport mode */
@@ -175,7 +175,7 @@ static int _whTest_Auth_SetupMemory(whClientContext** out_client)
175175

176176
#ifndef WOLFHSM_CFG_NO_CRYPTO
177177
WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init());
178-
WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, crypto->devId));
178+
WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID));
179179
#endif
180180

181181
/* Set up auth context following wh_posix_server pattern */
@@ -512,14 +512,14 @@ static int _whTest_Auth_MessageBadArgs(void)
512512
rc = wh_MessageAuth_TranslateLoginRequest(0, NULL, 0, &login_out);
513513
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
514514
rc = wh_MessageAuth_TranslateLoginRequest(0, &login_hdr, 0, &login_out);
515-
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
515+
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BUFFER_SIZE);
516516

517517
memset(&login_hdr, 0, sizeof(login_hdr));
518518
login_hdr.auth_data_len =
519519
(uint16_t)(WH_MESSAGE_AUTH_LOGIN_MAX_AUTH_DATA_LEN + 1);
520520
rc = wh_MessageAuth_TranslateLoginRequest(0, &login_hdr, sizeof(login_hdr),
521521
&login_out);
522-
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
522+
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BUFFER_SIZE);
523523

524524
rc = wh_MessageAuth_TranslateUserAddRequest(0, NULL, 0, &add_out);
525525
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
@@ -539,7 +539,7 @@ static int _whTest_Auth_MessageBadArgs(void)
539539
set_hdr.new_credentials_len = 4;
540540
rc = wh_MessageAuth_TranslateUserSetCredentialsRequest(
541541
0, &set_hdr, sizeof(set_hdr), &set_hdr);
542-
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
542+
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BUFFER_SIZE);
543543

544544
return WH_TEST_SUCCESS;
545545
}

0 commit comments

Comments
 (0)