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

📄 csp.cpp

📁 PKCS#11的微软CSP实现源码
💻 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 : csp
%
% VERSION : 1.00
%
% FILE : csp.cpp
%
% Entrance points of the DLL:  all the entrance points are redirected towards handlecontainer which manages a container
%----------------------------------------------------------------------------
% Version 1.00
% 
% CPX-31/03/2003-Creation
%----------------------------------------------------------------------------
% The PKCS#11 module must be defined in the csp_pkcs.ini as below :
% [CSP_PKCS]
% Name=name of pkcs#11 module
%
%----------------------------------------------------------------------------
% You can register the csp in Windows thanks to samples which one can find in 
% the CSPDK which is downloadable at the adress :
% http://www.microsoft.com/downloads/details.aspx?FamilyId=0F436C75-2304-42BB-B81A-BA0C2C47BAC2&displaylang=en
%
*/ 

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif


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


/*
%Declaration of the private static variables
%-------------------------------------------
*/
extern "C" HINSTANCE g_hModule = NULL;
extern bool		 already_initialized=false;
extern int		 pkcsInitialized=0;





/*
%--------------------------------------------------------------------------
% DllMain
%
% Main of the dll
%--------------------------------------------------------------------------
*/
BOOL WINAPI
DllMain(
  HINSTANCE hinstanceDLL, 
  DWORD ulRaison,     
  LPVOID lpvReserve)  
{
    if (ulRaison == DLL_PROCESS_ATTACH)
    {
		setTraceLevel();
        TRACE(__LINE__,"DLL_PROCESS_ATTACH",NULL);
        DisableThreadLibraryCalls(hinstanceDLL);
        g_hModule = hinstanceDLL;

	}	
	return TRUE;
}	

/*------------------------------------------------------------------------*/

/*
Each of these functions can be found in MSDN.  Please refer there
for additional information.
*/

/*------------------------------------------------------------------------
	CPAcquireContext
-------------------------------------------------------------------------*/
BOOL WINAPI
CPAcquireContext(
    OUT HCRYPTPROV *phProv,
    IN  CHAR *pszContainer,
    IN  DWORD dwFlags,
    IN  PVTableProvStruc pVTable)
{

	BOOL rv=TRUE;
    TRACE(__LINE__,"CPAcquireContext BEGIN :%d %s %d %d",phProv,pszContainer,dwFlags,pVTable);
	rv=Pkcs::Initialize();
	if(rv!=TRUE)
		return FALSE;

	if (((dwFlags & CRYPT_VERIFYCONTEXT) == CRYPT_VERIFYCONTEXT) &&
        (NULL != pszContainer) && (0 != *pszContainer))

    {
        SetLastError((DWORD) NTE_BAD_FLAGS);
	    TRACE(__LINE__,"CPAcquireContext ERROR :%d %s %d %d",phProv,pszContainer,dwFlags,pVTable);
        return FALSE;
    }
	
	const CHAR * pszFinalContainer;
    
	if(!pszContainer || (0 == *pszContainer))
		pszFinalContainer = DEFAULT_CONTAINER;
	else
		pszFinalContainer =pszContainer;

	HandleContainer* pHandleContainer = new HandleContainer();

	if(!pHandleContainer){
	    TRACE(__LINE__,"CPAcquireContext ERROR :%d %s %d %d",phProv,pszContainer,dwFlags,pVTable);
		return FALSE;
	}

	if(!pHandleContainer->CreateHandleContainer(pszFinalContainer, dwFlags, pVTable))
	{
		delete pHandleContainer;
	    TRACE(__LINE__,"CPAcquireContext ERROR :%d %s %d %d",phProv,pszContainer,dwFlags,pVTable);
		return FALSE;
	}

	if((dwFlags & CRYPT_DELETEKEYSET) == CRYPT_DELETEKEYSET)
		delete pHandleContainer;
	else
		*phProv = (HCRYPTPROV)pHandleContainer;


    TRACE(__LINE__,"CPAcquireContext TRUE :%d %s %d %d",phProv,pszContainer,dwFlags,pVTable);
	return TRUE;
}


/*------------------------------------------------------------------------
	CPReleaseContext
-------------------------------------------------------------------------*/
BOOL WINAPI
CPReleaseContext(
    IN  HCRYPTPROV hProv,
    IN  DWORD ulFlags)
{
	TRACE(__LINE__,"CPReleaseContext : %d, %d",hProv,ulFlags);
	if(!HandleContainer::VerifyHandleContainer((HandleContainer*)hProv))
	{
		TRACE(__LINE__,"CPReleaseContext ERROR: %d, %d",hProv,ulFlags);
		SetLastError(NTE_BAD_UID);
		return FALSE;
	}
	delete (HandleContainer*)hProv;
	Pkcs::FreePkcs();

	TRACE(__LINE__,"CPReleaseContext TRUE: %d, %d",hProv,ulFlags);
	return TRUE;
}




/*------------------------------------------------------------------------
	CPGenKey
-------------------------------------------------------------------------*/

BOOL WINAPI
CPGenKey(
    IN  HCRYPTPROV hProv,
    IN  ALG_ID uiAlgid,
    IN  DWORD ulFlags,
    OUT HCRYPTKEY *phKey)
{
	TRACE(__LINE__,"CPGenKey NOT IMPLEMENTED",NULL);
	SetLastError(E_NOTIMPL);
	return FALSE;
    
}


/*------------------------------------------------------------------------
	CPDeriveKey
-------------------------------------------------------------------------*/

BOOL WINAPI
CPDeriveKey(
    IN  HCRYPTPROV hProv,
    IN  ALG_ID Algid,
    IN  HCRYPTHASH hHash,
    IN  DWORD dwFlags,
    OUT HCRYPTKEY *phKey)
{
	TRACE(__LINE__,"CPDeriveKey NOT IMPLEMENTED",NULL);
	SetLastError(E_NOTIMPL);
	return FALSE;
}


/*------------------------------------------------------------------------
	CPDestroyKey
-------------------------------------------------------------------------*/

BOOL WINAPI
CPDestroyKey(
    IN  HCRYPTPROV hProv,
    IN  HCRYPTKEY hKey)
{
	TRACE(__LINE__,"CPDestroyKey NOT IMPLEMENTED",NULL);
    return CryptDestroyKey(hKey);
}



/*------------------------------------------------------------------------
	CPSetKeyParam
-------------------------------------------------------------------------*/

BOOL WINAPI
CPSetKeyParam(
    IN  HCRYPTPROV hProv,
    IN  HCRYPTKEY hKey,
    IN  DWORD ulParametre,
    IN  CONST BYTE *pucData,
    IN  DWORD ulFlags)
{
	BOOL result;
	TRACE(__LINE__,"CPSetKeyParam BEGIN: %d %d %x %d %d",hProv,hKey,ulParametre,pucData,ulFlags);
	if(!HandleContainer::VerifyHandleContainer((HandleContainer*)hProv))
	{
		SetLastError(NTE_BAD_UID);
		TRACE(__LINE__,"CPSetKeyParam ERROR: %d %d %x %d %d",hProv,hKey,ulParametre,pucData,ulFlags);
		return FALSE;
	}

	result=((HandleContainer*)hProv)->SetKeyParam(hKey, ulParametre, pucData, ulFlags);
	if(!result)
	{
		TRACE(__LINE__,"CPSetKeyParam ERROR: %d %d %x %d %d",hProv,hKey,ulParametre,pucData,ulFlags);
		return FALSE;
	}
	
	TRACE(__LINE__,"CPSetKeyParam TRUE: %d %d %x %d %d",hProv,hKey,ulParametre,pucData,ulFlags);
	return TRUE;
}
   

/*------------------------------------------------------------------------
	CPGetKeyParam
-------------------------------------------------------------------------*/

BOOL WINAPI
CPGetKeyParam(
    IN  HCRYPTPROV hProv,
    IN  HCRYPTKEY hKey,
    IN  DWORD ulParametre,
    OUT LPBYTE pucData,
    IN OUT LPDWORD pulDataLen,
    IN  DWORD ulFlags)
{
	BOOL result;
	TRACE(__LINE__,"CPGetKeyParam BEGIN :%d %d %d %d %d %d",hProv,hKey,ulParametre,pucData,pulDataLen,ulFlags);
	if(!HandleContainer::VerifyHandleContainer((HandleContainer*)hProv))
	{
		SetLastError(NTE_BAD_UID);
		TRACE(__LINE__,"CPGetKeyParam ERROR :%d %d %d %d %d %d",hProv,hKey,ulParametre,pucData,pulDataLen,ulFlags);
		return FALSE;
	}

	result= ((HandleContainer*)hProv)->GetKeyParam(hKey, ulParametre, pucData, pulDataLen, ulFlags);
	if(!result)
	{
		TRACE(__LINE__,"CPGetKeyParam ERROR :%d %d %d %d %d %d",hProv,hKey,ulParametre,pucData,pulDataLen,ulFlags);
		return FALSE;
	}

	TRACE(__LINE__,"CPGetKeyParam TRUE :%d %d %d %d %d %d",hProv,hKey,ulParametre,pucData,pulDataLen,ulFlags);
	return TRUE;
}



/*------------------------------------------------------------------------
	CPSetProvParam
-------------------------------------------------------------------------*/
BOOL WINAPI
CPSetProvParam(
    IN  HCRYPTPROV hProv,
    IN  DWORD ulParametre,
    IN  CONST BYTE *pucData,
    IN  DWORD ulFlags)
{
	TRACE(__LINE__,"CPSetProvParam NOT IMPLEMENTED",NULL);
	SetLastError(E_NOTIMPL);
	return FALSE;
}



/*------------------------------------------------------------------------
	CPGetProvParam
-------------------------------------------------------------------------*/

BOOL WINAPI
CPGetProvParam(
    IN  HCRYPTPROV hProv,
    IN  DWORD ulParametre,
    OUT LPBYTE pucData,
    IN OUT LPDWORD pulDataLen,
    IN  DWORD ulFlags)
{
	TRACE(__LINE__,"CPGetProvParam BEGIN :%d,%d;%d,%d,%d",hProv,ulParametre,pucData,pulDataLen,ulFlags);
	if(!HandleContainer::VerifyHandleContainer((HandleContainer*)hProv))
	{
		SetLastError(NTE_BAD_UID);
		TRACE(__LINE__,"CPGetProvParam ERROR :%d,%d;%d,%d,%d",hProv,ulParametre,pucData,pulDataLen,ulFlags);
		return FALSE;
	}

	TRACE(__LINE__,"CPGetProvParam TRUE :%d,%d;%d,%d,%d",hProv,ulParametre,pucData,pulDataLen,ulFlags);
	return ((HandleContainer*)hProv)->GetProvParam(ulParametre, pucData, pulDataLen, ulFlags);

⌨️ 快捷键说明

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