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

📄 enumobjdlg.cpp

📁 基于USBKEY和PKCS#11接口的数字证书枚举例子。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		ShowMsg(NEWLINE"Success login to Token!"NEWLINE);
		m_btnEnum.EnableWindow(TRUE);
		m_btnData.EnableWindow(TRUE);
		m_btnPublic.EnableWindow(TRUE);
		m_btnPrivate.EnableWindow(TRUE);
		m_btnSecret.EnableWindow(TRUE);
	
}

void CEnumObjDlg::OnBtnData() 
{
	// TODO: Add your control notification handler code here
	CK_OBJECT_CLASS dataClass = CKO_DATA;
	BOOL IsToken=true;
	CK_ATTRIBUTE pTempl[] = 
	{
		{CKA_CLASS, &dataClass, sizeof(CKO_DATA)},
		{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;
	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_LABEL, NULL, 0},
			{CKA_APPLICATION, NULL, 0},
			{CKA_VALUE, NULL,0},
		};
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 4);
		if(ckrv != CKR_OK)
		{
			break;
		}
		
		pAttrTemp[0].pValue = new char[pAttrTemp[0].ulValueLen];
		pAttrTemp[1].pValue = new char[pAttrTemp[1].ulValueLen + 1];
		pAttrTemp[2].pValue = new char[pAttrTemp[2].ulValueLen + 1];
		pAttrTemp[3].pValue = new char[pAttrTemp[3].ulValueLen];
		ZeroMemory(pAttrTemp[0].pValue, pAttrTemp[0].ulValueLen);
		ZeroMemory(pAttrTemp[1].pValue, pAttrTemp[1].ulValueLen + 1);
		ZeroMemory(pAttrTemp[2].pValue, pAttrTemp[2].ulValueLen + 1);
		ZeroMemory(pAttrTemp[3].pValue, pAttrTemp[3].ulValueLen );
		

		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 4);
		if(ckrv != CKR_OK)
		{
			delete[] pAttrTemp[0].pValue;
			delete[] pAttrTemp[1].pValue;
			delete[] pAttrTemp[2].pValue;
			delete[] pAttrTemp[3].pValue;
			break;
		}
		numObj++;		
		int nClassTemp = *((int*)pAttrTemp[0].pValue);
		CString strvale = (char*)pAttrTemp[1].pValue;
		CString strApp= (char*)pAttrTemp[2].pValue;

		ShowMsg(NEWLINE);
		StartOP();
		ShowMsg(NEWLINE"Begin this Object's Output:"NEWLINE);
		ShowMsg("The Attribute CKA_CLASS of this Obj is: CKO_DATA"NEWLINE);
		ShowMsg("The Attribute CKA_LABEL of this Obj is: ");
		ShowMsg(strvale);
		ShowMsg(NEWLINE"The Attribute CKA_APPLICATION of this Obj is:");
		ShowMsg(strApp);		
		ShowMsg(NEWLINE"The Content of this Obj(CKA_VALUE) is:"NEWLINE);
		ShowMsg(nByteToStr(pAttrTemp[3].ulValueLen, pAttrTemp[3].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;		
	}while(true);

	if(numObj==0)
	{
		StartOP();
		ShowMsg(NEWLINE"Can't find Data Obj"NEWLINE);
	}
	else
	{
		StartOP();
		CHAR strNumObj[4];
		wsprintf(strNumObj,"%d",numObj);
		ShowMsg(NEWLINE"Find ");
		ShowMsg(strNumObj);
		ShowMsg(" Data Obj"NEWLINE);
	}


}

void CEnumObjDlg::OnBtnPublic() 
{
	CK_OBJECT_CLASS dataClass = CKO_PUBLIC_KEY;
	BOOL IsToken=true;
	CK_ATTRIBUTE pTempl[] = 
	{
		{CKA_CLASS, &dataClass, sizeof(CKO_PUBLIC_KEY)},
		{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;
	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_KEY_TYPE,NULL,0},
			{CKA_LABEL, NULL, 0},
			{CKA_WRAP,NULL,0},
			{CKA_ENCRYPT,NULL,0},
			{CKA_MODULUS,NULL,0},
			{CKA_PUBLIC_EXPONENT,NULL,0},
		};
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 7);
		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];
		pAttrTemp[4].pValue = new char[pAttrTemp[4].ulValueLen];
		pAttrTemp[5].pValue = new char[pAttrTemp[5].ulValueLen];
		pAttrTemp[6].pValue = new char[pAttrTemp[6].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);
		ZeroMemory(pAttrTemp[4].pValue, pAttrTemp[4].ulValueLen);
		ZeroMemory(pAttrTemp[5].pValue, pAttrTemp[5].ulValueLen);
		ZeroMemory(pAttrTemp[6].pValue, pAttrTemp[6].ulValueLen);
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 7);
		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;
			delete[] pAttrTemp[6].pValue;
			break;
		}

		numObj++;
		CString strvale = (char*)pAttrTemp[2].pValue;

		
		ShowMsg(NEWLINE);
		StartOP();
		ShowMsg(NEWLINE"Begin this Object's Output:"NEWLINE);
		ShowMsg("The Attribute CKA_CLASS of this Obj is:: CKO_PUBLIC_KEY"NEWLINE);
		
		if(*(int*)pAttrTemp[1].pValue==CKK_RSA)
		{
		ShowMsg("The Attribute CKA_KEY_TYPE is: CKK_RSA"NEWLINE);
		}
		else 
			if(*(int*)pAttrTemp[1].pValue==CKK_DSA)
			{
				ShowMsg("The Attribute CKA_KEY_TYPE is CKK_DSA"NEWLINE);
			}
			else
				if(*(int*)pAttrTemp[1].pValue==CKK_DH)
				{
					ShowMsg("The Attribute CKA_KEY_TYPE is CKK_DH"NEWLINE);					
				}

		ShowMsg("The Attribute CKA_LABEL of this Obj is: ");
		ShowMsg(strvale);
		
		if((int)*(char*)pAttrTemp[3].pValue!=0)
		{
			ShowMsg(NEWLINE"The Attribute CKA_WRAP is: true");
		}
		else
		{
			ShowMsg(NEWLINE"The Attribute CKA_WRAP is: false");
		}

		
		if((int)*(char*)pAttrTemp[4].pValue!=0)
		{
			ShowMsg(NEWLINE"The Attribute CKA_ENCRYPT is: true");
		}
		else
		{
			ShowMsg(NEWLINE"The Attribute CKA_ENCRYPT is: false");
		}
		

		ShowMsg(NEWLINE"The Attribute CKA_MODULUS(public key) is:"NEWLINE);
		ShowMsg(nByteToStr(pAttrTemp[5].ulValueLen, pAttrTemp[5].pValue, 1, 16));
		ShowMsg(NEWLINE"The Attribute CKA_PUBLIC_EXPONENT is:"NEWLINE);
		ShowMsg(nByteToStr(pAttrTemp[6].ulValueLen, pAttrTemp[6].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;
		delete[] pAttrTemp[6].pValue;
		
		
	}while(true);
	if(numObj==0)
	{
		StartOP();
		ShowMsg(NEWLINE"Can't find public key Obj"NEWLINE);
	}
	else
	{
		StartOP();
		CHAR strNumObj[4];
		wsprintf(strNumObj,"%d",numObj);
		ShowMsg(NEWLINE"Find ");
		ShowMsg(strNumObj);
		ShowMsg(" public key"NEWLINE);
	}

}

void CEnumObjDlg::OnBtnPrivate() 
{
	// TODO: Add your control notification handler code here
	CK_OBJECT_CLASS dataClass = CKO_PRIVATE_KEY;
	BOOL IsToken=true;
	CK_ATTRIBUTE pTempl[] = 
	{
		{CKA_CLASS, &dataClass, sizeof(CKO_PRIVATE_KEY)},
		{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;
	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_KEY_TYPE,NULL,0},
			{CKA_LABEL, NULL, 0},
			{CKA_SUBJECT,NULL,0},
			{CKA_ID,NULL,0},
			{CKA_SENSITIVE,NULL,0},
			{CKA_DECRYPT,NULL,0},
			{CKA_SIGN,NULL,0},
			{CKA_MODULUS,NULL,0},
			{CKA_PUBLIC_EXPONENT,NULL,0},
		};
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 10);
		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];
		pAttrTemp[6].pValue = new char[pAttrTemp[6].ulValueLen];
		pAttrTemp[7].pValue = new char[pAttrTemp[7].ulValueLen];
		pAttrTemp[8].pValue = new char[pAttrTemp[8].ulValueLen];
		pAttrTemp[9].pValue = new char[pAttrTemp[9].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);
		ZeroMemory(pAttrTemp[6].pValue, pAttrTemp[6].ulValueLen);
		ZeroMemory(pAttrTemp[7].pValue, pAttrTemp[7].ulValueLen);
		ZeroMemory(pAttrTemp[8].pValue, pAttrTemp[8].ulValueLen);
		ZeroMemory(pAttrTemp[9].pValue, pAttrTemp[9].ulValueLen);
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 10);
		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;
			delete[] pAttrTemp[6].pValue;
			delete[] pAttrTemp[7].pValue;
			delete[] pAttrTemp[8].pValue;
			delete[] pAttrTemp[9].pValue;
			//			bRet = false;
			break;
		}
		numObj++;
		
		CString strvale = (char*)pAttrTemp[2].pValue;
		CString strsubject=(char*)pAttrTemp[3].pValue;
		CString strckaid=(char*)pAttrTemp[4].pValue;
		
		ShowMsg(NEWLINE);
		StartOP();
		ShowMsg(NEWLINE"Begin this Object's Output:"NEWLINE);
		ShowMsg("The Attribute CKA_CLASS of this Obj is:: CKO_PRIVATE_KEY"NEWLINE);
		
		if(*(int*)pAttrTemp[1].pValue==CKK_RSA)
		{
			ShowMsg("The Attribute CKA_KEY_TYPE is : CKK_RSA"NEWLINE);
		}
		else 
			if(*(int*)pAttrTemp[1].pValue==CKK_DSA)
			{
				ShowMsg("The Attribute CKA_KEY_TYPE is CKK_DSA"NEWLINE);
			}
			else
				if(*(int*)pAttrTemp[1].pValue==CKK_DH)
				{
					ShowMsg("The Attribute CKA_KEY_TYPE is CKK_DH"NEWLINE);					
				}
				
		ShowMsg("The Attribute CKA_LABEL of this Obj is: ");
		ShowMsg(strvale);
		ShowMsg(NEWLINE"The Attribute CKA_ID of this Obj is: ");
		ShowMsg(strckaid);
			
		if((int)*(char*)pAttrTemp[5].pValue!=0)
		{
			ShowMsg(NEWLINE"The Attribute CKA_SENSITIVE is: true");
		}
		else
		{
			ShowMsg(NEWLINE"The Attribute CKA_SENSITIVE is: false");
		}
			
			
		if((int)*(char*)pAttrTemp[6].pValue!=0)
		{
			ShowMsg(NEWLINE"The Attribute CKA_DECRYPT is: true");
		}
		else
		{
			ShowMsg(NEWLINE"The Attribute CKA_DECRYPT is: false");
		}

		if((int)*(char*)pAttrTemp[7].pValue!=0)
		{
			ShowMsg(NEWLINE"The Attribute CKA_SIGN is: true");
		}
		else
		{
			ShowMsg(NEWLINE"The Attribute CKA_SIGN is: false");
		}
			
			
		ShowMsg(NEWLINE"Private Key can't output because of the limit of hardware:"NEWLINE);

		
		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;
		delete[] pAttrTemp[6].pValue;
		delete[] pAttrTemp[7].pValue;
		delete[] pAttrTemp[8].pValue;
		delete[] pAttrTemp[9].pValue;

	}while(true);
	
	if(numObj==0)
	{
		StartOP();
		ShowMsg(NEWLINE"Can's find private key Obj"NEWLINE);
	}
	else
	{
		StartOP();
		CHAR strNumObj[4];
		wsprintf(strNumObj,"%d",numObj);
		ShowMsg(NEWLINE"Find ");
		ShowMsg(strNumObj);
		ShowMsg(" private key"NEWLINE);
	}
	
}

void CEnumObjDlg::OnBtnSecret() 
{
	// TODO: Add your control notification handler code here
	CK_OBJECT_CLASS dataClass = CKO_SECRET_KEY;
	BOOL IsToken=true;
	CK_ATTRIBUTE pTempl[] = 
	{
		{CKA_CLASS, &dataClass, sizeof(CKO_PUBLIC_KEY)},
		{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;

	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_KEY_TYPE,NULL,0},
			{CKA_LABEL, NULL, 0},
			{CKA_DERIVE,NULL,0},
			{CKA_VALUE,NULL,0},
		};
		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 5);
		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];
		pAttrTemp[4].pValue = new char[pAttrTemp[4].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);
		ZeroMemory(pAttrTemp[4].pValue, pAttrTemp[4].ulValueLen);

		
		ckrv = C_GetAttributeValue(m_hSession, hCKObj, pAttrTemp, 5);
		if(ckrv != CKR_OK)
		{
			delete[] pAttrTemp[0].pValue;
			delete[] pAttrTemp[1].pValue;
			delete[] pAttrTemp[2].pValue;
			delete[] pAttrTemp[3].pValue;
			delete[] pAttrTemp[4].pValue;
			break;
		}

		numObj++;

		CString strvale = (char*)pAttrTemp[2].pValue;

		
		ShowMsg(NEWLINE);
		StartOP();
		ShowMsg(NEWLINE"Begin this Object's Output:"NEWLINE);
		ShowMsg("The Attribute CKA_CLASS of this Obj is:: CKO_SECRET_KEY"NEWLINE);
		
		if(*(int*)pAttrTemp[1].pValue==CKK_GENERIC_SECRET)
		{
		ShowMsg("The Attribute CKA_KEY_TYPE is: CKK_GENERIC_SECRET"NEWLINE);
		}
		else 
			if(*(int*)pAttrTemp[1].pValue==CKK_RC2)
			{
				ShowMsg("The Attribute CKA_KEY_TYPE is CKK_RC2"NEWLINE);
			}
			else
				if(*(int*)pAttrTemp[1].pValue==CKK_RC4)
				{
					ShowMsg("The Attribute CKA_KEY_TYPE is CKK_RC4"NEWLINE);					
				}
				else
					if(*(int*)pAttrTemp[1].pValue==CKK_DES)
					{
						ShowMsg("The Attribute CKA_KEY_TYPE is CKK_DES"NEWLINE);					
					}
					else
						if(*(int*)pAttrTemp[1].pValue==CKK_DES2)
						{
							ShowMsg("The Attribute CKA_KEY_TYPE is CKK_DES2"NEWLINE);					
						}
						else
							if(*(int*)pAttrTemp[1].pValue==CKK_DES3)
							{
								ShowMsg("The Attribute CKA_KEY_TYPE is CKK_DES3"NEWLINE);					
							}
					
		ShowMsg("The Attribute CKA_LABEL of this Obj is: ");
		ShowMsg(strvale);
		
		if((int)*(char*)pAttrTemp[3].pValue!=0)
		{
			ShowMsg(NEWLINE"The Attribute CKA_DERIVE is: true");
		}
		else
		{
			ShowMsg(NEWLINE"The Attribute CKA_DERIVE is : false");
		}

		
		

		ShowMsg(NEWLINE"CKA_VALUE(Content of key) is:"NEWLINE);
		ShowMsg(nByteToStr(pAttrTemp[4].ulValueLen, pAttrTemp[4].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;		
		
	}while(true);
	

	if(numObj==0)
	{
		StartOP();
		ShowMsg(NEWLINE"Can't find secret key"NEWLINE);
	}
	else
	{
		StartOP();
		CHAR strNumObj[4];
		wsprintf(strNumObj,"%d",numObj);
		ShowMsg(NEWLINE"Find ");
		ShowMsg(strNumObj);
		ShowMsg(" secret key"NEWLINE);
	}
}

⌨️ 快捷键说明

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