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

📄 exportcertdlg.cpp

📁 基于PKCS#11和ft USBKEY的数字证书导出例子。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		delete[] m_pSlotList;
		m_pSlotList = NULL_PTR;
	}
	else
	{
		DlgUserPIN* dlgUserPIN = new DlgUserPIN;
		dlgUserPIN->DoModal();
		delete dlgUserPIN;
		if("" == g_strUserPIN)
		{
			AfxMessageBox("You must first input your pin", MB_OK | MB_ICONERROR);
			if(m_hSession)
			{
				C_CloseSession(m_hSession);
				m_hSession = NULL_PTR;
			}
			if(m_pSlotList)
			{
				delete[] m_pSlotList;
				m_pSlotList = NULL_PTR;
			}
			
			return;
		}
		
		CK_ULONG ulPIN = g_strUserPIN.GetLength();
		CK_BYTE_PTR pPIN = (CK_BYTE_PTR)g_strUserPIN.GetBuffer(ulPIN);
		
		::SetCursor(::LoadCursor(NULL, IDC_WAIT));
		CK_RV rv;
		rv = C_Login(m_hSession, CKU_USER, pPIN, ulPIN);
		if(CKR_OK != rv)
		{
			AfxMessageBox("Can't use your pin login to Token ", MB_OK | MB_ICONERROR);
			if(m_hSession)
			{
				C_CloseSession(m_hSession);
				m_hSession = NULL_PTR;
			}
			if(m_pSlotList)
			{
				delete[] m_pSlotList;
				m_pSlotList = NULL_PTR;
			}
			
			return;
		}
		else
			MessageBox("success login to Token", "login ePass" );
			m_btnlogin.EnableWindow(false);
			m_btnenum.EnableWindow(true);
	}
	
}

void CExportcertDlg::OnBtnEnum() 
{
	// TODO: Add your control notification handler code here
	
	CK_OBJECT_CLASS dataClass = CKO_CERTIFICATE;
	BOOL IsToken=true;
	CK_ATTRIBUTE pTempl[] = 
	{
		{CKA_CLASS, &dataClass, sizeof(CKO_CERTIFICATE)},
		{CKA_TOKEN, &IsToken, sizeof(true)}
	};

	
	C_FindObjectsInit(m_hSession, pTempl, 2);


	m_CertList.DeleteAllItems();
	int index=0;
	CK_OBJECT_HANDLE hCKObj;
	CK_ULONG ulRetCount = 0;
	CK_RV ckrv = 0;
	do
	{
		ckrv = C_FindObjects(m_hSession, &hCKObj, 1, &ulRetCount);
		if(CKR_OK != ckrv)
		{
			break;
		}
		if(1 != ulRetCount)
			break;
		
		CK_ATTRIBUTE pAttrTemp[] = 
		{
			{CKA_CLASS, NULL, 0},
			{CKA_CERTIFICATE_TYPE,NULL,0},
			{CKA_LABEL, NULL, 0},
			{CKA_SUBJECT,NULL,0},
			{CKA_ID,NULL,0},
			{CKA_VALUE,NULL,0}
		};
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 6);
		if(ckrv != CKR_OK)
		{
			break;
		}
		
		pAttrTemp[0].pValue = new char[pAttrTemp[0].ulValueLen];
		pAttrTemp[1].pValue = new char[pAttrTemp[1].ulValueLen];
		pAttrTemp[2].pValue = new char[pAttrTemp[2].ulValueLen+1];
		pAttrTemp[3].pValue = new char[pAttrTemp[3].ulValueLen+1];
		pAttrTemp[4].pValue = new char[pAttrTemp[4].ulValueLen+1];
		pAttrTemp[5].pValue = new char[pAttrTemp[5].ulValueLen ];
	
		ZeroMemory(pAttrTemp[0].pValue, pAttrTemp[0].ulValueLen);
		ZeroMemory(pAttrTemp[1].pValue, pAttrTemp[1].ulValueLen);
		ZeroMemory(pAttrTemp[2].pValue, pAttrTemp[2].ulValueLen+1);	
		ZeroMemory(pAttrTemp[3].pValue, pAttrTemp[3].ulValueLen+1);
		ZeroMemory(pAttrTemp[4].pValue, pAttrTemp[4].ulValueLen+1);
		ZeroMemory(pAttrTemp[5].pValue, pAttrTemp[5].ulValueLen);
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 6);
		if(ckrv != CKR_OK)
		{
			delete[] pAttrTemp[0].pValue;
			delete[] pAttrTemp[1].pValue;
			delete[] pAttrTemp[2].pValue;
			delete[] pAttrTemp[3].pValue;
			delete[] pAttrTemp[4].pValue;
			delete[] pAttrTemp[5].pValue;
			break;
		}
		
		CString strCertType;
		if(*(int*)pAttrTemp[1].pValue==CKC_X_509)
		{
			strCertType="CKC_X_509";
		}
		else 
			if(*(int*)pAttrTemp[1].pValue==CKC_X_509_ATTR_CERT)
			{
				strCertType="CKC_X_509_ATTR_CERT";
			}
			
		int itemIndex = m_CertList.InsertItem(LVIF_TEXT | LVIF_PARAM, index, strCertType, 0, 0, 0, 0);

		CString strvale = (char*)pAttrTemp[2].pValue;
		m_CertList.SetItem(itemIndex,1,LVIF_TEXT, strvale , 0, 0, 0, 0);	

		CString strsubject;
		CERT_NAME_BLOB  SubName;
		SubName.cbData=pAttrTemp[3].ulValueLen;
		SubName.pbData=(BYTE*)pAttrTemp[3].pValue;
		
		DWORD dwStrDetailType =
			CERT_NAME_STR_NO_QUOTING_FLAG | 
			CERT_NAME_STR_REVERSE_FLAG |
			CERT_SIMPLE_NAME_STR |
			CERT_OID_NAME_STR |
			CERT_NAME_STR_CRLF_FLAG
			;
		
		DWORD dwName = 0;
		dwName = CertNameToStr(
			MY_ENCODING_TYPE,
			&SubName,
			dwStrDetailType,
			NULL,
			0);
		
		if (0 == dwName)
		{
			AfxMessageBox("error get Subject Name length");
			return;
		}
		
		char* pszTemp = NULL;
		if(!(pszTemp = new char[dwName]))
		{
			AfxMessageBox("malloc memory  for subject name fail");
			return;
		}
		
		//--------------------------------------------------------------------
		//       Call the function again to get the string 
		dwName = CertNameToStr(
			MY_ENCODING_TYPE,
			&SubName,
			dwStrDetailType,
			pszTemp,
			dwName);
		
		//--------------------------------------------------------------------
		//      If the function succeeded, it returns the 
		//      number of bytes copied to the pszName buffer.
		if (1 < dwName)
		{
			strsubject = pszTemp;
		}
		delete[] pszTemp;

		m_CertList.SetItem(itemIndex,2,LVIF_TEXT, strsubject , 0, 0, 0, 0);
    
		CString strvalue=nByteToStr(pAttrTemp[5].ulValueLen, pAttrTemp[5].pValue, 1, 16);
		m_CertList.SetItem(itemIndex,3,LVIF_TEXT, strvalue , 0, 0, 0, 0);
		
		CertState=true;
		delete[] pAttrTemp[0].pValue;
		delete[] pAttrTemp[1].pValue;
		delete[] pAttrTemp[2].pValue;
		delete[] pAttrTemp[3].pValue;
		delete[] pAttrTemp[4].pValue;
		delete[] pAttrTemp[5].pValue;

		index++;
		
	}while(true);
	
	
}


void CExportcertDlg::OnRclickCert(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if(CertState)
	{
	
		CMenu PopMenu;
		CMenu *pMenu;
		
		CPoint pt;
		CRect rect;
		int tem=m_CertList.GetNextItem(-1,LVNI_SELECTED);
		m_CertList.GetItemPosition(tem,&pt);



		PopMenu.LoadMenu(IDR_MENU);
		ClientToScreen (&pt);

		pMenu = PopMenu.GetSubMenu (0);
		pMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON,
				pt.x+300, pt.y+50, this);
	}
	
	*pResult = 0;
}

void CExportcertDlg::OnExport() 
{
	// TODO: Add your command handler code here
	int nItem = m_CertList.GetNextItem(-1,LVNI_SELECTED);
	if(nItem==-1) 
	{
		AfxMessageBox("please select the certificate that will be export");
		return ;
	}

	CString   strlabel =m_CertList.GetItemText(nItem,1);
	
	CK_OBJECT_CLASS dataClass = CKO_CERTIFICATE;
	BOOL IsToken=true;
	CK_ATTRIBUTE pTempl[] = 
	{
		{CKA_CLASS, &dataClass, sizeof(CKO_CERTIFICATE)},
		{CKA_TOKEN, &IsToken, sizeof(true)}
	};

	
	C_FindObjectsInit(m_hSession, pTempl, 2);



	CK_OBJECT_HANDLE hCKObj;
	CK_ULONG ulRetCount = 0;
	CK_RV ckrv = 0;
	do
	{
		ckrv = C_FindObjects(m_hSession, &hCKObj, 1, &ulRetCount);
		if(CKR_OK != ckrv)
		{
			break;	
		}
		if(1 != ulRetCount)
			break;
		
		CK_ATTRIBUTE pAttrTemp[] = 
		{
			{CKA_CLASS, NULL, 0},
			{CKA_CERTIFICATE_TYPE,NULL,0},
			{CKA_LABEL, NULL, 0},
			{CKA_SUBJECT,NULL,0},
			{CKA_ID,NULL,0},
			{CKA_VALUE,NULL,0}
		};
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 6);
		if(ckrv != CKR_OK)
		{
			break;
		}
		
		pAttrTemp[0].pValue = new char[pAttrTemp[0].ulValueLen];
		pAttrTemp[1].pValue = new char[pAttrTemp[1].ulValueLen];
		pAttrTemp[2].pValue = new char[pAttrTemp[2].ulValueLen+1];
		pAttrTemp[3].pValue = new char[pAttrTemp[3].ulValueLen+1];
		pAttrTemp[4].pValue = new char[pAttrTemp[4].ulValueLen+1];
		pAttrTemp[5].pValue = new char[pAttrTemp[5].ulValueLen ];
	
		ZeroMemory(pAttrTemp[0].pValue, pAttrTemp[0].ulValueLen);
		ZeroMemory(pAttrTemp[1].pValue, pAttrTemp[1].ulValueLen);
		ZeroMemory(pAttrTemp[2].pValue, pAttrTemp[2].ulValueLen+1);	
		ZeroMemory(pAttrTemp[3].pValue, pAttrTemp[3].ulValueLen+1);
		ZeroMemory(pAttrTemp[4].pValue, pAttrTemp[4].ulValueLen+1);
		ZeroMemory(pAttrTemp[5].pValue, pAttrTemp[5].ulValueLen);
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 6);
		if(ckrv != CKR_OK)
		{
			delete[] pAttrTemp[0].pValue;
			delete[] pAttrTemp[1].pValue;
			delete[] pAttrTemp[2].pValue;
			delete[] pAttrTemp[3].pValue;
			delete[] pAttrTemp[4].pValue;
			delete[] pAttrTemp[5].pValue;
			break;
		}
		

		CString strvale = (char*)pAttrTemp[2].pValue;
		if(strlabel==strvale)
		{
			CFileDialog CertFileDialog(false,NULL,"feitian.cer");
			if(CertFileDialog.DoModal()==IDOK)
			{			
				CString CertFileName=CertFileDialog.GetPathName();
				CFile  certFile;
				certFile.Open(CertFileName,CFile::modeCreate|CFile::modeWrite);
				certFile.Write(pAttrTemp[5].pValue,pAttrTemp[5].ulValueLen);
				certFile.Close();
				MessageBox("The export successful.","Certificate Export Wizard");
			}
			return;
		}


		delete[] pAttrTemp[0].pValue;
		delete[] pAttrTemp[1].pValue;
		delete[] pAttrTemp[2].pValue;
		delete[] pAttrTemp[3].pValue;
		delete[] pAttrTemp[4].pValue;
		delete[] pAttrTemp[5].pValue;

		
		
	}while(true);
    AfxMessageBox("can't find the certificate");
	
}

⌨️ 快捷键说明

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