summaryrefslogtreecommitdiff
path: root/gck/gck-modules.c
blob: 9bd9e6e64614a330579d153af9ed75999751e6fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gck-modules.c - the GObject PKCS#11 wrapper library

   Copyright (C) 2010, Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "gck.h"
#include "gck-private.h"
#include "gck-marshal.h"

#include <p11-kit/p11-kit.h>

#include <string.h>
#include <stdlib.h>

/**
 * SECTION:gck-modules
 * @title: GckModule lists
 * @short_description: Dealing with lists of PKCS\#11 modules.
 *
 * These functions are useful for dealing with lists of modules, and performing
 * operations on all of them.
 */

/**
 * gck_modules_initialize_registered:
 * @flags: reserved options set to zero.
 *
 * Load and initialize all the registered modules.
 *
 * Returns: A newly allocated list of GckModule objects, which should
 * be released with gck_list_unref_free().
 */
GList*
gck_modules_initialize_registered (guint flags)
{
	GckModule *module;
	GList *results = NULL;
	CK_FUNCTION_LIST_PTR *modules, *funcs;
	CK_RV rv;

	rv = p11_kit_initialize_registered ();
	if (rv != CKR_OK) {
		g_warning ("couldn't initialize registered PKCS#11 modules: %s",
		           gck_message_from_rv (rv));
		return NULL;
	}

	modules = p11_kit_registered_modules ();

	for (funcs = modules; *funcs; ++funcs) {
		module = _gck_module_new_initialized (*funcs, 0);
		results = g_list_prepend (results, module);
	}

	free (modules);

	return results;
}

/**
 * gck_modules_get_slots:
 * @modules: The modules
 * @token_present: Whether to only list slots with token present
 *
 * Get a list of slots for across all of the modules.
 *
 * Returns: A list of #GckSlot objects, which should be freed with
 *     gck_list_unref_free().
 */
GList*
gck_modules_get_slots (GList *modules, gboolean token_present)
{
	GList *result = NULL;
	GList *m;

	for (m = modules; m; m = g_list_next (m)) {
		result = g_list_concat (result, gck_module_get_slots (m->data, token_present));
	}

	return result;
}

/**
 * gck_modules_enumerate_objects:
 * @modules: The modules
 * @attrs: Attributes that the objects must have, or empty for all objects
 * @session_options: Options from GckSessionOptions
 *
 * Setup an enumerator for listing matching objects on the modules.
 *
 * This call will not block but will return an enumerator immediately.

 * Return value: A new enumerator, which should be released with g_object_unref().
 **/
GckEnumerator*
gck_modules_enumerate_objects (GList *modules, GckAttributes *attrs, guint session_options)
{
	GckUriData *uri_data;

	g_return_val_if_fail (attrs, NULL);

	uri_data = gck_uri_data_new ();
	uri_data->attributes = gck_attributes_ref (attrs);

	return _gck_enumerator_new (modules, session_options, uri_data);
}

/**
 * gck_modules_token_for_uri:
 * @modules: The modules
 * @uri: The URI that the token must match
 * @error: A location to raise an error on failure
 *
 * Lookup a token that matches the URI.
 *
 * Returns: A newly allocated #GckSlot or %NULL if no such token was
 *    found.
 */
GckSlot*
gck_modules_token_for_uri (GList *modules, const gchar *uri, GError **error)
{
	GckTokenInfo *token_info;
	GckSlot *result = NULL;
	GckUriData *uri_data;
	GckModuleInfo *module_info;
	GList *slots;
	GList *m, *s;
	gboolean matched;

	uri_data = gck_uri_parse (uri, GCK_URI_CONTEXT_TOKEN, error);
	if (uri_data == NULL)
		return NULL;

	if (!uri_data->any_unrecognized) {
		for (m = modules; result == NULL && m != NULL; m = g_list_next (m)) {
			if (uri_data->module_info) {
				module_info = gck_module_get_info (m->data);
				matched = _gck_module_info_match (uri_data->module_info, module_info);
				gck_module_info_free (module_info);
				if (!matched)
					continue;
			}

			slots = gck_module_get_slots (m->data, TRUE);
			for (s = slots; result == NULL && s != NULL; s = g_list_next (s)) {
				if (!uri_data->token_info) {
					result = g_object_ref (s->data);
				} else {
					token_info = gck_slot_get_token_info (s->data);
					if (token_info && _gck_token_info_match (uri_data->token_info, token_info))
						result = g_object_ref (s->data);
					gck_token_info_free (token_info);
				}
			}
			gck_list_unref_free (slots);
		}
	}

	gck_uri_data_free (uri_data);
	return result;
}

/**
 * gck_modules_object_for_uri:
 * @modules: The modules
 * @uri: The URI the objects must match
 * @session_options: Options from GckSessionOptions
 * @error: A location to raise an error on failure.
 *
 * Find an object that matches a URI.
 *
 * This call can block. Use gck_modules_enumerate_uri() for a non-blocking
 * version.
 *
 * Returns: A new #GckObject which should be released with g_object_unref(),
 *     or %NULL if no matching object was found.
 */
GckObject*
gck_modules_object_for_uri (GList *modules, const gchar *uri, guint session_options,
                            GError **error)
{
	GckEnumerator *en;
	GckObject *result;

	g_return_val_if_fail (uri, NULL);
	g_return_val_if_fail (!error || !*error, NULL);

	en = gck_modules_enumerate_uri (modules, uri, session_options, error);
	if (en == NULL)
		return NULL;

	result = gck_enumerator_next (en, NULL, error);
	g_object_unref (en);

	return result;
}

/**
 * gck_modules_objects_for_uri:
 * @modules: The modules
 * @uri: The URI the objects must match
 * @session_options: Options from GckSessionOptions
 * @error: A location to raise an error on failure.
 *
 * Find objects that match a URI.
 *
 * This call can block. Use gck_modules_enumerate_uri() for a non-blocking
 * version.
 *
 * Returns: A list of #GckObject which should be released with gck_list_unref_free(),
 *     or %NULL if no matching object was found.
 */
GList*
gck_modules_objects_for_uri (GList *modules, const gchar *uri, guint session_options,
                             GError **error)
{
	GckEnumerator *en;
	GList *results;

	g_return_val_if_fail (uri, NULL);
	g_return_val_if_fail (!error || !*error, NULL);

	en = gck_modules_enumerate_uri (modules, uri, session_options, error);
	if (en == NULL)
		return NULL;

	results = gck_enumerator_next_n (en, -1, NULL, error);
	g_object_unref (en);

	return results;
}

/**
 * gck_modules_enumerate_uri:
 * @modules: The modules
 * @uri: The URI that the enumerator will match
 * @session_options: Options from GckSessionOptions
 * @error: A location to raise an error on failure.
 *
 * Enumerate objects that match a URI.
 *
 * This call will not block. Use the #GckEnumerator functions in order to
 * get at the actual objects that match.
 *
 * Returns: A new #GckEnumerator, or %NULL if an error occurs.
 */
GckEnumerator*
gck_modules_enumerate_uri (GList *modules, const gchar *uri, guint session_options,
                           GError **error)
{
	GckUriData *uri_data;

	uri_data = gck_uri_parse (uri, GCK_URI_CONTEXT_OBJECT, error);
	if (uri_data == NULL)
		return NULL;

	/* Takes ownership of uri_info */
	return _gck_enumerator_new (modules, session_options, uri_data);
}