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

📄 mainfrm.cpp

📁 可以实现一个简单的CA发放证书更新证书等等功能。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "CisoCA.h"

#include "MainFrm.h"
#include "LeftView.h"
#include "CisoCAView.h"
#include "sconfdlg.h"
#include "CAInitSheet.h"
#include "UserMakeCertSheet.h"
#include "CisoCADoc.h"
#include "getpwd.h"
#include "UserInfo.h"
#include "RevokeCertList.h"
#include "Splash.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern  CInfoReport InfoReport;
extern  DWORD dwMoudleID;

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_CA_INIT, OnCaInit)
	ON_COMMAND(ID_CA_MAKECERT, OnCaMakecert)
	ON_COMMAND(ID_CA_MAKECRL, OnCaMakecrl)
	ON_COMMAND(ID_CA_HELP, OnCaHelp)
	ON_COMMAND(ID_SYSTEM_OPTION, OnSystemOption)
	ON_COMMAND(ID_SYSTRAY_SHOW, OnSystrayShow)
	ON_COMMAND(ID_SYSTRAY_EXIT, OnSystrayExit)
	ON_WM_CLOSE()
	ON_COMMAND(ID_SYSTRAY_ABOUT, OnSystrayAbout)
	ON_WM_SHOWWINDOW()
	ON_WM_INITMENU()
	ON_WM_MEASUREITEM()
	ON_WM_MENUCHAR()
	ON_WM_INITMENUPOPUP()
	ON_WM_DRAWITEM()
	ON_COMMAND(ID_HELP_SITE, OnHelpSite)
	//}}AFX_MSG_MAP
	ON_UPDATE_COMMAND_UI_RANGE(AFX_ID_VIEW_MINIMUM, AFX_ID_VIEW_MAXIMUM, OnUpdateViewStyles)
	ON_COMMAND_RANGE(AFX_ID_VIEW_MINIMUM, AFX_ID_VIEW_MAXIMUM, OnViewStyle)
	ON_MESSAGE(WM_USER_TRAY_NOTIFICATION,OnTrayNotification)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame(): m_trayIcon(IDR_SYSTRAY)
{
	// TODO: add member initialization code here
	m_bShutdown = FALSE;
	m_bAutoMenuEnable=FALSE;
	hinstance = NULL;
	HtmlHelp  = NULL;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	m_trayIcon.SetNotificationWnd(this, WM_USER_TRAY_NOTIFICATION);
	m_trayIcon.SetIcon(IDI_PASSWORD,"企业级CA证书管理系统");

	//////////////////////////////////////////////////
    //  以XP的风格显示菜单							//
    //  菜单的显示风格在 BCMenu::DrawItem_xp中修改	//
	//////////////////////////////////////////////////


	BCMenu::SetMenuDrawMode(BCMENU_DRAWMODE_XP);
	
	//使窗口居中

	CenterWindow();	
	
	// 闪屏
	CSplashWnd::ShowSplashScreen(this);
	// 加载帮助动态库

	hinstance=::LoadLibrary("hhctrl.ocx");
	if(hinstance != NULL)
	(FARPROC&)HtmlHelp=::GetProcAddress(hinstance,"HtmlHelpA");

	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;
	
	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(100, 100), pContext) ||
		!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CCisoCAView), CSize(100, 100), pContext))
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}
	m_wndSplitter.SetColumnInfo(0,180,100);
	m_wndSplitter.RecalcLayout();
	return TRUE;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

CCisoCAView* CMainFrame::GetRightPane()
{
	CWnd* pWnd = m_wndSplitter.GetPane(0, 1);
	CCisoCAView* pView = DYNAMIC_DOWNCAST(CCisoCAView, pWnd);
	return pView;
}

void CMainFrame::OnUpdateViewStyles(CCmdUI* pCmdUI)
{
	// TODO: customize or extend this code to handle choices on the
	// View menu.

	CCisoCAView* pView = GetRightPane(); 

	// if the right-hand pane hasn't been created or isn't a view,
	// disable commands in our range

	if (pView == NULL)
		pCmdUI->Enable(FALSE);
	else
	{
		DWORD dwStyle = pView->GetStyle() & LVS_TYPEMASK;

		// if the command is ID_VIEW_LINEUP, only enable command
		// when we're in LVS_ICON or LVS_SMALLICON mode

		if (pCmdUI->m_nID == ID_VIEW_LINEUP)
		{
			if (dwStyle == LVS_ICON || dwStyle == LVS_SMALLICON)
				pCmdUI->Enable();
			else
				pCmdUI->Enable(FALSE);
		}
		else
		{
			// otherwise, use dots to reflect the style of the view
			pCmdUI->Enable();
			BOOL bChecked = FALSE;

			switch (pCmdUI->m_nID)
			{
			case ID_VIEW_DETAILS:
				bChecked = (dwStyle == LVS_REPORT);
				break;

			/*case ID_VIEW_SMALLICON:
				bChecked = (dwStyle == LVS_SMALLICON);
				break;

			case ID_VIEW_LARGEICON:
				bChecked = (dwStyle == LVS_ICON);
				break;

			case ID_VIEW_LIST:
				bChecked = (dwStyle == LVS_LIST);
				break;

			default:
				bChecked = FALSE;
				break;
			*/
			}

			pCmdUI->SetRadio(bChecked ? 1 : 0);
		}
	}
}


void CMainFrame::OnViewStyle(UINT nCommandID)
{
	// TODO: customize or extend this code to handle choices on the
	// View menu.
	CCisoCAView* pView = GetRightPane();

	// if the right-hand pane has been created and is a CCisoCAView,
	// process the menu commands...
	if (pView != NULL)
	{
		DWORD dwStyle = -1;

		switch (nCommandID)
		{
		case ID_VIEW_LINEUP:
			{
				// ask the list control to snap to grid
				CListCtrl& refListCtrl = pView->GetListCtrl();
				refListCtrl.Arrange(LVA_SNAPTOGRID);
			}
			break;

		// other commands change the style on the list control
		case ID_VIEW_DETAILS:
			dwStyle = LVS_REPORT;
			break;
/*
		case ID_VIEW_SMALLICON:
			dwStyle = LVS_SMALLICON;
			break;

		case ID_VIEW_LARGEICON:
			dwStyle = LVS_ICON;
			break;

		case ID_VIEW_LIST:
			dwStyle = LVS_LIST;
			break;
*/
		}

		// change the style; window will repaint automatically
		if (dwStyle != -1)
			pView->ModifyStyle(LVS_TYPEMASK, dwStyle);
	}
}

void CMainFrame::OnCaInit() 
{
	InfoReport.WriteInfo(dwMoudleID,"点击CA初始化按钮!");
	CCAInitSheet sheet("CA初始化向导");
	InfoReport.WriteInfo(dwMoudleID,"启动CA初始化向导!");
	
	// 取文档指针

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

	// 2004/03/25 张荣华 如果没有读入CA证书与私钥,则读入
	//zzy 2005 12 06
	if(pDoc->isInit == TRUE)
	{
		int ret;
		ret = ::MessageBox(NULL,"CA已经初始化,要再次初始化它吗?",
			"系统警告",MB_OKCANCEL|MB_ICONSTOP);
		if(ret != IDOK)return;
	}
	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 ;
		}
	} //20051206 zzy

	// 进行初始化操作

	int days = 365 ;
	int len  = 1024;
	
	if(sheet.DoModal() == ID_WIZFINISH)
	{
		InfoReport.WriteInfo(dwMoudleID,"设置CA的基本信息!");
		CUserInfo ui;
		// 国家

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

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

		// 城市

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

		if(!sheet.m_pBaseInfo->m_CAOrg.IsEmpty())
		{
			pDoc->m_CaCore.SetOrgName(sheet.m_pBaseInfo->m_CAOrg);
			ui.m_strOrg = sheet.m_pBaseInfo->m_CAOrg;
		}

		// 部门

		if(!sheet.m_pBaseInfo->m_CAOrg.IsEmpty())
		{
			pDoc->m_CaCore.SetDeptName(sheet.m_pBaseInfo->m_CADept);
			ui.m_strDept = sheet.m_pBaseInfo->m_CADept;
		}

		// 名称

		pDoc->m_CaCore.SetCommonName(sheet.m_pBaseInfo->m_CAName);
		ui.m_strName = sheet.m_pBaseInfo->m_CAName;

		// 有效期

		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,"为CA生成证书请求!");
		int ret = pDoc->m_CaCore.GenerateREQ(TRUE);
		if(ret == CA_FAIL)goto err;	
		InfoReport.WriteInfo(dwMoudleID,"CA生成自签名证书!");
		ret = pDoc->m_CaCore.MakeV3Certificate(TRUE,0L,days);
		if(ret == CA_FAIL)goto err;	
		
		// 写入下一个证书的序列号

		CPropertyFile *pConf= new CPropertyFile(CA_SN_DIR);
		pConf->SetValue("sn","1");
		pConf->SaveProperty(CA_SN_DIR);
		if(pConf)delete pConf;
		ui.m_strSN.Format("%ld",0L);
		// 密码

		ui.m_strPwd = sheet.m_pExtInfo->m_CAPwd;

		// 证书与私钥

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

		ret = pDoc->m_CaCore.PrivateKeyToPEM(TRUE,ui.m_strPwd,
			tmpBUF,10240);
		if(ret != CA_OK) goto err;
		ui.m_strPvk.Format("%s",tmpBUF);

		// 保存在PEM格式

		ret = pDoc->m_CaCore.SavePrivateKey(CA_PRIVATE_KEY,
			sheet.m_pExtInfo->m_CAPwd,PEM,TRUE);
		if(ret == CA_FAIL)goto err;	
		ret = pDoc->m_CaCore.SaveCertificate(CA_CERTIFICATE,PEM,TRUE);
		if(ret == CA_FAIL)goto err;
		// 公司名称
		pDoc->m_pConF->SetValue("CompanyName",
			(char*)(LPCTSTR)sheet.m_pBaseInfo->m_CAName);
		// 部门
		pDoc->m_pConF->SetValue("Dept",
			(char*)(LPCTSTR)sheet.m_pBaseInfo->m_CADept);
		// 组织
		pDoc->m_pConF->SetValue("Org",
			(char*)(LPCTSTR)sheet.m_pBaseInfo->m_CAOrg);
		// 城市
		pDoc->m_pConF->SetValue("City",
			(char*)(LPCTSTR)sheet.m_pBaseInfo->m_CACity);
		// 省份
		pDoc->m_pConF->SetValue("Province",
			(char*)(LPCTSTR)sheet.m_pBaseInfo->m_CAProv);
		// 国家
		pDoc->m_pConF->SetValue("Country",
			(char*)(LPCTSTR)sheet.m_pBaseInfo->m_CACountry);

		// 设置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());

		// 设置初始化标志

		pDoc->m_pConF->SetValue("Initailized","1");
		pDoc->isInit = TRUE;
		// 保存设置
		pDoc->m_pConF->SaveProperty(CONFIG_FILE);
		pDoc->ClearAllData();
		InfoReport.WriteInfo(dwMoudleID,"将CA信息写入数据库!");
		if(pDoc->InsertDB(&ui) == 0)
		{

⌨️ 快捷键说明

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