pkcs.cpp
资源名称:pkcs11.rar [点击查看]
上传用户:filter2008
上传日期:2013-02-01
资源大小:101k
文件大小:23k
源码类别:
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 : 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;
- }
- }
- if(tokenInSlotList)
- free(tokenInSlotList);
- if(phCertList)
- free(phCertList);
- TRACE(__LINE__,"PKCS CreateContainerTable TRUE",NULL );
- return TRUE;
- }
- /*
- %--------------------------------------------------------------------------
- % ~Pkcs()
- %
- % ~Pkcs() est le destructor
- %---------------------------------------------------------------------------
- */
- Pkcs::~Pkcs()
- {
- }
- /*
- %--------------------------------------------------------------------------
- % VerifyContainerExistance
- %
- % VerifyContainerExistance checks the existance container
- %
- %
- % Parameters of entry :
- % IN container The required container
- %
- % return : TRUE if the operation occurred well, FALSE if not
- %---------------------------------------------------------------------------
- */
- BOOL Pkcs::VerifyContainerExistance(PKCSContainer* container)
- {
- TRACE(__LINE__,"VerifyContainerExistance",NULL );
- return table_Containers.VerifyEntry(container);
- }
- /*
- %--------------------------------------------------------------------------
- % GetContainer
- %
- % GetContainer is used to recover a container starting from its name
- % in the table container_table
- %
- %
- % Parameters of entry :
- % IN szContainerName the name of the required container
- %
- % return : the container
- %---------------------------------------------------------------------------
- */
- PKCSContainer* Pkcs::GetContainer(const CHAR IN * szContainerName)
- {
- TRACE(__LINE__,"Pkcs::GetContainer",NULL );
- if(!szContainerName)
- return NULL;
- int iCookie = START;
- table_Containers.Lock();
- PKCSContainer * pCnt;
- /*One seeks the Container with the good name*/
- do
- {
- pCnt = (PKCSContainer*)table_Containers.GetNext(iCookie);
- } while(pCnt && strcmp(pCnt->GetName(), szContainerName) != 0);
- table_Containers.Unlock();
- return pCnt;
- }
- /*
- %--------------------------------------------------------------------------
- % DoSign
- %
- % DoSign is used to sign data. Two calls are carried out to DoSign
- % the first call, in order to recover the size with alouer for the signature
- % the second call, in order to really carry out the signature
- %
- %
- % Parameters of entry :
- % IN pContainer the container to be used
- % IN pbyHashLen length of the data to be signed
- % IN pbyHash data to be signed
- % IN dwKeySpec type of key to be used
- % OUT pbySignature signed data
- % OUT pdwSigLen length of the signed data
- % IN g_strPwd pine code to use to reach the key private
- %
- % return : TRUE if the operation occurred well, FALSE if not
- %---------------------------------------------------------------------------
- */
- BOOL Pkcs::DoSign(PKCSContainer* pContainer, unsigned long pbyHashLen, LPBYTE pbyHash,DWORD dwKeySpec, LPBYTE pbySignature, LPDWORD pdwSigLen)
- {
- CK_RV rv = CKR_OK;
- CK_OBJECT_HANDLE hPrivKey=NULL;
- CK_TOKEN_INFO tokenInfo;
- CK_BYTE_PTR signature=NULL;
- bool bret;
- bool correctPin=false;
- TRACE(__LINE__,"Pkcs::DoSign BEGIN",NULL );
- CK_SESSION_INFO info;
- /* one loggs */
- /* Are we already logged */
- rv=((pContainer->GetpFunctionList())->C_GetSessionInfo)(pContainer->GethSession(),&info);
- if(rv!=CKR_OK)
- return FALSE;
- if(info.state!= CKS_RW_USER_FUNCTIONS && info.state!= CKS_RO_USER_FUNCTIONS)
- {
- do
- {
- unsigned char * codepin=(unsigned char *)malloc(MAX_PIN_LEN);
- CK_ULONG codepinLen=MAX_PIN_LEN;
- rv=getCodePorteur(codepin,codepinLen);
- if(rv!=CKR_OK){
- free(codepin);
- TRACE(__LINE__,"Pkcs::DoSign Cancel of code pin",NULL );
- return FALSE;
- }
- rv = login(pContainer->GetpFunctionList(), pContainer->GethSession(),codepin,strlen((const char *)codepin));
- if(rv==CKR_OK)
- {
- correctPin=true;
- }
- memset(codepin,0,codepinLen);
- if(rv==CKR_PIN_INCORRECT)
- {
- rv =((pContainer->GetpFunctionList())->C_GetTokenInfo)(pContainer->GetslotID(), &tokenInfo);
- if ((rv == CKR_OK)&&((tokenInfo.flags & CKF_USER_PIN_FINAL_TRY)))
- {
- rv=getCodePorteur(codepin,codepinLen);
- if(rv!=CKR_OK){
- free(codepin);
- TRACE(__LINE__,"DoSign::Decrypt Cancel of code pin",NULL );
- return FALSE;
- }
- rv = login(pContainer->GetpFunctionList(), pContainer->GethSession(),codepin,strlen((const char *)codepin));
- if(rv!=CKR_OK)
- {
- free(codepin);
- TRACE(__LINE__,"Pkcs::DoSign FALSE",NULL );
- return FALSE;
- }
- free(codepin);
- break;
- }
- else if(rv != CKR_OK)
- {
- free(codepin);
- TRACE(__LINE__,"Pkcs::DoSign FALSE",NULL );
- return FALSE;
- }
- }
- if((rv!=CKR_OK)&&(rv!=CKR_PIN_INCORRECT))
- {
- free(codepin);
- TRACE(__LINE__,"Pkcs::DoSign FALSE",NULL );
- return FALSE;
- }
- }while(!correctPin);
- }
- /* Recovery of the private key */
- rv = getPrivateKeyFromX509Cert(pContainer->GetpFunctionList(),pContainer->GethSession(), &hPrivKey, pContainer->Gethcert());
- if (rv!=CKR_OK) {
- TRACE(__LINE__,"Pkcs::DoSign FALSE",NULL );
- return FALSE;
- }
- /* If one invites CPSign the first time in order to recover the size to allow for the signature */
- if(!pbySignature)
- {
- /*CK_BYTE_PTR defaultSignature=(CK_BYTE_PTR)malloc(256*sizeof(CK_BYTE));
- CK_ULONG defaultSignatureLength=256;*/
- rv = sign(pContainer->GetpFunctionList(),pContainer->GethSession(),hPrivKey, (CK_BYTE_PTR)pbyHash,pbyHashLen,NULL_PTR/*defaultSignature*/,(CK_ULONG_PTR)pdwSigLen/*&defaultSignatureLength*/,1);
- if(rv!=CKR_OK){
- TRACE(__LINE__,"Pkcs::DoSign FALSE",NULL );
- free(signature);
- return FALSE;
- }
- //*pdwSigLen=defaultSignatureLength;
- }
- /* Recovery of the signature */
- else
- {
- /* real signature */
- signature=(CK_BYTE_PTR)malloc((*pdwSigLen)*sizeof(CK_BYTE));
- rv = sign(pContainer->GetpFunctionList(),pContainer->GethSession(),hPrivKey, (CK_BYTE_PTR)pbyHash,pbyHashLen,(CK_BYTE_PTR)signature,(CK_ULONG_PTR)pdwSigLen,1);
- if(rv!=CKR_OK){
- TRACE(__LINE__,"Pkcs::DoSign FALSE",NULL );
- free(signature);
- return FALSE;
- }
- /* Inversion of the block signature because CAPI awaits a turned over block*/
- int i,j;
- for (i=(*pdwSigLen)-1, j=0; i > j; --i, ++j)
- {
- CK_BYTE bTmp = signature[i];
- signature[i] = signature[j];
- signature[j] = bTmp;
- }
- memcpy(pbySignature,signature,*pdwSigLen);
- }
- if(signature)
- free(signature);
- bret=TRUE;
- TRACE(__LINE__,"Pkcs::DoSign TRUE",NULL );
- return bret;
- }
- /*
- %--------------------------------------------------------------------------
- % Decrypt
- %
- % Decrypt is used to decipher data
- %
- %
- % Parameters of entry :
- % IN hPubKey handle on the container to use to decipher
- % IN pbySource data to be deciphered
- % IN wSourceLength length of data to be deciphered
- % OUT pbyDestination deciphered data
- % OUT pwDestinationLen length of deciphered data
- % IN pass pine code to use to reach the key private
- %
- % return : TRUE if the operation occurred well, FALSE if not
- %---------------------------------------------------------------------------
- */
- BOOL Pkcs::Decrypt(HCRYPTKEY hPubKey, BYTE* pbySource, DWORD wSourceLength, BYTE* pbyDestination, DWORD* pwDestinationLen)
- {
- CK_RV rv = CKR_OK;
- CK_TOKEN_INFO tokenInfo;
- bool correctPin=false;
- TRACE(__LINE__,"Pkcs::Decrypt BEGIN",NULL );
- if(!Pkcs::VerifyContainerExistance((PKCSContainer*) hPubKey)){
- TRACE(__LINE__,"Pkcs::Decrypt FALSE",NULL );
- return FALSE;
- }
- CK_SESSION_INFO info;
- /* one loggs */
- /* Are we already logged */
- rv=((((PKCSContainer*) hPubKey)->GetpFunctionList())->C_GetSessionInfo)(((PKCSContainer*) hPubKey)->GethSession(),&info);
- if(rv!=CKR_OK)
- return FALSE;
- if(info.state!= CKS_RW_USER_FUNCTIONS && info.state!= CKS_RO_USER_FUNCTIONS)
- {
- /* we logg */
- do
- {
- unsigned char * codepin=(unsigned char *)malloc(MAX_PIN_LEN);
- CK_ULONG codepinLen=MAX_PIN_LEN;
- rv=getCodePorteur(codepin,codepinLen);
- if(rv!=CKR_OK){
- free(codepin);
- TRACE(__LINE__,"Pkcs::Decrypt Cancel of code pin",NULL );
- return FALSE;
- }
- rv = login(((PKCSContainer*) hPubKey)->GetpFunctionList(), ((PKCSContainer*) hPubKey)->GethSession(),codepin,strlen((const char *)codepin));
- if(rv==CKR_OK)
- {
- correctPin=true;
- }
- memset(codepin,0,codepinLen);
- if(rv==CKR_PIN_INCORRECT)
- {
- rv =((((PKCSContainer*) hPubKey)->GetpFunctionList())->C_GetTokenInfo)(((PKCSContainer*) hPubKey)->GetslotID(), &tokenInfo);
- if ((rv == CKR_OK)&&((tokenInfo.flags & CKF_USER_PIN_FINAL_TRY)))
- {
- rv=getCodePorteur(codepin,codepinLen);
- if(rv!=CKR_OK){
- free(codepin);
- TRACE(__LINE__,"Pkcs::Decrypt Cancel of code pin",NULL );
- return FALSE;
- }
- rv = login(((PKCSContainer*) hPubKey)->GetpFunctionList(), ((PKCSContainer*) hPubKey)->GethSession(),codepin,strlen((const char *)codepin));
- if(rv!=CKR_OK)
- {
- free(codepin);
- TRACE(__LINE__,"Pkcs::Decrypt FALSE",NULL );
- return FALSE;
- }
- free(codepin);
- break;
- }
- else if(rv != CKR_OK)
- {
- free(codepin);
- TRACE(__LINE__,"Pkcs::Decrypt FALSE",NULL );
- return FALSE;
- }
- }
- if((rv!=CKR_OK)&&(rv!=CKR_PIN_INCORRECT))
- {
- free(codepin);
- TRACE(__LINE__,"Pkcs::Decrypt FALSE",NULL );
- return FALSE;
- }
- }while(!correctPin);
- }
- /* one recovers a handle on the private key */
- CK_OBJECT_HANDLE hPrivKey=NULL;
- rv = getPrivateKeyFromX509Cert(((PKCSContainer*)hPubKey)->GetpFunctionList(), ((PKCSContainer*)hPubKey)->GethSession(), &hPrivKey, ((PKCSContainer*)hPubKey)->Gethcert());
- if (rv!=CKR_OK) {
- TRACE(__LINE__,"Pkcs::Decrypt FALSE",NULL );
- return FALSE;
- }
- /* the data are deciphered */
- rv=decrypt(((PKCSContainer*)hPubKey)->GetpFunctionList(), ((PKCSContainer*)hPubKey)->GethSession(), hPrivKey, (CK_BYTE_PTR) pbySource, (CK_ULONG_PTR)&wSourceLength,(CK_BYTE_PTR)pbyDestination,(CK_ULONG_PTR)pwDestinationLen);
- if (rv!=CKR_OK) {
- TRACE(__LINE__,"Pkcs::Decrypt FALSE",NULL );
- return FALSE;
- }
- TRACE(__LINE__,"Pkcs::Decrypt TRUE",NULL );
- return TRUE;
- }
- CK_RV getCodePorteur(unsigned char * pCodePorteur, CK_ULONG codePorteurLen) {
- CK_RV rv=CKR_OK;
- if (pCodePorteur==NULL) {
- rv=CKR_ARGUMENTS_BAD;
- return rv;
- }
- // get the pin code
- DialogBox(g_hModule, MAKEINTRESOURCE(IDD_PWD), NULL, DialogProc);
- if (!strcmp((const char*)g_strPwd,"CANCEL")){
- memset(g_strPwd,0,sizeof(g_strPwd));
- rv=CKR_CANCEL;
- return rv;
- }
- memcpy(pCodePorteur, g_strPwd, codePorteurLen);
- memset(g_strPwd,0,sizeof(g_strPwd));
- return rv;
- }
- /*
- %--------------------------------------------------------------------------
- % DialogProc
- %
- % return : TRUE if the operation occurred well, FALSE if not
- %---------------------------------------------------------------------------
- */
- BOOL CALLBACK DialogProc(
- HWND hwndDlg, /* handle to dialog box*/
- UINT uMsg, /* message*/
- WPARAM wParam, /* first message parameter*/
- LPARAM lParam /* second message parameter*/
- )
- {
- switch( uMsg )
- {
- case WM_INITDIALOG:
- break;
- case WM_COMMAND:
- switch ( idCtrl(wParam, lParam) )
- {
- case IDOK:
- // memorisation du mot de passe
- GetDlgItemText(hwndDlg,IDC_PWD,(char *)g_strPwd,30);
- EndDialog(hwndDlg, idCtrl(wParam, lParam));
- return TRUE;
- case IDCANCEL:
- strncpy((char *)g_strPwd,"CANCEL",6);
- EndDialog(hwndDlg, idCtrl(wParam, lParam));
- return TRUE;
- }
- return TRUE;
- break;
- }
- return FALSE;
- }
- char getchar(char val){
- int valMod=val&0x0F;
- char temp;
- if(valMod<=9)
- temp='0'+valMod;
- else
- temp='A'+(valMod-0xA);
- return temp;
- }