⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pkcs.cpp

📁 信息安全开发使用的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
* library : pkcs_csp.dll
* Purpose : It is a cryptographic service provider which is an independent 
* software module that actually performs cryptography algorithms for 
* authentication, encoding, and encryption.
* This DLL can be interfaced on any PKCS#11 module.  
*
* Copyright (C) 2003 Ilex Syst鑝es Informatiques
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact :
* Ilex 
* 51 boulevard Voltaire
* 92600 Asni鑢es-sur-Seine
* pkizy@ilex.fr
*
* Author: Delouvrier Antoine
*
*******************************************************************************/

/*
%----------------------------------------------------------------------------
% PROJECT : CSP_PKCS
%
% MODULE : pkcs
%
% VERSION : 1.00
%
% FILE : pkcs.cpp
%
% cryptool: class giving access functions PKCS#11
%----------------------------------------------------------------------------
% Version 1.00
% 
% CPX-31/03/2003-Creation
%----------------------------------------------------------------------------
*/ 

/*
% Libraries ANSI or system
%------------------------------
*/
#include <windows.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>

/*
% HEADER Files include
%-----------------------
*/
#include "handlecontainer.h"
#include "pkcs.h"
#include "resource.h"


/*
% Macros
%-------
*/

#define      idCtrl(w,l)    (LOWORD(w))


TableOfHandle Pkcs::table_Containers;
CK_FUNCTION_LIST_PTR Pkcs::pFunctionList;


extern bool already_initialized;
extern int		 pkcsInitialized;


BOOL CALLBACK DialogProc(HWND hWnd, UINT wMsgID, WPARAM wParam, LPARAM lParam);
CK_RV getCodePorteur(unsigned char * pCodePorteur, CK_ULONG codePorteurLen);
char getchar(char val);



CK_CHAR		 g_strPwd[MAX_PIN_LEN] = "CANCEL";
extern "C" HINSTANCE g_hModule;



Pkcs::Pkcs() 
{
}


/*
%--------------------------------------------------------------------------
% Initialize
%
% Initialize is used to tinitialize the dll PKCS#11
%		 
%
% return :	TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL Pkcs::Initialize()
{
		
   	CK_RV rv=0;      
	pFunctionList=NULL;
	TRACE(__LINE__,"Pkcs::Initialize BEGIN",NULL);    
	
    /* Load the PKCS11 library */
   	rv= init(&pFunctionList);
	if (rv!=CKR_OK){
		TRACE(__LINE__,"Pkcs::Initialize FALSE",NULL);    
		return FALSE;
	}
	TRACE(__LINE__,"Pkcs::Initialize TRUE",NULL);    

	return TRUE;
}

/*
%--------------------------------------------------------------------------
% FreePkcs
%
% FreePkcs is used to release the dll PKCS#11
%		 
%
% return :	TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/

BOOL Pkcs::FreePkcs()
{
	TRACE(__LINE__,"Pkcs::FreePkcs BEGIN",NULL);    
	CK_RV rv=0; 
	
	// The sessions of all the containers are closed
	int i = START;
	table_Containers.Lock();
	PKCSContainer * pCnt;
	
	do
	{
		pCnt = (PKCSContainer*)table_Containers.GetNext(i);
		if(pCnt==NULL)
			break;
		pCnt->Delete();
		table_Containers.RemoveEntry(pCnt);
	} while(pCnt);
	table_Containers.Unlock();
	
	i = START;
	HandleContainer *phandleContainer;
	HandleContainer::handles_Container.Lock();
	do
	{
		phandleContainer= (HandleContainer*)((HandleContainer::handles_Container).GetNext(i));
		if(phandleContainer==NULL)
			break;
		delete phandleContainer;
	} while(phandleContainer);
	(HandleContainer::handles_Container).Unlock();


	if(!already_initialized){
		TRACE(__LINE__,"CleanUP PKCS#11 ",NULL );
		rv=cleanup(pFunctionList);
		if (rv!=CKR_OK)
			return FALSE;
		TRACE(__LINE__,"CleanUP OK PKCS#11 ",NULL );
		return TRUE;
	}
	TRACE(__LINE__,"NOT CleanUP ",NULL );
	return TRUE;
}


/*
%--------------------------------------------------------------------------
% FreeContainer
%
% FreeContainer is used to free the container
%		 
%
% return :	TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/

BOOL Pkcs::FreeContainer()
{
	TRACE(__LINE__,"FreeContainer ",NULL );
	CK_RV rv=0; 
	
	// The sessions of all the containers are closed
	int i = START;
	table_Containers.Lock();
	PKCSContainer * pCnt;
	
	do
	{
		pCnt = (PKCSContainer*)table_Containers.GetNext(i);
		if(pCnt==NULL)
			break;
		pCnt->Delete();
		table_Containers.RemoveEntry(pCnt);
		delete pCnt;
	} while(pCnt);
	table_Containers.Unlock();
	
	i = START;
	HandleContainer *phandleContainer;
	HandleContainer::handles_Container.Lock();
	do
	{
		phandleContainer= (HandleContainer*)((HandleContainer::handles_Container).GetNext(i));
		if(phandleContainer==NULL)
			break;
		delete phandleContainer;
	} while(phandleContainer);
	(HandleContainer::handles_Container).Unlock();

	TRACE(__LINE__,"Pkcs::FreeContainer TRUE",NULL);    
	return TRUE;
}


/*
%--------------------------------------------------------------------------
% CreateContainerTable
%
% CreateContainerTable is used to create a tablecontainer with initialization
%		 
%
% return :	TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL Pkcs::CreateContainerTable(){
	

	TRACE(__LINE__,"PKCS CreateContainerTable BEGIN",NULL );
	CK_RV rv=0;                   // Return Code
	CK_ULONG tokenIndex=0;
	CK_ULONG tokenInSlotListSize = 256;
	CK_SLOT_ID_PTR tokenInSlotList = (CK_SLOT_ID_PTR)malloc(tokenInSlotListSize*sizeof(CK_SLOT_ID));
	
	CK_SESSION_HANDLE hSession;
	 

	CK_ULONG certListSize = 256;
	CK_OBJECT_HANDLE_PTR phCertList = (CK_OBJECT_HANDLE_PTR) malloc(certListSize*sizeof(CK_OBJECT_HANDLE));
	
	
	CK_BYTE_PTR  curSubject=NULL;
	CK_ULONG    curSubjectLen=512;

	CK_ULONG keyIdLen=256; 
	CK_BYTE_PTR keyId = NULL;

	/* One recovers all the slots having tokens */
	rv = getSlotListWithToken(Pkcs::pFunctionList, tokenInSlotList, &tokenInSlotListSize);
	if (rv!=CKR_OK){
		TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
		free(tokenInSlotList);
		free(phCertList);
		return FALSE;
	}
	
	/* one traverses all the tokens */
	for ( tokenIndex = 0; tokenIndex < tokenInSlotListSize ; tokenIndex++)
    {
        rv=CKR_OK;
	
		/* One opens a session for each token */
		if (openSession(Pkcs::pFunctionList, tokenInSlotList[tokenIndex],&hSession)==CKR_OK)
	    {
			
			certListSize=256;
			rv = getAllX509CertificateList(pFunctionList, hSession, phCertList, &certListSize);

			/* one courses the list of the certificates available on this token */
			for (CK_ULONG j=0; j<certListSize; j++)
			{
				/* If the certificate has no public Key, we consider that it is certificate chain */
				CK_OBJECT_HANDLE hKey;
				rv=getPublicKeyFromX509Cert(pFunctionList,hSession,&hKey,phCertList[j]);
				if(rv!=CKR_OK)
					continue;

				/* one creates the name of the container */
				char containerName[256]="";			
				char curSubjectContainer[101]="";
				char keyIdContainer[101]="";
				rv =  getX509Subject( pFunctionList, hSession, phCertList[j],NULL_PTR, &curSubjectLen);
				if (rv!=CKR_OK)
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					free(tokenInSlotList);
					free(phCertList);
					return FALSE;
				}
				curSubject=(CK_BYTE_PTR) malloc(curSubjectLen*sizeof(CK_BYTE));
				if (curSubject == NULL)
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					free(tokenInSlotList);
					free(phCertList);
					return FALSE;
				}
				rv =  getX509Subject( pFunctionList, hSession, phCertList[j],curSubject, &curSubjectLen);
		
				if (rv == CKR_OK)
				{
					int i=0;
					for ( CK_ULONG j=0; j < min(curSubjectLen,(strlen(curSubjectContainer)-1)); j++)
					{
						/*if ( (unsigned)curSubject[j] > 0x20 && (unsigned)curSubject[j] < 0x7F)
						{	
							curSubjectContainer[i]=curSubject[j];
							i++;
						}*/
						curSubjectContainer[i]=getchar(curSubject[j]);
						i++;

					}
					curSubjectContainer[i]=0;
				}
				else
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					free(tokenInSlotList);
					free(curSubject);
					free(phCertList);
					return FALSE;
				}

				rv = getX509KeyId(pFunctionList, hSession,  phCertList[j], NULL_PTR, &keyIdLen);
				if (rv!=CKR_OK) 
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					free(tokenInSlotList);
					free(curSubject);
					free(phCertList);
					return FALSE;
				}

				keyId = (CK_BYTE_PTR) malloc(keyIdLen*sizeof(CK_BYTE));
				if ( keyId == NULL)
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					free(tokenInSlotList);
					free(curSubject);
					free(phCertList);
					return FALSE;
				}
				rv = getX509KeyId(pFunctionList, hSession, phCertList[j], keyId, &keyIdLen);
				if (rv == CKR_OK)
				{
					for ( CK_ULONG j=0; j < min(keyIdLen,(strlen(keyIdContainer)-1)); j++)
					{
						//keyIdContainer[j]=keyId[j];
						keyIdContainer[j]=getchar(keyId[j]);
					}
					keyIdContainer[j]=0;
															
				}
				else
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					free(tokenInSlotList);
					free(curSubject);
					free(keyId);
					free(phCertList);
					return FALSE;
				}
				strcpy(containerName,curSubjectContainer);
				strcat( containerName,keyIdContainer);
				TRACE(__LINE__," Pkcs containerName to be added: %s", containerName );
				PKCSContainer* pContainer = NULL;
				/* one creates the container object */
				pContainer=new PKCSContainer();
				if(!pContainer->Initialize(pFunctionList,tokenInSlotList[tokenIndex],hSession,phCertList[j],curSubject,curSubjectLen,keyId,keyIdLen,containerName))
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					delete pContainer;
					free(tokenInSlotList);
					free(curSubject);
					free(keyId);
					free(phCertList);
					return FALSE;
				}
				
				/* one adds this container to the table of the containers*/
				if(!table_Containers.AddEntry(pContainer))
				{
					TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
					delete pContainer;
					free(tokenInSlotList);
					free(curSubject);
					free(keyId);
					free(phCertList);
					return FALSE;
				}

			}
			if(curSubject){
				free(curSubject);
				curSubject=NULL_PTR;
			}
			if(keyId){
				free(keyId);
				keyId=NULL_PTR;
			}

		}
		else
		{
			TRACE(__LINE__,"PKCS CreateContainerTable FALSE",NULL );
			if(tokenInSlotList)
				free(tokenInSlotList);
			if(phCertList)
				free(phCertList);
			return FALSE;
		}
		
  
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -