pkcscontainer.cpp
资源名称:pkcs11.rar [点击查看]
上传用户:filter2008
上传日期:2013-02-01
资源大小:101k
文件大小:11k
源码类别:
CA认证
开发平台:
C/C++
- /****************************************************************************
- * 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 : PKCSContainer
- %
- % VERSION : 1.00
- %
- % FICHIER : container.cpp
- %
- % Class container : it has the parameters of a container and methods making
- % it possible to handle them
- %----------------------------------------------------------------------------
- % Version 1.00
- %
- % CPX-31/03/2003-Cr閍tion
- %----------------------------------------------------------------------------
- */
- /*
- % Libraries ANSI or system
- %------------------------------
- */
- #include <windows.h>
- /*
- % HEADER Files include
- %-----------------------
- */
- #include "pkcscontainer.h"
- #include "cryptool.h"
- PKCSContainer::PKCSContainer()
- {
- }
- /*
- %--------------------------------------------------------------------------
- % Initialize
- %
- % The Initialize method is used to initialize the container object
- %
- %
- % Parameters of entry :
- % IN pFunctionList pointer on the list of functions Pkcs#11
- % IN hSession handle on a session Pkcs#11
- % IN hcert handle on an object PKCS#11 certificate
- % IN cursubject character strings corresponding to the subject of the certificate
- % IN cursubjectLen length of the character strings above
- % IN keyId character strings corresponding to the keyID of the certificate
- % IN keyIdLen length of the character strings above
- % IN containerName the name of the container
- %
- % return : TRUE if the operation occurred well, FALSE if not
- %---------------------------------------------------------------------------
- */
- BOOL PKCSContainer::Initialize(CK_FUNCTION_LIST_PTR pFunctionList,CK_SLOT_ID slotID,CK_SESSION_HANDLE hSession,CK_OBJECT_HANDLE hcert,CK_BYTE_PTR cursubject,unsigned long cursubjectLen,CK_BYTE_PTR keyId,unsigned long keyIdLen,char * containerName )
- {
- TRACE(__LINE__,"PKCSContainer::Initialize BEGIN",NULL);
- this->pFunctionList=pFunctionList;
- this->slotID=slotID;
- this->hSession=hSession;
- this->hcert=hcert;
- this->cursubject=(CK_BYTE_PTR) malloc(cursubjectLen*sizeof(CK_BYTE));
- memcpy(this->cursubject,cursubject,cursubjectLen);
- this->keyId=(CK_BYTE_PTR) malloc(keyIdLen*sizeof(CK_BYTE));
- memcpy(this->keyId,keyId,keyIdLen);
- this->containerName=new char[strlen(containerName)];
- strcpy(this->containerName,containerName);
- TRACE(__LINE__,"PKCSContainer::Initialize TRUE",NULL);
- return TRUE;
- }
- /*
- %--------------------------------------------------------------------------
- % ~PKCSContainer()
- %
- % ~PKCSContainer() is the destructor of the container object
- %---------------------------------------------------------------------------
- */
- PKCSContainer::~PKCSContainer()
- {
- }
- /*
- %--------------------------------------------------------------------------
- % Delete()
- %
- % Delete() destroy parameters of the container object
- %
- %
- %
- % return : TRUE if the operation occurred well, FALSE if not
- %---------------------------------------------------------------------------
- */
- bool PKCSContainer::Delete()
- {
- TRACE(__LINE__,"PKCSContainer::Delete BEGIN",NULL);
- CK_RV rv=CKR_OK;
- free(this->cursubject);
- free(this->keyId);
- rv=closeSession(pFunctionList,hSession);
- if(rv!=CKR_OK)
- {
- if(rv==CKR_SESSION_HANDLE_INVALID){
- TRACE(__LINE__,"PKCSContainer::Delete TRUE",NULL);
- return true;
- }
- TRACE(__LINE__,"PKCSContainer::Delete FALSE",NULL);
- return false;
- }
- TRACE(__LINE__,"PKCSContainer::Delete TRUE",NULL);
- return true;
- }
- /*
- %--------------------------------------------------------------------------
- % GetpFunctionList()
- %
- % GetpFunctionList() return the pointer of function of DLL PKCS used for this container
- %
- %
- %
- % return : the pointer of function of DLL PKCS used for this container
- %---------------------------------------------------------------------------
- */
- CK_FUNCTION_LIST_PTR PKCSContainer::GetpFunctionList()
- {
- return pFunctionList;
- }
- /*
- %--------------------------------------------------------------------------
- % SetpFunctionList
- %
- % SetpFunctionList allows to set the pointer of function of DLL PKCS used for this container
- %
- % Parameters of entry :
- % IN pFunctionList pointer on the list of functions Pkcs#11
- %---------------------------------------------------------------------------
- */
- void PKCSContainer::SetpFunctionList(CK_FUNCTION_LIST_PTR pFunctionList)
- {
- this->pFunctionList=pFunctionList;
- }
- /*
- %--------------------------------------------------------------------------
- % GetslotID()
- %
- % GetslotID() returns the slotID of this container
- %
- %
- % return : the slotID of this container
- %---------------------------------------------------------------------------
- */
- CK_SLOT_ID PKCSContainer::GetslotID()
- {
- return slotID;
- }
- /*
- %--------------------------------------------------------------------------
- % SetslotID
- %
- % SetslotID allows to set the slotID of this container
- %
- %
- % Parameters of entry :
- % IN slotID the slotID of container
- %---------------------------------------------------------------------------
- */
- void PKCSContainer::SetslotID(CK_SLOT_ID slotID)
- {
- this->slotID=slotID;
- }
- /*
- %--------------------------------------------------------------------------
- % GethSession
- %
- % GethSession return the session to be used for this container
- %
- % return : the session to be used for this container
- %---------------------------------------------------------------------------
- */
- CK_SESSION_HANDLE PKCSContainer::GethSession()
- {
- return hSession;
- }
- /*
- %--------------------------------------------------------------------------
- % SethSession
- %
- % SethSession allows to set the session to be used for this container
- %
- %
- % Parameters of entry :
- % IN hSession the PKCS#11 session
- %
- %---------------------------------------------------------------------------
- */
- void PKCSContainer::SethSession(CK_SESSION_HANDLE hSession)
- {
- this->hSession=hSession;
- }
- /*
- %--------------------------------------------------------------------------
- % Gethcert
- %
- % Gethcert returns the handle of certificate of this container
- %
- %
- % return : the handle of certificate of this container
- %---------------------------------------------------------------------------
- */
- CK_OBJECT_HANDLE PKCSContainer::Gethcert()
- {
- return hcert;
- }
- /*
- %--------------------------------------------------------------------------
- % Sethcert
- %
- % Sethcert allows to set the handle of certificate of this container
- %
- %
- % Parameters of entry :
- % IN hcert the handle of certificate of this container
- %---------------------------------------------------------------------------
- */
- void PKCSContainer::Sethcert(CK_OBJECT_HANDLE hcert)
- {
- this->hcert=hcert;
- }
- /*
- %--------------------------------------------------------------------------
- % Getcursubject
- %
- % Getcursubject returns the subject of the certificate of this container
- %
- %
- % return : the subject of the certificate of this container
- %---------------------------------------------------------------------------
- */
- CK_BYTE_PTR PKCSContainer::Getcursubject()
- {
- return cursubject;
- }
- /*
- %--------------------------------------------------------------------------
- % GetkeyId
- %
- % GetkeyId returns the ID of the key of the certificate of this container
- %
- %
- % return : the ID of the key
- %---------------------------------------------------------------------------
- */
- CK_BYTE_PTR PKCSContainer::GetkeyId()
- {
- return keyId;
- }
- /*
- %--------------------------------------------------------------------------
- % GetdwKeySpec
- %
- % GetdwKeySpec return the type of key for this container
- %
- %
- % Parameters of entry :
- % IN dwKeySpec the type of key for this container
- %
- %
- % return : the type of key
- %---------------------------------------------------------------------------
- */
- BOOL PKCSContainer::GetdwKeySpec(DWORD dwKeySpec)
- {
- TRACE(__LINE__,"PKCSContainer::GetdwKeySpec BEGIN",NULL);
- CK_RV rv=CKR_OK;
- CK_BBOOL bbool;
- CK_OBJECT_HANDLE phKey=NULL;
- rv=getPublicKeyFromX509Cert(this->pFunctionList,this->hSession,&phKey ,this->hcert);
- if (rv != CKR_OK){
- TRACE(__LINE__,"PKCSContainer::GetdwKeySpec FALSE",NULL);
- return false;
- }
- if(dwKeySpec==AT_SIGNATURE)
- {
- bbool=isPubKeySupportSign(this->pFunctionList,this->hSession,phKey);
- if(bbool==TRUE){
- TRACE(__LINE__,"PKCSContainer::GetdwKeySpec TRUE",NULL);
- return TRUE;
- }
- else{
- TRACE(__LINE__,"PKCSContainer::GetdwKeySpec FALSE",NULL);
- return FALSE;
- }
- }
- else if(dwKeySpec==AT_KEYEXCHANGE)
- {
- bbool=isPubKeySupportEncrypt(this->pFunctionList,this->hSession,phKey);
- if(bbool==TRUE){
- TRACE(__LINE__,"PKCSContainer::GetdwKeySpec TRUE",NULL);
- return TRUE;
- }
- else{
- TRACE(__LINE__,"PKCSContainer::GetdwKeySpec FALSE",NULL);
- return FALSE;
- }
- }
- else{
- TRACE(__LINE__,"PKCSContainer::GetdwKeySpec FALSE",NULL);
- return FALSE;
- }
- }
- /*
- %--------------------------------------------------------------------------
- % GetUserKey
- %
- % GetUserKey returns a handle on the container
- %
- %
- % Parameters of entry :
- % IN dwKeySpec the type of key for this container
- % OUT phUserKey handle on the container
- %
- % Valeur retourn閑 : TRUE if the operation occurred well, FALSE if not
- %---------------------------------------------------------------------------
- */
- BOOL PKCSContainer::GetUserKey(DWORD dwKeySpec,HCRYPTKEY* phUserKey)
- {
- TRACE(__LINE__,"PKCSContainer::GetUserKey BEGIN",NULL);
- *phUserKey=(HCRYPTKEY)this;
- TRACE(__LINE__,"PKCSContainer::GetUserKey TRUE",NULL);
- return TRUE;
- }