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

📄 certinfopage.cpp

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

#include "stdafx.h"
#include "minica.h"
#include "CertInfoPage.h"
#include "MiniMainDlg.h"
#include <locale.h>
#include "Evp.h"
#include ".\GenericClass\Language.h"
#include "minict.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCertInfoPage property page
const UINT uIniTextLen = 20;
IMPLEMENT_DYNCREATE(CCertInfoPage, CPropertyPage)

CCertInfoPage::CCertInfoPage() : CPropertyPage(CCertInfoPage::IDD)
{
	//{{AFX_DATA_INIT(CCertInfoPage)
	//}}AFX_DATA_INIT
}

CCertInfoPage::~CCertInfoPage()
{
}

void CCertInfoPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCertInfoPage)
//	DDX_Control(pDX, IDC_EDIT_PATH, m_XpEdit);
	DDX_Control(pDX, IDC_CERTINFO_READ, m_Bread);
	DDX_Control(pDX, IDC_EDIT_INFO, m_RichEdit);
	DDX_Control(pDX, IDC_LIST_MOD, m_List);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCertInfoPage, CPropertyPage)
	//{{AFX_MSG_MAP(CCertInfoPage)
	ON_BN_CLICKED(IDC_CERTINFO_READ, OnBRead)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_MOD, OnItemchangedListMod)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCertInfoPage message handlers

BOOL CCertInfoPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();

	m_Bread.SetThemeHelper(((CMiniCaApp *)AfxGetApp())->GetThemeHelperST());
	m_Bread.SetIcon(IDI_ICON_SELECTDIR);
	m_Bread.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	// TODO: Add extra initialization here
	m_List.InsertColumn(0,MiniCT_1500,LVCFMT_LEFT,160); //字符域
	m_List.InsertColumn(1,MiniCT_1501,LVCFMT_LEFT,300); //"值"
	m_List.SetExtendedStyle(LVS_EX_UNDERLINECOLD|LVS_EX_FULLROWSELECT);
	
	CImageList * pImgList = ((CMiniCaApp *)AfxGetApp())->GetImgList();
	m_List.SetImageList(pImgList,LVSIL_SMALL);//用来改变LISTCTRL行宽度

/*	CHARFORMAT cfDefault;
	memset(&cfDefault,0,sizeof(cfDefault));
	cfDefault.cbSize=sizeof(cfDefault);
	cfDefault.dwMask=CFM_SIZE|CFM_SPACING;
	cfDefault.yHeight=200;
	m_RichEdit.SetDefaultCharFormat(cfDefault);//设置模式
*/

	// CG: The following block was added by the ToolTips component.	{		// Create the ToolTip control.		m_toolTip.Create(this);
	//	m_tooltip.AddTool(GetDlgItem(IDC_EDIT_PATH), CMiniCaApp::NormalCode("被解析的证书"));
		m_toolTip.AddTool(GetDlgItem(IDC_CERTINFO_READ), CMiniCaApp::NormalCode("选择要解析证书文件"));
		// TODO: Use one of the following forms to add controls:	}


	if(!((CMiniCaApp *)AfxGetApp())->IsXpStyle())
	{
		ClassXP(GetDlgItem(IDC_EDIT_PATH)->m_hWnd,TRUE);
	}

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

void CCertInfoPage::OnBRead() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, MiniCT_0721,NULL);

	CString strTitle = MiniCT_0719;
	dlgOpen.m_ofn.lpstrTitle = strTitle;//标题条

	if(dlgOpen.DoModal()!=IDOK) return;
	SetDlgItemText(IDC_EDIT_PATH,dlgOpen.GetPathName());
	char buf[256]={0};
	CString file = dlgOpen.GetPathName();
	if(!ViewCertInfo(file.GetBuffer(0), 0, NULL, buf))
		AddMsg(MiniCT_1502,	M_ERROR);
}

void CCertInfoPage::OnItemchangedListMod(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if (pNMListView->uChanged == LVIF_STATE)
	{
		if (pNMListView->uNewState)//选择改变
		{
			CString str=m_List.GetItemText(pNMListView->iItem,1);//得到内容
			if(strcmp(m_List.GetItemText(pNMListView->iItem,0), MiniCT_1510)==0)
			{
				CString strL,strR(m_strPubKey),strtemp;
				str.Empty();
				for(;;)
				{
					if(strlen(strR)==0)
						break;
					strL=strR.Left(4);
					strR=strR.Right(strR.GetLength()-4);
					strR.TrimLeft();
					strtemp.Format("%s ",strL);
					str+=strtemp;
				}
			}
			else
			{
				str.TrimRight("\n");
				for(int nStart=0;;)
				{
					int state=str.Find("\n",nStart);
					if(state==-1)
						break;
					str.Insert(state,"\r");
					nStart=state+2;
				}
			}
			SetDlgItemText(IDC_EDIT_INFO,str);
		}
	}

	*pResult = 0;
}

BOOL CCertInfoPage::ViewCertInfo(char * cert, UINT certLen,void *x509, char *buf)
{
	m_List.SetRedraw(FALSE);
	m_List.DeleteAllItems();
	SetDlgItemText(IDC_EDIT_INFO,"");

	if(certLen == 0 && x509 == 0)//表示拖动操作
	{
		SetDlgItemText(IDC_EDIT_PATH, cert);
	}

	stuCERTINFO info;
	if(!GetCertInfo(cert,certLen,x509,info,buf))
	{
		m_List.SetRedraw(TRUE);
		return FALSE;
	}

	CString str;
	int index=0;

	m_List.InsertItem(index, MiniCT_1503, 7);	//版本
	str.Format("V%d",info.VER);
	m_List.SetItemText(index++,1,str);
	if(strlen(info.SN))//序号
	{
		m_List.InsertItem(index, MiniCT_1504, 7);//序列号
		str.Format("%04s",info.SN);
		m_List.SetItemText(index++,1,str);
	}
	if(strlen(info.SIGNATURE))//签名算法
	{
		m_List.InsertItem(index, MiniCT_1505, 7); //签名算法
		str.Format("%s",info.SIGNATURE);
		m_List.SetItemText(index++,1,str);
	}
	if(strlen(info.ISSUER))//颁发者
	{
		m_List.InsertItem(index, MiniCT_1506, 7); //颁发者
		str.Format("%s",info.ISSUER);
		m_List.SetItemText(index++,1,str);

	}
	if(strlen(info.NOTBEFORE))//起始日期
	{
		m_List.InsertItem(index, MiniCT_1507, 7);
		str.Format("%s",info.NOTBEFORE);
		m_List.SetItemText(index++,1,CMiniCaApp::NormalCode(str));
	}
	if(strlen(info.NOTAFTER))//中止日期
	{
		m_List.InsertItem(index, MiniCT_1508, 7);
		str.Format("%s",info.NOTAFTER);
		m_List.SetItemText(index++,1,CMiniCaApp::NormalCode(str));
	}
	if(strlen(info.SUBJECT))//主题
	{
		m_List.InsertItem(index, MiniCT_1509, 7);
		str.Format("%s",info.SUBJECT);
		m_List.SetItemText(index++,1,str);
	}
	if(strlen(info.PUBKEY))//公钥
	{
		m_List.InsertItem(index, MiniCT_1510, 7);
		str.Format("%s (%d Bits)",info.PUBTYPE,info.PUBLEN);
		m_List.SetItemText(index++,1,str);
		m_strPubKey.Format("%s",info.PUBKEY);//保存公钥
	}
	if(strlen(info.V3EXT))//扩展信息
	{
		str.Format("%s",info.V3EXT);
		CString strtemp,strL,strR(str),strl,strr;
		for(;;)
		{
			int state=strR.Find("\r\n");
			if(state==-1)
				break;
			strL=strR.Left(state);
			strR=strR.Right(strR.GetLength()-state-1);
			strR.TrimLeft();
			strtemp.Format("%s",strL);
			int j=strtemp.Find(":");
			strl=strtemp.Left(j);
			strr=strtemp.Right(strtemp.GetLength()-j-1);
			strr.TrimLeft();
			m_List.InsertItem(index,CMiniCaApp::NormalCode(strl),8);
			m_List.SetItemText(index++,1,CMiniCaApp::NormalCode(strr));

		}
	}
	if(strlen(info.THUMB))//缩微图算法
	{
		m_List.InsertItem(index, MiniCT_1511, 9);
		str.Format("%s",info.THUMB);
		m_List.SetItemText(index++,1,str);
	}
	if(strlen(info.THUMBPRINT))//缩微图
	{
		m_List.InsertItem(index, MiniCT_1512, 9);
		str.Format("%s",info.THUMBPRINT);
		CString strL,strR(str),strtemp;
		str.Empty();
		for(;;)
		{
			if(strlen(strR)==0)
				break;
			strL=strR.Left(4);
			strR=strR.Right(strR.GetLength()-4);
			strR.TrimLeft();
			strtemp.Format("%s ",strL);
			str+=strtemp;
		}

		m_List.SetItemText(index++,1,str);
	}

	m_List.SetRedraw();
	return TRUE;
}

void CCertInfoPage::AddMsg(CString info, DWORD type)
{
	((CMiniMainDlg *)GetParent())->AddMsg(MiniCT_0004,info, type); //"数证解析"
}

/*void CCertInfoPage::OnDropFiles(HDROP hDropInfo) 
{
	//取得被拖动文件的数目
	UINT uiFiles = ::DragQueryFile(hDropInfo, (UINT)-1, NULL, 0);
    for (UINT uiFile = 0; uiFile < uiFiles; uiFile++)
    {
		//取得第uiFile个拖动文件名所占字节数
        UINT uiLen = ::DragQueryFile(hDropInfo, uiFile, 0, 0);
        TCHAR *pszFileName = new TCHAR[uiLen + 1];
        ::DragQueryFile(hDropInfo, uiFile, pszFileName, uiLen + 1);
		SetDlgItemText(IDC_EDIT_PATH,pszFileName);
		char buf[256]={0};
		ViewCertInfo(pszFileName, NULL, buf);
        delete[] pszFileName;
	}
	::DragFinish(hDropInfo);
}
*/

BOOL CCertInfoPage::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.	{		// Let the ToolTip process this message.		m_toolTip.RelayEvent(pMsg);	}	return CPropertyPage::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}

void CCertInfoPage::Utf8ToAnsi(const UCHAR * lpsrc,const int srclen, LPSTR lpdst, int& dstlen)
{
	WCHAR * Unicode;
    int len = MultiByteToWideChar ( CP_UTF8 , 0 ,(char*) lpsrc ,-1 ,NULL,0);
    Unicode = new WCHAR[len * sizeof(WCHAR)];
    MultiByteToWideChar ( CP_UTF8 , 0 ,( char * ) lpsrc, -1, Unicode , len );
    len = WideCharToMultiByte(CP_ACP,0,Unicode,-1,NULL,0,NULL,NULL);
    dstlen = WideCharToMultiByte (CP_ACP,0,Unicode,-1,lpdst,len,NULL,NULL);
    delete []Unicode;
}

BOOL CCertInfoPage::GetCertName(void *name,char * outName)
{
	if(name == NULL)		//X509_NAME_get_text_by_NID
		return FALSE;
	int num = X509_NAME_entry_count((X509_NAME *)name);
	X509_NAME_ENTRY * entry;
	ASN1_OBJECT * obj;
	ASN1_STRING * str;
	char objtmp[80] = {0};
	char  pmbbuf[3] = {0};
	int fn_nid = 0;
	const char * objbuf;


	setlocale(LC_CTYPE, ""); //NULL,””,”C”代表操作系统的缺省代码页 ; ”.ACP”,代表操作系统当前使用的代
	
	for(int i = 0 ;i < num;i ++)
	{
		entry = (X509_NAME_ENTRY *)X509_NAME_get_entry((X509_NAME *)name,i);
		obj = X509_NAME_ENTRY_get_object(entry);
		str = X509_NAME_ENTRY_get_data(entry);//得到 ASN1_STRING
		//判断str是否为UTF-8
		int len = 0;
		unsigned char * tmp = NULL; 
		if (str && ASN1_STRING_type(str) == V_ASN1_UTF8STRING)
		{
			len = ASN1_STRING_length(str);
			if (len >= 0)
			{
				tmp = (UCHAR *)OPENSSL_malloc(len+1);
				if (tmp)
				{
					memcpy(tmp, ASN1_STRING_data(str), len);
					tmp[len] = '\0';
				}
			}
		}
	    else /* not a UTF8 name */		///////////转UTF-8
		{
			len = ASN1_STRING_to_UTF8(&tmp, str);
		}
		char buf[1024] = {0};
		int dstlen = 0;
		Utf8ToAnsi(tmp, len, buf, dstlen);
		OPENSSL_free(tmp);
		fn_nid = OBJ_obj2nid(obj);
		if(fn_nid == NID_undef)
			OBJ_obj2txt(objtmp, sizeof objtmp, obj, 1);
		else
		{
			objbuf = OBJ_nid2sn(fn_nid);
			strcpy(objtmp,objbuf);
			//objbuf = OBJ_nid2ln(fn_nid);
		}

		BIO *mem = BIO_new(BIO_s_mem());
		BIO_set_close(mem, BIO_CLOSE); 

⌨️ 快捷键说明

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