Skip to content

authentication manager feature addition#270

Open
JacobBarthelmeh wants to merge 44 commits intowolfSSL:mainfrom
JacobBarthelmeh:auth_manager
Open

authentication manager feature addition#270
JacobBarthelmeh wants to merge 44 commits intowolfSSL:mainfrom
JacobBarthelmeh:auth_manager

Conversation

@JacobBarthelmeh
Copy link
Contributor

@JacobBarthelmeh JacobBarthelmeh commented Jan 16, 2026

The authentication manager feature adds support for a user login and checking a users permissions for performing a group+action. The API was designed with PKCS11 in mind.

Some things of note:

  • I added a callback function framework for checking authorization of key use based on key ID and user permissions but did not tie in that check yet. I would like to tie that in later when/if needed. This currently checks for authorization of user for a group/action that they can do. Which ties a user ID to crypto actions done.
  • The user list in port/posix/posix_auth.c is a simple list not yet in NVM. This initial simplicity is deliberate.
  • There is a TODO listed for logging of authentication events. Login failures, success, crypto actions should have logging additions in the future.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds a comprehensive authentication and authorization manager to wolfHSM, enabling user management, login/logout functionality, and permission-based access control for HSM operations.

Changes:

  • New authentication manager with PIN and certificate-based authentication support
  • Authorization system with group and action-level permission checks
  • User management APIs for adding, deleting, and modifying users and their credentials
  • Complete client and server implementation with message translation support

Reviewed changes

Copilot reviewed 22 out of 23 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
wolfhsm/wh_auth.h Core auth manager types, structures, and API definitions
wolfhsm/wh_message_auth.h Message structures and translation functions for auth operations
wolfhsm/wh_server_auth.h Server-side auth request handler declaration
wolfhsm/wh_client.h Client-side auth API function declarations
wolfhsm/wh_server.h Server context updated with auth context pointer
wolfhsm/wh_message.h New auth message group and action enums
wolfhsm/wh_error.h New auth-specific error codes
src/wh_auth.c Core auth manager implementation with callback wrappers
src/wh_message_auth.c Message translation implementations for auth messages
src/wh_server_auth.c Server-side request handler for auth operations
src/wh_client_auth.c Client-side auth API implementations
src/wh_server.c Server integration with authorization checks
src/wh_client.c Minor formatting fixes
port/posix/posix_auth.h POSIX auth backend declarations
port/posix/posix_auth.c POSIX auth backend implementation with in-memory user storage
test/wh_test_auth.h Auth test suite declarations
test/wh_test_auth.c Comprehensive auth test suite implementation
test/wh_test.c Test integration for auth tests
examples/posix/wh_posix_server/* Server configuration with auth context setup
examples/demo/client/wh_demo_client_all.c Demo integration for auth

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 24 out of 25 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 29 out of 30 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 29 out of 30 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bigbrett
Copy link
Contributor

@JacobBarthelmeh merge conflicts

@JacobBarthelmeh
Copy link
Contributor Author

Force pushed to resolve merge conflict.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 28 out of 29 changed files in this pull request and generated 17 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@bigbrett bigbrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a bug and have a few suggestions.

src/wh_auth.c Outdated
rc = WH_ERROR_OK;
}
else {
if (user->permissions.groupPermissions & group) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong / a bug. As written, it would grant users access to groups they don't belong to.

This check treats permissions.groupPermissions as a bitmask but compares it directly against group identifier values like WH_MESSAGE_GROUP_AUTH (0x0D00), which are not intended as bitmasks (e.g. not limited to powers of two). This means a permission set intended to allow only AUTH (0x0D00) also unintentionally authorizes COMM (0x0100), CRYPTO (0x0400), and COUNTER (0x0800) because those bits overlap.

You need to reconsolidate this logic to clearly define what is a identifier, what is a bit position index, and what is a bitmask. Pls make sure they are clearly identified and not easily mixed up when reading the code. Recommend helper macros to easily convert between the two representations (logical group ID and the actual group bit permission flag)

*/
int posixAuth_Login(void* context, uint8_t client_id, whAuthMethod method,
const char* username, const void* auth_data,
uint16_t auth_data_len, uint16_t* out_user_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user ID here (and elsewhere in this file) should be whUserId not uint16_t right?

unsigned char credentials[WH_AUTH_BASE_MAX_CREDENTIALS_LEN];
uint16_t credentials_len;
} whAuthBase_User;
/* TODO: Thread safety - The global users array is not protected by any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI #275 merged. Want to take a crack at adding the lock protection? Can also defer to a subsequent PR too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I'm wondering if this shouldn't be scoped to the POSIX port and called "posix auth" since there is nothing POSIX-specific about it. You could theoretically use this on any of our ports. Perhaps we can include this as a built-in auth mechanism that could be used by any port, similar to our shared memory transport impl (wh_transport_mem.{c,h}). Any objection to this? Could call it wh_auth_basic or something like that?

  2. Did you have a scheme to persist the auth database in mind? Here it seems like it is intended for the server to have a hardcoded list and just iterate over each element and "add user" on startup before processing requests. Is that the intent? Could we add an InitFromNvm function that takes an object ID as an argument, where the auth list could be pulled out of NVM and configured out-of-band (e.g. provisioned into NVM at factory)? I know there will be the typical issues of serializing a raw struct, compat across build config updates, but perhaps this is ok for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yeah it could be moved to a non posix location, at one point considered placing it in the examples directory. Showing it as a simplified example of user database. I'll look at moving it there or in demo's, I think in a different location than port makes sense for it.

  2. NVM had crossed my mind and it would be great for persistence. Wanted to keep this initial users setup as simple as possible though while ironing out the authentication part of the feature in wolfHSM itself.

Copy link
Contributor

@bigbrett bigbrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(part 2, sorry terminated my last review early)

Comment on lines 38 to 47
enum WH_MESSAGE_AUTH_ACTION_ENUM {
WH_MESSAGE_AUTH_ACTION_AUTHENTICATE = 0x01,
WH_MESSAGE_AUTH_ACTION_LOGIN = 0x02,
WH_MESSAGE_AUTH_ACTION_LOGOUT = 0x03,
WH_MESSAGE_AUTH_ACTION_USER_ADD = 0x04,
WH_MESSAGE_AUTH_ACTION_USER_DELETE = 0x05,
WH_MESSAGE_AUTH_ACTION_USER_GET = 0x06,
WH_MESSAGE_AUTH_ACTION_USER_SET_PERMISSIONS = 0x07,
WH_MESSAGE_AUTH_ACTION_USER_SET_CREDENTIALS = 0x08,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are duplicated in wh_message.h

WH_AUTH_ACTION_USER_DELETE,
WH_AUTH_ACTION_USER_MODIFY,
WH_AUTH_ACTION_PERMISSION_SET,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated in wh_message_auth.h. Imagine these ones can be deleted?

src/wh_server.c Outdated
/* Helper to format an authorization error response for any group/action.
* All response structures have int32_t rc as the first field.
* Returns the response size to send. */
static uint16_t _wh_Server_FormatAuthErrorResponse(uint16_t magic,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static uint16_t _wh_Server_FormatAuthErrorResponse(uint16_t magic,
static uint16_t _FormatAuthErrorResponse(uint16_t magic,

WH_AUTH_METHOD_CERTIFICATE,
} whAuthMethod;

#define WH_NUMBER_OF_GROUPS 14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be hardcoded here. It should ideally be defined with the actual groups in wh_message.h. Perhaps can be auto-computed based on the new group ID fields you may need to create to fix the bug in the previous review?

Comment on lines 195 to 204
/* Get word index and bitmask for this action */
uint32_t wordAndBit = WH_AUTH_ACTION_TO_WORD_AND_BIT(action);
uint32_t wordIndex = WH_AUTH_ACTION_WORD(wordAndBit);
uint32_t bitmask = WH_AUTH_ACTION_BIT(wordAndBit);

if (wordIndex < WH_AUTH_ACTION_WORDS &&
(user->permissions.actionPermissions[groupIndex]
[wordIndex] &
bitmask)) {
rc = WH_ERROR_OK;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think there is another bug here: The packing scheme here has a collision problem. The macros pack two values into overlapping bit ranges. The word index is stored in bits 16-23, but the bitmask occupies bits 0-31 - so when action % 32 > 16, the bitmask bleeds into the word index bits.

For example, action 49 gives word=1 and bit position=17. The packed value becomes (1 << 16) | (1 << 17) = 0x30000. When we extract the word index with (0x30000 >> 16) & 0xFF, we get 3 instead of 1. The bitmask extraction is also wrong - we get 0x30000 instead of 0x20000

This affects actions 48-63, 80-95, 112-127, 144-159, 176-191, 208-223, and 240-255 (about half of all possible actions). Authorization checks for these actions will look at the wrong word in the permissions array and check the wrong bits.

Suggest switching to a uint64_t return type with the word index in the upper 32 bits to avoid any overlap.

void* context;
} whAuthContext;

#define WOLFHSM_MAX_CERTIFICATE_LEN 2048
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be defined here. Don't we already have a macro for this? It should also be WOFHSM_CFG_XXX if you are adding a new config value.

src/wh_auth.c Outdated
Comment on lines 258 to 259
(void)context;
(void)action; /* Action could be used for future fine-grained key access
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we void-casting these?

#include "wolfhsm/wh_auth.h"

enum WH_MESSAGE_AUTH_ACTION_ENUM {
WH_MESSAGE_AUTH_ACTION_AUTHENTICATE = 0x01,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't ever used. Is there a future intent here?

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 36 out of 37 changed files in this pull request and generated 13 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 39 out of 41 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ique max credentials, add force zero to utils
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 39 out of 41 changed files in this pull request and generated 7 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants