Skip to content

Commit c1026aa

Browse files
fix for bitmask of permissions and remove permissions return from login
1 parent 836d970 commit c1026aa

12 files changed

Lines changed: 180 additions & 91 deletions

port/posix/posix_auth.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,22 @@ int posixAuth_CheckRequestAuthorization(void* context, uint16_t user_id,
232232
}
233233
else {
234234
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);
237-
if (user->user.permissions.actionPermissions[groupIndex] &
238-
actionBitmask) {
239-
rc = WH_ERROR_OK;
235+
/* Check if action is within supported range */
236+
if (action < WH_AUTH_ACTIONS_PER_GROUP) {
237+
/* Get word index and bitmask for this action */
238+
uint32_t wordAndBit = WH_AUTH_ACTION_TO_WORD_AND_BIT(action);
239+
uint32_t wordIndex = WH_AUTH_ACTION_WORD(wordAndBit);
240+
uint32_t bitmask = WH_AUTH_ACTION_BIT(wordAndBit);
241+
242+
if (wordIndex < WH_AUTH_ACTION_WORDS &&
243+
(user->user.permissions.actionPermissions[groupIndex]
244+
[wordIndex] &
245+
bitmask)) {
246+
rc = WH_ERROR_OK;
247+
}
248+
else {
249+
rc = WH_ERROR_ACCESS;
250+
}
240251
}
241252
else {
242253
rc = WH_ERROR_ACCESS;
@@ -495,4 +506,4 @@ int posixAuth_UserSetCredentials(void* context, uint16_t user_id,
495506

496507
(void)auth_context;
497508
return rc;
498-
}
509+
}

src/wh_client_auth.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ int wh_Client_AuthLoginRequest(whClientContext* c, whAuthMethod method,
9393
}
9494

9595
int wh_Client_AuthLoginResponse(whClientContext* c, int32_t* out_rc,
96-
whUserId* out_user_id,
97-
whAuthPermissions* out_permissions)
96+
whUserId* out_user_id)
9897
{
9998
uint8_t buffer[WOLFHSM_CFG_COMM_DATA_LEN] = {0};
10099
whMessageAuth_LoginResponse* msg = (whMessageAuth_LoginResponse*)buffer;
@@ -126,8 +125,6 @@ int wh_Client_AuthLoginResponse(whClientContext* c, int32_t* out_rc,
126125
if (out_user_id != NULL) {
127126
*out_user_id = msg->user_id;
128127
}
129-
/* @TODO: Set permissions */
130-
(void)out_permissions;
131128
}
132129
}
133130
return rc;
@@ -136,8 +133,7 @@ int wh_Client_AuthLoginResponse(whClientContext* c, int32_t* out_rc,
136133
int wh_Client_AuthLogin(whClientContext* c, whAuthMethod method,
137134
const char* username, const void* auth_data,
138135
uint16_t auth_data_len, int32_t* out_rc,
139-
whUserId* out_user_id,
140-
whAuthPermissions* out_permissions)
136+
whUserId* out_user_id)
141137
{
142138
int rc;
143139

@@ -151,8 +147,7 @@ int wh_Client_AuthLogin(whClientContext* c, whAuthMethod method,
151147
}
152148

153149
do {
154-
rc = wh_Client_AuthLoginResponse(c, out_rc, out_user_id,
155-
out_permissions);
150+
rc = wh_Client_AuthLoginResponse(c, out_rc, out_user_id);
156151
} while (rc == WH_ERROR_NOTREADY);
157152

158153
return rc;

src/wh_message_auth.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ int wh_MessageAuth_TranslateLoginResponse(
9595

9696
WH_T32(magic, dest, src, rc);
9797
WH_T16(magic, dest, src, user_id);
98-
WH_T32(magic, dest, src, permissions);
98+
9999
return 0;
100100
}
101101

@@ -128,13 +128,16 @@ 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 (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);
131+
/* Serialize actionPermissions array (4*WH_NUMBER_OF_GROUPS*WH_AUTH_ACTION_WORDS bytes) */
132+
for (i = 0; i < WH_NUMBER_OF_GROUPS; i++) {
133+
int j;
134+
for (j = 0; j < WH_AUTH_ACTION_WORDS; j++) {
135+
uint32_t actionPerm = permissions->actionPermissions[i][j];
136+
buffer[idx++] = (uint8_t)(actionPerm & 0xFF);
137+
buffer[idx++] = (uint8_t)((actionPerm >> 8) & 0xFF);
138+
buffer[idx++] = (uint8_t)((actionPerm >> 16) & 0xFF);
139+
buffer[idx++] = (uint8_t)((actionPerm >> 24) & 0xFF);
140+
}
138141
}
139142

140143
/* Serialize keyIdCount (2 bytes) */
@@ -145,7 +148,7 @@ int wh_MessageAuth_FlattenPermissions(whAuthPermissions* permissions,
145148
buffer[idx++] = (uint8_t)((keyIdCount >> 8) & 0xFF);
146149

147150
/* Serialize keyIds array (4*WH_AUTH_MAX_KEY_IDS bytes) */
148-
for (i = 0; i < WH_AUTH_MAX_KEY_IDS && (idx + 3) < buffer_len; i++) {
151+
for (i = 0; i < WH_AUTH_MAX_KEY_IDS; i++) {
149152
if (i < keyIdCount) {
150153
keyId = permissions->keyIds[i];
151154
}
@@ -178,14 +181,17 @@ int wh_MessageAuth_UnflattenPermissions(uint8_t* buffer, uint16_t buffer_len,
178181
permissions->groupPermissions = buffer[idx] | (buffer[idx + 1] << 8);
179182
idx += 2;
180183

181-
/* Deserialize actionPermissions array (4*WH_NUMBER_OF_GROUPS bytes) */
182-
for (i = 0; i < WH_NUMBER_OF_GROUPS && (idx + 3) < buffer_len; i++) {
183-
permissions->actionPermissions[i] =
184-
buffer[idx] |
185-
(buffer[idx + 1] << 8) |
186-
(buffer[idx + 2] << 16) |
187-
(buffer[idx + 3] << 24);
188-
idx += 4;
184+
/* Deserialize actionPermissions array (4*WH_NUMBER_OF_GROUPS*WH_AUTH_ACTION_WORDS bytes) */
185+
for (i = 0; i < WH_NUMBER_OF_GROUPS; i++) {
186+
int j;
187+
for (j = 0; j < WH_AUTH_ACTION_WORDS; j++) {
188+
permissions->actionPermissions[i][j] =
189+
buffer[idx] |
190+
(buffer[idx + 1] << 8) |
191+
(buffer[idx + 2] << 16) |
192+
(buffer[idx + 3] << 24);
193+
idx += 4;
194+
}
189195
}
190196

191197
/* Deserialize keyIdCount (2 bytes) */
@@ -197,7 +203,7 @@ int wh_MessageAuth_UnflattenPermissions(uint8_t* buffer, uint16_t buffer_len,
197203
permissions->keyIdCount = keyIdCount;
198204

199205
/* Deserialize keyIds array (4*WH_AUTH_MAX_KEY_IDS bytes) */
200-
for (i = 0; i < WH_AUTH_MAX_KEY_IDS && (idx + 3) < buffer_len; i++) {
206+
for (i = 0; i < WH_AUTH_MAX_KEY_IDS; i++) {
201207
keyId = buffer[idx] |
202208
(buffer[idx + 1] << 8) |
203209
(buffer[idx + 2] << 16) |

src/wh_server.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
/* Server API's */
5151
#include "wolfhsm/wh_server.h"
5252
#include "wolfhsm/wh_server_nvm.h"
53+
#ifndef WOLFHSM_CFG_NO_AUTHENTICATION
54+
#include "wolfhsm/wh_auth.h"
55+
#endif /* WOLFHSM_CFG_NO_AUTHENTICATION */
5356
#include "wolfhsm/wh_server_auth.h"
5457
#include "wolfhsm/wh_server_crypto.h"
5558
#include "wolfhsm/wh_server_keystore.h"
@@ -277,6 +280,16 @@ static int _wh_Server_HandleCommRequest(whServerContext* server,
277280
{
278281
/* No message */
279282
/* Process the close action */
283+
284+
#ifndef WOLFHSM_CFG_NO_AUTHENTICATION
285+
/* Log out the current user when communication channel closes */
286+
if (server->auth != NULL && server->auth->user.user_id !=
287+
WH_USER_ID_INVALID) {
288+
whUserId user_id = server->auth->user.user_id;
289+
(void)wh_Auth_Logout(server->auth, user_id);
290+
}
291+
#endif /* WOLFHSM_CFG_NO_AUTHENTICATION */
292+
280293
wh_Server_SetConnected(server, WH_COMM_DISCONNECTED);
281294
*out_resp_size = 0;
282295

@@ -350,7 +363,6 @@ static uint16_t _wh_Server_FormatAuthErrorResponse(uint16_t magic,
350363
whMessageAuth_LoginResponse resp = {0};
351364
resp.rc = error_code;
352365
resp.user_id = WH_USER_ID_INVALID;
353-
resp.permissions = 0;
354366
wh_MessageAuth_TranslateLoginResponse(
355367
magic, &resp,
356368
(whMessageAuth_LoginResponse*)resp_packet);

src/wh_server_auth.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ int wh_Server_HandleAuthRequest(whServerContext* server, uint16_t magic,
9595
}
9696
}
9797
}
98-
/* @TODO setting of permissions */
9998

10099
wh_MessageAuth_TranslateLoginResponse(
101100
magic, &resp, (whMessageAuth_LoginResponse*)resp_packet);

0 commit comments

Comments
 (0)