📄 enumobjdlg.cpp
字号:
CERT_SIMPLE_NAME_STR |
CERT_OID_NAME_STR |
CERT_NAME_STR_CRLF_FLAG
;
DWORD dwName = 0;
dwName = CertNameToStr(
MY_ENCODING_TYPE,
&SubName,
dwStrDetailType,
NULL,
0);
if (0 == dwName)
{
//AfxMessageBox("error get Subject Name length");
//return;
strRet += "[!! ERROR !!]"NEWLINE;
continue;
}
char* pszTemp = NULL;
if(!(pszTemp = new char[dwName]))
{
//AfxMessageBox("malloc memory for subject name fail");
//return;
strRet += "[!! ERROR !!]"NEWLINE;
continue;
}
//--------------------------------------------------------------------
// Call the function again to get the string
dwName = CertNameToStr(
MY_ENCODING_TYPE,
&SubName,
dwStrDetailType,
pszTemp,
dwName);
//--------------------------------------------------------------------
// If the function succeeded, it returns the
// number of bytes copied to the pszName buffer.
if (dwName > 1)
{
strRet += pszTemp;
strRet += NEWLINE;
}
else
strRet += "[!! ERROR !!]"NEWLINE;
delete[] pszTemp;
}
break;
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
// CEnumObjDlg dialog
CEnumObjDlg::CEnumObjDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEnumObjDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEnumObjDlg)
m_strInfo = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pSlotList = NULL_PTR;
m_pApplication = new char[255];
ZeroMemory(m_pApplication, 255);
lstrcpy((char*)m_pApplication, "Enum Object App");
m_hSession = NULL_PTR;
}
CEnumObjDlg::~CEnumObjDlg()
{
if(m_hSession)
{
C_CloseSession(m_hSession);
m_hSession = NULL_PTR;
}
delete[] m_pApplication;
if(m_pSlotList)
{
delete[] m_pSlotList;
m_pSlotList = NULL_PTR;
}
}
void CEnumObjDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEnumObjDlg)
DDX_Control(pDX, IDC_BTN_SECRET, m_btnSecret);
DDX_Control(pDX, IDC_BTN_PUBLIC, m_btnPublic);
DDX_Control(pDX, IDC_BTN_PRIVATE, m_btnPrivate);
DDX_Control(pDX, IDC_BTN_DATA, m_btnData);
DDX_Control(pDX, IDC_BTN_LOGIN, m_btnLogin);
DDX_Control(pDX, IDC_BTN_ENUM, m_btnEnum);
DDX_Control(pDX, IDC_BTN_CONNECT, m_btnConnect);
DDX_Control(pDX, IDC_INFO, m_edtInfo);
DDX_Text(pDX, IDC_INFO, m_strInfo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEnumObjDlg, CDialog)
//{{AFX_MSG_MAP(CEnumObjDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
ON_BN_CLICKED(IDC_BTN_CLEARINFO, OnBtnClearinfo)
ON_BN_CLICKED(IDC_BTN_ENUM, OnBtnEnum)
ON_BN_CLICKED(IDC_BTN_LOGIN, OnBtnLogin)
ON_BN_CLICKED(IDC_BTN_DATA, OnBtnData)
ON_BN_CLICKED(IDC_BTN_PUBLIC, OnBtnPublic)
ON_BN_CLICKED(IDC_BTN_PRIVATE, OnBtnPrivate)
ON_BN_CLICKED(IDC_BTN_SECRET, OnBtnSecret)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEnumObjDlg message handlers
BOOL CEnumObjDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
ShowMsg("First, you need connect to token!"NEWLINE);
m_btnLogin.EnableWindow(FALSE);
m_btnEnum.EnableWindow(FALSE);
m_btnData.EnableWindow(FALSE);
m_btnPublic.EnableWindow(FALSE);
m_btnPrivate.EnableWindow(FALSE);
m_btnSecret.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEnumObjDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEnumObjDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEnumObjDlg::OnBtnConnect()
{
// TODO: Add your control notification handler code here
if(m_hSession)
return;
::SetCursor(::LoadCursor(NULL, IDC_WAIT));
StartOP();
CK_RV rv;
CK_ULONG ulCount = 0;
rv = C_GetSlotList(TRUE, NULL_PTR, &ulCount);
if(CKR_OK != rv )
{
ShowErr(NEWLINE"Can't acquire the information of token, ErrorCode: 0x%08X."NEWLINE, rv);
return;
}
if(0 >= ulCount)
{
ShowMsg(NEWLINE"Can't connect to token, make sure one USB Token has been inserted."NEWLINE);
return;
}
m_pSlotList = (CK_SLOT_ID_PTR)new CK_SLOT_ID[ulCount];
if (! m_pSlotList)
{
ShowMsg(NEWLINE"Can't allocate enough memory!"NEWLINE);
return;
}
rv = C_GetSlotList(TRUE, m_pSlotList, &ulCount);
if(CKR_OK != rv )
{
ShowErr(NEWLINE"Can't acquire the information of token, ErrorCode: 0x%08X."NEWLINE, rv);
return;
}
if(0 >= ulCount)
{
ShowMsg(NEWLINE"Can't connect to token, make sure one USB Token has been inserted."NEWLINE);
return;
}
rv = C_OpenSession(
m_pSlotList[0], CKF_RW_SESSION | CKF_SERIAL_SESSION,
&m_pApplication, NULL_PTR, &m_hSession);
if(CKR_OK != rv )
{
ShowErr(NEWLINE"Can't Connect to token, Error Code 0x%08X."NEWLINE, rv);
delete[] m_pSlotList;
m_pSlotList = NULL_PTR;
}
else
{
ShowMsg(NEWLINE"Connect to token Successfully!"NEWLINE);
m_btnConnect.EnableWindow(FALSE);
ShowMsg(NEWLINE"Now You need to login with User PIN!"NEWLINE);
m_btnLogin.EnableWindow(TRUE);
}
}
void CEnumObjDlg::ShowMsg(CString strInfo)
{
m_strInfo += strInfo;
UpdateData(FALSE);
int nLastLine = m_edtInfo.GetLineCount();// GetFirstVisibleLine();
if (nLastLine > 0)
{
m_edtInfo.LineScroll(nLastLine, 0);
}
}
void CEnumObjDlg::StartOP()
{
ShowMsg(NEWLINE"================================================");
}
void CEnumObjDlg::ShowErr(CString strInfo, CK_RV rv)
{
CString strTemp("");
strTemp.Format(strInfo.GetBuffer(strInfo.GetLength()), rv);
ShowMsg(strTemp);
}
void CEnumObjDlg::OnBtnClearinfo()
{
// TODO: Add your control notification handler code here
m_strInfo = "";
UpdateData(FALSE);
}
void CEnumObjDlg::OnBtnEnum()
{
CK_OBJECT_CLASS dataClass = CKO_CERTIFICATE;
BOOL IsToken=true;
CK_ATTRIBUTE pTempl[] =
{
{CKA_CLASS, &dataClass, sizeof(CKO_CERTIFICATE)},
{CKA_TOKEN, &IsToken, sizeof(true)}
};
C_FindObjectsInit(m_hSession, pTempl, 2);
CK_OBJECT_HANDLE hCKObj;
CK_ULONG ulRetCount = 0;
CK_RV ckrv = 0;
int numObj=0;//object numbers
do
{
ckrv = C_FindObjects(m_hSession, &hCKObj, 1, &ulRetCount);
if(CKR_OK != ckrv)
{
break;
}
if(1 != ulRetCount)
break;
CK_ATTRIBUTE pAttrTemp[] =
{
{CKA_CLASS, NULL, 0},
{CKA_CERTIFICATE_TYPE,NULL,0},
{CKA_LABEL, NULL, 0},
{CKA_SUBJECT,NULL,0},
{CKA_ID,NULL,0},
{CKA_VALUE,NULL,0}
};
ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 6);
if(ckrv != CKR_OK)
{
break;
}
pAttrTemp[0].pValue = new char[pAttrTemp[0].ulValueLen];
pAttrTemp[1].pValue = new char[pAttrTemp[1].ulValueLen];
pAttrTemp[2].pValue = new char[pAttrTemp[2].ulValueLen+1];
pAttrTemp[3].pValue = new char[pAttrTemp[3].ulValueLen+1];
pAttrTemp[4].pValue = new char[pAttrTemp[4].ulValueLen+1];
pAttrTemp[5].pValue = new char[pAttrTemp[5].ulValueLen ];
ZeroMemory(pAttrTemp[0].pValue, pAttrTemp[0].ulValueLen);
ZeroMemory(pAttrTemp[1].pValue, pAttrTemp[1].ulValueLen);
ZeroMemory(pAttrTemp[2].pValue, pAttrTemp[2].ulValueLen+1);
ZeroMemory(pAttrTemp[3].pValue, pAttrTemp[3].ulValueLen+1);
ZeroMemory(pAttrTemp[4].pValue, pAttrTemp[4].ulValueLen+1);
ZeroMemory(pAttrTemp[5].pValue, pAttrTemp[5].ulValueLen);
ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 6);
if(ckrv != CKR_OK)
{
delete[] pAttrTemp[0].pValue;
delete[] pAttrTemp[1].pValue;
delete[] pAttrTemp[2].pValue;
delete[] pAttrTemp[3].pValue;
delete[] pAttrTemp[4].pValue;
delete[] pAttrTemp[5].pValue;
break;
}
numObj++;
//CString strvale = (char*)pAttrTemp[2].pValue;
/*
CString strsubject;
CERT_NAME_BLOB SubName;
SubName.cbData=pAttrTemp[3].ulValueLen;
SubName.pbData=(BYTE*)pAttrTemp[3].pValue;
DWORD dwStrDetailType =
CERT_NAME_STR_NO_QUOTING_FLAG |
CERT_NAME_STR_REVERSE_FLAG |
CERT_SIMPLE_NAME_STR |
CERT_OID_NAME_STR |
CERT_NAME_STR_CRLF_FLAG
;
DWORD dwName = 0;
dwName = CertNameToStr(
MY_ENCODING_TYPE,
&SubName,
dwStrDetailType,
NULL,
0);
if (0 == dwName)
{
AfxMessageBox("error get Subject Name length");
return;
}
char* pszTemp = NULL;
if(!(pszTemp = new char[dwName]))
{
AfxMessageBox("malloc memory for subject name fail");
return;
}
//--------------------------------------------------------------------
// Call the function again to get the string
dwName = CertNameToStr(
MY_ENCODING_TYPE,
&SubName,
dwStrDetailType,
pszTemp,
dwName);
//--------------------------------------------------------------------
// If the function succeeded, it returns the
// number of bytes copied to the pszName buffer.
if (1 < dwName)
{
strsubject = pszTemp;
}
delete[] pszTemp;
*/
// CString strckaid=(char*)pAttrTemp[4].pValue;
ShowMsg(NEWLINE);
StartOP();
CString strShow;
parse_object(pAttrTemp, 6, strShow);
ShowMsg(strShow);
/*
ShowMsg(NEWLINE"Begin this Object's Output:"NEWLINE);
ShowMsg("The Attribute CKA_CLASS of this Obj is:: CKO_CERTIFICATE"NEWLINE);
if(*(int*)pAttrTemp[1].pValue==CKC_X_509)
{
ShowMsg("The Attribute CKA_CERTIFICATE_TYPE is: CKC_X_509"NEWLINE);
}
else
if(*(int*)pAttrTemp[1].pValue==CKC_X_509_ATTR_CERT)
{
ShowMsg("CKA_CERTIFICATE_TYPE is CKC_X_509_ATTR_CERT"NEWLINE);
}
ShowMsg("The Attribute CKA_LABEL of this Obj is: ");
ShowMsg(strvale);
ShowMsg(NEWLINE"The Attribute CKA_SUBJECT of this Obj is: ");
ShowMsg(strsubject);
ShowMsg(NEWLINE"The Attribute CKA_ID of this Obj is: ");
ShowMsg(strckaid);
ShowMsg(NEWLINE"The Content of this Obj(CKA_VALUE) is:"NEWLINE);
ShowMsg(nByteToStr(pAttrTemp[5].ulValueLen, pAttrTemp[5].pValue, 1, 16));
ShowMsg(NEWLINE"Finish Output Obj"NEWLINE);
*/
delete[] pAttrTemp[0].pValue;
delete[] pAttrTemp[1].pValue;
delete[] pAttrTemp[2].pValue;
delete[] pAttrTemp[3].pValue;
delete[] pAttrTemp[4].pValue;
delete[] pAttrTemp[5].pValue;
}while(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -