in src/find_objects/find_objects.c [34:85]
CK_RV find_by_attr(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE *template, CK_ULONG attr_count, CK_ULONG *count,
CK_OBJECT_HANDLE_PTR *hObject) {
CK_RV rv;
if (NULL == hObject || NULL == template || NULL == count) {
return CKR_ARGUMENTS_BAD;
}
rv = funcs->C_FindObjectsInit(hSession, template, attr_count);
if (rv != CKR_OK) {
fprintf(stderr, "Can't initialize search\n");
return rv;
}
CK_ULONG max_objects = 25;
bool searching = 1;
*count = 0;
while (searching) {
CK_ULONG found = 0;
*hObject = realloc(*hObject, (*count + max_objects) * sizeof(CK_OBJECT_HANDLE));
if (NULL == *hObject) {
fprintf(stderr, "Could not allocate memory for objects\n");
return CKR_HOST_MEMORY;
}
CK_OBJECT_HANDLE_PTR loc = *hObject;
rv = funcs->C_FindObjects(hSession, &loc[*count], max_objects, &found);
if (rv != CKR_OK) {
fprintf(stderr, "Can't run search\n");
funcs->C_FindObjectsFinal(hSession);
return rv;
}
(*count) += found;
if (0 == found)
searching = 0;
}
rv = funcs->C_FindObjectsFinal(hSession);
if (rv != CKR_OK) {
fprintf(stderr, "Can't finalize search\n");
return rv;
}
if (0 == *count) {
fprintf(stderr, "Didn't find requested key\n");
return rv;
}
return CKR_OK;
}