Skip to content

Commit d22ad58

Browse files
add server simple response back of auth not enabled
1 parent f5d1a76 commit d22ad58

5 files changed

Lines changed: 60 additions & 8 deletions

File tree

examples/demo/client/wh_demo_client_all.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "wh_demo_client_nvm.h"
44
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
55
#include "wh_demo_client_auth.h"
6+
#include "wolfhsm/wh_error.h"
67
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
78
#include "wh_demo_client_keystore.h"
89
#include "wh_demo_client_crypto.h"
@@ -26,7 +27,7 @@ int wh_DemoClient_All(whClientContext* clientContext)
2627
4, &rc, &userId) != 0) {
2728
return -1;
2829
}
29-
if (rc != 0) {
30+
if (rc != WH_ERROR_OK && rc != WH_AUTH_NOT_ENABLED) {
3031
return rc;
3132
}
3233
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */

examples/demo/client/wh_demo_client_auth.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ static int wh_DemoClient_AuthPin(whClientContext* clientContext)
5050
/* login as the admin and add a new user */
5151
rc = wh_Client_AuthLogin(clientContext,
5252
WH_AUTH_METHOD_PIN, "admin", "1234", 4, &serverRc, &adminUserId);
53+
if (serverRc == WH_AUTH_NOT_ENABLED) {
54+
printf("[AUTH-DEMO] Authentication not enabled on server, "
55+
"skipping PIN demo.\n");
56+
return WH_ERROR_OK;
57+
}
58+
5359
if (rc != 0) {
5460
printf("[AUTH-DEMO] Failed to login as admin: %d\n", rc);
5561
return rc;
5662
}
63+
5764
if (serverRc != 0) {
5865
printf("[AUTH-DEMO] Server-side error logging in as admin: %d\n",
5966
(int)serverRc);
@@ -198,6 +205,12 @@ static int wh_DemoClient_AuthCertificate(whClientContext* clientContext)
198205
"1234", 4,
199206
&serverRc,
200207
&adminUserId);
208+
if (serverRc == WH_AUTH_NOT_ENABLED) {
209+
printf("[AUTH-DEMO] Authentication not enabled on server, "
210+
"skipping certificate demo.\n");
211+
return WH_ERROR_OK;
212+
}
213+
201214
if (rc != 0) {
202215
printf("[AUTH-DEMO] Failed to login as admin: %d\n", rc);
203216
return rc;
@@ -273,9 +286,16 @@ static int wh_DemoClient_AuthUserDelete(whClientContext* clientContext)
273286
"1234", 4,
274287
&serverRc,
275288
&adminUserId);
289+
if (serverRc == WH_AUTH_NOT_ENABLED) {
290+
printf("[AUTH-DEMO] Authentication not enabled on server, "
291+
"skipping user delete demo.\n");
292+
return WH_ERROR_OK;
293+
}
294+
276295
if (rc != 0) {
277296
return rc;
278297
}
298+
279299
if (serverRc != 0) {
280300
return (int)serverRc;
281301
}
@@ -328,6 +348,12 @@ static int wh_DemoClient_AuthUserSetPermissions(whClientContext* clientContext)
328348
"1234", 4,
329349
&serverRc,
330350
&adminUserId);
351+
if (serverRc == WH_AUTH_NOT_ENABLED) {
352+
printf("[AUTH-DEMO] Authentication not enabled on server, "
353+
"skipping user set permissions demo.\n");
354+
return WH_ERROR_OK;
355+
}
356+
331357
if (rc != 0) {
332358
return rc;
333359
}
@@ -394,22 +420,22 @@ int wh_DemoClient_Auth(whClientContext* clientContext)
394420

395421
printf("[AUTH-DEMO] Starting authentication demo...\n");
396422
rc = wh_DemoClient_AuthCertificate(clientContext);
397-
if (rc != 0) {
423+
if (rc != WH_ERROR_OK) {
398424
return rc;
399425
}
400426

401427
rc = wh_DemoClient_AuthPin(clientContext);
402-
if (rc != 0) {
428+
if (rc != WH_ERROR_OK) {
403429
return rc;
404430
}
405431

406432
rc = wh_DemoClient_AuthUserDelete(clientContext);
407-
if (rc != 0) {
433+
if (rc != WH_ERROR_OK) {
408434
return rc;
409435
}
410436

411437
rc = wh_DemoClient_AuthUserSetPermissions(clientContext);
412-
if (rc != 0) {
438+
if (rc != WH_ERROR_OK) {
413439
return rc;
414440
}
415441
printf("[AUTH-DEMO] Authentication demo completed.\n");

src/wh_client_auth.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,29 @@ int wh_Client_AuthLoginResponse(whClientContext* c, int32_t* out_rc,
110110
return WH_ERROR_BADARGS;
111111
}
112112

113+
if (out_user_id != NULL) {
114+
*out_user_id = WH_USER_ID_INVALID;
115+
}
116+
113117
rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size,
114118
buffer);
115119
if (rc == WH_ERROR_OK) {
116120
/* Validate response */
117121
if ((resp_group != WH_MESSAGE_GROUP_AUTH) ||
118122
(resp_action != WH_MESSAGE_AUTH_ACTION_LOGIN) ||
119123
(resp_size != sizeof(whMessageAuth_LoginResponse))) {
120-
/* Invalid message */
121124
rc = WH_ERROR_ABORTED;
125+
126+
/* check if server did not understand the request and responded with
127+
* a simple error response */
128+
if (resp_size == sizeof(whMessageAuth_SimpleResponse)) {
129+
/* NOT accepting WH_ERROR_OK from server if we got a response
130+
* other than a login response */
131+
if (out_rc != NULL && msg->rc != WH_ERROR_OK) {
132+
*out_rc = msg->rc;
133+
rc = WH_ERROR_OK;
134+
}
135+
}
122136
}
123137
else {
124138
/* Valid message */

src/wh_server.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,22 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
556556
size, data, &size, data);
557557
break;
558558

559-
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
560559
case WH_MESSAGE_GROUP_AUTH:
560+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
561561
rc = wh_Server_HandleAuthRequest(server, magic, action, seq, size,
562562
data, &size, data);
563-
break;
563+
#else
564+
/* Format simple error response indicating auth is not enabled */
565+
rc = WH_AUTH_NOT_ENABLED;
566+
if (data != NULL) {
567+
*(int32_t*)data = (int32_t)wh_Translate32(magic, (uint32_t)rc);
568+
size = sizeof(int32_t);
569+
}
570+
else {
571+
size = 0;
572+
}
564573
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
574+
break;
565575

566576
case WH_MESSAGE_GROUP_COUNTER:
567577
rc = wh_Server_HandleCounter(server, magic, action, size, data,

wolfhsm/wh_error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ enum WH_ERROR_ENUM {
7272
/* Auth error codes */
7373
WH_AUTH_LOGIN_FAILED = -2300, /* user login attempt failed */
7474
WH_AUTH_PERMISSION_ERROR = -2301, /* user attempted an action not allowed */
75+
WH_AUTH_NOT_ENABLED = -2302, /* server does not have auth feature */
7576
};
7677

7778
#define WH_SHE_ERC_NO_ERROR WH_ERROR_OK

0 commit comments

Comments
 (0)