Skip to content

Commit

Permalink
Objects with different types (same ids) are stored separately
Browse files Browse the repository at this point in the history
  • Loading branch information
danielinux committed Aug 7, 2024
1 parent 91bc42e commit 3c0e287
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions src/pkcs11_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ struct store_handle {
uint32_t flags;
uint32_t pos;
void *buffer;
struct obj_hdr *hdr;
uint32_t in_buffer_offset;
};

Expand Down Expand Up @@ -235,12 +236,13 @@ static void delete_object(uint32_t tok_id, uint32_t obj_id)
* started at physical 0x0000 0000, the buffers are stored from sector
* 2 onwards.
*/
static uint8_t *find_object_buffer(uint32_t tok_id, uint32_t obj_id)
static uint8_t *find_object_buffer(int32_t type, uint32_t tok_id, uint32_t obj_id)
{
struct obj_hdr *hdr = NODES_TABLE;
uint32_t *tok_obj_stored = NULL;
while ((uint32_t)hdr < ((uint32_t)NODES_TABLE + WOLFBOOT_SECTOR_SIZE)) {
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)) {
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)
&& (hdr->type == type)) {
tok_obj_stored = (uint32_t *) (vault_base + (2 * WOLFBOOT_SECTOR_SIZE) + (hdr->pos * KEYVAULT_OBJ_SIZE));
if ((tok_obj_stored[0] != tok_id) || (tok_obj_stored[1] != obj_id)) {
/* Id's don't match. Try backup sector. */
Expand All @@ -265,25 +267,27 @@ static uint8_t *find_object_buffer(uint32_t tok_id, uint32_t obj_id)
return NULL; /* object not found */
}

static struct obj_hdr *find_object_header(uint32_t tok_id, uint32_t obj_id)
static struct obj_hdr *find_object_header(int32_t type, uint32_t tok_id,
uint32_t obj_id)
{
struct obj_hdr *hdr = NODES_TABLE;
uint32_t *tok_obj_stored = NULL;
while ((uint32_t)hdr < ((uint32_t)NODES_TABLE + WOLFBOOT_SECTOR_SIZE)) {
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)) {
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)
&& (hdr->type == type)) {
return hdr;
}
hdr++;
}
return NULL;
}

static uint8_t *create_object(uint32_t type, uint32_t tok_id, uint32_t obj_id)
static struct obj_hdr *create_object(int32_t type, uint32_t tok_id, uint32_t obj_id)
{
struct obj_hdr *hdr = NULL;
uint32_t *tok_obj_id;
/* Refuse to create an object that's already in store */
if (find_object_buffer(tok_id, obj_id) != NULL) {
if (find_object_buffer(type, tok_id, obj_id) != NULL) {
return NULL;
}

Expand Down Expand Up @@ -324,25 +328,14 @@ static uint8_t *create_object(uint32_t type, uint32_t tok_id, uint32_t obj_id)
tok_obj_id[0] = tok_id;
tok_obj_id[1] = obj_id;
cache_commit(sector_base);
return find_object_buffer(tok_id, obj_id);
/* Return the address of the header in flash */
return (struct obj_hdr *)(vault_base + ((uint8_t *)hdr - (uint8_t *)cached_sector));
}
hdr++;
}
return NULL; /* No space left in the nodes table */
}

/* Return the size of the stored buffer,
* including the tok/obj ids at the beginning
* (payload starts at byte 8)
*/
static int get_store_size(uint32_t tok_id, uint32_t obj_id)
{
struct obj_hdr *hdr = find_object_header(tok_id, obj_id);
if (hdr == NULL)
return -1;
return hdr->size;
}

static void update_store_size(uint32_t tok_id, uint32_t obj_id, uint32_t size)
{
struct obj_hdr *hdr = (struct obj_hdr *)cached_sector;
Expand Down Expand Up @@ -380,6 +373,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
unsigned int i;
struct store_handle *handle;
uint8_t *buf;
struct obj_hdr *hdr = NULL;


/* Check if there is one handle available to open the slot */
Expand All @@ -391,26 +385,29 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,

/* Check if the target object exists */
check_vault();
buf = find_object_buffer(id1, id2);
buf = find_object_buffer(type, id1, id2);
if ((buf == NULL) && read) {
*store = NULL;
return NOT_AVAILABLE_E;
}

/* Check that type matches for existing items */
if (buf != NULL) {
struct obj_hdr *hdr = find_object_header(id1, id2);
if ((hdr == NULL) || (hdr->type != type)) {
if ((buf == NULL) && (!read)) {
handle->hdr = create_object(type, id1, id2);
if (handle->hdr == NULL) {
*store = NULL;
return NOT_AVAILABLE_E;
}
}
return FIND_FULL_E;

if ((buf == NULL) && (!read)) {
buf = create_object(type, id1, id2);
}
buf = find_object_buffer(type, id1, id2);
if (!buf) {
*store = NULL;
return FIND_FULL_E;
return NOT_AVAILABLE_E;
}
} else { /* buf != NULL, readonly */
handle->hdr = find_object_header(type, id1, id2);
if (!handle->hdr) {
*store = NULL;
return NOT_AVAILABLE_E;
}
}

Expand Down Expand Up @@ -438,18 +435,19 @@ void wolfPKCS11_Store_Close(void* store)
struct store_handle *handle = store;
/* This removes all flags (including STORE_FLAGS_OPEN) */
handle->flags = 0;
handle->hdr = NULL;
}

int wolfPKCS11_Store_Read(void* store, unsigned char* buffer, int len)
{
struct store_handle *handle = store;
uint32_t *tok_obj_id;
uint32_t obj_size = 0;
if (handle == NULL)
if ((handle == NULL) || (handle->hdr == NULL) || (handle->buffer == NULL))
return -1;

tok_obj_id = (uint32_t *)handle->buffer;
obj_size = get_store_size(tok_obj_id[0], tok_obj_id[1]);
obj_size = handle->hdr->size;
if (obj_size > KEYVAULT_OBJ_SIZE)
return -1;

Expand Down Expand Up @@ -478,15 +476,16 @@ int wolfPKCS11_Store_Write(void* store, unsigned char* buffer, int len)
uint32_t sector_base = 0;
int written = 0;

if (handle == NULL)

if ((handle == NULL) || (handle->hdr == NULL) || (handle->buffer == NULL))
return -1;
if ((handle->flags & STORE_FLAGS_READONLY) != 0)
return -1;

tok_obj_id = (uint32_t *)handle->buffer;
tok_id = tok_obj_id[0];
obj_id = tok_obj_id[1];
obj_size = get_store_size(tok_id, obj_id);
obj_size = handle->hdr->size;
if (obj_size > KEYVAULT_OBJ_SIZE)
return -1;

Expand All @@ -496,6 +495,7 @@ int wolfPKCS11_Store_Write(void* store, unsigned char* buffer, int len)
if (len < 0)
return -1;


while (written < len) {
in_sector_offset = ((uint32_t)(handle->buffer) + handle->in_buffer_offset)
% WOLFBOOT_SECTOR_SIZE;
Expand Down

0 comments on commit 3c0e287

Please sign in to comment.