return &enumerator->public;
}
+/**
+ * Enumerator over mechanisms
+ */
+typedef struct {
+ /* implements enumerator_t */
+ enumerator_t public;
+ /* PKCS#11 library */
+ pkcs11_library_t *lib;
+ /* slot of token */
+ CK_SLOT_ID slot;
+ /* mechanism type list */
+ CK_MECHANISM_TYPE_PTR mechs;
+ /* number of mechanism types */
+ CK_ULONG count;
+ /* current mechanism */
+ CK_ULONG current;
+} mechanism_enumerator_t;
+
+METHOD(enumerator_t, enumerate_mech, bool,
+ mechanism_enumerator_t *this, CK_MECHANISM_TYPE* type,
+ CK_MECHANISM_INFO *info)
+{
+ CK_RV rv;
+
+ if (this->current >= this->count)
+ {
+ return FALSE;
+ }
+ if (info)
+ {
+ rv = this->lib->f->C_GetMechanismInfo(this->slot,
+ this->mechs[this->current], info);
+ if (rv != CKR_OK)
+ {
+ DBG1(DBG_CFG, "C_GetMechanismInfo() failed: %N", ck_rv_names, rv);
+ return FALSE;
+ }
+ }
+ *type = this->mechs[this->current++];
+ return TRUE;
+}
+
+METHOD(enumerator_t, destroy_mech, void,
+ mechanism_enumerator_t *this)
+{
+ free(this->mechs);
+ free(this);
+}
+
+METHOD(pkcs11_library_t, create_mechanism_enumerator, enumerator_t*,
+ private_pkcs11_library_t *this, CK_SLOT_ID slot)
+{
+ mechanism_enumerator_t *enumerator;
+ CK_RV rv;
+
+ INIT(enumerator,
+ .public = {
+ .enumerate = (void*)_enumerate_mech,
+ .destroy = _destroy_mech,
+ },
+ .lib = &this->public,
+ .slot = slot,
+ );
+
+ rv = enumerator->lib->f->C_GetMechanismList(slot, NULL, &enumerator->count);
+ if (rv != CKR_OK)
+ {
+ DBG1(DBG_CFG, "C_GetMechanismList() failed: %N", ck_rv_names, rv);
+ free(enumerator);
+ return enumerator_create_empty();
+ }
+ enumerator->mechs = malloc(sizeof(CK_MECHANISM_TYPE) * enumerator->count);
+ enumerator->lib->f->C_GetMechanismList(slot, enumerator->mechs,
+ &enumerator->count);
+ if (rv != CKR_OK)
+ {
+ DBG1(DBG_CFG, "C_GetMechanismList() failed: %N", ck_rv_names, rv);
+ destroy_mech(enumerator);
+ return enumerator_create_empty();
+ }
+ return &enumerator->public;
+}
+
METHOD(pkcs11_library_t, destroy, void,
private_pkcs11_library_t *this)
{
.public = {
.get_name = _get_name,
.create_object_enumerator = _create_object_enumerator,
+ .create_mechanism_enumerator = _create_mechanism_enumerator,
.destroy = _destroy,
},
.name = name,
*/
static void print_mechs(lib_entry_t *entry, CK_SLOT_ID slot)
{
- CK_MECHANISM_TYPE_PTR mechs;
+ enumerator_t *enumerator;
+ CK_MECHANISM_TYPE type;
CK_MECHANISM_INFO info;
- CK_ULONG count;
- CK_RV rv;
- int i;
- rv = entry->lib->f->C_GetMechanismList(slot, NULL, &count);
- if (rv != CKR_OK)
+ enumerator = entry->lib->create_mechanism_enumerator(entry->lib, slot);
+ while (enumerator->enumerate(enumerator, &type, &info))
{
- DBG1(DBG_CFG, "C_GetMechanismList() failed: %N", ck_rv_names, rv);
- return;
+ DBG2(DBG_CFG, " %N %lu-%lu [ %s%s%s%s%s%s%s%s%s%s%s%s%s]",
+ ck_mech_names, type,
+ info.ulMinKeySize, info.ulMaxKeySize,
+ info.flags & CKF_HW ? "HW " : "",
+ info.flags & CKF_ENCRYPT ? "ENCR " : "",
+ info.flags & CKF_DECRYPT ? "DECR " : "",
+ info.flags & CKF_DIGEST ? "DGST " : "",
+ info.flags & CKF_SIGN ? "SIGN " : "",
+ info.flags & CKF_SIGN_RECOVER ? "SIGN_RCVR " : "",
+ info.flags & CKF_VERIFY ? "VRFY " : "",
+ info.flags & CKF_VERIFY_RECOVER ? "VRFY_RCVR " : "",
+ info.flags & CKF_GENERATE ? "GEN " : "",
+ info.flags & CKF_GENERATE_KEY_PAIR ? "GEN_KEY_PAIR " : "",
+ info.flags & CKF_WRAP ? "WRAP " : "",
+ info.flags & CKF_UNWRAP ? "UNWRAP " : "",
+ info.flags & CKF_DERIVE ? "DERIVE " : "");
}
- mechs = malloc(sizeof(CK_MECHANISM_TYPE) * count);
- entry->lib->f->C_GetMechanismList(slot, mechs, &count);
- if (rv != CKR_OK)
- {
- DBG1(DBG_CFG, "C_GetMechanismList() failed: %N", ck_rv_names, rv);
- return;
- }
- for (i = 0; i < count; i++)
- {
- rv = entry->lib->f->C_GetMechanismInfo(slot, mechs[i], &info);
- if (rv == CKR_OK)
- {
- DBG2(DBG_CFG, " %N %lu-%lu [ %s%s%s%s%s%s%s%s%s%s%s%s%s]",
- ck_mech_names, mechs[i],
- info.ulMinKeySize, info.ulMaxKeySize,
- info.flags & CKF_HW ? "HW " : "",
- info.flags & CKF_ENCRYPT ? "ENCR " : "",
- info.flags & CKF_DECRYPT ? "DECR " : "",
- info.flags & CKF_DIGEST ? "DGST " : "",
- info.flags & CKF_SIGN ? "SIGN " : "",
- info.flags & CKF_SIGN_RECOVER ? "SIGN_RCVR " : "",
- info.flags & CKF_VERIFY ? "VRFY " : "",
- info.flags & CKF_VERIFY_RECOVER ? "VRFY_RCVR " : "",
- info.flags & CKF_GENERATE ? "GEN " : "",
- info.flags & CKF_GENERATE_KEY_PAIR ? "GEN_KEY_PAIR " : "",
- info.flags & CKF_WRAP ? "WRAP " : "",
- info.flags & CKF_UNWRAP ? "UNWRAP " : "",
- info.flags & CKF_DERIVE ? "DERIVE " : "");
- }
- else
- {
- DBG1(DBG_CFG, "C_GetMechanismList(%N) failed: %N",
- ck_mech_names, mechs[i], ck_rv_names, rv);
- }
- }
- free(mechs);
+ enumerator->destroy(enumerator);
}
/**