Skip to content

Commit 836d970

Browse files
update action permissions and method in message layer
1 parent a5453ff commit 836d970

7 files changed

Lines changed: 66 additions & 84 deletions

File tree

examples/demo/client/wh_demo_client_all.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
int wh_DemoClient_All(whClientContext* clientContext)
1212
{
1313
int rc = 0;
14+
whUserId userId = WH_USER_ID_INVALID;
15+
whAuthPermissions permissions;
16+
17+
/* Auth demos */
18+
rc = wh_DemoClient_Auth(clientContext);
19+
if (rc != 0) {
20+
return rc;
21+
}
22+
23+
/* Log in as an admin user for the rest of the tests */
24+
if (wh_Client_AuthLogin(clientContext, WH_AUTH_METHOD_PIN, "admin", "1234",
25+
4, &rc, &userId, &permissions) != 0) {
26+
return -1;
27+
}
28+
if (rc != 0) {
29+
return rc;
30+
}
1431

1532
/* wolfCrypt test and benchmark */
1633
#ifdef WH_DEMO_WCTEST
@@ -31,12 +48,6 @@ int wh_DemoClient_All(whClientContext* clientContext)
3148
return rc;
3249
}
3350

34-
/* Auth demos */
35-
rc = wh_DemoClient_Auth(clientContext);
36-
if (rc != 0) {
37-
return rc;
38-
}
39-
4051
/* Keystore demos */
4152
rc = wh_DemoClient_KeystoreBasic(clientContext);
4253
if (rc != 0) {

port/posix/posix_auth.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,14 @@ int posixAuth_CheckRequestAuthorization(void* context, uint16_t user_id,
206206
int rc;
207207

208208
if (user_id == WH_USER_ID_INVALID) {
209-
/* allow user login request attempt */
210-
if (group == WH_MESSAGE_GROUP_AUTH) {
211-
if (action == WH_MESSAGE_AUTH_ACTION_LOGIN) {
212-
rc = WH_ERROR_OK;
213-
}
214-
else {
215-
rc = WH_ERROR_ACCESS;
216-
}
209+
/* allow user login request attempt and comm */
210+
if (group == WH_MESSAGE_GROUP_COMM ||
211+
(group == WH_MESSAGE_GROUP_AUTH &&
212+
action == WH_MESSAGE_AUTH_ACTION_LOGIN)) {
213+
rc = WH_ERROR_OK;
217214
}
218215
else {
219-
rc = WH_ERROR_OK; /*rc = WH_ERROR_ACCESS;*/
216+
rc = WH_ERROR_ACCESS;
220217
}
221218
}
222219
else {
@@ -235,8 +232,10 @@ int posixAuth_CheckRequestAuthorization(void* context, uint16_t user_id,
235232
}
236233
else {
237234
if (user->user.permissions.groupPermissions & group) {
235+
/* action enum value (0,1,...) to bitmask (0x01,0x02,...) */
236+
uint32_t actionBitmask = WH_AUTH_ACTION_TO_BITMASK(action);
238237
if (user->user.permissions.actionPermissions[groupIndex] &
239-
action) {
238+
actionBitmask) {
240239
rc = WH_ERROR_OK;
241240
}
242241
else {
@@ -342,7 +341,8 @@ int posixAuth_UserAdd(void* context, const char* username,
342341
new_user->user.permissions.keyIds[j] = 0;
343342
}
344343
}
345-
strcpy(new_user->user.username, username);
344+
strncpy(new_user->user.username, username,
345+
sizeof(new_user->user.username) - 1);
346346
new_user->user.is_active = false;
347347
new_user->user.failed_attempts = 0;
348348
new_user->user.lockout_until = 0;

src/wh_client_auth.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ int wh_Client_AuthUserSetCredentialsRequest(
599599
/* Build message header */
600600
msg->user_id = user_id;
601601
msg->method = method;
602-
msg->WH_PAD[0] = 0;
603602
msg->current_credentials_len = current_credentials_len;
604603
msg->new_credentials_len = new_credentials_len;
605604

src/wh_message_auth.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,14 @@ int wh_MessageAuth_FlattenPermissions(whAuthPermissions* permissions,
128128
buffer[idx++] = (uint8_t)(permissions->groupPermissions & 0xFF);
129129
buffer[idx++] = (uint8_t)((permissions->groupPermissions >> 8) & 0xFF);
130130

131-
/* Serialize actionPermissions array (2*WH_NUMBER_OF_GROUPS bytes) */
132-
for (i = 0; i < WH_NUMBER_OF_GROUPS && (idx + (i * 2) + 1) < buffer_len;
133-
i++) {
134-
buffer[idx + (i * 2)] =
135-
(uint8_t)(permissions->actionPermissions[i] & 0xFF);
136-
buffer[idx + (i * 2) + 1] =
137-
(uint8_t)((permissions->actionPermissions[i] >> 8) & 0xFF);
131+
/* Serialize actionPermissions array (4*WH_NUMBER_OF_GROUPS bytes) */
132+
for (i = 0; i < WH_NUMBER_OF_GROUPS && (idx + 3) < buffer_len; i++) {
133+
uint32_t actionPerm = permissions->actionPermissions[i];
134+
buffer[idx++] = (uint8_t)(actionPerm & 0xFF);
135+
buffer[idx++] = (uint8_t)((actionPerm >> 8) & 0xFF);
136+
buffer[idx++] = (uint8_t)((actionPerm >> 16) & 0xFF);
137+
buffer[idx++] = (uint8_t)((actionPerm >> 24) & 0xFF);
138138
}
139-
idx += (2 * WH_NUMBER_OF_GROUPS);
140139

141140
/* Serialize keyIdCount (2 bytes) */
142141
keyIdCount = (permissions->keyIdCount > WH_AUTH_MAX_KEY_IDS)
@@ -146,15 +145,17 @@ int wh_MessageAuth_FlattenPermissions(whAuthPermissions* permissions,
146145
buffer[idx++] = (uint8_t)((keyIdCount >> 8) & 0xFF);
147146

148147
/* Serialize keyIds array (4*WH_AUTH_MAX_KEY_IDS bytes) */
149-
for (i = 0; i < WH_AUTH_MAX_KEY_IDS && (idx + (i * 4) + 3) < buffer_len;
150-
i++) {
148+
for (i = 0; i < WH_AUTH_MAX_KEY_IDS && (idx + 3) < buffer_len; i++) {
151149
if (i < keyIdCount) {
152150
keyId = permissions->keyIds[i];
153151
}
154152
else {
155153
keyId = 0; /* Pad with zeros */
156154
}
157-
memcpy(&buffer[idx + (i * 4)], &keyId, sizeof(keyId));
155+
buffer[idx++] = (uint8_t)(keyId & 0xFF);
156+
buffer[idx++] = (uint8_t)((keyId >> 8) & 0xFF);
157+
buffer[idx++] = (uint8_t)((keyId >> 16) & 0xFF);
158+
buffer[idx++] = (uint8_t)((keyId >> 24) & 0xFF);
158159
}
159160

160161
return 0;
@@ -177,13 +178,15 @@ int wh_MessageAuth_UnflattenPermissions(uint8_t* buffer, uint16_t buffer_len,
177178
permissions->groupPermissions = buffer[idx] | (buffer[idx + 1] << 8);
178179
idx += 2;
179180

180-
/* Deserialize actionPermissions array (2*WH_NUMBER_OF_GROUPS bytes) */
181-
for (i = 0; i < WH_NUMBER_OF_GROUPS && (idx + (i * 2) + 1) < buffer_len;
182-
i++) {
181+
/* Deserialize actionPermissions array (4*WH_NUMBER_OF_GROUPS bytes) */
182+
for (i = 0; i < WH_NUMBER_OF_GROUPS && (idx + 3) < buffer_len; i++) {
183183
permissions->actionPermissions[i] =
184-
buffer[idx + (i * 2)] | (buffer[idx + (i * 2) + 1] << 8);
184+
buffer[idx] |
185+
(buffer[idx + 1] << 8) |
186+
(buffer[idx + 2] << 16) |
187+
(buffer[idx + 3] << 24);
188+
idx += 4;
185189
}
186-
idx += (2 * WH_NUMBER_OF_GROUPS);
187190

188191
/* Deserialize keyIdCount (2 bytes) */
189192
keyIdCount = buffer[idx] | (buffer[idx + 1] << 8);
@@ -194,10 +197,13 @@ int wh_MessageAuth_UnflattenPermissions(uint8_t* buffer, uint16_t buffer_len,
194197
permissions->keyIdCount = keyIdCount;
195198

196199
/* Deserialize keyIds array (4*WH_AUTH_MAX_KEY_IDS bytes) */
197-
for (i = 0; i < WH_AUTH_MAX_KEY_IDS && (idx + (i * 4) + 3) < buffer_len;
198-
i++) {
199-
memcpy(&keyId, &buffer[idx + (i * 4)], sizeof(keyId));
200+
for (i = 0; i < WH_AUTH_MAX_KEY_IDS && (idx + 3) < buffer_len; i++) {
201+
keyId = buffer[idx] |
202+
(buffer[idx + 1] << 8) |
203+
(buffer[idx + 2] << 16) |
204+
(buffer[idx + 3] << 24);
200205
permissions->keyIds[i] = keyId;
206+
idx += 4;
201207
}
202208

203209
return 0;

test/wh_test_auth.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,9 @@ int whTest_AuthSetPermissions(whClientContext* client)
850850
WH_TEST_PRINT(" Test: Set user permissions success\n");
851851
memset(&new_perms, 0, sizeof(new_perms));
852852
new_perms.groupPermissions = WH_MESSAGE_GROUP_AUTH;
853+
/* Convert action enum value to bitmask: action 0x04 -> bit 4 -> 0x10 */
853854
new_perms.actionPermissions[(WH_MESSAGE_GROUP_AUTH >> 8) & 0xFF] =
854-
WH_MESSAGE_AUTH_ACTION_USER_ADD;
855+
WH_AUTH_ACTION_TO_BITMASK(WH_MESSAGE_AUTH_ACTION_USER_ADD);
855856
server_rc = 0;
856857
WH_TEST_RETURN_ON_FAIL(
857858
_whTest_Auth_UserSetPermsOp(client, user_id, new_perms, &server_rc));
@@ -1082,8 +1083,9 @@ int whTest_AuthRequestAuthorization(whClientContext* client)
10821083

10831084
memset(&perms, 0, sizeof(perms));
10841085
perms.groupPermissions = WH_MESSAGE_GROUP_AUTH;
1086+
/* Convert action enum value to bitmask: action 0x04 -> bit 4 -> 0x10 */
10851087
perms.actionPermissions[(WH_MESSAGE_GROUP_AUTH >> 8) & 0xFF] =
1086-
WH_MESSAGE_AUTH_ACTION_USER_ADD;
1088+
WH_AUTH_ACTION_TO_BITMASK(WH_MESSAGE_AUTH_ACTION_USER_ADD);
10871089
WH_TEST_RETURN_ON_FAIL(
10881090
_whTest_Auth_UserAddOp(client, "alloweduser", perms, WH_AUTH_METHOD_PIN,
10891091
"pass", 4, &server_rc, &allowed_user_id));

wolfhsm/wh_auth.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ typedef enum {
5757
#define WH_NUMBER_OF_GROUPS 14
5858
#define WH_AUTH_MAX_KEY_IDS \
5959
2 /* Maximum number of key IDs a user can have access to */
60+
61+
/* Convert action enum value (0,1,2,3...) to bitmask (0x01,0x02,0x04,0x08...) */
62+
#define WH_AUTH_ACTION_TO_BITMASK(_action) \
63+
(((_action) < 32) ? (1UL << (_action)) : 0)
64+
6065
typedef struct {
6166
uint16_t groupPermissions; /* bit mask of if allowed for use in group */
62-
uint16_t
67+
uint32_t
6368
actionPermissions[WH_NUMBER_OF_GROUPS]; /* array of action permissions
6469
for each group */
6570
uint16_t keyIdCount; /* Number of key IDs in the keyIds array (0 to

wolfhsm/wh_message_auth.h

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ int wh_MessageAuth_TranslateLogoutRequest(
133133
/** Logout Response (SimpleResponse) */
134134

135135
/* whAuthPermissions struct
136-
* uint16_t (groupPermissions) + uint16_t[WH_NUMBER_OF_GROUPS]
136+
* uint16_t (groupPermissions) + uint32_t[WH_NUMBER_OF_GROUPS]
137137
* (actionPermissions) + uint16_t (keyIdCount) + uint32_t[WH_AUTH_MAX_KEY_IDS]
138138
* (keyIds) */
139139
#define WH_FLAT_PERMISSIONS_LEN \
140-
(2 + (2 * WH_NUMBER_OF_GROUPS) + 2 + (4 * WH_AUTH_MAX_KEY_IDS))
140+
(2 + (4 * WH_NUMBER_OF_GROUPS) + 2 + (4 * WH_AUTH_MAX_KEY_IDS))
141141

142142
/**
143143
* @brief Flatten permissions structure into a byte buffer.
@@ -287,8 +287,7 @@ int wh_MessageAuth_TranslateUserSetPermissionsRequest(
287287
/* Header structure - credentials follow as variable-length data */
288288
typedef struct {
289289
uint16_t user_id;
290-
uint8_t method;
291-
uint8_t WH_PAD[1]; /* Padding for alignment */
290+
uint16_t method;
292291
uint16_t current_credentials_len;
293292
uint16_t new_credentials_len;
294293
/* Variable-length data follows:
@@ -315,44 +314,4 @@ int wh_MessageAuth_TranslateUserSetCredentialsRequest(
315314

316315
/** User Set Credentials Response */
317316
/* Use SimpleResponse */
318-
319-
/** Check Authorization Request */
320-
typedef struct {
321-
uint32_t session_id;
322-
uint8_t action; /* whAuthAction */
323-
uint8_t WH_PAD[3];
324-
uint32_t object_id;
325-
} whMessageAuth_CheckAuthorizationRequest;
326-
327-
/**
328-
* @brief Translate a check authorization request message between different magic numbers.
329-
*
330-
* @param[in] magic The magic number for translation.
331-
* @param[in] src Pointer to the source check authorization request message.
332-
* @param[out] dest Pointer to the destination check authorization request message.
333-
* @return int Returns 0 on success, or a negative error code on failure.
334-
*/
335-
int wh_MessageAuth_TranslateCheckAuthorizationRequest(
336-
uint16_t magic, const whMessageAuth_CheckAuthorizationRequest* src,
337-
whMessageAuth_CheckAuthorizationRequest* dest);
338-
339-
/** Check Authorization Response */
340-
typedef struct {
341-
int32_t rc;
342-
uint8_t authorized;
343-
uint8_t WH_PAD[3];
344-
} whMessageAuth_CheckAuthorizationResponse;
345-
346-
/**
347-
* @brief Translate a check authorization response message between different magic numbers.
348-
*
349-
* @param[in] magic The magic number for translation.
350-
* @param[in] src Pointer to the source check authorization response message.
351-
* @param[out] dest Pointer to the destination check authorization response message.
352-
* @return int Returns 0 on success, or a negative error code on failure.
353-
*/
354-
int wh_MessageAuth_TranslateCheckAuthorizationResponse(
355-
uint16_t magic, const whMessageAuth_CheckAuthorizationResponse* src,
356-
whMessageAuth_CheckAuthorizationResponse* dest);
357-
358317
#endif /* !WOLFHSM_WH_MESSAGE_AUTH_H_ */

0 commit comments

Comments
 (0)