#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
int main(int argc,char *argv[])
{
CK_ULONG wrappedLength = 4000;
CK_ULONG ulSlotCount = 0;
CK_ULONG keyLength = 0;
int errorNumber = 0;
if (
CKR_OK != (rv = C_Initialize( &initArgs ))) {
printf( "PKCS11 example14: failed C_Initialize (error:%lx)", rv );
return -1;
}
{
printf( "PKCS11 example14: memory allocation failed" );
return -2;
}
if (
CKR_OK == (rv = C_GetSlotList(
CK_FALSE, pSlotList, &ulSlotCount )))
{
printf( "PKCS11 example14: slot 0 failed to open a session (error:%lx)", pSlotList[0], rv );
return -3;
}
{
keyLength = 16;
strncpy( (char*)label, "AES data key", 12 );
keyTemplate[0].
pValue = &bTFlag;
keyTemplate[1].
pValue = label;
keyTemplate[2].
pValue = &keyLength;
keyTemplate[3].
pValue = &bTFlag;
keyTemplate[4].
pValue = &bTFlag;
printf( "PKCS11 example14: slot %ld failed to generate data key on token (error:%lx)", pSlotList[0], rv );
errorNumber = -4;
}
{
strncpy( (char*)label, "AES key encrypting key (kek)", 28 );
keyTemplate[0].
pValue = &bTFlag;
keyTemplate[1].
pValue = label;
keyTemplate[2].
pValue = &keyLength;
keyTemplate[3].
pValue = &bTFlag;
keyTemplate[4].
pValue = &bTFlag;
printf( "PKCS11 example14: slot %ld failed to generate kek on token (error:%lx)", pSlotList[0], rv );
errorNumber = -5;
}
}
{
if (
CKR_OK != (rv = C_WrapKey( hSession, &oneMech, hKEK, hDataKey, wrappedKey, &wrappedLength ))) {
printf( "PKCS11 example14: slot %ld failed to wrap the data key in the KEK token (error:%lx)", pSlotList[0], rv );
errorNumber = -6;
}
else printf( "PKCS11 example14: wrapped key size is %d bytes", wrappedLength );
}
if (
CKR_OK != (rv = C_Logout( hSession ))) {
printf( "PKCS11 example14: slot %ld failed to logout of token (error:%lx)", pSlotList[0], rv );
}
}
if (
CKR_OK != (rv = C_CloseSession( hSession ))) {
printf( "PKCS11 example14: slot %ld failed to close an active token session (error:%lx)", pSlotList[0], rv );
}
}
}
if (
NULL_PTR != pSlotList) free( pSlotList );
printf( "PKCS11 example14: failed C_Finalize %lx", rv );
errorNumber = -7;
}
return errorNumber;
}