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 all 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
110 changes: 109 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_customcb.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,110 @@ int wh_Client_Echo(whClientContext* c, uint16_t snd_len, const void* snd_data,
return rc;
}

int wh_Client_CustomCbRequest(whClientContext* c, const whMessageCustomCb_Request* req)
{
if (NULL == c || req == NULL || req->id >= WH_CUSTOM_CB_NUM_CALLBACKS) {
return WH_ERROR_BADARGS;
}

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

int wh_Client_CustomCbResponse(whClientContext* c,
whMessageCustomCb_Response* outResp)
{
whMessageCustomCb_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_CUSTOM_CB_NUM_CALLBACKS) {
/* message invalid */
return WH_ERROR_ABORTED;
}

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

return WH_ERROR_OK;
}

int wh_Client_CustomCheckRegisteredRequest(whClientContext* c, uint32_t id)
{
whMessageCustomCb_Request req = {0};

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

req.id = id;
req.type = WH_MESSAGE_CUSTOM_CB_TYPE_QUERY;

return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_CUSTOM, req.id,
sizeof(req), &req);
}


int wh_Client_CustomCbCheckRegisteredResponse(whClientContext* c, uint16_t* outId, int* responseError)
{
int rc = 0;
whMessageCustomCb_Response resp = {0};

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

rc = wh_Client_CustomCbResponse(c, &resp);
if (rc != WH_ERROR_OK) {
return rc;
}

if (resp.type != WH_MESSAGE_CUSTOM_CB_TYPE_QUERY) {
/* message invalid */
return WH_ERROR_ABORTED;
}

if (resp.err != WH_ERROR_OK && resp.err != WH_ERROR_NO_HANDLER) {
/* error codes that aren't related to the query should be fatal */
return WH_ERROR_ABORTED;
}

*outId = resp.id;
*responseError = resp.err;

return WH_ERROR_OK;
}


int wh_Client_CustomCbCheckRegistered(whClientContext* c, uint16_t id, int* responseError)
{
int rc = 0;

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

do {
rc = wh_Client_CustomCheckRegisteredRequest(c, id);
} while (rc == WH_ERROR_NOTREADY);

if (rc == WH_ERROR_OK) {
do {
rc = wh_Client_CustomCbCheckRegisteredResponse(c, &id, responseError);
} while (rc == WH_ERROR_NOTREADY);
}

return rc;
}
84 changes: 84 additions & 0 deletions src/wh_message_customcb.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_customcb.h"
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_comm.h"


static void _translateCustomData(uint16_t magic, uint32_t translatedType,
const whMessageCustomCb_Data* src,
whMessageCustomCb_Data* dst)
{
if (translatedType < WH_MESSAGE_CUSTOM_CB_TYPE_USER_DEFINED_START) {
switch (translatedType) {
case WH_MESSAGE_CUSTOM_CB_TYPE_QUERY: {
/* right now, no further translations required */
} break;
case WH_MESSAGE_CUSTOM_CB_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_CB_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 {
/* use memmove in case data is translated "in place" */
memmove(dst->buffer.data, src->buffer.data, sizeof(dst->buffer.data));
}
}


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

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

return WH_ERROR_OK;
}


int wh_MessageCustomCb_TranslateResponse(uint16_t magic,
const whMessageCustomCb_Response* src,
whMessageCustomCb_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_Translate32(magic, src->id);
dst->type = wh_Translate32(magic, src->type);
_translateCustomData(magic, dst->type, &src->data, &dst->data);

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

/* 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"
#if defined(WOLFHSM_SHE_EXTENSION)
#include "wolfhsm/wh_server_she.h"
#endif
Expand All @@ -44,11 +44,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 +210,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 +248,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 +265,7 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
#endif

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

Expand Down
81 changes: 81 additions & 0 deletions src/wh_server_customcb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "wolfhsm/wh_server.h"
#include "wolfhsm/wh_common.h"
#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_message.h"
#include "wolfhsm/wh_message_customcb.h"


int wh_Server_RegisterCustomCb(whServerContext* server, uint16_t action,
whServerCustomCb handler)
{
if (NULL == server || NULL == handler ||
action >= WH_CUSTOM_CB_NUM_CALLBACKS) {
return WH_ERROR_BADARGS;
}

server->customHandlerTable[action] = handler;

return WH_ERROR_OK;
}


int wh_Server_HandleCustomCbRequest(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;
whMessageCustomCb_Request req = {0};
whMessageCustomCb_Response resp = {0};

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

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

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

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

if (server->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_CB_TYPE_QUERY) {
resp.rc = server->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;
}

/* tag response with requested callback ID for client-side bookkeeping*/
resp.id = req.id;

/* Translate the response */
if ((rc = wh_MessageCustomCb_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_customcb.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_customcb.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