4747#include "wolfhsm/wh_error.h"
4848
4949#include "wolfhsm/wh_auth.h"
50+ #include "wolfhsm/wh_message_auth.h"
5051
5152
5253int wh_Auth_Init (whAuthContext * context , const whAuthConfig * config )
@@ -155,17 +156,72 @@ int wh_Auth_CheckRequestAuthorization(whAuthContext* context, uint16_t group,
155156{
156157 uint16_t user_id ;
157158 int rc ;
159+ whAuthUser * user ;
158160
159- if ((context == NULL ) || (context -> cb == NULL ) ||
160- (context -> cb -> CheckRequestAuthorization == NULL )) {
161+ if ((context == NULL ) || (context -> cb == NULL )) {
161162 return WH_ERROR_BADARGS ;
162163 }
163164
164- user_id = context -> user .user_id ;
165+ user = & context -> user ;
166+ user_id = user -> user_id ;
165167 /* @TODO add logging call here and with resulting return value */
166168
167- rc = context -> cb -> CheckRequestAuthorization (context -> context , user_id ,
168- group , action );
169+ if (user_id == WH_USER_ID_INVALID ) {
170+ /* allow user login request attempt and comm */
171+ if (group == WH_MESSAGE_GROUP_COMM ||
172+ (group == WH_MESSAGE_GROUP_AUTH &&
173+ action == WH_MESSAGE_AUTH_ACTION_LOGIN )) {
174+ rc = WH_ERROR_OK ;
175+ }
176+ else {
177+ rc = WH_ERROR_ACCESS ;
178+ }
179+ }
180+ else {
181+ int groupIndex = (group >> 8 ) & 0xFF ;
182+
183+ /* some operations a user logged in should by default have access to;
184+ * - logging out
185+ * - updating own credentials */
186+ if (group == WH_MESSAGE_GROUP_AUTH &&
187+ (action == WH_MESSAGE_AUTH_ACTION_LOGOUT ||
188+ action == WH_MESSAGE_AUTH_ACTION_USER_SET_CREDENTIALS )) {
189+ rc = WH_ERROR_OK ;
190+ }
191+ else {
192+ if (user -> permissions .groupPermissions & group ) {
193+ /* Check if action is within supported range */
194+ if (action < WH_AUTH_ACTIONS_PER_GROUP ) {
195+ /* Get word index and bitmask for this action */
196+ uint32_t wordAndBit = WH_AUTH_ACTION_TO_WORD_AND_BIT (action );
197+ uint32_t wordIndex = WH_AUTH_ACTION_WORD (wordAndBit );
198+ uint32_t bitmask = WH_AUTH_ACTION_BIT (wordAndBit );
199+
200+ if (wordIndex < WH_AUTH_ACTION_WORDS &&
201+ (user -> permissions .actionPermissions [groupIndex ]
202+ [wordIndex ] &
203+ bitmask )) {
204+ rc = WH_ERROR_OK ;
205+ }
206+ else {
207+ rc = WH_ERROR_ACCESS ;
208+ }
209+ }
210+ else {
211+ rc = WH_ERROR_ACCESS ;
212+ }
213+ }
214+ else {
215+ rc = WH_ERROR_ACCESS ;
216+ }
217+ }
218+ }
219+
220+ /* allow authorization override if callback is set */
221+ if (context -> cb -> CheckRequestAuthorization != NULL ) {
222+ rc = context -> cb -> CheckRequestAuthorization (context -> context , rc ,
223+ user_id , group , action );
224+ }
169225 return rc ;
170226}
171227
@@ -176,16 +232,36 @@ int wh_Auth_CheckKeyAuthorization(whAuthContext* context, uint32_t key_id,
176232{
177233 uint16_t user_id ;
178234 int rc ;
235+ int i ;
236+ whAuthUser * user ;
179237
180- if ((context == NULL ) || (context -> cb == NULL ) ||
181- (context -> cb -> CheckKeyAuthorization == NULL )) {
238+ if ((context == NULL ) || (context -> cb == NULL )) {
182239 return WH_ERROR_BADARGS ;
183240 }
184241
185242 user_id = context -> user .user_id ;
243+ user = & context -> user ;
244+ if (user -> user_id == WH_USER_ID_INVALID ) {
245+ return WH_ERROR_ACCESS ;
246+ }
186247
187- rc = context -> cb -> CheckKeyAuthorization (context -> context , user_id , key_id ,
188- action );
248+ /* Check if the requested key_id is in the user's keyIds array */
249+ for (i = 0 ;
250+ i < user -> permissions .keyIdCount && i < WH_AUTH_MAX_KEY_IDS ;
251+ i ++ ) {
252+ if (user -> permissions .keyIds [i ] == key_id ) {
253+ rc = WH_ERROR_OK ;
254+ break ;
255+ }
256+ }
257+
258+ (void )context ;
259+ (void )action ; /* Action could be used for future fine-grained key access
260+ control */
261+ if (context -> cb -> CheckKeyAuthorization != NULL ) {
262+ rc = context -> cb -> CheckKeyAuthorization (context -> context , rc ,
263+ user_id , key_id , action );
264+ }
189265 return rc ;
190266}
191267
0 commit comments