Skip to content

Commit

Permalink
added client custom callback query capability
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbrett committed Apr 2, 2024
1 parent a5055f3 commit 903e5cb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/wh_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,19 @@ int wh_Client_CustomResponse(whClientContext* c,
memcpy(outResp, &resp, sizeof(resp));

return WH_ERROR_OK;
}

int wh_Client_CustomRequestCheckRegistered(whClientContext* c, uint32_t id)
{
whMessageCustom_Request req = {0};

if (c == NULL || id >= WH_MESSAGE_ACTION_MAX) {
return WH_ERROR_BADARGS;
}

req.id = id;
req.type = WH_MESSAGE_CUSTOM_TYPE_QUERY;

return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_CUSTOM, req.id,
sizeof(req), &req);
}
16 changes: 14 additions & 2 deletions src/wh_message_custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ static void _translateCustomData(uint16_t magic, uint32_t translatedType,
{
if (translatedType < WH_MESSAGE_CUSTOM_TYPE_USER_DEFINED_START) {
switch (translatedType) {
case WH_MESSAGE_CUSTOM_TYPE_QUERY: {
/* right now, no further translations required */
} break;
case WH_MESSAGE_CUSTOM_TYPE_DMA32: {
dst->dma32.client_addr =
wh_Translate32(magic, src->dma32.client_addr);
Expand Down Expand Up @@ -51,9 +54,14 @@ int wh_MessageCustom_TranslateRequest(uint16_t magic,
if ((src == NULL) || (dst == NULL)) {
return WH_ERROR_BADARGS;
}

dst->id = wh_Translate16(magic, src->id);
dst->type = wh_Translate32(magic, src->type);
_translateCustomData(magic, dst->type, &src->data, &dst->data);

/* Translate data if this isn't just a callback query */
if (dst->type != WH_MESSAGE_CUSTOM_TYPE_QUERY) {
_translateCustomData(magic, dst->type, &src->data, &dst->data);
}

return WH_ERROR_OK;
}
Expand All @@ -74,7 +82,11 @@ int wh_MessageCustom_TranslateResponse(uint16_t magic,
* Probably still should...*/
dst->id = wh_Translate16(magic, src->id);
dst->type = wh_Translate32(magic, src->type);
_translateCustomData(magic, dst->type, &src->data, &dst->data);

/* Translate data if this isn't just a callback query */
if (src->type != WH_MESSAGE_CUSTOM_TYPE_QUERY) {
_translateCustomData(magic, dst->type, &src->data, &dst->data);
}

return WH_ERROR_OK;
}
15 changes: 8 additions & 7 deletions src/wh_server_custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ int wh_Server_HandleCustomRequest(whServerContext* server, uint16_t magic,
}

if (customHandlerTable[action] != NULL) {
/* Invoke the registered callback, storing the return value in the
* reponse */
resp.rc = customHandlerTable[action](server, &req, &resp);
/* TODO: propagate wolfHSM error codes (requires modifiying caller
* function)*/
/* If this isn't a query to check if the callback exists, invoke the
* registered callback, storing the return value in the reponse */
if (req.type != WH_MESSAGE_CUSTOM_TYPE_QUERY) {
resp.rc = customHandlerTable[action](server, &req, &resp);
}
/* TODO: propagate other wolfHSM error codes (requires modifiying caller
* function) once generic server code supports it */
resp.err = WH_ERROR_OK;
}
else {
/* No callback was registered, populate response errors, but we must
/* No callback was registered, populate response error. We must
* return success to ensure the "error" response is sent */
/* TODO: what should we set resp.rc to? */
resp.err = WH_ERROR_NO_HANDLER;
}

Expand Down
17 changes: 14 additions & 3 deletions test/wh_test_clientserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ static int _testCallbacks(whServerContext* server, whClientContext* client)
char output[sizeof(input)] = {0};

for (counter = 0; counter < WH_MESSAGE_ACTION_MAX; counter++) {
/* First, test that an unregistered callback returns error */
req.id = counter;

/* Check that the callback shows as unregistered */
WH_TEST_RETURN_ON_FAIL(wh_Client_CustomRequestCheckRegistered(client, req.id));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
WH_TEST_RETURN_ON_FAIL(wh_Client_CustomResponse(client, &resp));
WH_TEST_ASSERT_RETURN(resp.err == WH_ERROR_NO_HANDLER);

/* Test that calling an unregistered callback returns error */
WH_TEST_RETURN_ON_FAIL(wh_Client_CustomRequest(client, &req));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
WH_TEST_RETURN_ON_FAIL(wh_Client_CustomResponse(client, &resp));
Expand All @@ -85,6 +92,12 @@ static int _testCallbacks(whServerContext* server, whClientContext* client)
WH_TEST_RETURN_ON_FAIL(
wh_Server_RegisterCustomCb(counter, _customServerCb));

/* Check that the callback now shows as registered */
WH_TEST_RETURN_ON_FAIL(wh_Client_CustomRequestCheckRegistered(client, req.id));
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
WH_TEST_RETURN_ON_FAIL(wh_Client_CustomResponse(client, &resp));
WH_TEST_ASSERT_RETURN(resp.err == WH_ERROR_OK);

/* prepare the rest of the request */
if (sizeof(uintptr_t) == sizeof(uint64_t)) {
/* 64-bit host system */
Expand Down Expand Up @@ -115,8 +128,6 @@ static int _testCallbacks(whServerContext* server, whClientContext* client)
memset(&resp, 0, sizeof(resp));
}

/* TODO: query registered callback list and ensure it is populated correctly */

return WH_ERROR_OK;
}

Expand Down
1 change: 1 addition & 0 deletions wolfhsm/wh_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ int wh_Client_NvmReadDma(whClientContext* c,


/* Client custom-callback support */
int wh_Client_CustomRequestCheckRegistered(whClientContext* c, uint32_t id);
int wh_Client_CustomRequest(whClientContext* c, const whMessageCustom_Request* req);
int wh_Client_CustomResponse(whClientContext* c, whMessageCustom_Response *resp);

Expand Down
6 changes: 3 additions & 3 deletions wolfhsm/wh_message_custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* to interpret whMessageCustomData */
typedef enum {
/* message types reserved for internal usage*/
WH_MESSAGE_CUSTOM_TYPE_DMA32 = 0,
WH_MESSAGE_CUSTOM_TYPE_DMA64 = 1,
WH_MESSAGE_CUSTOM_TYPE_RESERVED_2 = 2,
WH_MESSAGE_CUSTOM_TYPE_QUERY = 0,
WH_MESSAGE_CUSTOM_TYPE_DMA32 = 1,
WH_MESSAGE_CUSTOM_TYPE_DMA64 = 2,
WH_MESSAGE_CUSTOM_TYPE_RESERVED_3 = 3,
WH_MESSAGE_CUSTOM_TYPE_RESERVED_4 = 4,
WH_MESSAGE_CUSTOM_TYPE_RESERVED_5 = 5,
Expand Down

0 comments on commit 903e5cb

Please sign in to comment.