| 1 |
/* |
| 2 |
* gnome-keyring |
| 3 |
* |
| 4 |
* Copyright (C) 2009 Stefan Walter |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see |
| 18 |
* <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gkm-secret-binary.h" |
| 24 |
#include "gkm-secret-collection.h" |
| 25 |
#include "gkm-secret-data.h" |
| 26 |
#include "gkm-secret-item.h" |
| 27 |
#include "gkm-secret-textual.h" |
| 28 |
|
| 29 |
#include "egg/egg-error.h" |
| 30 |
|
| 31 |
#include "gkm/gkm-attributes.h" |
| 32 |
#include "gkm/gkm-credential.h" |
| 33 |
#include "gkm/gkm-secret.h" |
| 34 |
#include "gkm/gkm-session.h" |
| 35 |
#include "gkm/gkm-transaction.h" |
| 36 |
|
| 37 |
#include <glib/gi18n.h> |
| 38 |
|
| 39 |
#include "pkcs11/pkcs11i.h" |
| 40 |
|
| 41 |
enum { |
| 42 |
PROP_0, |
| 43 |
PROP_FILENAME |
| 44 |
}; |
| 45 |
|
| 46 |
struct _GkmSecretCollection { |
| 47 |
GkmSecretObject parent; |
| 48 |
GkmSecretData *sdata; |
| 49 |
GHashTable *items; |
| 50 |
gchar *filename; |
| 51 |
guint32 watermark; |
| 52 |
GArray *template; |
| 53 |
}; |
| 54 |
|
| 55 |
G_DEFINE_TYPE (GkmSecretCollection, gkm_secret_collection, GKM_TYPE_SECRET_OBJECT); |
| 56 |
|
| 57 |
/* Forward declarations */ |
| 58 |
static void add_item (GkmSecretCollection *, GkmTransaction *, GkmSecretItem *); |
| 59 |
static void remove_item (GkmSecretCollection *, GkmTransaction *, GkmSecretItem *); |
| 60 |
|
| 61 |
/* ----------------------------------------------------------------------------- |
| 62 |
* INTERNAL |
| 63 |
*/ |
| 64 |
|
| 65 |
static GkmDataResult |
| 66 |
load_collection_and_secret_data (GkmSecretCollection *self, GkmSecretData *sdata, |
| 67 |
const gchar *path) |
| 68 |
{ |
| 69 |
GkmDataResult res; |
| 70 |
GError *error = NULL; |
| 71 |
guchar *data; |
| 72 |
gsize n_data; |
| 73 |
|
| 74 |
/* Read in the keyring */ |
| 75 |
if (!g_file_get_contents (path, (gchar**)&data, &n_data, &error)) { |
| 76 |
g_message ("problem reading keyring: %s: %s", |
| 77 |
path, egg_error_message (error)); |
| 78 |
g_clear_error (&error); |
| 79 |
return GKM_DATA_FAILURE; |
| 80 |
} |
| 81 |
|
| 82 |
/* Try to load from an encrypted file, and otherwise plain text */ |
| 83 |
res = gkm_secret_binary_read (self, sdata, data, n_data); |
| 84 |
if (res == GKM_DATA_UNRECOGNIZED) |
| 85 |
res = gkm_secret_textual_read (self, sdata, data, n_data); |
| 86 |
|
| 87 |
g_free (data); |
| 88 |
|
| 89 |
return res; |
| 90 |
} |
| 91 |
|
| 92 |
static GkmCredential* |
| 93 |
lookup_unassociated_credential (GkmSession *session, CK_OBJECT_HANDLE handle) |
| 94 |
{ |
| 95 |
GkmObject *object; |
| 96 |
|
| 97 |
if (gkm_session_lookup_readable_object (session, handle, &object) != CKR_OK) |
| 98 |
return NULL; |
| 99 |
|
| 100 |
if (gkm_credential_get_object (GKM_CREDENTIAL (object)) != NULL) |
| 101 |
return NULL; |
| 102 |
|
| 103 |
return GKM_CREDENTIAL (object); |
| 104 |
} |
| 105 |
|
| 106 |
static gboolean |
| 107 |
find_unlocked_credential (GkmCredential *cred, GkmObject *object, gpointer user_data) |
| 108 |
{ |
| 109 |
CK_OBJECT_HANDLE *result = user_data; |
| 110 |
|
| 111 |
g_return_val_if_fail (!*result, FALSE); |
| 112 |
|
| 113 |
if (gkm_credential_peek_data (cred, GKM_TYPE_SECRET_DATA)) { |
| 114 |
*result = gkm_object_get_handle (GKM_OBJECT (cred)); |
| 115 |
return TRUE; |
| 116 |
} |
| 117 |
|
| 118 |
return FALSE; |
| 119 |
} |
| 120 |
|
| 121 |
static gboolean |
| 122 |
find_unlocked_secret_data (GkmCredential *cred, GkmObject *object, gpointer user_data) |
| 123 |
{ |
| 124 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (object); |
| 125 |
GkmSecretData **result = user_data; |
| 126 |
|
| 127 |
g_return_val_if_fail (!*result, FALSE); |
| 128 |
|
| 129 |
*result = gkm_credential_pop_data (cred, GKM_TYPE_SECRET_DATA); |
| 130 |
if (*result) { |
| 131 |
g_return_val_if_fail (*result == self->sdata, FALSE); |
| 132 |
return TRUE; |
| 133 |
} |
| 134 |
|
| 135 |
return FALSE; |
| 136 |
} |
| 137 |
|
| 138 |
static void |
| 139 |
track_secret_data (GkmSecretCollection *self, GkmSecretData *data) |
| 140 |
{ |
| 141 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 142 |
|
| 143 |
if (self->sdata) |
| 144 |
g_object_remove_weak_pointer (G_OBJECT (self->sdata), |
| 145 |
(gpointer*)&(self->sdata)); |
| 146 |
self->sdata = data; |
| 147 |
if (self->sdata) |
| 148 |
g_object_add_weak_pointer (G_OBJECT (self->sdata), |
| 149 |
(gpointer*)&(self->sdata)); |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
each_value_to_list (gpointer key, gpointer value, gpointer user_data) |
| 154 |
{ |
| 155 |
GList **list = user_data; |
| 156 |
*list = g_list_prepend (*list, value); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
expose_each_item (gpointer key, gpointer value, gpointer user_data) |
| 161 |
{ |
| 162 |
gboolean expose = GPOINTER_TO_INT (user_data); |
| 163 |
gkm_object_expose (value, expose); |
| 164 |
} |
| 165 |
|
| 166 |
static gboolean |
| 167 |
complete_add (GkmTransaction *transaction, GkmSecretCollection *self, GkmSecretItem *item) |
| 168 |
{ |
| 169 |
if (gkm_transaction_get_failed (transaction)) |
| 170 |
remove_item (self, NULL, item); |
| 171 |
g_object_unref (item); |
| 172 |
return TRUE; |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
add_item (GkmSecretCollection *self, GkmTransaction *transaction, GkmSecretItem *item) |
| 177 |
{ |
| 178 |
const gchar *identifier; |
| 179 |
guint32 number; |
| 180 |
|
| 181 |
g_assert (GKM_IS_SECRET_COLLECTION (self)); |
| 182 |
g_assert (GKM_IS_SECRET_ITEM (item)); |
| 183 |
|
| 184 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)); |
| 185 |
g_return_if_fail (identifier); |
| 186 |
|
| 187 |
/* Make note of the highest numeric identifier, for later use */ |
| 188 |
number = strtoul (identifier, NULL, 10); |
| 189 |
if (number > self->watermark) |
| 190 |
self->watermark = number; |
| 191 |
|
| 192 |
g_hash_table_replace (self->items, g_strdup (identifier), g_object_ref (item)); |
| 193 |
|
| 194 |
if (gkm_object_is_exposed (GKM_OBJECT (self))) |
| 195 |
gkm_object_expose_full (GKM_OBJECT (item), transaction, TRUE); |
| 196 |
if (transaction) |
| 197 |
gkm_transaction_add (transaction, self, (GkmTransactionFunc)complete_add, |
| 198 |
g_object_ref (item)); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
static gboolean |
| 203 |
complete_remove (GkmTransaction *transaction, GkmSecretCollection *self, GkmSecretItem *item) |
| 204 |
{ |
| 205 |
if (gkm_transaction_get_failed (transaction)) |
| 206 |
add_item (self, NULL, item); |
| 207 |
g_object_unref (item); |
| 208 |
return TRUE; |
| 209 |
} |
| 210 |
|
| 211 |
static void |
| 212 |
remove_item (GkmSecretCollection *self, GkmTransaction *transaction, GkmSecretItem *item) |
| 213 |
{ |
| 214 |
const gchar *identifier; |
| 215 |
|
| 216 |
g_assert (GKM_IS_SECRET_COLLECTION (self)); |
| 217 |
g_assert (GKM_IS_SECRET_ITEM (item)); |
| 218 |
|
| 219 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)); |
| 220 |
g_return_if_fail (identifier); |
| 221 |
|
| 222 |
g_object_ref (item); |
| 223 |
|
| 224 |
g_hash_table_remove (self->items, identifier); |
| 225 |
|
| 226 |
gkm_object_expose_full (GKM_OBJECT (item), transaction, FALSE); |
| 227 |
if (transaction) |
| 228 |
gkm_transaction_add (transaction, self, (GkmTransactionFunc)complete_remove, |
| 229 |
g_object_ref (item)); |
| 230 |
|
| 231 |
g_object_unref (item); |
| 232 |
} |
| 233 |
|
| 234 |
static GkmObject* |
| 235 |
factory_create_collection (GkmSession *session, GkmTransaction *transaction, |
| 236 |
CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs) |
| 237 |
{ |
| 238 |
GkmSecretCollection *collection = NULL; |
| 239 |
CK_OBJECT_HANDLE handle; |
| 240 |
CK_ATTRIBUTE *attr; |
| 241 |
GkmManager *manager; |
| 242 |
GkmModule *module; |
| 243 |
gchar *identifier = NULL; |
| 244 |
GkmSecretData *sdata; |
| 245 |
gchar *label = NULL; |
| 246 |
GkmCredential *cred; |
| 247 |
gboolean is_token; |
| 248 |
CK_RV rv; |
| 249 |
|
| 250 |
g_return_val_if_fail (GKM_IS_TRANSACTION (transaction), NULL); |
| 251 |
g_return_val_if_fail (attrs || !n_attrs, NULL); |
| 252 |
|
| 253 |
manager = gkm_manager_for_template (attrs, n_attrs, session); |
| 254 |
module = gkm_session_get_module (session); |
| 255 |
|
| 256 |
/* Must have a credential, which is not associated with an object yet */ |
| 257 |
if (!gkm_attributes_find_ulong (attrs, n_attrs, CKA_G_CREDENTIAL, &handle)) { |
| 258 |
gkm_transaction_fail (transaction, CKR_TEMPLATE_INCOMPLETE); |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
|
| 262 |
cred = lookup_unassociated_credential (session, handle); |
| 263 |
if (cred == NULL) { |
| 264 |
gkm_transaction_fail (transaction, CKR_ATTRIBUTE_VALUE_INVALID); |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
|
| 268 |
/* The identifier */ |
| 269 |
attr = gkm_attributes_find (attrs, n_attrs, CKA_ID); |
| 270 |
if (attr != NULL) { |
| 271 |
gkm_attribute_consume (attr); |
| 272 |
rv = gkm_attribute_get_string (attr, &identifier); |
| 273 |
if (rv != CKR_OK) { |
| 274 |
gkm_transaction_fail (transaction, rv); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
/* Try to find the collection with that identifier */ |
| 279 |
if (!gkm_attributes_find_boolean (attrs, n_attrs, CKA_TOKEN, &is_token)) |
| 280 |
collection = gkm_secret_collection_find (session, attr, |
| 281 |
gkm_module_get_manager (module), |
| 282 |
gkm_session_get_manager (session), NULL); |
| 283 |
else if (is_token) |
| 284 |
collection = gkm_secret_collection_find (session, attr, |
| 285 |
gkm_module_get_manager (module), NULL); |
| 286 |
else |
| 287 |
collection = gkm_secret_collection_find (session, attr, |
| 288 |
gkm_session_get_manager (session), NULL); |
| 289 |
|
| 290 |
/* Already have one with this identifier? Just return that */ |
| 291 |
if (collection != NULL) { |
| 292 |
gkm_session_complete_object_creation (session, transaction, GKM_OBJECT (collection), |
| 293 |
FALSE, attrs, n_attrs); |
| 294 |
return GKM_OBJECT (g_object_ref (collection)); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/* The label */ |
| 299 |
attr = gkm_attributes_find (attrs, n_attrs, CKA_LABEL); |
| 300 |
if (attr != NULL) { |
| 301 |
gkm_attribute_consume (attr); |
| 302 |
rv = gkm_attribute_get_string (attr, &label); |
| 303 |
if (rv != CKR_OK) { |
| 304 |
gkm_transaction_fail (transaction, rv); |
| 305 |
return NULL; |
| 306 |
} |
| 307 |
|
| 308 |
/* No identifier? Use label */ |
| 309 |
if (identifier == NULL) |
| 310 |
identifier = g_strdup (label); |
| 311 |
} |
| 312 |
|
| 313 |
if (!identifier || !identifier[0]) { |
| 314 |
g_free (identifier); |
| 315 |
identifier = g_strdup ("unnamed"); |
| 316 |
} |
| 317 |
|
| 318 |
if (!label || !label[0]) { |
| 319 |
g_free (label); |
| 320 |
if (identifier) { |
| 321 |
label = g_strdup (identifier); |
| 322 |
} else { |
| 323 |
/* TRANSLATORS: This is the label for an keyring created without a label */ |
| 324 |
label = g_strdup (_("Unnamed")); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
g_strdelimit (identifier, ":/\\<>|\t\n\r\v ", '_'); |
| 329 |
collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, |
| 330 |
"module", gkm_session_get_module (session), |
| 331 |
"identifier", identifier, |
| 332 |
"manager", manager, |
| 333 |
"label", label, |
| 334 |
NULL); |
| 335 |
|
| 336 |
gkm_secret_object_mark_created (GKM_SECRET_OBJECT (collection)); |
| 337 |
g_free (identifier); |
| 338 |
g_free (label); |
| 339 |
|
| 340 |
gkm_credential_connect (cred, GKM_OBJECT (collection)); |
| 341 |
sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 342 |
gkm_credential_set_data (cred, GKM_TYPE_SECRET_DATA, sdata); |
| 343 |
gkm_secret_data_set_master (sdata, gkm_credential_get_secret (cred)); |
| 344 |
track_secret_data (collection, sdata); |
| 345 |
g_object_unref (sdata); |
| 346 |
|
| 347 |
gkm_attributes_consume (attrs, n_attrs, CKA_G_CREDENTIAL, G_MAXULONG); |
| 348 |
gkm_session_complete_object_creation (session, transaction, GKM_OBJECT (collection), |
| 349 |
TRUE, attrs, n_attrs); |
| 350 |
return GKM_OBJECT (collection); |
| 351 |
} |
| 352 |
|
| 353 |
static gboolean |
| 354 |
complete_master_password (GkmTransaction *transaction, GObject *object, gpointer user_data) |
| 355 |
{ |
| 356 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (object); |
| 357 |
GkmSecret *previous = user_data; |
| 358 |
|
| 359 |
if (gkm_transaction_get_failed (transaction)) { |
| 360 |
if (self->sdata) |
| 361 |
gkm_secret_data_set_master (self->sdata, previous); |
| 362 |
} |
| 363 |
|
| 364 |
if (previous) |
| 365 |
g_object_unref (previous); |
| 366 |
|
| 367 |
return TRUE; |
| 368 |
} |
| 369 |
|
| 370 |
static void |
| 371 |
change_master_password (GkmSecretCollection *self, GkmTransaction *transaction, |
| 372 |
GkmCredential *cred) |
| 373 |
{ |
| 374 |
GkmSecret *previous; |
| 375 |
|
| 376 |
g_assert (GKM_IS_SECRET_COLLECTION (self)); |
| 377 |
g_assert (GKM_IS_TRANSACTION (transaction)); |
| 378 |
g_assert (GKM_IS_CREDENTIAL (cred)); |
| 379 |
|
| 380 |
if (!self->sdata) { |
| 381 |
gkm_transaction_fail (transaction, CKR_USER_NOT_LOGGED_IN); |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
previous = gkm_secret_data_get_master (self->sdata); |
| 386 |
if (previous != NULL) |
| 387 |
g_object_ref (previous); |
| 388 |
|
| 389 |
gkm_credential_connect (cred, GKM_OBJECT (self)); |
| 390 |
gkm_credential_set_data (cred, GKM_TYPE_SECRET_DATA, self->sdata); |
| 391 |
gkm_secret_data_set_master (self->sdata, gkm_credential_get_secret (cred)); |
| 392 |
|
| 393 |
gkm_transaction_add (transaction, self, complete_master_password, previous); |
| 394 |
} |
| 395 |
|
| 396 |
/* ----------------------------------------------------------------------------- |
| 397 |
* OBJECT |
| 398 |
*/ |
| 399 |
|
| 400 |
static CK_RV |
| 401 |
gkm_secret_collection_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE_PTR attr) |
| 402 |
{ |
| 403 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (base); |
| 404 |
const gchar *identifier; |
| 405 |
GkmSecret *master; |
| 406 |
|
| 407 |
switch (attr->type) { |
| 408 |
case CKA_CLASS: |
| 409 |
return gkm_attribute_set_ulong (attr, CKO_G_COLLECTION); |
| 410 |
case CKA_G_CREDENTIAL_TEMPLATE: |
| 411 |
return gkm_attribute_set_template (attr, self->template); |
| 412 |
case CKA_G_LOGIN_COLLECTION: |
| 413 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (base)); |
| 414 |
g_return_val_if_fail (identifier, CKR_GENERAL_ERROR); |
| 415 |
return gkm_attribute_set_bool (attr, g_str_equal (identifier, "login")); |
| 416 |
case CKA_TRUSTED: |
| 417 |
if (!self->sdata) |
| 418 |
return gkm_attribute_set_bool (attr, CK_FALSE); |
| 419 |
master = gkm_secret_data_get_master (self->sdata); |
| 420 |
return gkm_attribute_set_bool (attr, (master && !gkm_secret_is_trivially_weak (master))); |
| 421 |
} |
| 422 |
return GKM_OBJECT_CLASS (gkm_secret_collection_parent_class)->get_attribute (base, session, attr); |
| 423 |
} |
| 424 |
|
| 425 |
static void |
| 426 |
gkm_secret_collection_set_attribute (GkmObject *object, GkmSession *session, |
| 427 |
GkmTransaction *transaction, CK_ATTRIBUTE *attr) |
| 428 |
{ |
| 429 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (object); |
| 430 |
CK_OBJECT_HANDLE handle = 0; |
| 431 |
GkmCredential *cred; |
| 432 |
GArray *template; |
| 433 |
CK_RV rv; |
| 434 |
|
| 435 |
switch (attr->type) { |
| 436 |
case CKA_G_CREDENTIAL: |
| 437 |
gkm_credential_for_each (session, GKM_OBJECT (self), |
| 438 |
find_unlocked_credential, &handle); |
| 439 |
if (handle == 0) { |
| 440 |
gkm_transaction_fail (transaction, CKR_USER_NOT_LOGGED_IN); |
| 441 |
return; |
| 442 |
} |
| 443 |
rv = gkm_attribute_get_ulong (attr, &handle); |
| 444 |
if (rv != CKR_OK) { |
| 445 |
gkm_transaction_fail (transaction, rv); |
| 446 |
return; |
| 447 |
} |
| 448 |
cred = lookup_unassociated_credential (session, handle); |
| 449 |
if (cred == NULL) { |
| 450 |
gkm_transaction_fail (transaction, CKR_ATTRIBUTE_VALUE_INVALID); |
| 451 |
return; |
| 452 |
} |
| 453 |
change_master_password (self, transaction, cred); |
| 454 |
return; |
| 455 |
case CKA_G_CREDENTIAL_TEMPLATE: |
| 456 |
rv = gkm_attribute_get_template (attr, &template); |
| 457 |
if (rv != CKR_OK) { |
| 458 |
gkm_transaction_fail (transaction, rv); |
| 459 |
return; |
| 460 |
} |
| 461 |
gkm_template_free (self->template); |
| 462 |
self->template = template; |
| 463 |
return; |
| 464 |
}; |
| 465 |
|
| 466 |
GKM_OBJECT_CLASS (gkm_secret_collection_parent_class)->set_attribute (object, session, transaction, attr); |
| 467 |
} |
| 468 |
|
| 469 |
static CK_RV |
| 470 |
gkm_secret_collection_real_unlock (GkmObject *obj, GkmCredential *cred) |
| 471 |
{ |
| 472 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (obj); |
| 473 |
GkmDataResult res; |
| 474 |
GkmSecretData *sdata; |
| 475 |
GkmSecret *master; |
| 476 |
CK_RV rv = CKR_GENERAL_ERROR; |
| 477 |
|
| 478 |
master = gkm_credential_get_secret (cred); |
| 479 |
|
| 480 |
/* Already unlocked, make sure pin matches */ |
| 481 |
if (self->sdata) { |
| 482 |
if (!gkm_secret_equal (gkm_secret_data_get_master (self->sdata), master)) |
| 483 |
return CKR_PIN_INCORRECT; |
| 484 |
|
| 485 |
/* Credential now tracks our secret data */ |
| 486 |
gkm_credential_set_data (cred, GKM_TYPE_SECRET_DATA, self->sdata); |
| 487 |
return CKR_OK; |
| 488 |
} |
| 489 |
|
| 490 |
/* New secret data object, setup master password */ |
| 491 |
sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 492 |
gkm_secret_data_set_master (sdata, master); |
| 493 |
|
| 494 |
/* Load the data from a file, and decrypt if necessary */ |
| 495 |
if (self->filename) { |
| 496 |
res = load_collection_and_secret_data (self, sdata, self->filename); |
| 497 |
|
| 498 |
/* No filename, password must be null */ |
| 499 |
} else { |
| 500 |
if (gkm_secret_equals (master, NULL, 0)) |
| 501 |
res = GKM_DATA_SUCCESS; |
| 502 |
else |
| 503 |
res = GKM_DATA_LOCKED; |
| 504 |
} |
| 505 |
|
| 506 |
switch (res) { |
| 507 |
case GKM_DATA_SUCCESS: |
| 508 |
gkm_credential_set_data (cred, GKM_TYPE_SECRET_DATA, sdata); |
| 509 |
track_secret_data (self, sdata); |
| 510 |
rv = CKR_OK; |
| 511 |
break; |
| 512 |
case GKM_DATA_LOCKED: |
| 513 |
rv = CKR_PIN_INCORRECT; |
| 514 |
break; |
| 515 |
case GKM_DATA_UNRECOGNIZED: |
| 516 |
g_message ("unrecognized or invalid keyring: %s", self->filename); |
| 517 |
rv = CKR_FUNCTION_FAILED; |
| 518 |
break; |
| 519 |
case GKM_DATA_FAILURE: |
| 520 |
g_message ("failed to read or parse keyring: %s", self->filename); |
| 521 |
rv = CKR_GENERAL_ERROR; |
| 522 |
break; |
| 523 |
default: |
| 524 |
g_assert_not_reached (); |
| 525 |
} |
| 526 |
|
| 527 |
g_object_unref (sdata); |
| 528 |
return rv; |
| 529 |
} |
| 530 |
|
| 531 |
static void |
| 532 |
gkm_secret_collection_expose (GkmObject *base, gboolean expose) |
| 533 |
{ |
| 534 |
GKM_OBJECT_CLASS (gkm_secret_collection_parent_class)->expose_object (base, expose); |
| 535 |
g_hash_table_foreach (GKM_SECRET_COLLECTION (base)->items, expose_each_item, GINT_TO_POINTER (expose)); |
| 536 |
} |
| 537 |
|
| 538 |
static gboolean |
| 539 |
gkm_secret_collection_real_is_locked (GkmSecretObject *obj, GkmSession *session) |
| 540 |
{ |
| 541 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (obj); |
| 542 |
return !gkm_secret_collection_unlocked_have (self, session); |
| 543 |
} |
| 544 |
|
| 545 |
static void |
| 546 |
gkm_secret_collection_init (GkmSecretCollection *self) |
| 547 |
{ |
| 548 |
CK_ULONG idle = 0; |
| 549 |
CK_ULONG after = 0; |
| 550 |
CK_BBOOL token = CK_TRUE; |
| 551 |
CK_ATTRIBUTE attrs[] = { |
| 552 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 553 |
{ CKA_GNOME_TRANSIENT, &token, sizeof (token) }, |
| 554 |
{ CKA_G_DESTRUCT_IDLE, &idle, sizeof (idle) }, |
| 555 |
{ CKA_G_DESTRUCT_AFTER, &after, sizeof (after) }, |
| 556 |
}; |
| 557 |
|
| 558 |
self->items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); |
| 559 |
self->template = gkm_template_new (attrs, G_N_ELEMENTS (attrs)); |
| 560 |
} |
| 561 |
|
| 562 |
static void |
| 563 |
gkm_secret_collection_set_property (GObject *obj, guint prop_id, const GValue *value, |
| 564 |
GParamSpec *pspec) |
| 565 |
{ |
| 566 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (obj); |
| 567 |
|
| 568 |
switch (prop_id) { |
| 569 |
case PROP_FILENAME: |
| 570 |
gkm_secret_collection_set_filename (self, g_value_get_string (value)); |
| 571 |
break; |
| 572 |
default: |
| 573 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); |
| 574 |
break; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
static void |
| 579 |
gkm_secret_collection_get_property (GObject *obj, guint prop_id, GValue *value, |
| 580 |
GParamSpec *pspec) |
| 581 |
{ |
| 582 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (obj); |
| 583 |
|
| 584 |
switch (prop_id) { |
| 585 |
case PROP_FILENAME: |
| 586 |
g_value_set_string (value, gkm_secret_collection_get_filename (self)); |
| 587 |
break; |
| 588 |
default: |
| 589 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); |
| 590 |
break; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
static void |
| 595 |
gkm_secret_collection_dispose (GObject *obj) |
| 596 |
{ |
| 597 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (obj); |
| 598 |
|
| 599 |
track_secret_data (self, NULL); |
| 600 |
g_hash_table_remove_all (self->items); |
| 601 |
|
| 602 |
G_OBJECT_CLASS (gkm_secret_collection_parent_class)->dispose (obj); |
| 603 |
} |
| 604 |
|
| 605 |
static void |
| 606 |
gkm_secret_collection_finalize (GObject *obj) |
| 607 |
{ |
| 608 |
GkmSecretCollection *self = GKM_SECRET_COLLECTION (obj); |
| 609 |
|
| 610 |
g_assert (self->sdata == NULL); |
| 611 |
|
| 612 |
g_hash_table_destroy (self->items); |
| 613 |
self->items = NULL; |
| 614 |
|
| 615 |
g_free (self->filename); |
| 616 |
self->filename = NULL; |
| 617 |
|
| 618 |
gkm_template_free (self->template); |
| 619 |
self->template = NULL; |
| 620 |
|
| 621 |
G_OBJECT_CLASS (gkm_secret_collection_parent_class)->finalize (obj); |
| 622 |
} |
| 623 |
|
| 624 |
static void |
| 625 |
gkm_secret_collection_class_init (GkmSecretCollectionClass *klass) |
| 626 |
{ |
| 627 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 628 |
GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass); |
| 629 |
GkmSecretObjectClass *secret_class = GKM_SECRET_OBJECT_CLASS (klass); |
| 630 |
|
| 631 |
gkm_secret_collection_parent_class = g_type_class_peek_parent (klass); |
| 632 |
|
| 633 |
gobject_class->set_property = gkm_secret_collection_set_property; |
| 634 |
gobject_class->get_property = gkm_secret_collection_get_property; |
| 635 |
gobject_class->dispose = gkm_secret_collection_dispose; |
| 636 |
gobject_class->finalize = gkm_secret_collection_finalize; |
| 637 |
|
| 638 |
gkm_class->get_attribute = gkm_secret_collection_get_attribute; |
| 639 |
gkm_class->set_attribute = gkm_secret_collection_set_attribute; |
| 640 |
gkm_class->unlock = gkm_secret_collection_real_unlock; |
| 641 |
gkm_class->expose_object = gkm_secret_collection_expose; |
| 642 |
|
| 643 |
secret_class->is_locked = gkm_secret_collection_real_is_locked; |
| 644 |
|
| 645 |
g_object_class_install_property (gobject_class, PROP_FILENAME, |
| 646 |
g_param_spec_string ("filename", "Filename", "Collection filename (without path)", |
| 647 |
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); |
| 648 |
|
| 649 |
gkm_secret_object_class_unique_identifiers (secret_class); |
| 650 |
} |
| 651 |
|
| 652 |
/* ----------------------------------------------------------------------------- |
| 653 |
* PUBLIC |
| 654 |
*/ |
| 655 |
|
| 656 |
GkmFactory* |
| 657 |
gkm_secret_collection_get_factory (void) |
| 658 |
{ |
| 659 |
static CK_OBJECT_CLASS klass = CKO_G_COLLECTION; |
| 660 |
|
| 661 |
static CK_ATTRIBUTE attributes[] = { |
| 662 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 663 |
}; |
| 664 |
|
| 665 |
static GkmFactory factory = { |
| 666 |
attributes, |
| 667 |
G_N_ELEMENTS (attributes), |
| 668 |
factory_create_collection |
| 669 |
}; |
| 670 |
|
| 671 |
return &factory; |
| 672 |
} |
| 673 |
|
| 674 |
const gchar* |
| 675 |
gkm_secret_collection_get_filename (GkmSecretCollection *self) |
| 676 |
{ |
| 677 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), NULL); |
| 678 |
return self->filename; |
| 679 |
} |
| 680 |
|
| 681 |
void |
| 682 |
gkm_secret_collection_set_filename (GkmSecretCollection *self, const gchar *filename) |
| 683 |
{ |
| 684 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 685 |
|
| 686 |
if (self->filename == filename) |
| 687 |
return; |
| 688 |
g_free (self->filename); |
| 689 |
self->filename = g_strdup (filename); |
| 690 |
g_object_notify (G_OBJECT (self), "filename"); |
| 691 |
} |
| 692 |
|
| 693 |
GList* |
| 694 |
gkm_secret_collection_get_items (GkmSecretCollection *self) |
| 695 |
{ |
| 696 |
GList *items = NULL; |
| 697 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), NULL); |
| 698 |
g_hash_table_foreach (self->items, each_value_to_list, &items); |
| 699 |
return items; |
| 700 |
} |
| 701 |
|
| 702 |
GkmSecretItem* |
| 703 |
gkm_secret_collection_get_item (GkmSecretCollection *self, const gchar *identifier) |
| 704 |
{ |
| 705 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), NULL); |
| 706 |
g_return_val_if_fail (identifier, NULL); |
| 707 |
return g_hash_table_lookup (self->items, identifier); |
| 708 |
} |
| 709 |
|
| 710 |
gboolean |
| 711 |
gkm_secret_collection_has_item (GkmSecretCollection *self, GkmSecretItem *item) |
| 712 |
{ |
| 713 |
const gchar *identifier; |
| 714 |
|
| 715 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), FALSE); |
| 716 |
g_return_val_if_fail (GKM_IS_SECRET_ITEM (item), FALSE); |
| 717 |
|
| 718 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)); |
| 719 |
return g_hash_table_lookup (self->items, identifier) == item; |
| 720 |
} |
| 721 |
|
| 722 |
GkmSecretCollection* |
| 723 |
gkm_secret_collection_find (GkmSession *session, CK_ATTRIBUTE_PTR attr, ...) |
| 724 |
{ |
| 725 |
CK_OBJECT_CLASS klass = CKO_G_COLLECTION; |
| 726 |
GkmSecretCollection *result = NULL; |
| 727 |
GkmManager *manager; |
| 728 |
CK_ATTRIBUTE attrs[2]; |
| 729 |
GList *objects; |
| 730 |
va_list va; |
| 731 |
|
| 732 |
g_assert (attr); |
| 733 |
|
| 734 |
attrs[0].type = CKA_CLASS; |
| 735 |
attrs[0].ulValueLen = sizeof (klass); |
| 736 |
attrs[0].pValue = &klass; |
| 737 |
attrs[1].type = CKA_ID; |
| 738 |
attrs[1].ulValueLen = attr->ulValueLen; |
| 739 |
attrs[1].pValue = attr->pValue; |
| 740 |
|
| 741 |
va_start (va, attr); |
| 742 |
while (!result && (manager = va_arg (va, GkmManager*)) != NULL) { |
| 743 |
objects = gkm_manager_find_by_attributes (manager, session, attrs, 2); |
| 744 |
if (objects && GKM_IS_SECRET_COLLECTION (objects->data)) |
| 745 |
result = objects->data; |
| 746 |
g_list_free (objects); |
| 747 |
} |
| 748 |
va_end (va); |
| 749 |
|
| 750 |
return result; |
| 751 |
} |
| 752 |
|
| 753 |
GkmSecretItem* |
| 754 |
gkm_secret_collection_new_item (GkmSecretCollection *self, const gchar *identifier) |
| 755 |
{ |
| 756 |
GkmSecretItem *item; |
| 757 |
|
| 758 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), NULL); |
| 759 |
g_return_val_if_fail (identifier, NULL); |
| 760 |
g_return_val_if_fail (!g_hash_table_lookup (self->items, identifier), NULL); |
| 761 |
|
| 762 |
item = g_object_new (GKM_TYPE_SECRET_ITEM, |
| 763 |
"module", gkm_object_get_module (GKM_OBJECT (self)), |
| 764 |
"manager", gkm_object_get_manager (GKM_OBJECT (self)), |
| 765 |
"collection", self, |
| 766 |
"identifier", identifier, |
| 767 |
NULL); |
| 768 |
|
| 769 |
add_item (self, NULL, item); |
| 770 |
g_object_unref (item); |
| 771 |
return item; |
| 772 |
} |
| 773 |
|
| 774 |
GkmSecretItem* |
| 775 |
gkm_secret_collection_create_item (GkmSecretCollection *self, GkmTransaction *transaction) |
| 776 |
{ |
| 777 |
GkmSecretItem *item; |
| 778 |
gchar *identifier = NULL; |
| 779 |
|
| 780 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), NULL); |
| 781 |
g_return_val_if_fail (transaction, NULL); |
| 782 |
g_return_val_if_fail (!gkm_transaction_get_failed (transaction), NULL); |
| 783 |
|
| 784 |
do { |
| 785 |
g_free (identifier); |
| 786 |
identifier = g_strdup_printf ("%d", ++(self->watermark)); |
| 787 |
} while (g_hash_table_lookup (self->items, identifier)); |
| 788 |
|
| 789 |
item = g_object_new (GKM_TYPE_SECRET_ITEM, |
| 790 |
"module", gkm_object_get_module (GKM_OBJECT (self)), |
| 791 |
"manager", gkm_object_get_manager (GKM_OBJECT (self)), |
| 792 |
"collection", self, |
| 793 |
"identifier", identifier, |
| 794 |
NULL); |
| 795 |
|
| 796 |
g_free (identifier); |
| 797 |
add_item (self, transaction, item); |
| 798 |
gkm_secret_object_mark_created (GKM_SECRET_OBJECT (item)); |
| 799 |
g_object_unref (item); |
| 800 |
return item; |
| 801 |
} |
| 802 |
|
| 803 |
void |
| 804 |
gkm_secret_collection_remove_item (GkmSecretCollection *self, GkmSecretItem *item) |
| 805 |
{ |
| 806 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 807 |
g_return_if_fail (GKM_IS_SECRET_ITEM (item)); |
| 808 |
g_return_if_fail (gkm_secret_collection_has_item (self, item)); |
| 809 |
|
| 810 |
remove_item (self, NULL, item); |
| 811 |
} |
| 812 |
|
| 813 |
void |
| 814 |
gkm_secret_collection_destroy_item (GkmSecretCollection *self, GkmTransaction *transaction, |
| 815 |
GkmSecretItem *item) |
| 816 |
{ |
| 817 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 818 |
g_return_if_fail (GKM_IS_TRANSACTION (transaction)); |
| 819 |
g_return_if_fail (GKM_IS_SECRET_ITEM (item)); |
| 820 |
g_return_if_fail (gkm_secret_collection_has_item (self, item)); |
| 821 |
|
| 822 |
remove_item (self, transaction, item); |
| 823 |
} |
| 824 |
|
| 825 |
gboolean |
| 826 |
gkm_secret_collection_unlocked_have (GkmSecretCollection *self, GkmSession *session) |
| 827 |
{ |
| 828 |
CK_OBJECT_HANDLE handle = 0; |
| 829 |
|
| 830 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), FALSE); |
| 831 |
g_return_val_if_fail (GKM_IS_SESSION (session), FALSE); |
| 832 |
|
| 833 |
/* |
| 834 |
* Look for credential objects that this session has access |
| 835 |
* to, and use those to find the secret data. If a secret data is |
| 836 |
* found, it should match the one we are tracking in self->sdata. |
| 837 |
*/ |
| 838 |
|
| 839 |
gkm_credential_for_each (session, GKM_OBJECT (self), find_unlocked_credential, &handle); |
| 840 |
return handle != 0; |
| 841 |
} |
| 842 |
|
| 843 |
GkmSecretData* |
| 844 |
gkm_secret_collection_unlocked_use (GkmSecretCollection *self, GkmSession *session) |
| 845 |
{ |
| 846 |
GkmSecretData *sdata = NULL; |
| 847 |
|
| 848 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), NULL); |
| 849 |
g_return_val_if_fail (GKM_IS_SESSION (session), NULL); |
| 850 |
|
| 851 |
/* |
| 852 |
* Look for credential objects that this session has access |
| 853 |
* to, and use those to find the secret data. If a secret data is |
| 854 |
* found, it should match the one we are tracking in self->sdata. |
| 855 |
*/ |
| 856 |
|
| 857 |
gkm_credential_for_each (session, GKM_OBJECT (self), |
| 858 |
find_unlocked_secret_data, &sdata); |
| 859 |
|
| 860 |
return sdata; |
| 861 |
} |
| 862 |
|
| 863 |
void |
| 864 |
gkm_secret_collection_unlocked_clear (GkmSecretCollection *self) |
| 865 |
{ |
| 866 |
/* |
| 867 |
* TODO: This is a tough one to implement. I'm holding off and wondering |
| 868 |
* if we don't need it, perhaps? As it currently stands, what needs to happen |
| 869 |
* here is we need to find each and every credential that references the |
| 870 |
* secret data for this collection and completely delete those objects. |
| 871 |
*/ |
| 872 |
g_warning ("Clearing of secret data needs implementing"); |
| 873 |
track_secret_data (self, NULL); |
| 874 |
} |
| 875 |
|
| 876 |
GkmDataResult |
| 877 |
gkm_secret_collection_load (GkmSecretCollection *self) |
| 878 |
{ |
| 879 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), GKM_DATA_FAILURE); |
| 880 |
|
| 881 |
if (!self->filename) |
| 882 |
return GKM_DATA_SUCCESS; |
| 883 |
|
| 884 |
return load_collection_and_secret_data (self, self->sdata, self->filename); |
| 885 |
} |
| 886 |
|
| 887 |
void |
| 888 |
gkm_secret_collection_save (GkmSecretCollection *self, GkmTransaction *transaction) |
| 889 |
{ |
| 890 |
GkmSecret *master; |
| 891 |
GkmDataResult res; |
| 892 |
gpointer data; |
| 893 |
gsize n_data; |
| 894 |
|
| 895 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 896 |
g_return_if_fail (GKM_IS_TRANSACTION (transaction)); |
| 897 |
g_return_if_fail (!gkm_transaction_get_failed (transaction)); |
| 898 |
|
| 899 |
/* HACK: We can't save unless the secret data was loaded */ |
| 900 |
if (!self->sdata) { |
| 901 |
gkm_transaction_fail (transaction, CKR_USER_NOT_LOGGED_IN); |
| 902 |
return; |
| 903 |
} |
| 904 |
|
| 905 |
/* Don't save ourselves if no filename */ |
| 906 |
if (!self->filename) |
| 907 |
return; |
| 908 |
|
| 909 |
master = gkm_secret_data_get_master (self->sdata); |
| 910 |
if (master == NULL || gkm_secret_equals (master, NULL, 0)) |
| 911 |
res = gkm_secret_textual_write (self, self->sdata, &data, &n_data); |
| 912 |
else |
| 913 |
res = gkm_secret_binary_write (self, self->sdata, &data, &n_data); |
| 914 |
|
| 915 |
switch (res) { |
| 916 |
case GKM_DATA_FAILURE: |
| 917 |
case GKM_DATA_UNRECOGNIZED: |
| 918 |
g_warning ("couldn't prepare to write out keyring: %s", self->filename); |
| 919 |
gkm_transaction_fail (transaction, CKR_GENERAL_ERROR); |
| 920 |
break; |
| 921 |
case GKM_DATA_LOCKED: |
| 922 |
g_warning ("locked error while writing out keyring: %s", self->filename); |
| 923 |
gkm_transaction_fail (transaction, CKR_GENERAL_ERROR); |
| 924 |
break; |
| 925 |
case GKM_DATA_SUCCESS: |
| 926 |
gkm_transaction_write_file (transaction, self->filename, data, n_data); |
| 927 |
g_free (data); |
| 928 |
break; |
| 929 |
default: |
| 930 |
g_assert_not_reached (); |
| 931 |
}; |
| 932 |
} |
| 933 |
|
| 934 |
void |
| 935 |
gkm_secret_collection_destroy (GkmSecretCollection *self, GkmTransaction *transaction) |
| 936 |
{ |
| 937 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 938 |
g_return_if_fail (GKM_IS_TRANSACTION (transaction)); |
| 939 |
g_return_if_fail (!gkm_transaction_get_failed (transaction)); |
| 940 |
|
| 941 |
gkm_object_expose_full (GKM_OBJECT (self), transaction, FALSE); |
| 942 |
if (self->filename) |
| 943 |
gkm_transaction_remove_file (transaction, self->filename); |
| 944 |
} |
| 945 |
|
| 946 |
gint |
| 947 |
gkm_secret_collection_get_lock_idle (GkmSecretCollection *self) |
| 948 |
{ |
| 949 |
gulong value; |
| 950 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), 0); |
| 951 |
if (!gkm_template_find_ulong (self->template, CKA_G_DESTRUCT_IDLE, &value)) |
| 952 |
value = 0; |
| 953 |
return (gint)value; |
| 954 |
} |
| 955 |
|
| 956 |
void |
| 957 |
gkm_secret_collection_set_lock_idle (GkmSecretCollection *self, gint lock_timeout) |
| 958 |
{ |
| 959 |
CK_ULONG value = (lock_timeout < 0) ? 0 : lock_timeout; |
| 960 |
CK_ATTRIBUTE attr = { CKA_G_DESTRUCT_IDLE, &value, sizeof (value) }; |
| 961 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 962 |
gkm_template_set (self->template, &attr); |
| 963 |
} |
| 964 |
|
| 965 |
gint |
| 966 |
gkm_secret_collection_get_lock_after (GkmSecretCollection *self) |
| 967 |
{ |
| 968 |
gulong value; |
| 969 |
g_return_val_if_fail (GKM_IS_SECRET_COLLECTION (self), 0); |
| 970 |
if (!gkm_template_find_ulong (self->template, CKA_G_DESTRUCT_AFTER, &value)) |
| 971 |
value = 0; |
| 972 |
return (gint)value; |
| 973 |
} |
| 974 |
|
| 975 |
void |
| 976 |
gkm_secret_collection_set_lock_after (GkmSecretCollection *self, gint lock_timeout) |
| 977 |
{ |
| 978 |
CK_ULONG value = (lock_timeout < 0) ? 0 : lock_timeout; |
| 979 |
CK_ATTRIBUTE attr = { CKA_G_DESTRUCT_AFTER, &value, sizeof (value) }; |
| 980 |
g_return_if_fail (GKM_IS_SECRET_COLLECTION (self)); |
| 981 |
gkm_template_set (self->template, &attr); |
| 982 |
} |