Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom user callbacks (custom requests) #14

Merged
merged 6 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/wh_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/* Message definitions */
#include "wolfhsm/wh_message.h"
#include "wolfhsm/wh_message_comm.h"
#include "wolfhsm/wh_message_custom.h"

#include "wolfhsm/wh_client.h"

Expand All @@ -34,7 +35,7 @@ int wh_Client_Init(whClientContext* c, const whClientConfig* config)
}

memset(c, 0, sizeof(*c));

if ( ((rc = wh_CommClient_Init(c->comm, config->comm)) == 0) &&
((rc = wolfCrypt_Init()) == 0) &&
((rc = wc_CryptoCb_RegisterDevice(WOLFHSM_DEV_ID, wolfHSM_CryptoCb, c)) == 0) &&
Expand Down Expand Up @@ -201,3 +202,57 @@ int wh_Client_Echo(whClientContext* c, uint16_t snd_len, const void* snd_data,
return rc;
}

int wh_Client_CustomRequest(whClientContext* c, const whMessageCustom_Request* req)
{
if (NULL == c || req == NULL || req->id >= WH_MESSAGE_ACTION_MAX) {
return WH_ERROR_BADARGS;
}

return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_CUSTOM, req->id,
sizeof(*req), req);
}

int wh_Client_CustomResponse(whClientContext* c,
whMessageCustom_Response* outResp)
{
whMessageCustom_Response resp;
uint16_t resp_group = 0;
uint16_t resp_action = 0;
uint16_t resp_size = 0;
int32_t rc = 0;

if (NULL == c || outResp == NULL) {
return WH_ERROR_BADARGS;
}

rc =
wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &resp);
if (rc != WH_ERROR_OK) {
return rc;
}

if (resp_size != sizeof(resp) || resp_group != WH_MESSAGE_GROUP_CUSTOM ||
resp_action >= WH_MESSAGE_ACTION_MAX) {
/* message invalid */
return WH_ERROR_ABORTED;
}

memcpy(outResp, &resp, sizeof(resp));

return WH_ERROR_OK;
}

int wh_Client_CustomRequestCheckRegistered(whClientContext* c, uint32_t id)
billphipps marked this conversation as resolved.
Show resolved Hide resolved
{
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);
}
84 changes: 84 additions & 0 deletions src/wh_message_custom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <stddef.h>
#include <string.h>

#include "wolfhsm/wh_message_custom.h"
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_comm.h"


static void _translateCustomData(uint16_t magic, uint32_t translatedType,
const whMessageCustom_Data* src,
whMessageCustom_Data* dst)
{
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);
dst->dma32.client_sz =
wh_Translate32(magic, src->dma32.client_sz);
dst->dma32.server_addr =
wh_Translate32(magic, src->dma32.server_addr);
dst->dma32.server_sz =
wh_Translate32(magic, src->dma32.server_sz);
} break;
case WH_MESSAGE_CUSTOM_TYPE_DMA64: {
dst->dma64.client_addr =
wh_Translate64(magic, src->dma64.client_addr);
dst->dma64.client_sz =
wh_Translate64(magic, src->dma64.client_sz);
dst->dma64.server_addr =
wh_Translate64(magic, src->dma64.server_addr);
dst->dma64.server_sz =
wh_Translate64(magic, src->dma64.server_sz);
} break;
default: {
/* reserved message types - no translation for now */
} break;
}
}
else {
memcpy(dst->buffer.data, src->buffer.data,
billphipps marked this conversation as resolved.
Show resolved Hide resolved
sizeof(dst->buffer.data));
}
}


int wh_MessageCustom_TranslateRequest(uint16_t magic,
const whMessageCustom_Request* src,
whMessageCustom_Request* dst)
{
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);

return WH_ERROR_OK;
}


int wh_MessageCustom_TranslateResponse(uint16_t magic,
const whMessageCustom_Response* src,
whMessageCustom_Response* dst)
{
if ((src == NULL) || (dst == NULL)) {
return WH_ERROR_BADARGS;
}

dst->rc = wh_Translate32(magic, src->rc);
dst->err = wh_Translate32(magic, src->err);

/* TODO: should we continue to translate responses for err != 0?
* 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);

return WH_ERROR_OK;
}
29 changes: 6 additions & 23 deletions src/wh_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

/* Server API's */
#include "wolfhsm/wh_server.h"
#include "wolfhsm/wh_server_crypto.h"
#include "wolfhsm/wh_server_internal.h"
#include "wolfhsm/wh_server_crypto.h"
#include "wolfhsm/wh_server_custom.h"
#if defined(WOLFHSM_SHE_EXTENSION)
#include "wolfhsm/wh_server_she.h"
#endif
Expand All @@ -44,11 +45,6 @@ static int _wh_Server_HandleSheRequest(whServerContext* server,
uint16_t req_size, const void* req_packet,
uint16_t *out_resp_size, void* resp_packet);
#endif
static int _wh_Server_HandleCustomRequest(whServerContext* server,
uint16_t magic, uint16_t action, uint16_t seq,
uint16_t req_size, const void* req_packet,
uint16_t *out_resp_size, void* resp_packet);


int wh_Server_Init(whServerContext* server, whServerConfig* config)
{
Expand Down Expand Up @@ -215,20 +211,7 @@ static int _wh_Server_HandleSheRequest(whServerContext* server,
}
#endif

static int _wh_Server_HandleCustomRequest(whServerContext* server,
uint16_t magic, uint16_t action, uint16_t seq,
uint16_t req_size, const void* req_packet,
uint16_t *out_resp_size, void* resp_packet)
{
int rc = 0;
switch (action) {
/* TODO: Add custom/user callback message handling here */
default:
/* Unknown request. Respond with empty packet */
*out_resp_size = 0;
}
return rc;
}


int wh_Server_HandleRequestMessage(whServerContext* server)
{
Expand Down Expand Up @@ -266,11 +249,11 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
rc = _wh_Server_HandleKeyRequest(server, magic, action, seq,
size, data, &size, data);
break;

case WH_MESSAGE_GROUP_CRYPTO:
rc = wh_Server_HandleCryptoRequest(server, action, data, &size);
break;

case WH_MESSAGE_GROUP_PKCS11:
rc = _wh_Server_HandlePkcs11Request(server, magic, action, seq,
size, data, &size, data);
Expand All @@ -283,7 +266,7 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
#endif

case WH_MESSAGE_GROUP_CUSTOM:
rc = _wh_Server_HandleCustomRequest(server, magic, action, seq,
rc = wh_Server_HandleCustomRequest(server, magic, action, seq,
size, data, &size, data);
break;

Expand Down
79 changes: 79 additions & 0 deletions src/wh_server_custom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "wolfhsm/wh_server_custom.h"
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_message.h"
#include "wolfhsm/wh_message_custom.h"


/* Statically allocated callback table holding user callbacks */
static whServerCustomCb customHandlerTable[WH_MESSAGE_ACTION_MAX] = {NULL};
billphipps marked this conversation as resolved.
Show resolved Hide resolved


int wh_Server_RegisterCustomCb(uint16_t action, whServerCustomCb handler)
{
if (NULL == handler || action >= WH_MESSAGE_ACTION_MAX) {
return WH_ERROR_BADARGS;
}

customHandlerTable[action] = handler;

return WH_ERROR_OK;
}


int wh_Server_HandleCustomRequest(whServerContext* server, uint16_t magic,
uint16_t action, uint16_t seq,
uint16_t req_size, const void* req_packet,
uint16_t* out_resp_size, void* resp_packet)
{
int rc = 0;
whMessageCustom_Request req = {0};
whMessageCustom_Response resp = {0};

if (NULL == server || NULL == req_packet || NULL == resp_packet ||
out_resp_size == NULL) {
return WH_ERROR_BADARGS;
}

if (action >= WH_MESSAGE_ACTION_MAX) {
/* Invalid callback index */
/* TODO: is this the appropriate error to return? */
return WH_ERROR_BADARGS;
}

if (req_size != sizeof(whMessageCustom_Request)) {
/* Request is malformed */
return WH_ERROR_ABORTED;
}

/* Translate the request */
if ((rc = wh_MessageCustom_TranslateRequest(magic, req_packet, &req)) !=
WH_ERROR_OK) {
return rc;
}

if (customHandlerTable[action] != NULL) {
/* 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 error. We must
* return success to ensure the "error" response is sent */
resp.err = WH_ERROR_NO_HANDLER;
}

/* Translate the response and set output size */
if ((rc = wh_MessageCustom_TranslateResponse(magic, &resp, resp_packet)) !=
WH_ERROR_OK) {
return rc;
}

*out_resp_size = sizeof(resp);

return WH_ERROR_OK;
}
2 changes: 2 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ SRC_C += \
$(WOLFHSM_DIR)/src/wh_flash_unit.c \
$(WOLFHSM_DIR)/src/wh_message_comm.c \
$(WOLFHSM_DIR)/src/wh_message_nvm.c \
$(WOLFHSM_DIR)/src/wh_message_custom.c \
$(WOLFHSM_DIR)/src/wh_nvm.c \
$(WOLFHSM_DIR)/src/wh_nvm_flash.c \
$(WOLFHSM_DIR)/src/wh_server.c \
$(WOLFHSM_DIR)/src/wh_server_nvm.c \
$(WOLFHSM_DIR)/src/wh_server_crypto.c \
$(WOLFHSM_DIR)/src/wh_server_custom.c \
$(WOLFHSM_DIR)/src/wh_client_cryptocb.c \
$(WOLFHSM_DIR)/src/wh_transport_mem.c \
$(WOLFHSM_DIR)/src/wh_flash_ramsim.c
Expand Down
Loading