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

📄 minica.cpp

📁 MiniCA V2.0版本源码。《小型CA系统V2.1含源码》发表以来
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MiniCA.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "MiniCA.h"
#include "..\html\include\htmlhelp.h"
#include "Evp.h"
#include "MiniMainDlg.h"
#define _WIN32_WINNT  0x0400
#include "wincrypt.h"
#include "MiniMainDlg.h"
#include ".\GenericClass\GetHardSoftInfo.h"
#include ".\GenericClass\cgfiltyp.h"	//	CGCFileTypeAccess
#include <locale.h>
#include ".\Control\Splash\SplashWnd.h"
#include ".\GenericClass\Language.h"
#include "MiniCt.h"

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


BOOL CMiniCaApp::m_bIsBig = FALSE;
/////////////////////////////////////////////////////////////////////////////
// CMiniCaApp

BEGIN_MESSAGE_MAP(CMiniCaApp, CWinApp)
	//{{AFX_MSG_MAP(CMiniCaApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)//屏蔽此句可以去掉帮助按钮
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMiniCaApp construction

CMiniCaApp::CMiniCaApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
//	m_bServer = FALSE;
	m_bHpxs = FALSE;
	m_HelpArray.Add("ca_ext.htm");
	m_HelpArray.Add("ca_center.htm");
	m_HelpArray.Add("ca_sslclient.htm");
	m_HelpArray.Add("ca_sslserver.htm");
	m_HelpArray.Add("ca_evp.htm");
	m_HelpArray.Add("ca_raserver.htm");
	m_HelpArray.Add("ca_asn1.htm");
	m_HelpArray.Add("ca_ocspserver.htm");
	m_HelpArray.Add("ca_ocspclient.htm");
	m_HelpArray.Add("cacert_info.htm");
	m_HelpArray.Add("ca_reg.htm");
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMiniCaApp object

CMiniCaApp theApp;
HINSTANCE g_hInst = NULL;

/////////////////////////////////////////////////////////////////////////////
// CMiniCaApp initialization

int CMiniCaApp::HexToTen(const char * pHex)//16--->10
{
    DWORD dwHexNum=0;
    for (; *pHex!=0 ; pHex++)
    {
        dwHexNum *= 16;
        if ((*pHex>='0') && (*pHex<='9'))
            dwHexNum += *pHex-'0';
        else if ((*pHex>='a') && (*pHex<='f'))
            dwHexNum += *pHex-'a'+10;
        else if ((*pHex>='A') && (*pHex<='F'))
            dwHexNum += *pHex-'A'+10;
        else
            -1;
    }
	return dwHexNum;
}

BOOL CMiniCaApp::CheckSolfCode(CString pathExt/*被验证文件路径*/,
							   CString dllName/*dll名称*/,UINT dllId/*资源ID*/)//检验验证码
{
	static HINSTANCE hLibrary;
	if ((hLibrary = LoadLibrary(dllName)) == NULL)
	{
		return FALSE;
	}
	else
	{
		//得到摘要结果
		CString mdname("sha1");
		char outMsg[100]="";
		unsigned char md_value[MAX_MD_SIZE]="";
		char bufsing[MAX_MD_SIZE*2]="";
		unsigned int md_len;
		if(!CEvp::Digest(mdname.GetBuffer(0),pathExt.GetBuffer(0),0,md_value,&md_len, outMsg))
		{
			return FALSE;
		}

		/*得到公钥*/
		HRSRC hRsrc=FindResource(NULL,MAKEINTRESOURCE(IDR_VERIFY_CERT),"CERT");
		DWORD lenCert = SizeofResource(NULL, hRsrc); 
		HGLOBAL hgCert=LoadResource(NULL,hRsrc);
		LPSTR lpCert=(LPSTR)LockResource(hgCert);

		//取得DLL中的验证码
		char buf[513]={0};//长度512
		int nBufferMax=513;
		if(LoadString(hLibrary,dllId,buf,nBufferMax)==0)//取得错误IDS_STR_MINICA
		{
			LPVOID lpMsgBuf;
			FormatMessage( 
				FORMAT_MESSAGE_ALLOCATE_BUFFER | 
				FORMAT_MESSAGE_FROM_SYSTEM | 
				FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL,
				GetLastError(),
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				(LPTSTR) &lpMsgBuf,
				0,
				NULL 
				);
			TRACE0((LPTSTR)lpMsgBuf);
			LocalFree( lpMsgBuf );
			return FALSE;
		}

		char lm[257]={0};//保存乱码
		char temp[3]={0};//临时变量

		//regcode 保存16进制数据 类似B5C3 D6F8->对应2个乱码汉字
		//取出B5转换成10进制,存入乱码的一个位置
		for(UINT j=0;j<strlen(buf);j+=2)
		{
			strncpy(temp,buf+j,2);
			lm[j/2]=HexToTen(temp);
		}
		if(CEvp::VerifySign(lpCert,lenCert,NULL,mdname.GetBuffer(0),(char *)md_value,md_len,//内存区域
			lm/*签名结果,注册表->转换*/,outMsg))
		{
			return TRUE;
		}
		else
			return FALSE;
	}
}

BOOL CMiniCaApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.
	//	if(IsReg() == 1)
	//		m_bServer = FALSE;
	//	else if(IsReg() == 2)
	//	m_bServer = TRUE;//是否开发服务器程序

	
	// get full file path to program executable file
//	GetModuleFileName(NULL,sMiniCA.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
//	sMiniCA.ReleaseBuffer();
//	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	TCHAR	szProgPath[MAX_PATH * 2];
	::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));

	CString sMiniCA;
	sMiniCA.Format("%s", szProgPath);

	
	//得到可执行文件路径
	int nPos;
	nPos = sMiniCA.ReverseFind ('\\');
	m_AppPath = sMiniCA.Left (nPos);
	
	CharSetIsBig();

	CString strCmdLine;
	strCmdLine.Format("%s", m_lpCmdLine);

	if(strCmdLine.Find("I love hpxs") != -1)
	{
		CSplashWnd::EnableSplashScreen(0);	//splash 3 - 2
		m_bHpxs = TRUE;
		//		m_bServer = TRUE;
	}

	//得到命令行参数,判断是否WINEVP
	CString strEvpFile;
	strEvpFile.Format("%s", strCmdLine);
	//命令行是带""的,需要去掉""
	strEvpFile.Replace("\"", " ");
	strEvpFile.TrimLeft();
	strEvpFile.TrimRight();
	CString ext;
	ext = strEvpFile.Right(3);
	if(ext.CompareNoCase("Evp") == 0)
	{
		CSplashWnd::ShowSplashScreen(3000, "JPG", "IDR_SPLASHEVP", NULL);	//splash 3 - 1
	}
	else
	{
		CSplashWnd::ShowSplashScreen(3000, "JPG", "IDR_SPLASHCA", NULL);	//splash 3 - 1
	}
	
	
	if (!AfxOleInit())///初始化COM库,MFC 程序不用收回COM库      用于托放支持
	{
		AfxMessageBox(NormalCode("初始化ADO失败"));
		return FALSE;
	}
	

	::AfxInitRichEdit();

//	AfxEnableControlContainer(); //SimpleBrowser 控件专用


	//初始化OPENSSL
	SSL_load_error_strings();		// leak  ERR_free_strings()
	OpenSSL_add_all_algorithms();	//leak EVP_cleanup(void);



	//设置文件关联开始 - 注册表删除只能靠卸载程序
	CString csTempText;

	CGCFileTypeAccess TheFTA;
	TheFTA.SetExtension("evp");

	// just pass file path in quotes on command line
	csTempText  = szProgPath;
	csTempText += " \"%1\"";
	TheFTA.SetShellOpenCommand(csTempText);
	TheFTA.SetDocumentShellOpenCommand(csTempText);

	TheFTA.SetDocumentClassName("evpfile");

	// use first icon in program
	csTempText  = szProgPath;
	csTempText += ",0";
	TheFTA.SetDocumentDefaultIcon(csTempText);

	TheFTA.RegSetAllInfo();

	//设置关联结束
	
#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif
	
	m_ImgList.Create(16,16,TRUE|ILC_COLOR24,16,1);
	
	HICON hIcon =  NULL;
	
	hIcon =  (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_NONE), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//0
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_ALL), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//1
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_SOME), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//2
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON15), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//3
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON14), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//4
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON18), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//5
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON4), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//6
	DestroyIcon(hIcon);
	
	hIcon =  (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_MOD1), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//7
	DestroyIcon(hIcon);
	
	hIcon =  (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_MOD1), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//8
	DestroyIcon(hIcon);
	
	hIcon =  (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_MOD3), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//9
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_ASN1VIEW), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//10
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_BLUE), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//11
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_LIST), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//12
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_GREEN), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//13
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_RED), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//14
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_RESERT), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//15
	DestroyIcon(hIcon);
	
	hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDI_ICON_ERROR), IMAGE_ICON, 16, 16, 0);
	m_ImgList.Add(hIcon);//16
	DestroyIcon(hIcon);
	
	CMiniMainDlg dlg;
	//安装根证书
	LPSTR lpCert = NULL;
	DWORD lenCert=0;//公钥长度,私钥长度
	/*得到公钥*/
	HRSRC hRsrc=FindResource(NULL,MAKEINTRESOURCE(IDR_DERCERT_ROOT),"CERT");
	lenCert = SizeofResource(NULL, hRsrc); 
	HGLOBAL hgCert=LoadResource(NULL,hRsrc);
	lpCert = (LPSTR)LockResource(hgCert);

	AddRootToStore(lpCert, lenCert);
	m_pMainWnd = &dlg;
	//重启,存在前一个实例则不检测数据库
	//	if ((_stricmp(m_lpCmdLine,"reset")!=0))//重启
	{
		//检测是否已经打开,如果打不开,则重新写入
		CString lpszFile = 	m_AppPath + "\\MiniCA.mdb";
		CFile file;
		if(!file.Open(lpszFile, CFile::modeRead))
		{
			HRSRC hRsrc=FindResource(NULL,MAKEINTRESOURCE(IDR_INI_MDB),"INI");
			DWORD lenCert = SizeofResource(NULL, hRsrc); 
			HGLOBAL hgCert=LoadResource(NULL,hRsrc);
			LPSTR lpCert=(LPSTR)LockResource(hgCert);
			
			if(file.Open(lpszFile,CFile::modeCreate|CFile::modeWrite))	//存文件
			{
				file.Write(lpCert,lenCert);
				file.Close();
			}
		}
	}
	

	g_hInst = theApp.m_hInstance;

	int nResponse = dlg.DoModal();
	
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}
	
	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

BOOL CMiniCaApp::AddRootToStore(char * lpCert, UINT lenCert) //加入der格式根证书
{
	HCERTSTORE      hLinkStoreHandle;
	PCCERT_CONTEXT pCertContext = NULL;
	BOOL bAdd = FALSE;

⌨️ 快捷键说明

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