4
* Copyright (C) 2005 Stefan Walter
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
* See the GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the
17
* Free Software Foundation, Inc.,
18
* 59 Temple Place, Suite 330,
19
* Boston, MA 02111-1307, USA.
27
#include <glib/gi18n.h>
29
#include "seahorse-context.h"
30
#include "seahorse-source.h"
31
#include "seahorse-ssh-source.h"
32
#include "seahorse-ssh-key.h"
33
#include "seahorse-ssh-operation.h"
34
#include "seahorse-gtkstock.h"
50
struct _SeahorseSSHKeyPrivate {
55
G_DEFINE_TYPE (SeahorseSSHKey, seahorse_ssh_key, SEAHORSE_TYPE_KEY);
57
/* -----------------------------------------------------------------------------
62
parse_first_word (const gchar *line)
66
#define PARSE_CHARS "\t \n@;,.\\?()[]{}+/"
67
line += strspn (line, PARSE_CHARS);
68
pos = strcspn (line, PARSE_CHARS);
69
return pos == 0 ? NULL : g_strndup (line, pos);
73
changed_key (SeahorseSSHKey *skey)
75
SeahorseKey *key = SEAHORSE_KEY (skey);
76
SeahorseObject *obj = SEAHORSE_OBJECT (skey);
78
g_free (skey->priv->displayname);
79
skey->priv->displayname = NULL;
81
g_free (skey->priv->simplename);
82
skey->priv->simplename = NULL;
86
/* Try to make display and simple names */
87
if (skey->keydata->comment) {
88
skey->priv->displayname = g_strdup (skey->keydata->comment);
89
skey->priv->simplename = parse_first_word (skey->keydata->comment);
91
/* No names when not even the fingerpint loaded */
92
} else if (!skey->keydata->fingerprint) {
93
skey->priv->displayname = g_strdup (_("(Unreadable Secure Shell Key)"));
95
/* No comment, but loaded */
97
skey->priv->displayname = g_strdup (_("Secure Shell Key"));
100
if (skey->priv->simplename == NULL)
101
skey->priv->simplename = g_strdup (_("Secure Shell Key"));
105
/* Now start setting the main SeahorseKey fields */
106
obj->_tag = SEAHORSE_SSH;
109
if (!skey->keydata || !skey->keydata->fingerprint) {
111
obj->_location = SEAHORSE_LOCATION_INVALID;
112
obj->_usage = SEAHORSE_USAGE_NONE;
113
key->loaded = SKEY_INFO_NONE;
114
obj->_flags = SKEY_FLAG_DISABLED;
115
key->keydesc = _("Invalid");
121
obj->_id = seahorse_ssh_key_get_cannonical_id (skey->keydata->fingerprint);
122
key->rawid = skey->keydata->fingerprint;
123
obj->_location = SEAHORSE_LOCATION_LOCAL;
124
key->loaded = SKEY_INFO_COMPLETE;
125
obj->_flags = skey->keydata->authorized ? SKEY_FLAG_TRUSTED : 0;
127
if (skey->keydata->privfile) {
128
obj->_usage = SEAHORSE_USAGE_PRIVATE_KEY;
129
key->keydesc = _("Private Secure Shell Key");
131
obj->_usage = SEAHORSE_USAGE_PUBLIC_KEY;
132
key->keydesc = _("Public Secure Shell Key");
137
obj->_id = g_quark_from_string (SEAHORSE_SSH_STR ":UNKNOWN ");
139
seahorse_object_fire_changed (obj, SKEY_CHANGE_ALL);
143
calc_validity (SeahorseSSHKey *skey)
145
if (skey->keydata->privfile)
146
return SEAHORSE_VALIDITY_ULTIMATE;
151
calc_trust (SeahorseSSHKey *skey)
153
if (skey->keydata->authorized)
154
return SEAHORSE_VALIDITY_FULL;
158
/* -----------------------------------------------------------------------------
163
seahorse_ssh_key_get_num_names (SeahorseKey *key)
169
seahorse_ssh_key_get_name (SeahorseKey *key, guint index)
171
SeahorseSSHKey *skey;
173
g_assert (SEAHORSE_IS_SSH_KEY (key));
174
skey = SEAHORSE_SSH_KEY (key);
176
g_assert (index == 0);
178
return g_strdup (skey->priv->displayname);
182
seahorse_ssh_key_get_name_cn (SeahorseKey *skey, guint index)
184
g_assert (index != 0);
188
static SeahorseValidity
189
seahorse_ssh_key_get_name_validity (SeahorseKey *skey, guint index)
191
g_return_val_if_fail (index == 0, SEAHORSE_VALIDITY_UNKNOWN);
192
return calc_validity (SEAHORSE_SSH_KEY (skey));
196
seahorse_ssh_key_get_property (GObject *object, guint prop_id,
197
GValue *value, GParamSpec *pspec)
199
SeahorseSSHKey *skey = SEAHORSE_SSH_KEY (object);
203
g_value_set_pointer (value, skey->keydata);
205
case PROP_DISPLAY_NAME:
206
g_value_set_string (value, skey->priv->displayname);
208
case PROP_DISPLAY_ID:
209
g_value_set_string (value, seahorse_key_get_short_keyid (SEAHORSE_KEY (skey)));
211
case PROP_SIMPLE_NAME:
212
g_value_set_string (value, skey->priv->simplename);
214
case PROP_FINGERPRINT:
215
g_value_set_string (value, skey->keydata ? skey->keydata->fingerprint : NULL);
218
g_value_set_uint (value, calc_validity (skey));
221
g_value_set_uint (value, calc_trust (skey));
224
g_value_set_ulong (value, 0);
227
g_value_set_uint (value, skey->keydata ? skey->keydata->length : 0);
230
g_value_set_string (value, SEAHORSE_STOCK_KEY_SSH);
236
seahorse_ssh_key_set_property (GObject *object, guint prop_id,
237
const GValue *value, GParamSpec *pspec)
239
SeahorseSSHKey *skey = SEAHORSE_SSH_KEY (object);
240
SeahorseSSHKeyData *keydata;
244
keydata = (SeahorseSSHKeyData*)g_value_get_pointer (value);
245
if (skey->keydata != keydata) {
246
seahorse_ssh_key_data_free (skey->keydata);
247
skey->keydata = keydata;
254
/* Unrefs gpgme key and frees data */
256
seahorse_ssh_key_finalize (GObject *gobject)
258
SeahorseSSHKey *skey = SEAHORSE_SSH_KEY (gobject);
260
g_free (skey->priv->displayname);
261
g_free (skey->priv->simplename);
265
seahorse_ssh_key_data_free (skey->keydata);
267
G_OBJECT_CLASS (seahorse_ssh_key_parent_class)->finalize (gobject);
271
seahorse_ssh_key_init (SeahorseSSHKey *skey)
273
/* init private vars */
274
skey->priv = g_new0 (SeahorseSSHKeyPrivate, 1);
278
seahorse_ssh_key_class_init (SeahorseSSHKeyClass *klass)
280
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
281
SeahorseKeyClass *key_class = SEAHORSE_KEY_CLASS (klass);
283
seahorse_ssh_key_parent_class = g_type_class_peek_parent (klass);
285
gobject_class->finalize = seahorse_ssh_key_finalize;
286
gobject_class->set_property = seahorse_ssh_key_set_property;
287
gobject_class->get_property = seahorse_ssh_key_get_property;
289
key_class->get_num_names = seahorse_ssh_key_get_num_names;
290
key_class->get_name = seahorse_ssh_key_get_name;
291
key_class->get_name_cn = seahorse_ssh_key_get_name_cn;
292
key_class->get_name_validity = seahorse_ssh_key_get_name_validity;
294
g_object_class_install_property (gobject_class, PROP_KEY_DATA,
295
g_param_spec_pointer ("key-data", "SSH Key Data", "SSH key data pointer",
298
g_object_class_install_property (gobject_class, PROP_DISPLAY_NAME,
299
g_param_spec_string ("display-name", "Display Name", "User Displayable name for this key",
300
"", G_PARAM_READABLE));
302
g_object_class_install_property (gobject_class, PROP_DISPLAY_ID,
303
g_param_spec_string ("display-id", "Display ID", "User Displayable id for this key",
304
"", G_PARAM_READABLE));
306
g_object_class_install_property (gobject_class, PROP_SIMPLE_NAME,
307
g_param_spec_string ("simple-name", "Simple Name", "Simple name for this key",
308
"", G_PARAM_READABLE));
310
g_object_class_install_property (gobject_class, PROP_FINGERPRINT,
311
g_param_spec_string ("fingerprint", "Fingerprint", "Unique fingerprint for this key",
312
"", G_PARAM_READABLE));
314
g_object_class_install_property (gobject_class, PROP_VALIDITY,
315
g_param_spec_uint ("validity", "Validity", "Validity of this key",
316
0, G_MAXUINT, 0, G_PARAM_READABLE));
318
g_object_class_install_property (gobject_class, PROP_TRUST,
319
g_param_spec_uint ("trust", "Trust", "Trust in this key",
320
0, G_MAXUINT, 0, G_PARAM_READABLE));
322
g_object_class_install_property (gobject_class, PROP_EXPIRES,
323
g_param_spec_ulong ("expires", "Expires On", "Date this key expires on",
324
0, G_MAXULONG, 0, G_PARAM_READABLE));
326
g_object_class_install_property (gobject_class, PROP_LENGTH,
327
g_param_spec_uint ("length", "Length of", "The length of this key",
328
0, G_MAXUINT, 0, G_PARAM_READABLE));
330
g_object_class_install_property (gobject_class, PROP_STOCK_ID,
331
g_param_spec_string ("stock-id", "The stock icon", "The stock icon id",
332
NULL, G_PARAM_READABLE));
335
/* -----------------------------------------------------------------------------
340
seahorse_ssh_key_new (SeahorseSource *sksrc, SeahorseSSHKeyData *data)
342
SeahorseSSHKey *skey;
343
skey = g_object_new (SEAHORSE_TYPE_SSH_KEY, "key-source", sksrc,
344
"key-data", data, NULL);
349
seahorse_ssh_key_get_algo (SeahorseSSHKey *skey)
351
g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), SSH_ALGO_UNK);
352
return skey->keydata->algo;
356
seahorse_ssh_key_get_algo_str (SeahorseSSHKey *skey)
358
g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), "");
360
switch(skey->keydata->algo) {
368
g_assert_not_reached ();
374
seahorse_ssh_key_get_strength (SeahorseSSHKey *skey)
376
g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), 0);
377
return skey->keydata ? skey->keydata->length : 0;
381
seahorse_ssh_key_get_location (SeahorseSSHKey *skey)
383
g_return_val_if_fail (SEAHORSE_IS_SSH_KEY (skey), NULL);
386
return skey->keydata->privfile ?
387
skey->keydata->privfile : skey->keydata->pubfile;
391
seahorse_ssh_key_get_cannonical_id (const gchar *id)
393
#define SSH_ID_SIZE 16
394
gchar *hex, *canonical_id = g_malloc0 (SSH_ID_SIZE + 1);
395
gint i, off, len = strlen (id);
398
/* Strip out all non alpha numeric chars and limit length to SSH_ID_SIZE */
399
for (i = len, off = SSH_ID_SIZE; i >= 0 && off > 0; --i) {
400
if (g_ascii_isalnum (id[i]))
401
canonical_id[--off] = g_ascii_toupper (id[i]);
404
/* Not enough characters */
405
g_return_val_if_fail (off == 0, 0);
407
hex = g_strdup_printf ("%s:%s", SEAHORSE_SSH_STR, canonical_id);
408
ret = g_quark_from_string (hex);
410
g_free (canonical_id);