Object handling procedures
When you use your sample code with a static linked library you can access the APIs directly. View some openCryptoki code samples for procedures dealing with object handling.
C_CreateKeyObject:
/*
* createKeyObject
*/
CK_RV createKeyObject(CK_SESSION_HANDLE hSession, CK_BYTE_PTR key, CK_ULONG keyLength) {
CK_RV rc;
CK_OBJECT_HANDLE hKey;
CK_BBOOL true = TRUE;
CK_BBOOL false = FALSE;
CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
CK_KEY_TYPE keyType = CKK_AES;
CK_ATTRIBUTE keyTempl[] = {
{CKA_CLASS, &keyClass, sizeof(keyClass)},
{CKA_KEY_TYPE, &keyType, sizeof(keyType)},
{CKA_ENCRYPT, &true, sizeof(true)},
{CKA_DECRYPT, &true, sizeof(true)},
{CKA_SIGN, &true, sizeof(true)},
{CKA_VERIFY, &true, sizeof(true)},
{CKA_TOKEN, &true, sizeof(true)}, /* token object */
{CKA_PRIVATE, &false, sizeof(false)}, /* public object */
{CKA_VALUE, keyValue, keyLength}, /* AES key */
{CKA_LABEL, "My_AES_Key", sizeof("My_AES_Key")}
};
rc = C_CreateObject(hSession, keyTempl, sizeof (keyTempl)/sizeof (CK_ATTRIBUTE), &hKey);
if (rc != CKR_OK) {
printf("Error creating key object: 0x%X\n", rc); return rc;
}
printf("AES Key object creation successful.\n");
return CKR_OK;
}
C_FindObjects:
/*
* findObjects
*/
CK_RV getKey(CK_CHAR_PTR label, int labelLen, CK_OBJECT_HANDLE_PTR hObject,
CK_SESSION_HANDLE hSession) {
CK_RV rc;
CK_ULONG ulMaxObjectCount = 1;
CK_ULONG ulObjectCount;
CK_ATTRIBUTE objectMask[] = { {CKA_LABEL, label, labelLen} };
rc = C_FindObjectsInit(hSession, objectMask, 1);
if (rc != CKR_OK) {
printf("Error FindObjectsInit: 0x%X\n", rc); return rc;
}
rc = C_FindObjects(hSession, hObject, ulMaxObjectCount, &ulObjectCount);
if (rc != CKR_OK) {
printf("Error FindObjects: 0x%X\n", rc); return rc;
}
rc = C_FindObjectsFinal(hSession);
if (rc != CKR_OK) {
printf("Error FindObjectsFinal: 0x%X\n", rc); return rc;
}
return CKR_OK;
}