summaryrefslogtreecommitdiff
path: root/gcr/gcr-gnupg-key.c
blob: 908261a5875d5c013d6e98659579757f4c2a27af (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
/*
 * Copyright (C) 2011 Collabora Ltd.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Author: Stef Walter <stefw@collabora.co.uk>
 */

#include "config.h"

#include "gcr-gnupg-key.h"
#include "gcr-gnupg-records.h"
#include "gcr-record.h"
#include "gcr-memory-icon.h"

#include "gck/gck.h"

#include <glib/gi18n-lib.h>

enum {
	PROP_0,
	PROP_KEYID,
	PROP_PUBLIC_RECORDS,
	PROP_SECRET_RECORDS,
	PROP_LABEL,
	PROP_MARKUP,
	PROP_DESCRIPTION,
	PROP_SHORT_KEYID,
	PROP_ICON
};

struct _GcrGnupgKeyPrivate {
	GPtrArray *public_records;
	GPtrArray *secret_records;
	GIcon *icon;
};

G_DEFINE_TYPE (GcrGnupgKey, _gcr_gnupg_key, G_TYPE_OBJECT);

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

/* Copied from GPGME */
static void
parse_user_id (const gchar *uid, gchar **name, gchar **email, gchar **comment)
{
	gchar *src, *tail, *x;
	int in_name = 0;
	int in_email = 0;
	int in_comment = 0;

	*name = NULL;
	*email = NULL;
	*comment = NULL;

	x = tail = src = g_strdup (uid);

	while (*src) {
		if (in_email) {
			/* Not legal but anyway.  */
			if (*src == '<')
				in_email++;
			else if (*src == '>') {
				if (!--in_email && !*email) {
					*email = tail;
					*src = 0;
					tail = src + 1;
				}
			}
		} else if (in_comment) {
			if (*src == '(')
				in_comment++;
			else if (*src == ')') {
				if (!--in_comment && !*comment) {
					*comment = tail;
					*src = 0;
					tail = src + 1;
				}
			}
		} else if (*src == '<') {
			if (in_name) {
				if (!*name) {
					*name = tail;
					*src = 0;
					tail = src + 1;
				}
				in_name = 0;
			} else
				tail = src + 1;

			in_email = 1;
		} else if (*src == '(') {
			if (in_name) {
				if (!*name) {
					*name = tail;
					*src = 0;
					tail = src + 1;
				}
				in_name = 0;
			}
			in_comment = 1;
		} else if (!in_name && *src != ' ' && *src != '\t') {
			in_name = 1;
		}
		src++;
	}

	if (in_name) {
		if (!*name) {
			*name = tail;
			*src = 0;
			tail = src + 1;
		}
	}

	/* Let unused parts point to an EOS.  */
	*name = g_strdup (*name ? *name : "");
	*email = g_strdup (*email ? *email : "");
	*comment = g_strdup (*comment ? *comment : "");

	g_strstrip (*name);
	g_strstrip (*email);
	g_strstrip (*comment);

	g_free (x);
}

static gchar *
calculate_name (GcrGnupgKey *self)
{
	GcrRecord* record;

	record = _gcr_records_find (self->pv->public_records, GCR_RECORD_SCHEMA_UID);
	g_return_val_if_fail (record, NULL);

	return _gcr_record_get_string (record, GCR_RECORD_UID_USERID);
}

static gchar *
calculate_markup (GcrGnupgKey *self)
{
	gchar *markup = NULL;
	gchar *uid, *name, *email, *comment;

	uid = calculate_name (self);
	if (uid == NULL)
		return NULL;

	parse_user_id (uid, &name, &email, &comment);
	if (comment != NULL && comment[0] != '\0')
		markup = g_markup_printf_escaped ("%s\n<small>%s \'%s\'</small>", name, email, comment);
	else
		markup = g_markup_printf_escaped ("%s\n<small>%s</small>", name, email);
	g_free (name);
	g_free (email);
	g_free (comment);
	g_free (uid);

	return markup;
}

static void
_gcr_gnupg_key_init (GcrGnupgKey *self)
{
	self->pv = (G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_GNUPG_KEY, GcrGnupgKeyPrivate));
}

static void
_gcr_gnupg_key_finalize (GObject *obj)
{
	GcrGnupgKey *self = GCR_GNUPG_KEY (obj);

	if (self->pv->public_records)
		g_ptr_array_unref (self->pv->public_records);
	if (self->pv->secret_records)
		g_ptr_array_unref (self->pv->secret_records);

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

static void
_gcr_gnupg_key_set_property (GObject *obj, guint prop_id, const GValue *value,
                             GParamSpec *pspec)
{
	GcrGnupgKey *self = GCR_GNUPG_KEY (obj);

	switch (prop_id) {
	case PROP_PUBLIC_RECORDS:
		_gcr_gnupg_key_set_public_records (self, g_value_get_boxed (value));
		break;
	case PROP_SECRET_RECORDS:
		_gcr_gnupg_key_set_secret_records (self, g_value_get_boxed (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
_gcr_gnupg_key_get_property (GObject *obj, guint prop_id, GValue *value,
                             GParamSpec *pspec)
{
	GcrGnupgKey *self = GCR_GNUPG_KEY (obj);

	switch (prop_id) {
	case PROP_PUBLIC_RECORDS:
		g_value_set_boxed (value, self->pv->public_records);
		break;
	case PROP_SECRET_RECORDS:
		g_value_set_boxed (value, self->pv->secret_records);
		break;
	case PROP_KEYID:
		g_value_set_string (value, _gcr_gnupg_key_get_keyid (self));
		break;
	case PROP_LABEL:
		g_value_take_string (value, calculate_name (self));
		break;
	case PROP_DESCRIPTION:
		g_value_set_string (value, _("PGP Key"));
		break;
	case PROP_MARKUP:
		g_value_take_string (value, calculate_markup (self));
		break;
	case PROP_SHORT_KEYID:
		g_value_set_string (value, _gcr_gnupg_records_get_short_keyid (self->pv->public_records));
		break;
	case PROP_ICON:
		g_value_set_object (value, _gcr_gnupg_key_get_icon (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
_gcr_gnupg_key_class_init (GcrGnupgKeyClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	_gcr_gnupg_key_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GcrGnupgKeyPrivate));

	gobject_class->finalize = _gcr_gnupg_key_finalize;
	gobject_class->set_property = _gcr_gnupg_key_set_property;
	gobject_class->get_property = _gcr_gnupg_key_get_property;

	/**
	 * GcrGnupgKey:public-records:
	 *
	 * Public key data. Should always be present.
	 */
	g_object_class_install_property (gobject_class, PROP_PUBLIC_RECORDS,
	         g_param_spec_boxed ("public-records", "Public Records", "Public Key Colon Records",
	                             G_TYPE_PTR_ARRAY, G_PARAM_READWRITE));

	/**
	 * GcrGnupgKey:secret-records:
	 *
	 * Secret key data. The keyid of this data must match public-dataset.
	 * If present, this key represents a secret key.
	 */
	g_object_class_install_property (gobject_class, PROP_SECRET_RECORDS,
	         g_param_spec_boxed ("secret-records", "Secret Records", "Secret Key Colon Records",
	                             G_TYPE_PTR_ARRAY, G_PARAM_READWRITE));

	/**
	 * GcrGnupgKey:keyid:
	 *
	 * Key identifier.
	 */
	g_object_class_install_property (gobject_class, PROP_KEYID,
	         g_param_spec_string ("keyid", "Key ID", "Key identifier",
	                              "", G_PARAM_READABLE));

	/**
	 * GcrGnupgKey:label:
	 *
	 * User readable label for this key.
	 */
	g_object_class_install_property (gobject_class, PROP_LABEL,
	         g_param_spec_string ("label", "Label", "Key label",
	                              "", G_PARAM_READABLE));

	/**
	 * GcrGnupgKey::description:
	 *
	 * Description of type of key.
	 */
	g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
	         g_param_spec_string ("description", "Description", "Description of object type",
	                              "", G_PARAM_READABLE));

	/**
	 * GcrGnupgKey:markup:
	 *
	 * User readable markup which contains key label.
	 */
	g_object_class_install_property (gobject_class, PROP_MARKUP,
	         g_param_spec_string ("markup", "Markup", "Markup which describes key",
	                              "", G_PARAM_READABLE));

	/**
	 * GcrGnupgKey:short-keyid:
	 *
	 * User readable key identifier.
	 */
	g_object_class_install_property (gobject_class, PROP_SHORT_KEYID,
	         g_param_spec_string ("short-keyid", "Short Key ID", "Display key identifier",
	                              "", G_PARAM_READABLE));

	/**
	 * GcrGnupgKey:icon:
	 *
	 * Icon for this key.
	 */
	g_object_class_install_property (gobject_class, PROP_ICON,
	         g_param_spec_object ("icon", "Icon", "Icon for this key",
	                              G_TYPE_ICON, G_PARAM_READABLE));
}

/**
 * _gcr_gnupg_key_new:
 * @pubset: array of GcrRecord* representing public part of key
 * @secset: (allow-none): array of GcrRecord* representing secret part of key.
 *
 * Create a new GcrGnupgKey for the record data passed. If the secret part
 * of the key is set, then this represents a secret key; otherwise it represents
 * a public key.
 *
 * Returns: (transfer full): A newly allocated key.
 */
GcrGnupgKey*
_gcr_gnupg_key_new (GPtrArray *pubset, GPtrArray *secset)
{
	g_return_val_if_fail (pubset, NULL);
	return g_object_new (GCR_TYPE_GNUPG_KEY,
	                     "public-records", pubset,
	                     "secret-records", secset,
	                     NULL);
}

/**
 * _gcr_gnupg_key_get_public_records:
 * @self: The key
 *
 * Get the record data this key is based on.
 *
 * Returns: (transfer none): An array of GcrRecord*.
 */
GPtrArray*
_gcr_gnupg_key_get_public_records (GcrGnupgKey *self)
{
	g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
	return self->pv->public_records;
}

/**
 * _gcr_gnupg_key_set_public_records:
 * @self: The key
 * @records: The new array of GcrRecord*
 *
 * Change the record data that this key is based on.
 */
void
_gcr_gnupg_key_set_public_records (GcrGnupgKey *self, GPtrArray *records)
{
	GObject *obj;

	g_return_if_fail (GCR_IS_GNUPG_KEY (self));
	g_return_if_fail (records);

	/* Check that it matches previous */
	if (self->pv->public_records) {
		const gchar *old_keyid = _gcr_gnupg_records_get_keyid (self->pv->public_records);
		const gchar *new_keyid = _gcr_gnupg_records_get_keyid (records);

		if (g_strcmp0 (old_keyid, new_keyid) != 0) {
			g_warning ("it is an error to change a gnupg key so that the "
			           "fingerprint is no longer the same: %s != %s",
			           old_keyid, new_keyid);
			return;
		}
	}

	g_ptr_array_ref (records);
	if (self->pv->public_records)
		g_ptr_array_unref (self->pv->public_records);
	self->pv->public_records = records;

	obj = G_OBJECT (self);
	g_object_freeze_notify (obj);
	g_object_notify (obj, "public-records");
	g_object_notify (obj, "label");
	g_object_notify (obj, "markup");
	g_object_thaw_notify (obj);
}

/**
 * _gcr_gnupg_key_get_secret_records:
 * @self: The key
 *
 * Get the record secret data this key is based on. %NULL if a public key.
 *
 * Returns: (transfer none) (allow-none): An array of GcrColons*.
 */
GPtrArray*
_gcr_gnupg_key_get_secret_records (GcrGnupgKey *self)
{
	g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
	return self->pv->secret_records;
}

/**
 * _gcr_gnupg_key_set_secret_records:
 * @self: The key
 * @records: (allow-none): The new array of GcrRecord*
 *
 * Set the secret data for this key. %NULL if public key.
 */
void
_gcr_gnupg_key_set_secret_records (GcrGnupgKey *self, GPtrArray *records)
{
	GObject *obj;

	g_return_if_fail (GCR_IS_GNUPG_KEY (self));

	/* Check that it matches public key */
	if (self->pv->public_records && records) {
		const gchar *pub_keyid = _gcr_gnupg_records_get_keyid (self->pv->public_records);
		const gchar *sec_keyid = _gcr_gnupg_records_get_keyid (records);

		if (g_strcmp0 (pub_keyid, sec_keyid) != 0) {
			g_warning ("it is an error to create a gnupg key so that the "
			           "fingerprint of thet pub and sec parts are not the same: %s != %s",
			           pub_keyid, sec_keyid);
			return;
		}
	}

	if (records)
		g_ptr_array_ref (records);
	if (self->pv->secret_records)
		g_ptr_array_unref (self->pv->secret_records);
	self->pv->secret_records = records;

	obj = G_OBJECT (self);
	g_object_freeze_notify (obj);
	g_object_notify (obj, "secret-records");
	g_object_thaw_notify (obj);
}

/**
 * _gcr_gnupg_key_get_keyid:
 * @self: The key
 *
 * Get the keyid for this key.
 *
 * Returns: (transfer none): The keyid.
 */
const gchar*
_gcr_gnupg_key_get_keyid (GcrGnupgKey *self)
{
	g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
	return _gcr_gnupg_records_get_keyid (self->pv->public_records);
}

/**
 * _gcr_gnupg_key_get_icon:
 * @self: A gnupg key.
 *
 * Get the display icon for this key.
 *
 * Return value: (transfer none): The icon, owned by the key.
 */
GIcon*
_gcr_gnupg_key_get_icon (GcrGnupgKey *self)
{
	g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);

	if (self->pv->icon == NULL) {
		self->pv->icon = _gcr_gnupg_records_get_icon (self->pv->public_records);
		if (self->pv->icon == NULL) {
			if (self->pv->secret_records)
				self->pv->icon = g_themed_icon_new ("gcr-key-pair");
			else
				self->pv->icon = g_themed_icon_new ("gcr-key");
		}
	}

	return self->pv->icon;
}

/**
 * _gcr_gnupg_key_get_columns:
 *
 * Get the columns that we should display for gnupg keys.
 *
 * Returns: (transfer none): The columns, NULL terminated, should not be freed.
 */
const GcrColumn*
_gcr_gnupg_key_get_columns (void)
{
	static GcrColumn columns[] = {
		{ "icon", /* later */ 0, /* later */ 0, NULL, 0, NULL, 0 },
		{ "label", G_TYPE_STRING, G_TYPE_STRING, NC_("column", "Name"),
		  GCR_COLUMN_SORTABLE, NULL, 0 },
		{ "short-keyid", G_TYPE_STRING, G_TYPE_STRING, NC_("column", "Key ID"),
		  GCR_COLUMN_SORTABLE, NULL, 0 },
		{ NULL }
	};

	columns[0].property_type = columns[0].column_type = G_TYPE_ICON;
	return columns;
}