summaryrefslogtreecommitdiff
path: root/gck/gck-uri.c
blob: 91ec2b56046a5869c6f595e9cb1219ce810bf336 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gck-uri.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,
   see <http://www.gnu.org/licenses/>.

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

#include "config.h"

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

#include "gck/gck-marshal.h"

#include <glib/gi18n-lib.h>

#include <p11-kit/uri.h>

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

/**
 * GckUriData:
 * @any_unrecognized: whether any parts of the PKCS#11 URI were unsupported or unrecognized.
 * @module_info: information about the PKCS#11 modules matching the URI.
 * @token_info: information about the PKCS#11 tokens matching the URI.
 * @attributes: information about the PKCS#11 objects matching the URI.
 *
 * Information about the contents of a PKCS#11 URI. Various fields may be %NULL
 * depending on the context that the URI was parsed for.
 *
 * Since PKCS#11 URIs represent a set which results from the intersections of
 * all of the URI parts, if @any_recognized is set to %TRUE then usually the URI
 * should be treated as not matching anything.
 */

/**
 * GckUriFlags:
 * @GCK_URI_FOR_MODULE: the URI will be used to match modules.
 * @GCK_URI_FOR_TOKEN: the URI will be used to match tokens.
 * @GCK_URI_FOR_OBJECT: the URI will be used to match objects.
 * @GCK_URI_WITH_VERSION: the URI has specific version numbers for module and/or token
 * @GCK_URI_FOR_ANY: parse all recognized components of the URI.
 *
 * Which parts of the PKCS#11 URI will be parsed or formatted. These can be
 * combined.
 */

/**
 * GCK_URI_FOR_MODULE_WITH_VERSION:
 *
 * The URI will match specific version of modules. To be used as a GckUriFlags argument.
 */

/**
 * GCK_URI_FOR_OBJECT_ON_TOKEN:
 *
 * The URI will match objects on a specific token. To be used as a GckUriFlags argument.
 */

/**
 * GCK_URI_FOR_OBJECT_ON_TOKEN_AND_MODULE:
 *
 * The token inserted into a device with a specific module.
 */

/**
 * GckUriError:
 * @GCK_URI_BAD_SCHEME: invalid URI scheme
 * @GCK_URI_BAD_ENCODING: bad URI encoding
 * @GCK_URI_BAD_SYNTAX: bad URI syntax
 * @GCK_URI_BAD_VERSION: bad URI version component
 * @GCK_URI_NOT_FOUND: piece of the URI was not found
 *
 * Various error codes used with PKCS#11 URIs
 */

/**
 * GCK_URI_ERROR:
 *
 * Error domain for URI errors.
 */

#define URI_PREFIX "pkcs11:"
#define N_URI_PREFIX 7

struct _GckUri {
	gboolean any_unrecognized;
	GckModuleInfo *module_info;
	GckTokenInfo *token_info;
	GckAttributes *attributes;
};

G_DEFINE_QUARK(GckUriError, gck_uri_error)

/**
 * gck_uri_data_new:
 *
 * Allocate a new GckUriData structure. None of the fields
 * will be set.
 *
 * Returns: (transfer full): a newly allocated GckUriData, free with
 *          gck_uri_data_free()
 */
GckUriData *
gck_uri_data_new (void)
{
	return g_new0 (GckUriData, 1);
}

/**
 * gck_uri_data_parse:
 * @string: the URI to parse.
 * @flags: the context in which the URI will be used.
 * @error: a #GError, or %NULL.
 *
 * Parse a PKCS#11 URI for use in a given context.
 *
 * The result will contain the fields that are relevant for
 * the given context. See #GckUriData  for more info.
 * Other fields will be set to %NULL.
 *
 * Returns: (transfer full): a newly allocated #GckUriData; which should be
 *          freed with gck_uri_data_free()
 */
GckUriData*
gck_uri_data_parse (const gchar *string, GckUriFlags flags, GError **error)
{
	GckUriData *uri_data = NULL;
	GckBuilder builder;
	CK_ATTRIBUTE_PTR attrs;
	CK_ULONG i, n_attrs;
	P11KitUri *p11_uri;
	gint res;

	g_return_val_if_fail (string, FALSE);
	g_return_val_if_fail (!error || !*error, FALSE);

	p11_uri = p11_kit_uri_new ();
	if (!p11_uri)
		g_error ("failed to allocate P11KitUri");

	res = p11_kit_uri_parse (string, flags, p11_uri);
	if (res != P11_KIT_URI_OK) {
		p11_kit_uri_free (p11_uri);
		switch (res) {
		case P11_KIT_URI_NO_MEMORY:
			g_error ("failed to allocate memory in p11_kit_uri_parse()");
			break;
		case P11_KIT_URI_BAD_ENCODING:
			g_set_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING,
				     _("The URI has invalid encoding."));
			break;
		case P11_KIT_URI_BAD_SCHEME:
			g_set_error_literal (error, GCK_URI_ERROR, GCK_URI_BAD_SCHEME,
			                     _("The URI does not have the “pkcs11” scheme."));
			break;
		case P11_KIT_URI_BAD_SYNTAX:
			g_set_error_literal (error, GCK_URI_ERROR, GCK_URI_BAD_SYNTAX,
			                     _("The URI has bad syntax."));
			break;
		case P11_KIT_URI_BAD_VERSION:
			g_set_error_literal (error, GCK_URI_ERROR, GCK_URI_BAD_SYNTAX,
			                     _("The URI has a bad version number."));
			break;
		case P11_KIT_URI_NOT_FOUND:
			g_assert_not_reached ();
			break;
		};
		return NULL;
	}

	/* Convert it to a GckUri */
	uri_data = gck_uri_data_new ();
	if (flags & GCK_URI_FOR_MODULE_WITH_VERSION)
		uri_data->module_info = _gck_module_info_from_pkcs11 (p11_kit_uri_get_module_info (p11_uri));
	if (flags & GCK_URI_FOR_TOKEN)
		uri_data->token_info = _gck_token_info_from_pkcs11 (p11_kit_uri_get_token_info (p11_uri));
	if (flags & GCK_URI_FOR_OBJECT) {
		attrs = p11_kit_uri_get_attributes (p11_uri, &n_attrs);
		gck_builder_init (&builder);
		for (i = 0; i < n_attrs; ++i)
			gck_builder_add_data (&builder, attrs[i].type, attrs[i].pValue, attrs[i].ulValueLen);
		uri_data->attributes = gck_builder_end (&builder);
	}
	uri_data->any_unrecognized = p11_kit_uri_any_unrecognized (p11_uri);

	p11_kit_uri_free (p11_uri);
	return uri_data;
}

/**
 * gck_uri_data_build:
 * @uri_data: the info to build the URI from.
 * @flags: The context that the URI is for
 *
 * Build a PKCS#11 URI. The various parts relevant to the flags
 * specified will be used to build the URI.
 *
 * Return value: a newly allocated string containing a PKCS#11 URI.
 */
gchar*
gck_uri_data_build (GckUriData *uri_data, GckUriFlags flags)
{
	const GckAttribute *attr;
	P11KitUri *p11_uri = 0;
	gchar *string;
	int res;
	guint i;

	g_return_val_if_fail (uri_data != NULL, NULL);

	p11_uri = p11_kit_uri_new ();

	if ((flags & GCK_URI_FOR_MODULE_WITH_VERSION) && uri_data->module_info)
		_gck_module_info_to_pkcs11 (uri_data->module_info,
		                            p11_kit_uri_get_module_info (p11_uri));
	if ((flags & GCK_URI_FOR_TOKEN) && uri_data->token_info)
		_gck_token_info_to_pkcs11 (uri_data->token_info,
		                           p11_kit_uri_get_token_info (p11_uri));
	if ((flags & GCK_URI_FOR_OBJECT) && uri_data->attributes) {
		for (i = 0; i < gck_attributes_count (uri_data->attributes); ++i) {
			attr = gck_attributes_at (uri_data->attributes, i);
			res = p11_kit_uri_set_attribute (p11_uri, (CK_ATTRIBUTE_PTR)attr);
			if (res == P11_KIT_URI_NO_MEMORY)
				g_error ("failed to allocate memory in p11_kit_uri_set_attribute()");
			else if (res != P11_KIT_URI_NOT_FOUND && res != P11_KIT_URI_OK)
				g_return_val_if_reached (NULL);
		}
	}

	res = p11_kit_uri_format (p11_uri, flags & GCK_URI_FOR_ANY, &string);
	if (res == P11_KIT_URI_NO_MEMORY)
		g_error ("failed to allocate memory in p11_kit_uri_format()");
	else if (res != P11_KIT_URI_OK)
		g_return_val_if_reached (NULL);

	p11_kit_uri_free (p11_uri);
	return string;
}

G_DEFINE_BOXED_TYPE (GckUriData, gck_uri_data,
                     gck_uri_data_copy, gck_uri_data_free)

/**
 * gck_uri_data_copy:
 * @uri_data: URI data to copy
 *
 * Copy a #GckUriData
 *
 * Returns: (transfer full): newly allocated copy of the uri data
 */
GckUriData *
gck_uri_data_copy (GckUriData *uri_data)
{
	GckUriData *copy;

	copy = g_memdup2 (uri_data, sizeof (GckUriData));
	copy->attributes = gck_attributes_ref (uri_data->attributes);
	copy->module_info = gck_module_info_copy (copy->module_info);
	copy->token_info = gck_token_info_copy (copy->token_info);
	return copy;
}

/**
 * gck_uri_data_free:
 * @uri_data: (transfer full): URI data to free.
 *
 * Free a #GckUriData.
 */
void
gck_uri_data_free (GckUriData *uri_data)
{
	if (!uri_data)
		return;

	g_clear_pointer (&uri_data->attributes, gck_attributes_unref);
	g_clear_pointer (&uri_data->module_info, gck_module_info_free);
	g_clear_pointer (&uri_data->token_info, gck_token_info_free);
	g_free (uri_data);
}