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

📄 mainfrm.cpp

📁 可以实现一个简单的CA发放证书更新证书等等功能。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			::MessageBox(NULL,"CA初始化失败!",
				"系统提示",MB_OK|MB_ICONINFORMATION);
			return;
		}
		// 更新树形的根结点名称
		CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
		CLeftView* pView = DYNAMIC_DOWNCAST(CLeftView, pWnd);
		if(pView != NULL)
		{
			pView->bShow = TRUE;
			CTreeCtrl *ptree=&(pView->GetTreeCtrl());
			HTREEITEM tm = ptree->GetRootItem();
			CString strName=sheet.m_pBaseInfo->m_CAName;
			ptree->SetItemText(tm,(LPCTSTR)strName);
		}
		pDoc->UpdateAllViews(0);
		::MessageBox(NULL,"CA初始化成功!","系统提示",MB_OK|MB_ICONINFORMATION);
		InfoReport.WriteInfo(dwMoudleID,"CA初始化成功!");
		return ;
	}
	return;
	err:
	char strErrStr[256] = {0};
	pDoc->m_CaCore.GetErrString(strErrStr);
	CString strErr;
	strErr.Format("%s - %s","CA初始化失败",strErrStr);
	::MessageBox(NULL,strErr,"系统提示",MB_OK|MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR);
	InfoReport.WriteInfo(dwMoudleID,"CA初始化失败!");
	return;
}

void CMainFrame::OnCaMakecert() 
{
	// 取文档指针
	InfoReport.WriteInfo(dwMoudleID,"点击制作X509证书工具/菜单按钮!");
	CCisoCADoc *pDoc = (CCisoCADoc*)GetActiveDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->isInit == FALSE)
	{
		AfxMessageBox("CA未初始化!");
		InfoReport.WriteInfo(dwMoudleID,"系统提示 - CA未初始化!");
		return ;
	}

	// 如果没有读入CA证书与私钥,则读入

	if(!pDoc->m_CaCore.HasValidCA())
	{
		CGetPwd gpDlg;
		gpDlg.strTip="请输入CA私钥的密码:";
		if(gpDlg.DoModal() != IDOK ) return;
		int ret= -1;
		ret = pDoc->m_CaCore.GetCertificate(CA_CERTIFICATE,PEM,TRUE);
		if(ret != CA_OK)
		{
			char buf[256]={0};
			pDoc->m_CaCore.GetErrString(buf);
			AfxMessageBox(buf);
			return ;
		}
		ret = pDoc->m_CaCore.GetPrivateKey(CA_PRIVATE_KEY,gpDlg.m_pwd,PEM,TRUE);
		if(ret != CA_OK)
		{
			char buf[256]={0};
			pDoc->m_CaCore.GetErrString(buf);
			AfxMessageBox(buf);
			return ;
		}
	}

	// 启动向导
	InfoReport.WriteInfo(dwMoudleID,"启动制作X509证书向导");
	CUserMakeCertSheet sheet("制作X509证书向导");
	int days = 365;
	if(sheet.DoModal() == ID_WIZFINISH)
	{
		CUserInfo ui;
		// 客户证书的生成
		// 国家

		pDoc->m_CaCore.SetCountryName(sheet.m_pBaseInfo->m_country);
		ui.m_strCountry = sheet.m_pBaseInfo->m_country;
		// 省份

		if(!sheet.m_pBaseInfo->m_prov.IsEmpty())
		{
			pDoc->m_CaCore.SetProvinceName(sheet.m_pBaseInfo->m_prov);
			ui.m_strProvince = sheet.m_pBaseInfo->m_prov;
		}

		// 城市

		pDoc->m_CaCore.SetCityName(sheet.m_pBaseInfo->m_city);
		ui.m_strCity = sheet.m_pBaseInfo->m_city;
		// 组织

		pDoc->m_CaCore.SetOrgName(sheet.m_pBaseInfo->m_org);
		ui.m_strOrg = sheet.m_pBaseInfo->m_org;
		// 部门

		pDoc->m_CaCore.SetDeptName(sheet.m_pBaseInfo->m_dept);
		ui.m_strDept = sheet.m_pBaseInfo->m_dept;
		// 名称

		pDoc->m_CaCore.SetCommonName(sheet.m_pBaseInfo->m_name);
		ui.m_strName = sheet.m_pBaseInfo->m_name;
		// 有效期
		
		pDoc->m_CaCore.SetCertValidate(sheet.m_pExtInfo->m_valid);
		days = atoi((char*)(LPCTSTR)sheet.m_pExtInfo->m_valid);

		// 密钥的长度

		pDoc->m_CaCore.SetKeyLength(sheet.m_pExtInfo->m_len);
			InfoReport.WriteInfo(dwMoudleID,"为客户生成证书REQ!");
		int ret = pDoc->m_CaCore.GenerateREQ(FALSE);
		if(ret == CA_FAIL)goto err;	

		// 取证书的序列号

		char snBUF[MAX_CONF_LINE]={0};
		long lngSN=1L;
		CPropertyFile *pConf= new CPropertyFile(CA_SN_DIR);
		// 取出序列号
		pConf->GetValue("sn",snBUF);
		if(strlen(snBUF)>0)	lngSN = atol(snBUF);
		// 生成证书
		char szMsg[100]={0};
		sprintf(szMsg,"生成证书:序列号=%ld,名字=%s",lngSN,ui.m_strName);
		InfoReport.WriteInfo(dwMoudleID,szMsg);
		ret = pDoc->m_CaCore.MakeV3Certificate(FALSE,lngSN,days);
		ui.m_strSN.Format("%ld",lngSN);
		// 序列号加1
		sprintf(snBUF,"%ld",lngSN+1);
		pConf->SetValue("sn",snBUF);
		// 写入文件
		pConf->SaveProperty(CA_SN_DIR);
		// 释放内存
		if(pConf)delete pConf;
		// 如果出错,跳到出错处理的语句
		if(ret == CA_FAIL)goto err;	

		// 密码

		ui.m_strPwd = sheet.m_pExtInfo->m_pwd;

		// 证书与私钥

		char tmpBUF[1024*10]={0}; // 10K
		ret = pDoc->m_CaCore.CertificateToPEM(FALSE,tmpBUF,10240);
		if(ret != CA_OK) goto err;
		ui.m_strCert.Format("%s",tmpBUF);

		ret = pDoc->m_CaCore.PrivateKeyToPEM(FALSE,ui.m_strPwd,
			tmpBUF,10240);
		if(ret != CA_OK) goto err;
		ui.m_strPvk.Format("%s",tmpBUF);
		
		// 打包成P12格式

		if(sheet.m_pExtInfo->certFormat == 0)
		{
			InfoReport.WriteInfo(dwMoudleID,"将证书打包成P12格式!");
			CString strFriendName = sheet.m_pExtInfo->m_friendName;
			if(strFriendName.IsEmpty())strFriendName ="";
			CString strFileName = sheet.m_pFileName->m_certFileName;
			strFileName = ".\\UserCert\\"+strFileName + ".p12";
			ret = pDoc->m_CaCore.ToPKCS12(sheet.m_pExtInfo->m_pwd,
				strFileName,
				strFriendName);
			if(ret == CA_FAIL)goto err;	
		}
		// PEM格式的证书与私钥
		else
		{
			InfoReport.WriteInfo(dwMoudleID,"将证书保存成PEM格式!");
			CString strCertFileName = sheet.m_pFileName->m_certFileName;
			CString strPvkFileName  = sheet.m_pFileName->m_pvkFileName;
			strCertFileName = ".\\UserCert\\" + strCertFileName + ".crt";
			strPvkFileName  = ".\\UserCert\\" + strPvkFileName  + ".pem";
			
			ret = pDoc->m_CaCore.SavePrivateKey(strPvkFileName,
			sheet.m_pExtInfo->m_pwd,PEM,FALSE);
			if(ret == CA_FAIL)goto err;	
			ret = pDoc->m_CaCore.SaveCertificate(strCertFileName,
				PEM,FALSE);
			if(ret == CA_FAIL)goto err;	
		}
		// 设置notBefore 与 notAfter时间

		CTime t = CTime::GetCurrentTime();
		ui.m_notBefore.Format("%04d-%02d-%02d %02d:%02d:%02d",
		t.GetYear(),t.GetMonth(),t.GetDay(),     // date 
		t.GetHour(),t.GetMinute(),t.GetSecond());// time

		CTimeSpan ts(days,0,0,0);

		CTime t2 = t + ts;
		ui.m_notAfter.Format("%04d-%02d-%02d %02d:%02d:%02d",
		t2.GetYear(),t2.GetMonth(),t2.GetDay(),
		t2.GetHour(),t2.GetMinute(),t2.GetSecond());

		// 
		// ===========写入数据库===========
		//
		InfoReport.WriteInfo(dwMoudleID,"将用户信息写入数据库!");
		if(pDoc->InsertDB(&ui) == 0)
		{
			::MessageBox(NULL,"制作X509证书失败!",
				"系统提示",MB_OK|MB_ICONINFORMATION);
			InfoReport.WriteInfo(dwMoudleID,"制作X509证书失败!");
			return;
		}
		pDoc->UpdateAllViews(0);
		InfoReport.WriteInfo(dwMoudleID,"制作X509证书成功!");
		::MessageBox(NULL,"制作X509证书成功!","系统提示",MB_OK|MB_ICONINFORMATION);
	}
	return ;
	err:
	CString strErr;
	char strErrStr[256] = {0};
	pDoc->m_CaCore.GetErrString(strErrStr);
	InfoReport.WriteInfo(dwMoudleID,"制作X509证书失败!");
	strErr.Format("%s - %s","制作X509证书失败",strErrStr);
	::MessageBox(NULL,strErr,"系统提示",MB_OK|MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR);
	return;

}

void CMainFrame::OnCaMakecrl() 
{
	InfoReport.WriteInfo(dwMoudleID,"点击制作CRL按钮!");
	// 取文档指针
	int ret;
	CCisoCADoc *pDoc = (CCisoCADoc*)GetActiveDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->isInit == FALSE)
	{
		AfxMessageBox("CA未初始化!");
		InfoReport.WriteInfo(dwMoudleID,"系统提示 - CA未初始化!");
		return ;
	}

	// 如果没有读入CA证书与私钥,则读入

	if(!pDoc->m_CaCore.HasValidCA())
	{
		CGetPwd gpDlg;
		gpDlg.strTip="请输入CA私钥的密码:";
		if(gpDlg.DoModal() != IDOK ) return;
		int ret= -1;
		ret = pDoc->m_CaCore.GetCertificate(CA_CERTIFICATE,PEM,TRUE);
		if(ret != CA_OK)
		{
			char buf[256]={0};
			pDoc->m_CaCore.GetErrString(buf);
			AfxMessageBox(buf);
			return ;
		}
		ret = pDoc->m_CaCore.GetPrivateKey(CA_PRIVATE_KEY,gpDlg.m_pwd,PEM,TRUE);
		if(ret != CA_OK)
		{
			char buf[256]={0};
			pDoc->m_CaCore.GetErrString(buf);
			AfxMessageBox(buf);
			return ;
		}
	}
	ret = pDoc->SelectDB(WANT_TO_GENERATE_CRL,NULL);
	if(ret != 1)
	{
		CString strErr;
		strErr.Format("%s - %s","制作X509证书失败","无法查询证书状态");
		InfoReport.WriteInfo(dwMoudleID,"错误:无法查询证书状态!");
		::MessageBox(NULL,strErr,"系统提示",MB_OK|MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR);
		return;
	}
	CRevokeCertList rcList;
	rcList.AutoAssemble(pDoc->m_pSet,"USER_CERT_SN","CERT_REVOKE_TIME");
	ret = pDoc->m_CaCore.MakeCRL(".\\CACert\\cacrl.crl",&rcList,8,0);
	if(ret != CA_OK)goto err;		
	if(pDoc->m_pSet->State == adStateOpen)pDoc->m_pSet->Close();
	InfoReport.WriteInfo(dwMoudleID,"制作CRL成功!");
	::MessageBox(NULL,"制作证书吊销列表成功!","系统提示",MB_OK|MB_ICONINFORMATION);
	return;

	err:
	CString strErr;
	char strErrStr[256] = {0};
	pDoc->m_CaCore.GetErrString(strErrStr);
	if(pDoc->m_pSet->State == adStateOpen)pDoc->m_pSet->Close();
	InfoReport.WriteInfo(dwMoudleID,"制作CRL失败!");
	strErr.Format("%s - %s","制作CRL失败",strErrStr);
	::MessageBox(NULL,strErr,"系统提示",MB_OK|MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR);
	return;
}

void CMainFrame::OnCaHelp() 
{
	// TODO: Add your command handler code here
	InfoReport.WriteInfo(dwMoudleID,"启动帮助!");
	if(HtmlHelp != NULL)
	{
		//HtmlHelp(NULL,"CisoCA.chm",0,0);
		HINSTANCE hinstance;
		CString str="http://www.infosecurity.org.cn/forum/forum.html";
		//hinstance=ShellExecute(GetSafeHwnd(),_T("open"),str,NULL,NULL,SW_SHOWNORMAL);
		hinstance=ShellExecute(NULL,_T("open"),str,NULL,NULL,SW_SHOWNORMAL);

	}
	else
	{
		CString msg;
		msg = "缺少hhctrl.ocx文件!请确认系统中存在该文件!\n";
		msg += "如果没有,请与系统管理员联系!";
		::MessageBox(NULL,msg,"系统警告",MB_OK|MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR);
	}
}

void CMainFrame::OnSystemOption() 
{
	// TODO: Add your command handler code here

	InfoReport.WriteInfo(dwMoudleID,"对系统进行设置!");

	// 取文档指针

	CCisoCADoc *pDoc = (CCisoCADoc*)GetActiveDocument();
	ASSERT_VALID(pDoc);

	// 2004/03/25 张荣华 如果没有读入CA证书与私钥,则读入

	if(pDoc->isInit == TRUE && !pDoc->m_CaCore.HasValidCA())
	{
		CGetPwd gpDlg;
		gpDlg.strTip="请输入CA私钥的密码:";
		if(gpDlg.DoModal() != IDOK ) return;
		int ret= -1;
		ret = pDoc->m_CaCore.GetCertificate(CA_CERTIFICATE,PEM,TRUE);
		if(ret != CA_OK)
		{
			char buf[256]={0};
			pDoc->m_CaCore.GetErrString(buf);
			AfxMessageBox(buf);
			return ;
		}
		ret = pDoc->m_CaCore.GetPrivateKey(CA_PRIVATE_KEY,gpDlg.m_pwd,PEM,TRUE);
		if(ret != CA_OK)
		{
			char buf[256]={0};
			pDoc->m_CaCore.GetErrString(buf);
			AfxMessageBox(buf);
			return ;
		}
	}

	CSConfDlg dlg;
	dlg.DoModal();
}

LRESULT CMainFrame::OnTrayNotification(WPARAM wparam, LPARAM lparam)
{   
	return m_trayIcon.OnTrayNotification(wparam, lparam);
}

void CMainFrame::OnSystrayShow() 
{
	// TODO: Add your command handler code here
	ShowWindow(SW_NORMAL);	
	SetForegroundWindow();
}

void CMainFrame::OnSystrayExit() 
{
	// TODO: Add your command handler code here
	m_bShutdown = TRUE;		// really exit
	SendMessage(WM_CLOSE);	
}

void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if (m_bShutdown)
	{
		CFrameWnd::OnClose();
		//释放
		if(hinstance != NULL)::FreeLibrary(hinstance);
	}
	else
		ShowWindow(SW_HIDE);
}

void CMainFrame::OnSystrayAbout() 
{
	// TODO: Add your command handler code here
	((CCisoCAApp*)AfxGetApp())->OnAppAbout();
}

void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CFrameWnd::OnShowWindow(bShow, nStatus);
	// TODO: Add your message handler code here
}

HMENU CMainFrame::NewMenu()
{
	// Load the menu from the resources
	m_menu.LoadMenu(IDR_MAINFRAME); 
	// replace IDR_MAINFRAME with your menu ID
	//m_menu.LoadToolbar(IDR_MAINFRAME);
	return(m_menu.Detach());
}
//This handler ensure that the popup menu items are drawn correctly
void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	BOOL setflag=FALSE;
	if(lpMeasureItemStruct->CtlType==ODT_MENU)
	{
		if(IsMenu((HMENU)lpMeasureItemStruct->itemID))
		{
			CMenu* cmenu=CMenu::FromHandle((HMENU)lpMeasureItemStruct->itemID);
			if(BCMenu::IsMenu(cmenu))
			{
				m_menu.MeasureItem(lpMeasureItemStruct);
				setflag=TRUE;
			}
		}
	}
	if(!setflag)CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

//This handler ensures that keyboard shortcuts work
LRESULT CMainFrame::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu) 
{
	LRESULT lresult;
	if(BCMenu::IsMenu(pMenu))
		lresult=BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
	else
		lresult=CFrameWnd::OnMenuChar(nChar, nFlags, pMenu);
	return(lresult);
}

//This handler updates the menus from time to time
void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
	CFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	if(!bSysMenu)
	{
		if(BCMenu::IsMenu(pPopupMenu))BCMenu::UpdateMenu(pPopupMenu);
	}
}

void CMainFrame::OnInitMenu(CMenu* pMenu) 
{
	CFrameWnd::OnInitMenu(pMenu);
	
	// TODO: Add your message handler code here	
}
void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

void CMainFrame::OnHelpSite() 
{
	// TODO: Add your command handler code here
			//HtmlHelp(NULL,"CisoCA.chm",0,0);
	HINSTANCE hinstance;
	CString str="http://www.infosecurity.org.cn";
	//hinstance=ShellExecute(GetSafeHwnd(),_T("open"),str,NULL,NULL,SW_SHOWNORMAL);
	hinstance=ShellExecute(NULL,_T("open"),str,NULL,NULL,SW_SHOWNORMAL);

}

⌨️ 快捷键说明

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