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

📄 cacertinfopage.cpp

📁 MiniCA V2.0版本源码。《小型CA系统V2.1含源码》发表以来
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CaCertInfoPage.cpp : implementation file
//

#include "stdafx.h"
#include "minica.h"
#include "CaCertInfoPage.h"
#include "MiniMainDlg.h"
#include ".\GenericClass\Language.h"
#include "minict.h"

#define _WIN32_WINNT  0x0400
#include "WinCrypt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCaCertInfoPage property page

IMPLEMENT_DYNCREATE(CCaCertInfoPage, CPropertyPage)

CCaCertInfoPage::CCaCertInfoPage() : CPropertyPage(CCaCertInfoPage::IDD)
{
	//{{AFX_DATA_INIT(CCaCertInfoPage)
	m_RadioDisk = 0;
	m_Day = 0;
	m_Pwd = _T("");
	//}}AFX_DATA_INIT
	m_IniPathName = ((CMiniCaApp *)AfxGetApp())->GetAppPath() +  "\\MiniCA.ini";
	m_CertFormat = DER;
}

CCaCertInfoPage::~CCaCertInfoPage()
{
	//清除链表内容
	m_ArrayInside.RemoveAll();
}

void CCaCertInfoPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCaCertInfoPage)
//	DDX_Control(pDX, IDC_EDIT_CONT, m_XpEditCont);
	DDX_Control(pDX, IDC_LIST, m_CheckList);
//	DDX_Control(pDX, IDC_EDIT_PWD, m_XpEditReq);
//	DDX_Control(pDX, IDC_EDIT_PATH, m_XpEdit);
//	DDX_Control(pDX, IDC_EDIT_DAY, m_XpEditDay);
	DDX_Control(pDX, IDC_COMBO_CSP, m_CspBox);
	DDX_Radio(pDX, IDC_RADIO_DISK, m_RadioDisk);
	DDX_Text(pDX, IDC_EDIT_DAY, m_Day);
	DDV_MinMaxUInt(pDX, m_Day, 1, 3650);
	DDX_Text(pDX, IDC_EDIT_PWD, m_Pwd);
	DDV_MaxChars(pDX, m_Pwd, 8);
	//}}AFX_DATA_MAP
    DDX_FileEditCtrl(pDX, IDC_EDIT_PATH, m_XpEdit, FEC_FOLDER);
}


BEGIN_MESSAGE_MAP(CCaCertInfoPage, CPropertyPage)
	//{{AFX_MSG_MAP(CCaCertInfoPage)
	ON_BN_CLICKED(IDC_R_CSP, OnRCsp)
	ON_BN_CLICKED(IDC_RADIO_DISK, OnRadioDisk)
	ON_BN_CLICKED(IDC_CAINFO_RADIODER, OnCainfoRadioder)
	ON_BN_CLICKED(IDC_CAINFO_RADIOPEM, OnCainfoRadiopem)
	//}}AFX_MSG_MAP
	ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCaCertInfoPage message handlers
BOOL CCaCertInfoPage::OnToolTipNotify( UINT id, NMHDR * pTTTStruct, LRESULT * pResult )
{
	TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;    
    UINT nID = pTTTStruct->idFrom;
	BOOL bRadioDisk = ((CButton * )GetDlgItem(IDC_RADIO_DISK))->GetCheck();
	BOOL bRadioCsp = ((CButton * )GetDlgItem(IDC_R_CSP))->GetCheck();
	BOOL bPkcs12 = ((CButton * )GetDlgItem(IDC_STATIC_CAINFO21))->GetCheck();
	BOOL bDer = ((CButton * )GetDlgItem(IDC_CAINFO_RADIODER))->GetCheck();
	CString strPwd(""),strText;
	GetDlgItemText(IDC_EDIT_PWD, strPwd);
	switch(nID)
	{
	case IDC_EDIT_PATH:
		if(bRadioDisk)
		{
			strText = CMiniCaApp::NormalCode("证书存储路径");
		}
		else
		{
			strText = "";
		}
		_tcscpy(pTTT->szText, strText);//设置
		return TRUE;
	case IDC_COMBO_CSP:
		if(bRadioCsp)
		{
			strText = CMiniCaApp::NormalCode("CSP名称");
		}
		else
		{
			strText = "";
		}
		_tcscpy(pTTT->szText,strText);//设置
		return TRUE;
	case IDC_EDIT_CONT:
		if(bRadioCsp)
		{
			strText = CMiniCaApp::NormalCode("密钥容器名称");
		}
		else
		{
			strText = "";
		}
		_tcscpy(pTTT->szText,strText);//设置
		return TRUE;
	case IDC_CAINFO_RADIODER:
	case IDC_CAINFO_RADIOPEM:
		if(bDer)
		{
			strText = "证书格式\r当前DER格式";
		}
		else
		{
			strText = "证书格式\r当前PEM格式";
		}
		_tcscpy(pTTT->szText,strText);//设置
		return TRUE;
	case IDC_STATIC_CAINFO21:	//PKCS12
		if(bPkcs12)
		{
			strText = "是否生成PFX包\r当前生成";
		}
		else
		{
			strText = "是否生成PFX包\r当前不生成";
		}
		_tcscpy(pTTT->szText,strText);//设置
		return TRUE;
	case IDC_EDIT_PWD:
		if(!strPwd.IsEmpty())
		{
			_tcscpy(pTTT->szText, "私钥和PFX包密码\r密码有效");//设置
		}
		else
		{
			_tcscpy(pTTT->szText, "私钥和PFX包密码\r密码无效");//设置
		}
		return TRUE;
	}
	return FALSE;
}

BOOL CCaCertInfoPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here

	//默认选中PFX
	((CButton * )GetDlgItem(IDC_STATIC_CAINFO21))->SetCheck(1);

	((CButton * )GetDlgItem(IDC_CAINFO_RADIODER))->SetCheck(1);

	if(!((CMiniCaApp *)AfxGetApp())->IsXpStyle())
	{
		ClassXP(GetDlgItem(IDC_CAINFO_RADIODER)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_CAINFO_RADIOPEM)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_STATIC_CAINFO21)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_COMBO_L)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_RADIO_DISK)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_R_CSP)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_COMBO_CSP)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_EDIT_CONT)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_EDIT_PWD)->m_hWnd,TRUE);
		ClassXP(GetDlgItem(IDC_EDIT_DAY)->m_hWnd,TRUE);
	}

    HANDLE handle = ::LoadImage(AfxGetInstanceHandle(),
                                MAKEINTRESOURCE(IDI_ICON_DIR),
                                IMAGE_ICON,
                                0,
                                0,
                                LR_DEFAULTCOLOR);
    m_XpEdit.SetButtonImage(handle, PJAI_ICON | PJAI_AUTODELETE | PJAI_STRETCHED );
	m_XpEdit.SetButtonWidth(18);

	//CMiniCaApp::NormalCode(buf);

	CImageList * pImgList = ((CMiniCaApp *)AfxGetApp())->GetImgList();
	m_CheckList.SetImageList(pImgList,LVSIL_SMALL);//用来改变LISTCTRL行宽度

	ListView_SetExtendedListViewStyle(m_CheckList.m_hWnd, LVS_EX_CHECKBOXES | LVS_EX_SUBITEMIMAGES |
		LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	CRect rect;
	m_CheckList.GetClientRect(rect);
	int width = rect.Width();

	m_CheckList.InsertColumn(0, MiniCT_0300, LVCFMT_LEFT, width/3);	//MiniCT_0300 "字段名称"
	m_CheckList.InsertColumn(1, MiniCT_0301, LVCFMT_LEFT, width*2/3 - 15);	//MiniCT_0301 "内容"

	GetIniInfo(m_IniPathName);

	char buf[256] = {0};

	GetPrivateProfileString("CA", "RSALEN","2048",buf,50, m_IniPathName);
	//查找加载项是否有效512 768 1024 1536 2048
	int index = ((CComboBox *)GetDlgItem(IDC_COMBO_L))->FindString(-1,buf);
	if(index == -1)
		index = 2;
	((CComboBox *)GetDlgItem(IDC_COMBO_L))->SetCurSel(index);


	GetPrivateProfileString("CA", "DAY","365",buf,50, m_IniPathName);
	SetDlgItemText(IDC_EDIT_DAY,buf);

	GetPrivateProfileString("CA", "SN","1",buf,50, m_IniPathName);
	SetDlgItemText(IDC_EDIT_SN,buf);

	GetPrivateProfileString("CA", "KEYPWD","MiniCA",buf,50, m_IniPathName);
	SetDlgItemText(IDC_EDIT_PWD,buf);

//	m_PathName = ((CMiniCaApp *)AfxGetApp())->GetAppPath() +  "\\Cert\\";
//	SetDlgItemText(IDC_EDIT_PATH,m_PathName);

//	GetPrivateProfileString("PATH", "Cert","",m_PathName.GetBuffer(256),255, m_IniPathName);
//	m_PathName.ReleaseBuffer();//在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要操作的这个缓冲的 CString 对象的任何方法

/*	CMiniMainDlg * pMain = (CMiniMainDlg *)AfxGetMainWnd();
	CCertDbPage * pSetup = (CCertDbPage *)(pMain->GetPage("CCertDbPage"));

	if(pSetup) 
	{
		CString m_PathName = pSetup->GetCertPath(2);
		SetDlgItemText(IDC_EDIT_PATH,m_PathName);
	}*/

	
	//BE98E302-2CAA-ED1A-93CF-A77559FEC4AC
	SetDlgItemText(IDC_EDIT_CONT,"BE98E302-2CAA-ED1A-93CF-A77559FEC4AC");
	// CG: The following block was added by the ToolTips component.	{		// Create the ToolTip control.		m_toolTip.Create(this);
		m_toolTip.AddTool(GetDlgItem(IDC_RADIO_DISK), CMiniCaApp::NormalCode("磁盘证书"));
		m_toolTip.AddTool(GetDlgItem(IDC_EDIT_DAY), CMiniCaApp::NormalCode("证书有效期限\r从申请时刻计起"));
		m_toolTip.AddTool(GetDlgItem(IDC_COMBO_L), CMiniCaApp::NormalCode("密钥长度\r生成高强度密钥可能要很长时间"));
		m_toolTip.AddTool(GetDlgItem(IDC_R_CSP), CMiniCaApp::NormalCode("CSP证书"));
		m_toolTip.AddTool(GetDlgItem(IDC_LIST), "证书基本信息列表\r详细设置请转到配置选项卡");
		m_toolTip.AddTool(GetDlgItem(IDC_EDIT_PATH), LPSTR_TEXTCALLBACK);
		m_toolTip.AddTool(GetDlgItem(IDC_COMBO_CSP), LPSTR_TEXTCALLBACK);
		m_toolTip.AddTool(GetDlgItem(IDC_EDIT_CONT), LPSTR_TEXTCALLBACK);

		m_toolTip.AddTool(GetDlgItem(IDC_CAINFO_RADIODER), LPSTR_TEXTCALLBACK);
		m_toolTip.AddTool(GetDlgItem(IDC_CAINFO_RADIOPEM), LPSTR_TEXTCALLBACK);
		m_toolTip.AddTool(GetDlgItem(IDC_STATIC_CAINFO21), LPSTR_TEXTCALLBACK);
		m_toolTip.AddTool(GetDlgItem(IDC_EDIT_PWD), LPSTR_TEXTCALLBACK);
	}


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCaCertInfoPage::OnRadioDisk() //磁盘文件
{
	// TODO: Add your control notification handler code here
	m_CspBox.EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_PATH)->EnableWindow();
//	GetDlgItem(IDC_STATIC1)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_CONT)->EnableWindow(FALSE);	
}

void CCaCertInfoPage::OnRCsp() //枚举CSP
{
	// TODO: Add your control notification handler code here
	m_CspBox.EnableWindow();
	GetDlgItem(IDC_EDIT_PATH)->EnableWindow(FALSE);
//	GetDlgItem(IDC_STATIC1)->EnableWindow();
	GetDlgItem(IDC_EDIT_CONT)->EnableWindow();
	CString csp;
	GetDlgItemText(IDC_COMBO_CSP,csp);
	if(csp.IsEmpty())
	{
		m_CspBox.ResetContent();
		EnumCsp();
		CString str = GetDefaultCsp();
		int select = m_CspBox.FindString(-1,str);
		if(select!=-1)
			m_CspBox.SetCurSel(select);
	}
}

BOOL CCaCertInfoPage::EnumCsp()
{
	DWORD       cbName;
    DWORD       dwType;
    DWORD       dwIndex;
    CHAR        *pszName = NULL; 
	
    // Loop through enumerating providers.
    dwIndex = 0;
    while(CryptEnumProviders(
		dwIndex,
		NULL,
		0,
		&dwType,
		NULL,
		&cbName
		))
    {
		
        //--------------------------------------------------------------------
        //  cbName returns the length of the name of the next provider.
        //  Allocate memory in a buffer to retrieve that name.
		
        if (!(pszName = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, cbName)))
        {
			return FALSE;
        }
        //--------------------------------------------------------------------
        //  Get the provider name.
        if (CryptEnumProviders(
			dwIndex++,
			NULL,
			0,
			&dwType,
			pszName,
			&cbName
			))
        {
			if(PROV_RSA_FULL == dwType)
				m_CspBox.AddString(pszName);
   //         printf ("     %4.0d\t%s\n",dwType, pszName);
        }
        LocalFree(pszName);
		
    } // End of while loop
	return FALSE;
}


CString CCaCertInfoPage::GetDefaultCsp()
{
	DWORD       cbProvName=0;
    LPTSTR      pbProvName=NULL;
	CString str;
    // Get the length of the RSA_FULL default provider name.
    if (!(CryptGetDefaultProvider(
		PROV_RSA_FULL, 
		NULL, 
		CRYPT_MACHINE_DEFAULT,
		NULL, 
		&cbProvName))) 
    { 
		return "";
    }
	
    // Allocate local memory for the name of the default provider.
    if (!(pbProvName = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, cbProvName)))
    {
		return "";
    }

⌨️ 快捷键说明

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