summaryrefslogtreecommitdiff
path: root/pkcs11/xdg-store/gkm-xdg-module.c
blob: e2588e1f554b8824e1b59ef58d05145917227c97 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
 * gnome-keyring
 *
 * Copyright (C) 2010 Stefan Walter
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gkm-xdg-assertion.h"
#include "gkm-xdg-module.h"
#include "gkm-xdg-store.h"
#include "gkm-xdg-trust.h"

#include "egg/egg-asn1x.h"
#include "egg/egg-asn1-defs.h"
#include "egg/egg-dn.h"
#include "egg/egg-error.h"
#include "egg/egg-hex.h"

#include "gkm/gkm-assertion.h"
#include "gkm/gkm-certificate.h"
#define DEBUG_FLAG GKM_DEBUG_STORAGE
#include "gkm/gkm-debug.h"
#include "gkm/gkm-file-tracker.h"
#include "gkm/gkm-serializable.h"
#include "gkm/gkm-transaction.h"
#include "gkm/gkm-util.h"

#include "pkcs11x.h"

#include <string.h>

struct _GkmXdgModule {
	GkmModule parent;
	gchar *directory;
	GHashTable *objects_by_path;
	GkmFileTracker *tracker;
	CK_TOKEN_INFO token_info;
};

static const CK_SLOT_INFO user_module_slot_info = {
	"User Key Storage",
	"Gnome Keyring",
	CKF_TOKEN_PRESENT,
	{ 0, 0 },
	{ 0, 0 }
};

static const CK_TOKEN_INFO user_module_token_info = {
	"User Key Storage",
	"Gnome Keyring",
	"1.0",
	"1:XDG:DEFAULT", /* Unique serial number for manufacturer */
	CKF_TOKEN_INITIALIZED,
	CK_EFFECTIVELY_INFINITE,
	CK_EFFECTIVELY_INFINITE,
	CK_EFFECTIVELY_INFINITE,
	CK_EFFECTIVELY_INFINITE,
	1024,
	1,
	CK_UNAVAILABLE_INFORMATION,
	CK_UNAVAILABLE_INFORMATION,
	CK_UNAVAILABLE_INFORMATION,
	CK_UNAVAILABLE_INFORMATION,
	{ 0, 0 },
	{ 0, 0 },
	""
};

#define UNUSED_VALUE (GUINT_TO_POINTER (1))

#define UNWANTED_FILENAME_CHARS  ":/\\<>|\t\n\r\v "

G_DEFINE_TYPE (GkmXdgModule, gkm_xdg_module, GKM_TYPE_MODULE);

GkmModule*  _gkm_xdg_store_get_module_for_testing (void);

/* Forward declarations */
static void  remove_object_from_module (GkmXdgModule *self, GkmObject *object,
                                        const gchar *filename, GkmTransaction *transaction);
static void  add_object_to_module (GkmXdgModule *self, GkmObject *object,
                                   const gchar *filename, GkmTransaction *transaction);

/* -----------------------------------------------------------------------------
 * ACTUAL PKCS#11 Module Implementation
 */

/* Include all the module entry points */
#include "gkm/gkm-module-ep.h"
GKM_DEFINE_MODULE (gkm_xdg_module, GKM_TYPE_XDG_MODULE);

/* -----------------------------------------------------------------------------
 * INTERNAL
 */

static GType
type_from_path (const gchar *path)
{
	const gchar *ext;

	ext = strrchr (path, '.');

	/* The file tracker doesn't match files without exts */
	g_return_val_if_fail (ext, 0);

	if (g_str_equal (ext, ".trust"))
		return GKM_XDG_TYPE_TRUST;
	else if (strcmp (ext, ".cer") == 0)
		return GKM_TYPE_CERTIFICATE;

	return 0;
}

static const gchar*
lookup_filename_for_object (GkmObject *object)
{
	return g_object_get_data (G_OBJECT (object), "xdg-module-filename");
}

static gboolean
complete_add_object (GkmTransaction *transaction, GObject *module, gpointer user_data)
{
	GkmXdgModule *self = GKM_XDG_MODULE (module);
	GkmObject *object = GKM_OBJECT (user_data);
	const gchar *filename;

	/* If the transaction failed, revert it */
	if (gkm_transaction_get_failed (transaction)) {
		filename = g_object_get_data (G_OBJECT (object), "xdg-module-filename");
		g_return_val_if_fail (filename, FALSE);
		remove_object_from_module (self, object, filename, NULL);
	}

	g_object_unref (object);
	return TRUE;
}

static void
add_object_to_module (GkmXdgModule *self, GkmObject *object,
                      const gchar *filename, GkmTransaction *transaction)
{
	g_assert (!g_hash_table_lookup (self->objects_by_path, filename));
	g_hash_table_insert (self->objects_by_path, g_strdup (filename), g_object_ref (object));

	g_assert (!lookup_filename_for_object (object));
	g_object_set_data_full (G_OBJECT (object), "xdg-module-filename",
	                        g_strdup (filename), g_free);

	gkm_object_expose (object, TRUE);

	if (transaction != NULL)
		gkm_transaction_add (transaction, self, complete_add_object, g_object_ref (object));
}

static gboolean
complete_remove_object (GkmTransaction *transaction, GObject *module, gpointer user_data)
{
	GkmXdgModule *self = GKM_XDG_MODULE (module);
	GkmObject *object = GKM_OBJECT (user_data);
	const gchar *filename;

	/* If the transaction failed, revert it */
	if (gkm_transaction_get_failed (transaction)) {
		filename = g_object_get_data (G_OBJECT (object), "xdg-module-filename");
		g_return_val_if_fail (filename, FALSE);
		add_object_to_module (self, object, filename, NULL);
	}

	g_object_unref (object);
	return TRUE;
}

static void
remove_object_from_module (GkmXdgModule *self, GkmObject *object,
                           const gchar *filename, GkmTransaction *transaction)
{
	gkm_object_expose (object, FALSE);

	if (transaction != NULL)
		gkm_transaction_add (transaction, self, complete_remove_object, g_object_ref (object));

	g_assert (g_hash_table_lookup (self->objects_by_path, filename) == object);
	g_hash_table_remove (self->objects_by_path, filename);
}

static void
file_load (GkmFileTracker *tracker, const gchar *path, GkmXdgModule *self)
{
	GkmObject *object;
	GkmManager *manager;
	gboolean added = FALSE;
	GError *error = NULL;
	GBytes *bytes;
	GType type;
	guchar *data;
	gsize n_data;

	g_return_if_fail (path);
	g_return_if_fail (GKM_IS_XDG_MODULE (self));

	manager = gkm_module_get_manager (GKM_MODULE (self));

	/* Already have this object? */
	object = g_hash_table_lookup (self->objects_by_path, path);
	if (object == NULL) {

		/* Figure out what type of object we're dealing with */
		type = type_from_path (path);
		if (type == 0) {
			gkm_debug ("don't know how to load file in key store: %s", path);
			return;
		}

		/* Create a new object for this identifier */
		object = g_object_new (type,
		                       "module", GKM_MODULE (self),
		                       "manager", manager, NULL);
		g_return_if_fail (GKM_IS_SERIALIZABLE (object));
		g_return_if_fail (GKM_SERIALIZABLE_GET_INTERFACE (object)->extension);

		added = TRUE;

	} else {
		g_object_ref (object);
	}

	/* Read the file in */
	if (!g_file_get_contents (path, (gchar**)&data, &n_data, &error)) {
		g_warning ("couldn't read file in key store: %s: %s", path,
		           egg_error_message (error));
		g_object_unref (object);
		g_clear_error (&error);
		return;
	}

	bytes = g_bytes_new_take (data, n_data);

	/* And load the data into it */
	if (gkm_serializable_load (GKM_SERIALIZABLE (object), NULL, bytes)) {
		if (added)
			add_object_to_module (self, object, path, NULL);
		gkm_object_expose (object, TRUE);

	} else {
		g_message ("failed to load file in user store: %s", path);
		if (!added) {
			gkm_object_expose (object, FALSE);
			remove_object_from_module (self, object, path, NULL);
		}
	}

	g_bytes_unref (bytes);
	g_object_unref (object);
}

static void
file_remove (GkmFileTracker *tracker, const gchar *path, GkmXdgModule *self)
{
	GkmObject *object;

	g_return_if_fail (path);
	g_return_if_fail (GKM_IS_XDG_MODULE (self));

	object = g_hash_table_lookup (self->objects_by_path, path);
	if (object != NULL)
		remove_object_from_module (self, object, path, NULL);
}

static gchar*
name_for_subject (gconstpointer data,
                  gsize n_data)
{
	GBytes *subject;
	GNode *asn;
	gchar *name;

	g_assert (data != NULL);

	subject = g_bytes_new (data, n_data);
	asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "Name", subject);
	g_return_val_if_fail (asn != NULL, NULL);
	g_bytes_unref (subject);

	name = egg_dn_read_part (egg_asn1x_node (asn, "rdnSequence", NULL), "CN");
	egg_asn1x_destroy (asn);

	return name;
}

static gchar*
guess_basename_for_object (GkmObject *object)
{
	GkmSerializableIface *serial;
	const gchar *ext;
	gchar *filename;
	gchar *name = NULL;
	guchar *data;
	gsize n_data;

	g_assert (GKM_IS_OBJECT (object));
	g_assert (GKM_IS_SERIALIZABLE (object));

	/* Figure out the extension and prefix */
	serial = GKM_SERIALIZABLE_GET_INTERFACE (object);
	ext = serial->extension;
	g_return_val_if_fail (ext, NULL);

	/* First we try to use the CN of a subject */
	data = gkm_object_get_attribute_data (object, NULL, CKA_SUBJECT, &n_data);
	if (data && n_data)
		name = name_for_subject (data, n_data);
	g_free (data);

	/* Next we try and see if it has a peer (is a trust assertion) */
	if (name == NULL) {
		data = gkm_object_get_attribute_data (object, NULL, CKA_X_PEER, &n_data);
		if (data && n_data)
			name = g_strndup ((gchar *)data, n_data);
		g_free (data);
	}

	/* Next we try hex encoding the ID */
	if (name == NULL) {
		data = gkm_object_get_attribute_data (object, NULL, CKA_ID, &n_data);
		if (data && n_data)
			name = egg_hex_encode (data, n_data);
		g_free (data);
	}

	if (name == NULL)
		name = g_strdup_printf ("object-%08x", ABS (g_random_int ()));

	/* Build up the identifier */
	filename = g_strconcat (name, ext, NULL);
	g_strdelimit (filename, UNWANTED_FILENAME_CHARS, '_');

	g_free (name);
	return filename;
}

/* -----------------------------------------------------------------------------
 * OBJECT
 */

static const CK_SLOT_INFO*
gkm_xdg_module_real_get_slot_info (GkmModule *base)
{
	return &user_module_slot_info;
}

static const CK_TOKEN_INFO*
gkm_xdg_module_real_get_token_info (GkmModule *base)
{
	GkmXdgModule *self = GKM_XDG_MODULE (base);

	/* TODO: Update the info with current info */
	return &self->token_info;
}

static void
gkm_xdg_module_real_parse_argument (GkmModule *base, const gchar *name, const gchar *value)
{
	GkmXdgModule *self = GKM_XDG_MODULE (base);
	if (g_str_equal (name, "directory")) {
		g_free (self->directory);
		self->directory = g_strdup (value);
	}
}

static CK_RV
gkm_xdg_module_real_refresh_token (GkmModule *base)
{
	GkmXdgModule *self = GKM_XDG_MODULE (base);
	gkm_file_tracker_refresh (self->tracker, FALSE);
	return CKR_OK;
}

static void
gkm_xdg_module_real_add_token_object (GkmModule *module, GkmTransaction *transaction,
                                      GkmObject *object)
{
	gchar *filename = NULL;
	GkmXdgModule *self;
	GkmTrust *trust;
	gchar *basename;
	gchar *actual;

	self = GKM_XDG_MODULE (module);

	/* Always serialize the trust object for each assertion */
	if (GKM_XDG_IS_ASSERTION (object)) {
		trust = gkm_assertion_get_trust_object (GKM_ASSERTION (object));
		object = GKM_OBJECT (trust);

		/* If this trust object has already been added, then ignore */
		if (lookup_filename_for_object (object))
			return;
	}

	/* Double check that the object is in fact serializable */
	if (!GKM_IS_SERIALIZABLE (object)) {
		g_message ("can't store object of type '%s' on token", G_OBJECT_TYPE_NAME (object));
		gkm_transaction_fail (transaction, CKR_TEMPLATE_INCONSISTENT);
		return;
	}

	g_return_if_fail (lookup_filename_for_object (object) == NULL);

	basename = guess_basename_for_object (object);
	g_return_if_fail (basename);

	actual = gkm_transaction_unique_file (transaction, self->directory, basename);
	if (!gkm_transaction_get_failed (transaction)) {
		filename = g_build_filename (self->directory, actual, NULL);
		add_object_to_module (self, object, filename, transaction);
		g_free (filename);
	}

	g_free (actual);
	g_free (basename);
}

static void
gkm_xdg_module_real_store_token_object (GkmModule *module, GkmTransaction *transaction,
                                        GkmObject *object)
{
	GkmXdgModule *self = GKM_XDG_MODULE (module);
	const gchar *filename;
	GBytes *bytes;
	GkmTrust *trust;

	/* Always serialize the trust object for each assertion */
	if (GKM_XDG_IS_ASSERTION (object)) {
		trust = gkm_assertion_get_trust_object (GKM_ASSERTION (object));
		object = GKM_OBJECT (trust);
	}

	/* Double check that the object is in fact serializable */
	if (!GKM_IS_SERIALIZABLE (object)) {
		g_message ("can't store object of type '%s' on token", G_OBJECT_TYPE_NAME (object));
		gkm_transaction_fail (transaction, CKR_TEMPLATE_INCONSISTENT);
		return;
	}

	/* Serialize the object in question */
	bytes = gkm_serializable_save (GKM_SERIALIZABLE (object), NULL);

	if (bytes == NULL) {
		gkm_transaction_fail (transaction, CKR_FUNCTION_FAILED);
		g_return_if_reached ();
	}

	filename = lookup_filename_for_object (object);
	g_return_if_fail (filename != NULL);
	g_return_if_fail (g_hash_table_lookup (self->objects_by_path, filename) == object);

	gkm_transaction_write_file (transaction, filename,
	                            g_bytes_get_data (bytes, NULL),
	                            g_bytes_get_size (bytes));
	g_bytes_unref (bytes);
}

static void
gkm_xdg_module_real_remove_token_object (GkmModule *module, GkmTransaction *transaction,
                                         GkmObject *object)
{
	GkmXdgModule *self = GKM_XDG_MODULE (module);
	const gchar *filename;
	GkmXdgTrust *trust;

	/* Always serialize the trust object for each assertion */
	if (GKM_XDG_IS_ASSERTION (object)) {
		trust = GKM_XDG_TRUST (gkm_assertion_get_trust_object (GKM_ASSERTION (object)));
		gkm_xdg_trust_remove_assertion (trust, GKM_ASSERTION (object), transaction);

		/* Remove the trust object if it has no assertions */
		if (!gkm_xdg_trust_have_assertion (trust))
			object = GKM_OBJECT (trust);
		else
			object = NULL;
	}

	if (object && !gkm_transaction_get_failed (transaction)) {
		filename = lookup_filename_for_object (object);
		g_return_if_fail (filename != NULL);
		g_return_if_fail (g_hash_table_lookup (self->objects_by_path, filename) == object);

		gkm_transaction_remove_file (transaction, filename);
		remove_object_from_module (self, object, filename, transaction);
	}
}

static GObject*
gkm_xdg_module_constructor (GType type, guint n_props, GObjectConstructParam *props)
{
	GkmXdgModule *self = GKM_XDG_MODULE (G_OBJECT_CLASS (gkm_xdg_module_parent_class)->constructor(type, n_props, props));
	g_return_val_if_fail (self, NULL);

	if (!self->directory)
		self->directory = g_build_filename (g_get_user_data_dir (), "keystore", NULL);

	self->tracker = gkm_file_tracker_new (self->directory, "*.*", NULL);
	g_signal_connect (self->tracker, "file-added", G_CALLBACK (file_load), self);
	g_signal_connect (self->tracker, "file-changed", G_CALLBACK (file_load), self);
	g_signal_connect (self->tracker, "file-removed", G_CALLBACK (file_remove), self);

	return G_OBJECT (self);
}

static void
gkm_xdg_module_init (GkmXdgModule *self)
{
	self->objects_by_path = g_hash_table_new_full (g_str_hash, g_str_equal,
	                                               g_free, gkm_util_dispose_unref);

	/* Our default token info, updated as module runs */
	memcpy (&self->token_info, &user_module_token_info, sizeof (CK_TOKEN_INFO));

	/* For creating stored objects */
	gkm_module_register_factory (GKM_MODULE (self), GKM_XDG_FACTORY_ASSERTION);
	gkm_module_register_factory (GKM_MODULE (self), GKM_FACTORY_CERTIFICATE);
}

static void
gkm_xdg_module_dispose (GObject *obj)
{
	GkmXdgModule *self = GKM_XDG_MODULE (obj);

	if (self->tracker)
		g_object_unref (self->tracker);
	self->tracker = NULL;

	g_hash_table_remove_all (self->objects_by_path);

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

static void
gkm_xdg_module_finalize (GObject *obj)
{
	GkmXdgModule *self = GKM_XDG_MODULE (obj);

	g_assert (self->tracker == NULL);

	g_hash_table_destroy (self->objects_by_path);
	self->objects_by_path = NULL;

	g_free (self->directory);
	self->directory = NULL;

	G_OBJECT_CLASS (gkm_xdg_module_parent_class)->finalize (obj);
}

static void
gkm_xdg_module_class_init (GkmXdgModuleClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GkmModuleClass *module_class = GKM_MODULE_CLASS (klass);

	gobject_class->constructor = gkm_xdg_module_constructor;
	gobject_class->dispose = gkm_xdg_module_dispose;
	gobject_class->finalize = gkm_xdg_module_finalize;

	module_class->get_slot_info = gkm_xdg_module_real_get_slot_info;
	module_class->get_token_info = gkm_xdg_module_real_get_token_info;
	module_class->parse_argument = gkm_xdg_module_real_parse_argument;
	module_class->refresh_token = gkm_xdg_module_real_refresh_token;
	module_class->add_token_object = gkm_xdg_module_real_add_token_object;
	module_class->store_token_object = gkm_xdg_module_real_store_token_object;
	module_class->remove_token_object = gkm_xdg_module_real_remove_token_object;
}

/* ----------------------------------------------------------------------------
 * PUBLIC
 */

CK_FUNCTION_LIST_PTR
gkm_xdg_store_get_functions (void)
{
	gkm_crypto_initialize ();
	return gkm_xdg_module_function_list;
}

GkmModule*
_gkm_xdg_store_get_module_for_testing (void)
{
	return pkcs11_module;
}