Debian Codegraph @

pkcs11/gnome2-store/gkm-gnome2-file.c

raw
1 /*
2 * gnome-keyring
3 *
4 * Copyright (C) 2008 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-gnome2-file.h"
24
25 #include "gkm/gkm-attributes.h"
26 #include "gkm/gkm-crypto.h"
27 #include "gkm/gkm-data-types.h"
28 #include "gkm/gkm-util.h"
29
30 #include "gkm-marshal.h"
31
32 #include "egg/egg-buffer.h"
33 #include "egg/egg-hex.h"
34 #include "egg/egg-secure-memory.h"
35 #include "egg/egg-symkey.h"
36
37 #include <glib/gstdio.h>
38
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include <gcrypt.h>
45
46 enum {
47 ENTRY_ADDED,
48 ENTRY_CHANGED,
49 ENTRY_REMOVED,
50 LAST_SIGNAL
51 };
52
53 static guint signals[LAST_SIGNAL] = { 0 };
54
55 struct _GkmGnome2File {
56 GObject parent;
57
58 /* The data itself */
59 GHashTable *identifiers;
60 GHashTable *privates;
61 GHashTable *publics;
62 GList *unknowns;
63
64 /* All the sections seen */
65 guint sections;
66 gboolean incomplete;
67
68 /* Stuff notseen on this read */
69 GHashTable *checks;
70 };
71
72 typedef struct _UnknownBlock {
73 guint type;
74 EggBuffer buffer;
75 } UnknownBlock;
76
77 G_DEFINE_TYPE (GkmGnome2File, gkm_gnome2_file, G_TYPE_OBJECT);
78
79 #define PUBLIC_ALLOC (EggBufferAllocator)g_realloc
80 #define PRIVATE_ALLOC (EggBufferAllocator)egg_secure_realloc
81
82 typedef GkmDataResult (*BlockFunc) (guint block, EggBuffer *buffer, GkmSecret *login, gpointer user_data);
83
84 #define FILE_HEADER ((const guchar*)"Gnome Keyring Store 2\n\r\0")
85 #define FILE_HEADER_LEN 24
86
87 #define FILE_BLOCK_INDEX 0x49445832 /* ie: "IDX2" */
88 #define FILE_BLOCK_PRIVATE 0x50525632 /* ie: "PRV2" */
89 #define FILE_BLOCK_PUBLIC 0x50554232 /* ie: "PUB2" */
90
91 #define UNUSED_VALUE GUINT_TO_POINTER (1)
92
93 EGG_SECURE_DECLARE (data_file);
94
95 /* -----------------------------------------------------------------------------
96 * HELPERS
97 */
98
99 static void
100 attribute_free (gpointer data)
101 {
102 CK_ATTRIBUTE_PTR attr = data;
103 if (attr) {
104 g_free (attr->pValue);
105 g_slice_free (CK_ATTRIBUTE, attr);
106 }
107 }
108
109 static CK_ATTRIBUTE_PTR
110 attribute_dup (CK_ATTRIBUTE_PTR attr)
111 {
112 CK_ATTRIBUTE_PTR copy;
113 g_assert (attr);
114 copy = g_slice_new (CK_ATTRIBUTE);
115 copy->ulValueLen = attr->ulValueLen;
116 copy->pValue = g_memdup2 (attr->pValue, copy->ulValueLen);
117 copy->type = attr->type;
118 return copy;
119 }
120
121 static GHashTable*
122 attributes_new (void)
123 {
124 return g_hash_table_new_full (gkm_util_ulong_hash, gkm_util_ulong_equal, NULL, attribute_free);
125 }
126
127 static GHashTable*
128 entries_new (void)
129 {
130 return g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify)g_free, (GDestroyNotify)g_hash_table_unref);
131 }
132
133 static gboolean
134 read_all_bytes (int fd, guchar *buf, gsize len)
135 {
136 gsize all = len;
137 int res;
138
139 while (len > 0) {
140 res = read (fd, buf, len);
141 if (res < 0) {
142 if (errno == EAGAIN || errno == EINTR)
143 continue;
144 g_warning ("couldn't read %u bytes from store file: %s",
145 (guint)all, g_strerror (errno));
146 return FALSE;
147 } else if (res == 0) {
148 if (len != all)
149 g_warning ("couldn't read %u bytes from store file", (guint)all);
150 return FALSE;
151 } else {
152 len -= res;
153 buf += res;
154 }
155 }
156
157 return TRUE;
158 }
159
160 static gboolean
161 write_all_bytes (int fd, const guchar *buf, gsize len)
162 {
163 gsize all = len;
164 int res;
165
166 while (len > 0) {
167
168 res = write (fd, buf, len);
169 if (res < 0) {
170 if (errno == EAGAIN || errno == EINTR)
171 continue;
172 g_warning ("couldn't write %u bytes to store file: %s",
173 (guint)all, g_strerror (errno));
174 return FALSE;
175 } else if (res == 0) {
176 g_warning ("couldn't write %u bytes to store file", (guint)all);
177 return FALSE;
178 } else {
179 len -= res;
180 buf += res;
181 }
182 }
183
184 return TRUE;
185 }
186
187 static GkmDataResult
188 parse_file_blocks (int file, BlockFunc block_func, GkmSecret *login, gpointer user_data)
189 {
190 gchar header[FILE_HEADER_LEN];
191 GkmDataResult res;
192 EggBuffer buffer;
193 guint32 block;
194 guint32 length;
195 gsize offset;
196
197 g_assert (file != -1);
198 g_assert (block_func);
199
200 /* Zero length file is valid */
201 if (!read_all_bytes (file, (guchar*)header, FILE_HEADER_LEN))
202 return TRUE;
203
204 /* Check the header */
205 if (memcmp (header, FILE_HEADER, FILE_HEADER_LEN) != 0) {
206 g_message ("invalid header in store file");
207 return FALSE;
208 }
209
210 egg_buffer_init_full (&buffer, 1024, (EggBufferAllocator)g_realloc);
211
212 res = GKM_DATA_SUCCESS;
213 for (;;) {
214
215 egg_buffer_reset (&buffer);
216 egg_buffer_resize (&buffer, 8);
217 offset = 0;
218
219 /* Read in a set of bytes */
220 if (!read_all_bytes (file, buffer.buf, 8)) {
221 res = GKM_DATA_SUCCESS; /* end of file */
222 break;
223 }
224
225 /* Decode it as the number of bytes in the next section */
226 if (!egg_buffer_get_uint32 (&buffer, offset, &offset, &length) ||
227 !egg_buffer_get_uint32 (&buffer, offset, &offset, &block) ||
228 length < 8) {
229 res = GKM_DATA_FAILURE;
230 g_message ("invalid block size or length in store file");
231 break;
232 }
233
234 /* Read in that amount of bytes */
235 egg_buffer_resize (&buffer, length - 8);
236 if (!read_all_bytes (file, buffer.buf, length - 8)) {
237 res = GKM_DATA_FAILURE;
238 break;
239 }
240
241 res = (block_func) (block, &buffer, login, user_data);
242 if (res != GKM_DATA_SUCCESS)
243 break;
244 }
245
246 egg_buffer_uninit (&buffer);
247 return res;
248 }
249
250 static gboolean
251 write_file_block (int file, guint block, EggBuffer *buffer)
252 {
253 EggBuffer header;
254 gboolean ret;
255
256 g_assert (file != -1);
257 g_assert (buffer);
258
259 /* Write out the 8 bytes of header */
260 egg_buffer_init_full (&header, 8, (EggBufferAllocator)g_realloc);
261 egg_buffer_add_uint32 (&header, buffer->len + 8);
262 egg_buffer_add_uint32 (&header, block);
263 g_assert (!egg_buffer_has_error (&header));
264 g_assert (header.len == 8);
265 ret = write_all_bytes (file, header.buf, header.len);
266 egg_buffer_uninit (&header);
267
268 if (ret != TRUE)
269 return FALSE;
270
271 /* Now write out the remainder of the data */
272 return write_all_bytes (file, buffer->buf, buffer->len);
273 }
274
275 static gboolean
276 hash_buffer (EggBuffer *buffer)
277 {
278 const gchar *salgo;
279 gsize length;
280 guchar *hash;
281 gsize n_hash;
282 int algo;
283
284 /* The length needs to be the first thing in the buffer */
285 g_assert (buffer->len > 4);
286 g_assert (egg_buffer_decode_uint32 (buffer->buf) == buffer->len);
287
288 length = buffer->len;
289
290 algo = GCRY_MD_SHA256;
291 salgo = gcry_md_algo_name (algo);
292 g_return_val_if_fail (salgo, FALSE);
293 n_hash = gcry_md_get_algo_dlen (algo);
294 g_return_val_if_fail (n_hash > 0, FALSE);
295
296 egg_buffer_add_string (buffer, salgo);
297 hash = egg_buffer_add_byte_array_empty (buffer, n_hash);
298 g_return_val_if_fail (hash, FALSE);
299
300 gcry_md_hash_buffer (algo, hash, buffer->buf, length);
301 return TRUE;
302 }
303
304 static gboolean
305 validate_buffer (EggBuffer *buffer, gsize *offset)
306 {
307 const guchar *hash;
308 gchar *salgo, *check;
309 gsize n_hash, hash_offset;
310 guint32 length;
311 int algo;
312 gboolean valid;
313
314 g_assert (buffer);
315 g_assert (offset);
316
317 *offset = 0;
318
319 if (!egg_buffer_get_uint32 (buffer, *offset, offset, &length) ||
320 !egg_buffer_get_string (buffer, length, &hash_offset, &salgo, PUBLIC_ALLOC))
321 return FALSE;
322
323 algo = gcry_md_map_name (salgo);
324 if (algo == 0) {
325 g_warning ("unsupported hash algorithm: %s", salgo);
326 g_free (salgo);
327 return FALSE;
328 }
329 g_free (salgo);
330
331 if (!egg_buffer_get_byte_array (buffer, hash_offset, &hash_offset, &hash, &n_hash))
332 return FALSE;
333
334 if (n_hash != gcry_md_get_algo_dlen (algo)) {
335 g_warning ("invalid hash length in store file");
336 return FALSE;
337 }
338
339 check = g_malloc0 (n_hash);
340 gcry_md_hash_buffer (algo, check, buffer->buf, length);
341 valid = (memcmp (check, hash, n_hash) == 0);
342 g_free (check);
343
344 return valid;
345 }
346
347 static gboolean
348 create_cipher (GkmSecret *login, int calgo, int halgo, const guchar *salt,
349 gsize n_salt, guint iterations, gcry_cipher_hd_t *cipher)
350 {
351 gsize n_key, n_block;
352 const gchar *password;
353 gsize n_password;
354 guchar *key, *iv;
355 gcry_error_t gcry;
356
357 g_assert (login);
358 g_assert (salt);
359 g_assert (cipher);
360
361 n_key = gcry_cipher_get_algo_keylen (calgo);
362 g_return_val_if_fail (n_key, FALSE);
363 n_block = gcry_cipher_get_algo_blklen (calgo);
364 g_return_val_if_fail (n_block, FALSE);
365
366 password = gkm_secret_get_password (login, &n_password);
367
368 if (!egg_symkey_generate_simple (calgo, halgo, password, n_password,
369 salt, n_salt, iterations, &key, &iv)) {
370 return FALSE;
371 }
372
373 gcry = gcry_cipher_open (cipher, calgo, GCRY_CIPHER_MODE_CBC, 0);
374 if (gcry) {
375 g_warning ("couldn't create cipher context: %s", gcry_strerror (gcry));
376 egg_secure_free (key);
377 g_free (iv);
378 return FALSE;
379 }
380
381 gcry = gcry_cipher_setkey (*cipher, key, n_key);
382 g_return_val_if_fail (!gcry, FALSE);
383 egg_secure_free (key);
384
385 gcry = gcry_cipher_setiv (*cipher, iv, n_block);
386 g_return_val_if_fail (!gcry, FALSE);
387 g_free (iv);
388
389 return TRUE;
390 }
391
392 static gboolean
393 encrypt_buffer (EggBuffer *input, GkmSecret *login, EggBuffer *output)
394 {
395 gcry_cipher_hd_t cipher;
396 gcry_error_t gcry;
397 guchar salt[8];
398 guint32 iterations;
399 int calgo, halgo;
400 const gchar *salgo;
401 guchar *dest;
402 gsize n_block;
403
404 g_assert (input);
405 g_assert (output);
406 g_assert (login);
407
408 /* The algorithms we're going to use */
409 calgo = GCRY_CIPHER_AES128;
410 halgo = GCRY_MD_SHA256;
411
412 /* Prepare us some salt */
413 gcry_create_nonce (salt, sizeof (salt));
414
415 /* Prepare us the iterations */
416 iterations = 1000 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
417
418 /* Write out crypto algorithm */
419 salgo = gcry_cipher_algo_name (calgo);
420 g_return_val_if_fail (salgo, FALSE);
421 egg_buffer_add_string (output, salgo);
422
423 /* Write out the hash algorithm */
424 salgo = gcry_md_algo_name (halgo);
425 g_return_val_if_fail (halgo, FALSE);
426 egg_buffer_add_string (output, salgo);
427
428 /* Write out the iterations */
429 egg_buffer_add_uint32 (output, iterations);
430
431 /* And write out the salt */
432 egg_buffer_add_byte_array (output, salt, sizeof (salt));
433
434 /* Okay now use the above info to create our cipher context */
435 if (!create_cipher (login, calgo, halgo, salt, sizeof (salt), iterations, &cipher))
436 return FALSE;
437
438 /* Significant block sizes */
439 n_block = gcry_cipher_get_algo_blklen (calgo);
440 g_return_val_if_fail (n_block, FALSE);
441
442 /* Pad the buffer to a multiple of block length */
443 while (input->len % n_block != 0)
444 egg_buffer_add_byte (input, 0);
445
446 /* Now reserve space for it in the output block, and encrypt */
447 dest = egg_buffer_add_byte_array_empty (output, input->len);
448 g_return_val_if_fail (dest, FALSE);
449
450 gcry = gcry_cipher_encrypt (cipher, dest, input->len, input->buf, input->len);
451 g_return_val_if_fail (!gcry, FALSE);
452
453 gcry_cipher_close (cipher);
454
455 return TRUE;
456 }
457
458 static gboolean
459 decrypt_buffer (EggBuffer *input, gsize *offset, GkmSecret *login, EggBuffer *output)
460 {
461 gcry_cipher_hd_t cipher;
462 gcry_error_t gcry;
463 const guchar *salt, *data;
464 gsize n_block, n_salt, n_data;
465 guint32 iterations;
466 int calgo, halgo;
467 gchar *salgo;
468
469 g_assert (input);
470 g_assert (output);
471 g_assert (offset);
472 g_assert (login);
473
474 /* Read in and interpret the cipher algorithm */
475 if (!egg_buffer_get_string (input, *offset, offset, &salgo, NULL))
476 return FALSE;
477 calgo = gcry_cipher_map_name (salgo);
478 if (!calgo) {
479 g_warning ("unsupported crypto algorithm: %s", salgo);
480 g_free (salgo);
481 return FALSE;
482 }
483 g_free (salgo);
484
485 /* Read in and interpret the hash algorithm */
486 if (!egg_buffer_get_string (input, *offset, offset, &salgo, NULL))
487 return FALSE;
488 halgo = gcry_md_map_name (salgo);
489 if (!halgo) {
490 g_warning ("unsupported crypto algorithm: %s", salgo);
491 g_free (salgo);
492 return FALSE;
493 }
494 g_free (salgo);
495
496 /* Read in the iterations, salt, and encrypted data */
497 if (!egg_buffer_get_uint32 (input, *offset, offset, &iterations) ||
498 !egg_buffer_get_byte_array (input, *offset, offset, &salt, &n_salt) ||
499 !egg_buffer_get_byte_array (input, *offset, offset, &data, &n_data))
500 return FALSE;
501
502 /* Significant block sizes */
503 n_block = gcry_cipher_get_algo_blklen (calgo);
504 g_return_val_if_fail (n_block, FALSE);
505
506 /* Make sure the encrypted data is of a good length */
507 if (n_data % n_block != 0) {
508 g_warning ("encrypted data in file store is of an invalid length for algorithm");
509 return FALSE;
510 }
511
512 /* Create the cipher context */
513 if (!create_cipher (login, calgo, halgo, salt, n_salt, iterations, &cipher))
514 return FALSE;
515
516 /* Now reserve space for it in the output block, and encrypt */
517 egg_buffer_reset (output);
518 egg_buffer_resize (output, n_data);
519
520 gcry = gcry_cipher_decrypt (cipher, output->buf, output->len, data, n_data);
521 g_return_val_if_fail (!gcry, FALSE);
522
523 gcry_cipher_close (cipher);
524
525 return TRUE;
526 }
527
528 /* ----------------------------------------------------------------------------------------
529 * INTERNAL
530 */
531
532 static GkmDataResult
533 update_entries_from_block (GkmGnome2File *self, guint section, GHashTable *entries,
534 EggBuffer *buffer, gsize *offset)
535 {
536 GHashTable *attributes;
537 const gchar *identifier;
538 gboolean added;
539 CK_ATTRIBUTE_PTR at;
540 CK_ATTRIBUTE attr;
541 gpointer key, value;
542 guint32 n_entries, i;
543 guint32 n_attrs, j;
544 gchar *str;
545 guint sect;
546 const guchar *data;
547 gsize n_data;
548 guint64 type;
549
550 g_assert (GKM_IS_GNOME2_FILE (self));
551 g_assert (entries);
552 g_assert (buffer);
553 g_assert (offset);
554
555 /* The number of entries */
556 if (!egg_buffer_get_uint32 (buffer, *offset, offset, &n_entries))
557 return GKM_DATA_FAILURE;
558
559 for (i = 0; i < n_entries; ++i) {
560
561 added = FALSE;
562
563 /* The attributes */
564 if (!egg_buffer_get_string (buffer, *offset, offset, &str, (EggBufferAllocator)g_realloc))
565 return GKM_DATA_FAILURE;
566
567 /* Make sure we have this one */
568 sect = GPOINTER_TO_UINT (g_hash_table_lookup (self->identifiers, str));
569 if (sect != section) {
570 g_message ("data file entry in wrong section: %s", str);
571 g_free (str);
572 return GKM_DATA_FAILURE;
573 }
574
575 /* Lookup or create a new table for it */
576 if (!g_hash_table_lookup_extended (entries, str, &key, &value)) {
577 added = TRUE;
578 value = attributes_new ();
579 key = g_strdup (str);
580 g_hash_table_replace (entries, key, value);
581 }
582
583 g_free (str);
584 identifier = key;
585 attributes = value;
586
587 if (!egg_buffer_get_uint32 (buffer, *offset, offset, &n_attrs))
588 return GKM_DATA_FAILURE;
589
590 for (j = 0; j < n_attrs; ++j) {
591 if (!egg_buffer_get_uint64 (buffer, *offset, offset, &type) ||
592 !egg_buffer_get_byte_array (buffer, *offset, offset, &data, &n_data))
593 return GKM_DATA_FAILURE;
594
595 attr.type = type;
596 attr.pValue = (CK_VOID_PTR)data;
597 attr.ulValueLen = n_data;
598
599 at = g_hash_table_lookup (attributes, &attr.type);
600 if (at != NULL && gkm_attribute_equal (&attr, at))
601 continue;
602
603 at = attribute_dup (&attr);
604 g_hash_table_replace (attributes, &(at->type), at);
605
606 /* Only emit the changed signal if we haven't just added this one */
607 if (added == FALSE)
608 g_signal_emit (self, signals[ENTRY_CHANGED], 0, identifier, attr.type);
609 }
610
611 /* A new entry was loaded */
612 if (added == TRUE)
613 g_signal_emit (self, signals[ENTRY_ADDED], 0, identifier);
614 }
615
616 return GKM_DATA_SUCCESS;
617 }
618
619 static GkmDataResult
620 update_from_public_block (GkmGnome2File *self, EggBuffer *buffer)
621 {
622 gsize offset = 0;
623
624 g_assert (GKM_IS_GNOME2_FILE (self));
625 g_assert (buffer);
626
627 self->sections |= GKM_GNOME2_FILE_SECTION_PUBLIC;
628
629 /* Validate the buffer hash, failure in this case is corruption */
630 if (!validate_buffer (buffer, &offset))
631 return GKM_DATA_FAILURE;
632
633 return update_entries_from_block (self, GKM_GNOME2_FILE_SECTION_PUBLIC,
634 self->publics, buffer, &offset);
635 }
636
637 static GkmDataResult
638 update_from_private_block (GkmGnome2File *self, EggBuffer *buffer, GkmSecret *login)
639 {
640 EggBuffer custom;
641 GkmDataResult res;
642 gsize offset;
643
644 g_assert (GKM_IS_GNOME2_FILE (self));
645 g_assert (buffer);
646
647 self->sections |= GKM_GNOME2_FILE_SECTION_PRIVATE;
648
649 /* Skip private blocks when not unlocked */
650 if (login == NULL) {
651 if (self->privates)
652 g_hash_table_destroy (self->privates);
653 self->privates = NULL;
654 return GKM_DATA_UNRECOGNIZED;
655 }
656
657 offset = 0;
658 egg_buffer_init_full (&custom, 1024, egg_secure_realloc);
659
660 /* Decrypt the buffer */
661 if (!decrypt_buffer (buffer, &offset, login, &custom)) {
662 egg_buffer_uninit (&custom);
663 return GKM_DATA_FAILURE;
664 }
665
666 offset = 0;
667
668 /* Validate the buffer hash, failure is usually a bad password */
669 if (!validate_buffer (&custom, &offset)) {
670 egg_buffer_uninit (&custom);
671 return GKM_DATA_LOCKED;
672 }
673
674 /* We're loading privates, so fill that in */
675 if (!self->privates)
676 self->privates = entries_new ();
677
678 res = update_entries_from_block (self, GKM_GNOME2_FILE_SECTION_PRIVATE,
679 self->privates, &custom, &offset);
680 egg_buffer_uninit (&custom);
681 return res;
682 }
683
684 static void
685 copy_each_identifier (gpointer key, gpointer value, gpointer data)
686 {
687 g_hash_table_insert (data, g_strdup (key), UNUSED_VALUE);
688 }
689
690 static void
691 remove_each_identifier (gpointer key, gpointer value, gpointer data)
692 {
693 GkmGnome2File *self = GKM_GNOME2_FILE (data);
694 GHashTable *entries;
695 guint section;
696
697 g_assert (GKM_IS_GNOME2_FILE (self));
698 g_assert (key);
699
700 if (!gkm_gnome2_file_lookup_entry (self, key, &section))
701 g_assert_not_reached ();
702
703 if (section == GKM_GNOME2_FILE_SECTION_PRIVATE)
704 entries = self->privates;
705 else
706 entries = self->publics;
707
708 if (!g_hash_table_remove (self->identifiers, key))
709 g_assert_not_reached ();
710
711 if (entries != NULL) {
712 if (!g_hash_table_remove (entries, key))
713 g_return_if_reached ();
714
715 /*
716 * Note that we only fire the removed signal when the identifier
717 * was accessible. We don't fire removed for private items in
718 * a locked file.
719 */
720 g_signal_emit (self, signals[ENTRY_REMOVED], 0, key);
721 }
722 }
723
724 static GkmDataResult
725 update_from_index_block (GkmGnome2File *self, EggBuffer *buffer)
726 {
727 gchar *identifier;
728 gsize offset;
729 guint section;
730 guint count, i;
731 guint value;
732
733 g_assert (GKM_IS_GNOME2_FILE (self));
734 g_assert (buffer);
735
736 offset = 0;
737
738 /* The number of entries */
739 if (!egg_buffer_get_uint32 (buffer, offset, &offset, &count))
740 return FALSE;
741
742 for (i = 0; i < count; ++i) {
743
744 /* The identifier */
745 if (!egg_buffer_get_string (buffer, offset, &offset, &identifier, (EggBufferAllocator)g_realloc))
746 break;
747
748 /* The section */
749 if (!egg_buffer_get_uint32 (buffer, offset, &offset, &value)) {
750 g_free (identifier);
751 break;
752 }
753
754 section = value;
755 g_hash_table_replace (self->identifiers, identifier, GUINT_TO_POINTER (section));
756
757 /* Track that we've seen this identifier */
758 g_hash_table_remove (self->checks, identifier);
759 }
760
761 /* Completed reading all */
762 if (i == count)
763 return GKM_DATA_SUCCESS;
764
765 /* Failed for some reason, data is bad */
766 return GKM_DATA_FAILURE;
767 }
768
769 static GkmDataResult
770 update_from_any_block (guint block, EggBuffer *buffer, GkmSecret *login, gpointer user_data)
771 {
772 UnknownBlock *unknown;
773 GkmGnome2File *self;
774 GkmDataResult res;
775
776 g_assert (GKM_IS_GNOME2_FILE (user_data));
777 self = GKM_GNOME2_FILE (user_data);
778
779 switch (block) {
780 case FILE_BLOCK_INDEX:
781 res = update_from_index_block (self, buffer);
782 break;
783 case FILE_BLOCK_PRIVATE:
784 res = update_from_private_block (self, buffer, login);
785 break;
786 case FILE_BLOCK_PUBLIC:
787 res = update_from_public_block (self, buffer);
788 break;
789 default:
790 res = GKM_DATA_UNRECOGNIZED;
791 break;
792 };
793
794 /* If unrecognized data block, then stash as unknown */
795 if (res == GKM_DATA_UNRECOGNIZED) {
796 unknown = g_slice_new0 (UnknownBlock);
797 unknown->type = block;
798 egg_buffer_init_full (&unknown->buffer, buffer->len, PUBLIC_ALLOC);
799 egg_buffer_append (&unknown->buffer, buffer->buf, buffer->len);
800 self->unknowns = g_list_prepend (self->unknowns, unknown);
801 res = GKM_DATA_SUCCESS;
802 }
803
804 return res;
805 }
806
807 static void
808 write_each_attribute (gpointer key, gpointer value, gpointer data)
809 {
810 CK_ATTRIBUTE_PTR attr = value;
811 EggBuffer *buffer = data;
812 egg_buffer_add_uint64 (buffer, attr->type);
813 g_assert (attr->ulValueLen != (gulong)-1);
814 egg_buffer_add_byte_array (buffer, attr->pValue, attr->ulValueLen);
815 }
816
817 static void
818 write_each_entry (gpointer key, gpointer value, gpointer data)
819 {
820 EggBuffer *buffer = data;
821 const gchar *unique = key;
822 GHashTable *attributes = value;
823
824 egg_buffer_add_string (buffer, unique);
825 egg_buffer_add_uint32 (buffer, g_hash_table_size (attributes));
826 g_hash_table_foreach (attributes, write_each_attribute, buffer);
827 }
828
829 static GkmDataResult
830 write_entries_to_block (GkmGnome2File *self, GHashTable *entries, EggBuffer *buffer)
831 {
832 gsize offset;
833
834 g_assert (GKM_GNOME2_FILE (self));
835 g_assert (entries);
836 g_assert (buffer);
837
838 /* Reserve space for the length */
839 offset = buffer->len;
840 egg_buffer_add_uint32 (buffer, 0);
841
842 /* The number of attributes we'll be encountering */
843 egg_buffer_add_uint32 (buffer, g_hash_table_size (entries));
844
845 /* Fill in the attributes */
846 g_hash_table_foreach (entries, write_each_entry, buffer);
847
848 g_return_val_if_fail (!egg_buffer_has_error (buffer), GKM_DATA_FAILURE);
849
850 /* Fill in the length */
851 egg_buffer_set_uint32 (buffer, offset, buffer->len);
852
853 /* Hash the entire dealio */
854 if (!hash_buffer (buffer))
855 return GKM_DATA_FAILURE;
856
857 return GKM_DATA_SUCCESS;
858 }
859
860 static GkmDataResult
861 write_private_to_block (GkmGnome2File *self, EggBuffer *buffer, GkmSecret *login)
862 {
863 EggBuffer secure;
864 GkmDataResult res;
865
866 g_assert (GKM_IS_GNOME2_FILE (self));
867 g_assert (buffer);
868
869 if (login == NULL) {
870 /* Must lock the private data in some way */
871 if (self->privates && g_hash_table_size (self->privates))
872 return GKM_DATA_LOCKED;
873
874 /* Not storing privates */
875 else
876 return GKM_DATA_UNRECOGNIZED;
877 } else {
878 /* We didn't load the privates, can't store them back */
879 if (self->privates == NULL)
880 return GKM_DATA_LOCKED;
881 }
882
883 egg_buffer_init_full (&secure, 1024, PRIVATE_ALLOC);
884
885 res = write_entries_to_block (self, self->privates, &secure);
886 if (res == GKM_DATA_SUCCESS)
887 res = encrypt_buffer (&secure, login, buffer);
888
889 egg_buffer_uninit (&secure);
890 return res;
891 }
892
893 static GkmDataResult
894 write_public_to_block (GkmGnome2File *self, EggBuffer *buffer)
895 {
896 g_assert (GKM_IS_GNOME2_FILE (self));
897 g_assert (buffer);
898
899 return write_entries_to_block (self, self->publics, buffer);
900 }
901
902 static void
903 write_each_index_identifier (gpointer key, gpointer value, gpointer data)
904 {
905 egg_buffer_add_string (data, key);
906 egg_buffer_add_uint32 (data, GPOINTER_TO_UINT (value));
907 }
908
909 static GkmDataResult
910 write_index_to_block (GkmGnome2File *self, EggBuffer *buffer)
911 {
912 g_assert (GKM_IS_GNOME2_FILE (self));
913 g_assert (buffer);
914
915 /* The number of entries */
916 egg_buffer_add_uint32 (buffer, g_hash_table_size (self->identifiers));
917
918 /* Now write out all the entries */
919 g_hash_table_foreach (self->identifiers, write_each_index_identifier, buffer);
920
921 return egg_buffer_has_error (buffer) ? GKM_DATA_FAILURE : GKM_DATA_SUCCESS;
922 }
923
924 static GkmDataResult
925 identifier_to_attributes (GkmGnome2File *self, const gchar *identifier, GHashTable **attributes)
926 {
927 GHashTable *entries;
928 gpointer value;
929 guint section;
930
931 g_assert (GKM_IS_GNOME2_FILE (self));
932 g_assert (identifier);
933 g_assert (attributes);
934
935 if (!g_hash_table_lookup_extended (self->identifiers, identifier, NULL, &value))
936 return GKM_DATA_UNRECOGNIZED;
937
938 section = GPOINTER_TO_UINT (value);
939 if (section == GKM_GNOME2_FILE_SECTION_PRIVATE)
940 entries = self->privates;
941 else
942 entries = self->publics;
943
944 if (entries == NULL)
945 return GKM_DATA_LOCKED;
946
947 *attributes = g_hash_table_lookup (entries, identifier);
948 g_return_val_if_fail (*attributes, GKM_DATA_UNRECOGNIZED);
949
950 return GKM_DATA_SUCCESS;
951 }
952
953 static void
954 free_unknown_block_list (GList *list)
955 {
956 UnknownBlock *unknown;
957 GList *l;
958
959 for (l = list; l; l = g_list_next (l)) {
960 unknown = l->data;
961 g_assert (unknown);
962 egg_buffer_uninit (&unknown->buffer);
963 g_slice_free (UnknownBlock, unknown);
964 }
965
966 g_list_free (list);
967 }
968
969 static gint
970 sort_unknowns_by_type (gconstpointer a, gconstpointer b)
971 {
972 const UnknownBlock *ua = a;
973 const UnknownBlock *ub = b;
974
975 g_assert (ua);
976 g_assert (ub);
977
978 if (ua->type == ub->type)
979 return 0;
980
981 return ua->type > ub->type ? 1 : -1;
982 }
983
984 typedef struct _ForeachArgs {
985 GkmGnome2File *self;
986 GkmGnome2FileFunc func;
987 gpointer user_data;
988 } ForeachArgs;
989
990 static void
991 foreach_identifier (gpointer key, gpointer value, gpointer data)
992 {
993 ForeachArgs *args = data;
994 g_assert (GKM_IS_GNOME2_FILE (args->self));
995 (args->func) (args->self, key, args->user_data);
996 }
997
998 static void
999 dump_attributes (gpointer key, gpointer value, gpointer user_data)
1000 {
1001 CK_ATTRIBUTE_PTR attr = value;
1002 gulong *type = key;
1003 gchar *text;
1004
1005 g_assert (type);
1006 g_assert (value);
1007
1008 if (attr->pValue == NULL)
1009 text = g_strdup ("NULL");
1010 else
1011 text = egg_hex_encode_full (attr->pValue, attr->ulValueLen, TRUE, " ", 1);
1012
1013 g_print ("\t0x%08x: %s\n", (guint)*type, text);
1014 g_free (text);
1015 }
1016
1017 static void
1018 dump_identifier_and_attributes (GkmGnome2File *self, const gchar *identifier, gpointer user_data)
1019 {
1020 GHashTable *attributes;
1021 guint section;
1022
1023 g_assert (GKM_IS_GNOME2_FILE (self));
1024
1025 if (!gkm_gnome2_file_lookup_entry (self, identifier, &section))
1026 g_assert_not_reached ();
1027
1028 if (GPOINTER_TO_UINT (user_data) == section) {
1029 g_print ("%s\n", identifier);
1030 if (identifier_to_attributes (self, identifier, &attributes) != GKM_DATA_SUCCESS)
1031 g_assert_not_reached ();
1032 g_hash_table_foreach (attributes, dump_attributes, NULL);
1033 g_print ("\n");
1034 }
1035 }
1036
1037
1038 /* -----------------------------------------------------------------------------
1039 * OBJECT
1040 */
1041
1042 static void
1043 gkm_gnome2_file_init (GkmGnome2File *self)
1044 {
1045 self->identifiers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1046 self->publics = entries_new ();
1047 self->privates = entries_new ();
1048
1049 self->unknowns = NULL;
1050
1051 self->checks = NULL;
1052 }
1053
1054 static void
1055 gkm_gnome2_file_finalize (GObject *obj)
1056 {
1057 GkmGnome2File *self = GKM_GNOME2_FILE (obj);
1058
1059 g_assert (self->identifiers);
1060 g_hash_table_destroy (self->identifiers);
1061 self->identifiers = NULL;
1062
1063 g_assert (self->checks == NULL);
1064
1065 g_assert (self->publics);
1066 g_hash_table_destroy (self->publics);
1067 self->publics = NULL;
1068
1069 if (self->privates)
1070 g_hash_table_destroy (self->privates);
1071 self->privates = NULL;
1072
1073 free_unknown_block_list (self->unknowns);
1074 self->unknowns = NULL;
1075
1076 G_OBJECT_CLASS (gkm_gnome2_file_parent_class)->finalize (obj);
1077 }
1078
1079 static void
1080 gkm_gnome2_file_set_property (GObject *obj, guint prop_id, const GValue *value,
1081 GParamSpec *pspec)
1082 {
1083 switch (prop_id) {
1084 default:
1085 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
1086 break;
1087 }
1088 }
1089
1090 static void
1091 gkm_gnome2_file_get_property (GObject *obj, guint prop_id, GValue *value,
1092 GParamSpec *pspec)
1093 {
1094 switch (prop_id) {
1095 default:
1096 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
1097 break;
1098 }
1099 }
1100
1101 static void
1102 gkm_gnome2_file_class_init (GkmGnome2FileClass *klass)
1103 {
1104 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1105
1106 gobject_class->finalize = gkm_gnome2_file_finalize;
1107 gobject_class->set_property = gkm_gnome2_file_set_property;
1108 gobject_class->get_property = gkm_gnome2_file_get_property;
1109
1110 signals[ENTRY_ADDED] = g_signal_new ("entry-added", GKM_TYPE_GNOME2_FILE,
1111 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GkmGnome2FileClass, entry_added),
1112 NULL, NULL, g_cclosure_marshal_VOID__STRING,
1113 G_TYPE_NONE, 1, G_TYPE_STRING);
1114
1115 signals[ENTRY_CHANGED] = g_signal_new ("entry-changed", GKM_TYPE_GNOME2_FILE,
1116 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GkmGnome2FileClass, entry_changed),
1117 NULL, NULL, gkm_marshal_VOID__STRING_ULONG,
1118 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_ULONG);
1119
1120 signals[ENTRY_REMOVED] = g_signal_new ("entry-removed", GKM_TYPE_GNOME2_FILE,
1121 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GkmGnome2FileClass, entry_removed),
1122 NULL, NULL, g_cclosure_marshal_VOID__STRING,
1123 G_TYPE_NONE, 1, G_TYPE_STRING);
1124 }
1125
1126 /* -----------------------------------------------------------------------------
1127 * PUBLIC
1128 */
1129
1130 GkmGnome2File*
1131 gkm_gnome2_file_new (void)
1132 {
1133 return g_object_new (GKM_TYPE_GNOME2_FILE, NULL);
1134 }
1135
1136 GkmDataResult
1137 gkm_gnome2_file_read_fd (GkmGnome2File *self, int fd, GkmSecret *login)
1138 {
1139 GkmDataResult res;
1140
1141 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), GKM_DATA_FAILURE);
1142
1143 /* Reads are not reentrant for a single data file */
1144 g_return_val_if_fail (self->checks == NULL, GKM_DATA_FAILURE);
1145
1146 self->sections = 0;
1147
1148 /* Free all the old unknowns */
1149 free_unknown_block_list (self->unknowns);
1150 self->unknowns = NULL;
1151
1152 /* Setup a hash table to monitor the actual data read */
1153 self->checks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1154 g_hash_table_foreach (self->identifiers, copy_each_identifier, self->checks);
1155
1156 res = parse_file_blocks (fd, update_from_any_block, login, self);
1157 if (res == GKM_DATA_SUCCESS) {
1158
1159 /* Our last read was a success, can write */
1160 self->incomplete = FALSE;
1161
1162 /* Remove the ones we didn't see */
1163 g_hash_table_foreach (self->checks, remove_each_identifier, self);
1164
1165 /*
1166 * There's a special where we've read a file without a private section.
1167 * We should be ready to accept privates (and then lock them next
1168 * time around).
1169 */
1170
1171 if (self->privates == NULL && !(self->sections & GKM_GNOME2_FILE_SECTION_PRIVATE))
1172 self->privates = entries_new ();
1173
1174 /* Note that our last read failed */
1175 } else {
1176 self->incomplete = TRUE;
1177 }
1178
1179 g_hash_table_destroy (self->checks);
1180 self->checks = NULL;
1181
1182 return res;
1183 }
1184
1185 GkmDataResult
1186 gkm_gnome2_file_write_fd (GkmGnome2File *self, int fd, GkmSecret *login)
1187 {
1188 guint types[3] = { FILE_BLOCK_INDEX, FILE_BLOCK_PRIVATE, FILE_BLOCK_PUBLIC };
1189 GList *unknowns, *unk;
1190 UnknownBlock *block;
1191 GkmDataResult res;
1192 EggBuffer buffer;
1193 guint type;
1194 gint i;
1195
1196 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), GKM_DATA_FAILURE);
1197 g_return_val_if_fail (!self->incomplete, GKM_DATA_FAILURE);
1198
1199 /* Write out the header */
1200 if (!write_all_bytes (fd, FILE_HEADER, FILE_HEADER_LEN))
1201 return GKM_DATA_FAILURE;
1202
1203 unknowns = g_list_copy (self->unknowns);
1204 unknowns = g_list_sort (unknowns, sort_unknowns_by_type);
1205 egg_buffer_init_full (&buffer, 8192, PUBLIC_ALLOC);
1206
1207 /*
1208 * All blocks are written in sorted order by their block
1209 * type. This includes unknown blocks.
1210 */
1211
1212 unk = unknowns;
1213 res = GKM_DATA_SUCCESS;
1214
1215 for (i = 0; i < G_N_ELEMENTS(types); ++i) {
1216 type = types[i];
1217
1218 /* Write out all the unknowns before this block */
1219 while (unk != NULL && res == GKM_DATA_SUCCESS) {
1220 block = (UnknownBlock*)unk->data;
1221 if (block->type > type)
1222 break;
1223 res = write_file_block (fd, block->type, &block->buffer);
1224 unk = g_list_next (unk);
1225 }
1226
1227 if (res != GKM_DATA_SUCCESS)
1228 break;
1229
1230 /* Prepare the block of this type */
1231 egg_buffer_reset (&buffer);
1232 switch (type) {
1233 case FILE_BLOCK_INDEX:
1234 res = write_index_to_block (self, &buffer);
1235 break;
1236 case FILE_BLOCK_PRIVATE:
1237 res = write_private_to_block (self, &buffer, login);
1238 break;
1239 case FILE_BLOCK_PUBLIC:
1240 res = write_public_to_block (self, &buffer);
1241 break;
1242 }
1243
1244 /* Write it out, if we got anything */
1245 if (res == GKM_DATA_SUCCESS)
1246 res = write_file_block (fd, type, &buffer);
1247 else if (res == GKM_DATA_UNRECOGNIZED)
1248 res = GKM_DATA_SUCCESS;
1249
1250 if (res != GKM_DATA_SUCCESS)
1251 break;
1252 }
1253
1254 /* Write out all remaining unknowns */
1255 while (unk != NULL && res == GKM_DATA_SUCCESS) {
1256 block = (UnknownBlock*)unk->data;
1257 res = write_file_block (fd, block->type, &block->buffer);
1258 unk = g_list_next (unk);
1259 }
1260
1261 g_list_free (unknowns);
1262 egg_buffer_uninit (&buffer);
1263 return res;
1264 }
1265
1266 gboolean
1267 gkm_gnome2_file_lookup_entry (GkmGnome2File *self, const gchar *identifier, guint *section)
1268 {
1269 gpointer value;
1270
1271 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), FALSE);
1272 g_return_val_if_fail (identifier, FALSE);
1273
1274 if (!g_hash_table_lookup_extended (self->identifiers, identifier, NULL, &value))
1275 return FALSE;
1276
1277 if (section != NULL)
1278 *section = GPOINTER_TO_UINT (value);
1279
1280 return TRUE;
1281 }
1282
1283 void
1284 gkm_gnome2_file_foreach_entry (GkmGnome2File *self, GkmGnome2FileFunc func, gpointer user_data)
1285 {
1286 ForeachArgs args = { self, func, user_data };
1287
1288 g_return_if_fail (GKM_IS_GNOME2_FILE (self));
1289 g_return_if_fail (func);
1290
1291 g_hash_table_foreach (self->identifiers, foreach_identifier, &args);
1292 }
1293
1294 GkmDataResult
1295 gkm_gnome2_file_unique_entry (GkmGnome2File *self, gchar **identifier)
1296 {
1297 gchar *base, *ext;
1298 guint seed = 1;
1299
1300 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), GKM_DATA_FAILURE);
1301 g_return_val_if_fail (identifier, GKM_DATA_FAILURE);
1302
1303 /* Check if original is unique */
1304 if (*identifier != NULL) {
1305 if (!gkm_gnome2_file_lookup_entry (self, *identifier, NULL))
1306 return GKM_DATA_SUCCESS;
1307 }
1308
1309 if (*identifier == NULL)
1310 *identifier = g_strdup_printf ("object-%08x", ABS (g_random_int ()));
1311
1312 /* Take ownership of the identifier, and out an extension */
1313 base = *identifier;
1314 *identifier = NULL;
1315 ext = strrchr (base, '.');
1316 if (ext != NULL)
1317 *(ext++) = '\0';
1318
1319 for (seed = 0; TRUE; ++seed) {
1320 *identifier = g_strdup_printf ("%s-%d%s%s", base, seed, ext ? "." : "", ext ? ext : "");
1321 if (!gkm_gnome2_file_lookup_entry (self, *identifier, NULL))
1322 break;
1323
1324 if (seed > 1000000) {
1325 g_warning ("couldn't find a unique identifier in a %d tries", seed);
1326 g_free (base);
1327 return GKM_DATA_FAILURE;
1328 }
1329
1330 g_free (*identifier);
1331 *identifier = NULL;
1332 }
1333
1334 g_free (base);
1335 return GKM_DATA_SUCCESS;
1336 }
1337
1338 GkmDataResult
1339 gkm_gnome2_file_create_entry (GkmGnome2File *self, const gchar *identifier, guint section)
1340 {
1341 GHashTable *attributes;
1342 GHashTable *entries;
1343
1344 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), GKM_DATA_FAILURE);
1345 g_return_val_if_fail (identifier, GKM_DATA_FAILURE);
1346
1347 if (section == GKM_GNOME2_FILE_SECTION_PRIVATE) {
1348 if (!self->privates)
1349 return GKM_DATA_LOCKED;
1350 entries = self->privates;
1351 } else {
1352 entries = self->publics;
1353 }
1354
1355 /* Make sure it's not already here */
1356 g_return_val_if_fail (g_hash_table_lookup (entries, identifier) == NULL, GKM_DATA_FAILURE);
1357
1358 /* Add the new entry to the appropriate table */
1359 attributes = attributes_new ();
1360 g_hash_table_replace (entries, g_strdup (identifier), attributes);
1361 g_hash_table_replace (self->identifiers, g_strdup (identifier), GUINT_TO_POINTER (section));
1362
1363 g_signal_emit (self, signals[ENTRY_ADDED], 0, identifier);
1364 return GKM_DATA_SUCCESS;
1365 }
1366
1367 GkmDataResult
1368 gkm_gnome2_file_destroy_entry (GkmGnome2File *self, const gchar *identifier)
1369 {
1370 GHashTable *entries;
1371 guint section;
1372
1373 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), GKM_DATA_FAILURE);
1374 g_return_val_if_fail (identifier, GKM_DATA_FAILURE);
1375
1376 if (!gkm_gnome2_file_lookup_entry (self, identifier, &section))
1377 return GKM_DATA_UNRECOGNIZED;
1378
1379 if (section == GKM_GNOME2_FILE_SECTION_PRIVATE) {
1380 if (!self->privates)
1381 return GKM_DATA_LOCKED;
1382 entries = self->privates;
1383 } else {
1384 entries = self->publics;
1385 }
1386
1387 if (!g_hash_table_remove (self->identifiers, identifier))
1388 g_return_val_if_reached (GKM_DATA_UNRECOGNIZED);
1389 if (!g_hash_table_remove (entries, identifier))
1390 g_return_val_if_reached (GKM_DATA_UNRECOGNIZED);
1391
1392 g_signal_emit (self, signals[ENTRY_REMOVED], 0, identifier);
1393 return GKM_DATA_SUCCESS;
1394 }
1395
1396 GkmDataResult
1397 gkm_gnome2_file_write_value (GkmGnome2File *self, const gchar *identifier,
1398 gulong type, gconstpointer value, gsize n_value)
1399 {
1400 GHashTable *attributes;
1401 CK_ATTRIBUTE_PTR at;
1402 CK_ATTRIBUTE attr;
1403 GkmDataResult res;
1404
1405 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), GKM_DATA_FAILURE);
1406 g_return_val_if_fail (identifier, GKM_DATA_FAILURE);
1407 g_return_val_if_fail (value || !n_value, GKM_DATA_FAILURE);
1408
1409 /* Find the right set of attributes */
1410 res = identifier_to_attributes (self, identifier, &attributes);
1411 if (res != GKM_DATA_SUCCESS)
1412 return res;
1413
1414 attr.type = type;
1415 attr.pValue = (void*)value;
1416 attr.ulValueLen = n_value;
1417
1418 at = g_hash_table_lookup (attributes, &type);
1419 if (at != NULL && gkm_attribute_equal (at, &attr))
1420 return GKM_DATA_SUCCESS;
1421
1422 at = attribute_dup (&attr);
1423 g_hash_table_replace (attributes, &(at->type), at);
1424
1425 g_signal_emit (self, signals[ENTRY_CHANGED], 0, identifier, type);
1426 return GKM_DATA_SUCCESS;
1427 }
1428
1429 GkmDataResult
1430 gkm_gnome2_file_read_value (GkmGnome2File *self, const gchar *identifier,
1431 gulong type, gconstpointer *value, gsize *n_value)
1432 {
1433 CK_ATTRIBUTE_PTR attr;
1434 GHashTable *attributes;
1435 GkmDataResult res;
1436
1437 g_return_val_if_fail (GKM_IS_GNOME2_FILE (self), GKM_DATA_FAILURE);
1438 g_return_val_if_fail (identifier, GKM_DATA_FAILURE);
1439 g_return_val_if_fail (value, GKM_DATA_FAILURE);
1440 g_return_val_if_fail (n_value, GKM_DATA_FAILURE);
1441
1442 /* Find the right set of attributes */
1443 res = identifier_to_attributes (self, identifier, &attributes);
1444 if (res != GKM_DATA_SUCCESS)
1445 return res;
1446
1447 attr = g_hash_table_lookup (attributes, &type);
1448 if (attr == NULL)
1449 return GKM_DATA_UNRECOGNIZED;
1450
1451 g_assert (attr->type == type);
1452 *value = attr->pValue;
1453 *n_value = attr->ulValueLen;
1454 return GKM_DATA_SUCCESS;
1455 }
1456
1457 gboolean
1458 gkm_gnome2_file_have_section (GkmGnome2File *self, guint section)
1459 {
1460 return (self->sections & section) ? TRUE : FALSE;
1461 }
1462
1463 void
1464 gkm_gnome2_file_dump (GkmGnome2File *self)
1465 {
1466 g_print ("PUBLIC:\n\n");
1467 gkm_gnome2_file_foreach_entry (self, dump_identifier_and_attributes,
1468 GUINT_TO_POINTER (GKM_GNOME2_FILE_SECTION_PUBLIC));
1469 g_print ("PRIVATE:\n\n");
1470 gkm_gnome2_file_foreach_entry (self, dump_identifier_and_attributes,
1471 GUINT_TO_POINTER (GKM_GNOME2_FILE_SECTION_PRIVATE));
1472 }