Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Supply FreeRTOS mutexes to PKCS #11
Browse files Browse the repository at this point in the history
For multithreading support, created mutex wrappers to match PKCS #11 expected
function footprint.  Functions calling xInitialzePkcs11Session will get
mutex functions (but have not updated for all places that call C_Initialize
directly).
  • Loading branch information
alexa-noxon committed Jan 10, 2019
1 parent 6dbcd62 commit 3276a2d
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion lib/pkcs11/aws_pkcs11.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,89 @@ CK_RV prvOpenSession( CK_SESSION_HANDLE * pxSession,
}

/*-----------------------------------------------------------*/
#define CreateMutex CreateMutex /* This is a hack because CreateMutex is redefined to CreateMutexW in synchapi.h in windows. :/ */
CK_RV prvCreateMutex( CK_VOID_PTR_PTR ppxMutex )
{
SemaphoreHandle_t xMutex;
CK_RV xResult;

xMutex = xSemaphoreCreateMutex();

if( xMutex == NULL )
{
xResult = CKR_HOST_MEMORY;
}
else
{
*ppxMutex = xMutex;
xResult = CKR_OK;
}

return xResult;
}

CK_RV prvDestroyMutex( CK_VOID_PTR pxMutex )
{
vSemaphoreDelete( ( SemaphoreHandle_t ) pxMutex );
return CKR_OK;
}

CK_RV prvLockMutex( CK_VOID_PTR pxMutex )
{
CK_RV xResult;

if( xSemaphoreTake( pxMutex, portMAX_DELAY ) )
{
xResult = CKR_OK;
}
else
{
xResult = CKR_CANT_LOCK;
}

return xResult;
}

CK_RV prvUnlockMutex( CK_VOID_PTR pxMutex )
{
CK_RV xResult;

if( xSemaphoreGive( pxMutex ) )
{
xResult = CKR_OK;
}
else
{
xResult = CKR_GENERAL_ERROR;
}

return xResult;
}



CK_RV xInitializePkcs11Session( CK_SESSION_HANDLE * pxSession )
{
CK_RV xResult;
CK_SLOT_ID * pxSlotId;
CK_FUNCTION_LIST_PTR pxFunctionList;
CK_ULONG xSlotCount;
CK_C_INITIALIZE_ARGS xInitArgs;

xInitArgs.CreateMutex = prvCreateMutex;
xInitArgs.DestroyMutex = prvDestroyMutex;
xInitArgs.LockMutex = prvLockMutex;
xInitArgs.UnlockMutex = prvUnlockMutex;
xInitArgs.flags = 0;
xInitArgs.pReserved = NULL;


xResult = C_GetFunctionList( &pxFunctionList );

/* Initialize the PKCS #11 module. */
if( xResult == CKR_OK )
{
xResult = pxFunctionList->C_Initialize( NULL );
xResult = pxFunctionList->C_Initialize( &xInitArgs );
}

/* Get a list of slots available. */
Expand Down

0 comments on commit 3276a2d

Please sign in to comment.