// PKIInit.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "init_header.h"

typedef CK_RV (* C_GETFUNCTIONLISTPROC)(CK_FUNCTION_LIST_PTR_PTR); 
typedef CK_RV (* PRIINIT)(CK_SLOT_ID, INIT_TOKEN_PRARM_PTR); 
typedef CK_RV (* PKIINIT)(CK_SLOT_ID);
typedef CK_RV (* SetIsSupportSSF33)(CK_BBOOL);

static C_GETFUNCTIONLISTPROC pC_GetFunctionList;
static PRIINIT pPriInit; 
static PKIINIT pPkiInit;
static CK_FUNCTION_LIST_PTR ePass = NULL; 

HINSTANCE m_hLibP11;
HINSTANCE m_hLibInit;


void ShowMainCmdMenu()
{
	printf("\n\nMain Menu:\n");
	printf("------------------------------------\n");
	printf("Press any key to init the token,or ESC to exit......\n");
	printf("\n");
	printf("Please select your operation.\n");
}

unsigned long Do_Init()
{
	CK_RV ck_rv = 0;
	CK_ULONG ulTokenCount = 0;
	CK_SLOT_ID_PTR pSlotID = NULL;

	
	INIT_TOKEN_PARAM initParam={0};
	DWORD dwPri=0, dwPub=0;
	

	ck_rv = ePass->C_Initialize(NULL_PTR);
	if(ck_rv != CKR_OK && ck_rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)
	{
		printf("C_Initialize Error: %08x", ck_rv);
	}
	
	ck_rv = ePass->C_GetSlotList(TRUE, NULL_PTR, &ulTokenCount);
	if(ck_rv != CKR_OK)
	{
		printf("C_GetSlotList 1 Error: %08x", ck_rv);
	}
	
	if(ulTokenCount == 0)
	{
		printf("No Found Slot!");
		goto InitClean;
	}
	
	pSlotID = new CK_SLOT_ID[ulTokenCount*sizeof(CK_SLOT_ID)];
	ck_rv = ePass->C_GetSlotList(TRUE,pSlotID,&ulTokenCount);
	if(ck_rv != CKR_OK)
	{
		printf("C_GetSlotList 2 Error: %08x", ck_rv);
		
		goto InitClean;
	}


	initParam.InitType = 3;
	initParam.ulPrvSize = 15360;
	initParam.ulPubSize = 10240;
	initParam.SoPinEC = 15;
	initParam.userPinEC = 15;

	
	ck_rv = pPriInit(pSlotID[0],&initParam);
	if(ck_rv != CKR_OK)
	{
		printf("pPriInit Error: %08x", ck_rv);
		goto InitClean;
	}
	
	ck_rv = pPkiInit(pSlotID[0]);
	if(ck_rv != CKR_OK)
	{
		printf("pPkiInit Error: %08x", ck_rv);
		goto InitClean;
	}
	
	printf("Success!\n");
	
InitClean:
	
	ck_rv = ePass->C_Finalize(NULL_PTR);
	if(ck_rv != CKR_OK)
	{
		printf("C_Initialize Error: %08x", ck_rv);
	}
	
	if(pSlotID != NULL)
	{
		delete [] pSlotID;
		pSlotID = NULL;
	}
	return CKR_OK;	
}

int main(int argc, char* argv[])
{
	m_hLibP11 = LoadLibrary("ngp11v211.dll");
	if(!m_hLibP11)
	{
		printf("Load ngp11v211.dll Error!");
		return FALSE;
	}
	
	m_hLibInit = LoadLibrary("init_ft12_sc.dll");
	if(!m_hLibInit)
	{
		printf("Load init_ft12_sc.dll Error!");
		return FALSE;
	}
	
	pC_GetFunctionList = (C_GETFUNCTIONLISTPROC)GetProcAddress(m_hLibP11,"C_GetFunctionList");
	pPriInit = (PRIINIT)GetProcAddress(m_hLibInit,"Init_DoPrvInit");
	pPkiInit = (PKIINIT)GetProcAddress(m_hLibInit,"Init_DoPKInit");

	pC_GetFunctionList(&ePass);	
	
	//
	while(true)
	{
		ShowMainCmdMenu();
		
		char ch = getch();
		if(ch != 0x1b)
			Do_Init();
		else
		{
			if(m_hLibP11)
			{
				FreeLibrary(m_hLibP11);	
				m_hLibP11 = NULL;
			}
			if(m_hLibInit)
			{
				FreeLibrary(m_hLibInit);
				m_hLibInit = NULL;
			}
		
			return 0;
		}
	}

	
	if(m_hLibP11)
		FreeLibrary(m_hLibP11);	
	
	if(m_hLibInit)
		FreeLibrary(m_hLibInit);
	

	return 0;
}

