in source/portable/os/posix/core_pkcs11_pal.c [150:209]
CK_OBJECT_HANDLE PKCS11_PAL_SaveObject( CK_ATTRIBUTE_PTR pxLabel,
CK_BYTE_PTR pucData,
CK_ULONG ulDataSize )
{
FILE * pxFile = NULL;
size_t ulBytesWritten;
const char * pcFileName = NULL;
CK_OBJECT_HANDLE xHandle = ( CK_OBJECT_HANDLE ) eInvalidHandle;
if( ( pxLabel != NULL ) && ( pucData != NULL ) )
{
/* Converts a label to its respective filename and handle. */
PAL_UTILS_LabelToFilenameHandle( pxLabel->pValue,
&pcFileName,
&xHandle );
}
else
{
LogError( ( "Could not save object. Received invalid parameters." ) );
}
if( pcFileName != NULL )
{
/* Overwrite the file every time it is saved. */
pxFile = fopen( pcFileName, "w" );
if( NULL == pxFile )
{
LogError( ( "PKCS #11 PAL was unable to save object to file. "
"The PAL was unable to open a file with name %s in write mode.", pcFileName ) );
xHandle = ( CK_OBJECT_HANDLE ) eInvalidHandle;
}
else
{
ulBytesWritten = fwrite( pucData, sizeof( uint8_t ), ulDataSize, pxFile );
if( ulBytesWritten != ulDataSize )
{
LogError( ( "PKCS #11 PAL was unable to save object to file. "
"Expected to write %lu bytes, but wrote %lu bytes.", ulDataSize, ulBytesWritten ) );
xHandle = ( CK_OBJECT_HANDLE ) eInvalidHandle;
}
else
{
LogDebug( ( "Successfully wrote %lu to %s", ulBytesWritten, pcFileName ) );
}
}
if( NULL != pxFile )
{
( void ) fclose( pxFile );
}
}
else
{
LogError( ( "Could not save object. Unable to find the correct file." ) );
}
return xHandle;
}