Esempio n. 1
0
static int
anchor_store (int argc,
              char *argv[],
              bool *changed)
{
	CK_ATTRIBUTE *attrs;
	CK_FUNCTION_LIST *module = NULL;
	CK_SESSION_HANDLE session;
	CK_OBJECT_HANDLE object;
	p11_array *anchors;
	int ret;
	int i;

	anchors = files_to_attrs (argc, argv);
	if (anchors == NULL)
		return 1;

	if (anchors->num == 0) {
		p11_message ("specify at least one anchor input file");
		p11_array_free (anchors);
		return 2;
	}

	session = session_for_store (&module);
	if (session == 0UL) {
		p11_array_free (anchors);
		return 1;
	}

	for (i = 0, ret = 0; i < anchors->num; i++) {
		attrs = anchors->elem[i];
		anchors->elem[i] = NULL;

		object = find_anchor (module, session, attrs);
		if (object == 0) {
			p11_debug ("don't yet have this anchor");
			if (create_anchor (module, session, attrs)) {
				*changed = true;
			} else {
				ret = 1;
				break;
			}
		} else {
			p11_debug ("already have this anchor");
			if (modify_anchor (module, session, object, attrs)) {
				*changed = true;
			} else {
				ret = 1;
				break;
			}
		}
	}

	p11_array_free (anchors);
	p11_kit_module_finalize (module);
	p11_kit_module_release (module);

	return ret;
}
Esempio n. 2
0
static CK_SESSION_HANDLE
session_for_store_on_module (const char *name,
                             CK_FUNCTION_LIST *module,
                             bool *found_read_only)
{
	CK_SESSION_HANDLE session = 0;
	CK_SLOT_ID *slots = NULL;
	CK_TOKEN_INFO info;
	CK_ULONG count;
	CK_ULONG i;
	CK_RV rv;

	rv = p11_kit_module_initialize (module);
	if (rv != CKR_OK) {
		p11_message ("%s: couldn't initialize: %s", name, p11_kit_message ());
		return 0UL;
	}

	rv = (module->C_GetSlotList) (CK_TRUE, NULL, &count);
	if (rv == CKR_OK) {
		slots = calloc (count, sizeof (CK_ULONG));
		return_val_if_fail (slots != NULL, 0UL);
		rv = (module->C_GetSlotList) (CK_TRUE, slots, &count);
	}
	if (rv != CKR_OK) {
		p11_message ("%s: couldn't enumerate slots: %s", name, p11_kit_strerror (rv));
		free (slots);
		return 0UL;
	}

	for (i = 0; session == 0 && i < count; i++) {
		rv = (module->C_GetTokenInfo) (slots[i], &info);
		if (rv != CKR_OK) {
			p11_message ("%s: couldn't get token info: %s", name, p11_kit_strerror (rv));
			continue;
		}

		if (info.flags & CKF_WRITE_PROTECTED) {
			*found_read_only = true;
			continue;
		}

		rv = (module->C_OpenSession) (slots[i], CKF_SERIAL_SESSION | CKF_RW_SESSION,
		                              NULL, NULL, &session);
		if (rv != CKR_OK) {
			p11_message ("%s: couldn't open session: %s", name, p11_kit_strerror (rv));
			session = 0;
		}

		p11_debug ("opened writable session on: %s", name);
	}

	free (slots);

	if (session == 0UL)
		p11_kit_module_finalize (module);

	return session;
}
Esempio n. 3
0
static void
teardown_mock_module (CK_FUNCTION_LIST_PTR module)
{
	CK_RV rv;

	rv = p11_kit_module_finalize (module);
	assert (rv == CKR_OK);

	p11_lock ();

	rv = p11_module_release_inlock_reentrant (module);
	assert (rv == CKR_OK);

	p11_unlock ();
}
Esempio n. 4
0
static void
gck_module_dispose (GObject *obj)
{
	GckModule *self = GCK_MODULE (obj);
	gboolean finalize = FALSE;
	CK_RV rv;

	if (self->pv->initialized && self->pv->funcs) {
		if (g_atomic_int_compare_and_exchange (&self->pv->finalized, 0, 1))
			finalize = TRUE;
	}

	/* Must be careful when accessing funcs */
	if (finalize) {
		rv = p11_kit_module_finalize (self->pv->funcs);
		if (rv != CKR_OK) {
			g_warning ("C_Finalize on module '%s' failed: %s",
			           self->pv->path, gck_message_from_rv (rv));
		}
	}

	G_OBJECT_CLASS (gck_module_parent_class)->dispose (obj);
}