Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex-pkcs11-8.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include "pkcs11.h"
#include "pkcs11p6r.h" // optional, only needed if using P6R vendor extensions
// PKCS 11 is a C language API.
//
int main(int argc,char *argv[])
{
CK_SLOT_ID slotList[30];
CK_ULONG ulSlotCount = 0;
CK_SLOT_ID P6RslotId = 1;
CK_ULONG i = 0;
CK_RV rv = 0;
// [A] We must first initialize the entire PKCS 11 library
memset( &initArgs, 0, sizeof( CK_C_INITIALIZE_ARGS ));
initArgs.flags = 0;
if (CKR_OK != (rv = C_Initialize( &initArgs ))) {
printf( "PKCS11 example8: failed C_Initialize (error:%x)", (unsigned int) rv );
return -1;
}
// [B] Query the 3rd party HSM token directly to get its list of slots
// -> slotId for a Thales nShield Connect was 492971158 this is how we found it.
// -> how many slots does the HSM token define?
if ( CKR_OK == (rv = P6R_GetSlotList( P6RslotId, CK_FALSE, NULL_PTR, &ulSlotCount )))
{
// -> For 3rd party HSM tokens we map a slot in the P6R PKCS 11 library to an HSM slot.
// To do this we add the paramter "vendorSlotId=<number>" to the p6pkcs11.conf file for a 3rd party token proper slot
// (see the p6pkcs11.conf file [p6pkcs11-slot-1]),
// For a Thales nShield connect we tested the <number> value was 492971158, but for a Utimaco HSM their slot numbers started at 0.
if ( CKR_OK == (rv = P6R_GetSlotList( P6RslotId, CK_FALSE, (CK_SLOT_ID_PTR)slotList, &ulSlotCount )))
{
for( i=0; i < ulSlotCount; i++ ) printf( "%ld> slot identifer [%ld]\n", i, slotList[i] );
}
else printf( "PKCS11 example8: failed P6R_GetSlotList get slot identifer values (error:%x)", (unsigned int) rv );
}
else printf( "PKCS11 example8: failed P6R_GetSlotList get slot count (error:%x)", (unsigned int) rv );
// [C] All done clean up
if (CKR_OK != (rv = C_Finalize( NULL_PTR ))) {
printf( "PKCS11 example8: failed C_Finalize %lx", rv );
return -3;
}
return 0;
}