CKA_CLASS
根据 PKCS#11,X509 证书是其数据类(属性)等于的某种存储对象CKO_CERTIFICATE
。
如果你想从你的令牌中检索一个证书对象,你只需要使用C_GetAttributeValue
函数。
在执行此操作之前,您可能希望找到存储在您的令牌上的所有 x.509 证书:
CK_BBOOL _true = CK_TRUE;
CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;
CK_CERTIFICATE_TYPE certType = CKC_X_509_ATTR_CERT;
CK_ATTRIBUTE certificateSearchTemplate[] = {
{CKA_CLASS, &certClass, sizeof(dataClass)},
{CKA_CERTIFICATE_TYPE, &certType, sizeof(certType)},
{CKA_TOKEN, &_true, sizeof(_true)}
};
CK_OBJECT_HANDLE hObject;
CK_ULONG ulObjectCount;
CK_RV rv = C_FindObjectsInit(hSession, certificateSearchTemplate, 3);
assert(rv == CKR_OK);
while (1) {
rv = C_FindObjects(hSession, &hObject, 1, &ulObjectCount);
if (rv != CKR_OK || ulObjectCount == 0)
break;
//hObject is handle of a x.509 certificate, so you can fetch your desired attributes from it using C_GetAttributeValue
getCertificateAttributes(hObject);
}
rv = C_FindObjectsFinal(hSession);
assert(rv == CKR_OK);
在getCertificateAttributes
功能中,您可以获得所需的证书属性:
void getCertificateAttributes(CK_OBJECT_HANDLE hCert)
{
CK_ATTRIBUTE pTemplate[] = {
//List your desired attributes here
};
...
CK_RV rv = C_GetAttributeValue(hSession, hCert, &pTemplate, pTemplateLen);
if (rv == CKR_OK) {
//here you have your desired certificate attributes
}
}