#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
int main(int argc,char *argv[])
{
CK_OBJECT_CLASS objClass = 0;
CK_KEY_TYPE keyType = 0;
CK_ULONG ulSlotCount = 0;
CK_ULONG foundCount = 0;
CK_ULONG keyLength = 0;
CK_ULONG delCount = 0;
CK_ULONG i = 0;
int match = 0;
if (
CKR_OK != (rv = C_Initialize( &initArgs ))) {
printf( "PKCS11 example3: failed C_Initialize (error:%lx)", rv );
return -1;
}
{
printf( "PKCS11 example3: memory allocation failed" );
return -2;
}
if (
CKR_OK == (rv = C_GetSlotList(
CK_FALSE, pSlotList, &ulSlotCount )))
{
for( i=0; i < ulSlotCount; i++ )
{
{
match = strncmp( (const char*)oneSlot.slotDescription, "KMIP SLOT1 ", 64 );
if (0 == match)
{
if (
CKR_OK != (rv = C_OpenSession( pSlotList[i], flags, NULL, NULL, &hSession ))) {
printf( "PKCS11 example3: slot %ld failed to open a session (error:%lx)", pSlotList[i], rv );
break;
}
{
strncpy( (char*)label, "An updated AES secret key object on token", 41 );
findTemplate[0].
pValue = &objClass;
findTemplate[1].
pValue = &keyType;
findTemplate[2].
pValue = &bTFlag;
findTemplate[3].
pValue = &label;
findTemplate[4].
pValue = &bTFlag;
if (
CKR_OK == rv) rv2 = C_FindObjects( hSession, &hObject, 1, &foundCount );
{
{
keyLength = getTemplate[0].ulValueLen;
getTemplate[0].pValue = pKeyMaterial;
getTemplate[0].ulValueLen = keyLength;
getTemplate[1].pValue = &bDecrypt;
getTemplate[1].ulValueLen =
sizeof(
CK_BBOOL);
{
;
}
else printf( "PKCS11 example3: slot %ld failed read key off of token (error:%lx)", pSlotList[i], rv );
}
else printf( "PKCS11 example3: slot %ld failed read key size off of token (error:%lx)", pSlotList[i], rv );
}
else
{ printf( "PKCS11 example3: slot %ld failed to find key on token (error:%lx)", pSlotList[i], rv );
}
if (
CKR_OK == rv) rv = C_FindObjectsFinal( hSession );
if (NULL != pKeyMaterial) free( pKeyMaterial );
delCount = 0;
rv = C_FindObjectsInit( hSession,
NULL_PTR, 0 );
{
hObject = 0;
rv = C_FindObjects( hSession, &hObject, 1, &foundCount );
if (0 == foundCount) {
break;
}
if (delCount < 5) {
rv = C_DestroyObject( hSession, hObject );
}
}
rv = C_FindObjectsFinal( hSession );
if (
CKR_OK != (rv = C_Logout( hSession ))) {
printf( "PKCS11 example3: slot %ld failed to logout of token (error:%lx)", pSlotList[i], rv );
}
}
if (
CKR_OK != (rv = C_CloseSession( hSession ))) {
printf( "PKCS11 example3: slot %ld failed to close an active token session (error:%lx)", pSlotList[i], rv );
break;
}
}
else {
printf( "PKCS11 example3: slot %ld failed to log user into token (error:%lx)", pSlotList[i], rv );
}
}
}
}
}
if (
NULL_PTR != pSlotList) free( pSlotList );
printf( "PKCS11 example2: failed C_Finalize %lx", rv );
return -3;
}
return 0;
}