00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "dbus-keyring.h"
00025 #include "dbus-protocol.h"
00026 #include <dbus/dbus-string.h>
00027 #include <dbus/dbus-list.h>
00028 #include <dbus/dbus-sysdeps.h>
00029
00066 #define NEW_KEY_TIMEOUT_SECONDS (60*5)
00067
00072 #define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
00073
00076 #define MAX_TIME_TRAVEL_SECONDS (60*5)
00077
00082 #ifdef DBUS_BUILD_TESTS
00083 #define MAX_KEYS_IN_FILE 10
00084 #else
00085 #define MAX_KEYS_IN_FILE 256
00086 #endif
00087
00091 typedef struct
00092 {
00093 dbus_int32_t id;
00095 long creation_time;
00100 DBusString secret;
00102 } DBusKey;
00103
00110 struct DBusKeyring
00111 {
00112 int refcount;
00113 DBusString directory;
00114 DBusString filename;
00115 DBusString filename_lock;
00116 DBusKey *keys;
00117 int n_keys;
00118 DBusCredentials *credentials;
00119 };
00120
00121 static DBusKeyring*
00122 _dbus_keyring_new (void)
00123 {
00124 DBusKeyring *keyring;
00125
00126 keyring = dbus_new0 (DBusKeyring, 1);
00127 if (keyring == NULL)
00128 goto out_0;
00129
00130 if (!_dbus_string_init (&keyring->directory))
00131 goto out_1;
00132
00133 if (!_dbus_string_init (&keyring->filename))
00134 goto out_2;
00135
00136 if (!_dbus_string_init (&keyring->filename_lock))
00137 goto out_3;
00138
00139 keyring->refcount = 1;
00140 keyring->keys = NULL;
00141 keyring->n_keys = 0;
00142
00143 return keyring;
00144
00145
00146 _dbus_string_free (&keyring->filename_lock);
00147 out_3:
00148 _dbus_string_free (&keyring->filename);
00149 out_2:
00150 _dbus_string_free (&keyring->directory);
00151 out_1:
00152 dbus_free (keyring);
00153 out_0:
00154 return NULL;
00155 }
00156
00157 static void
00158 free_keys (DBusKey *keys,
00159 int n_keys)
00160 {
00161 int i;
00162
00163
00164
00165 i = 0;
00166 while (i < n_keys)
00167 {
00168 _dbus_string_free (&keys[i].secret);
00169 ++i;
00170 }
00171
00172 dbus_free (keys);
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00192 #define MAX_LOCK_TIMEOUTS 32
00193
00194 #define LOCK_TIMEOUT_MILLISECONDS 250
00195
00196 static dbus_bool_t
00197 _dbus_keyring_lock (DBusKeyring *keyring)
00198 {
00199 int n_timeouts;
00200
00201 n_timeouts = 0;
00202 while (n_timeouts < MAX_LOCK_TIMEOUTS)
00203 {
00204 DBusError error;
00205
00206 dbus_error_init (&error);
00207 if (_dbus_create_file_exclusively (&keyring->filename_lock,
00208 &error))
00209 break;
00210
00211 _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n",
00212 LOCK_TIMEOUT_MILLISECONDS, error.message);
00213 dbus_error_free (&error);
00214
00215 _dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS);
00216
00217 ++n_timeouts;
00218 }
00219
00220 if (n_timeouts == MAX_LOCK_TIMEOUTS)
00221 {
00222 DBusError error;
00223
00224 _dbus_verbose ("Lock file timed out %d times, assuming stale\n",
00225 n_timeouts);
00226
00227 dbus_error_init (&error);
00228
00229 if (!_dbus_delete_file (&keyring->filename_lock, &error))
00230 {
00231 _dbus_verbose ("Couldn't delete old lock file: %s\n",
00232 error.message);
00233 dbus_error_free (&error);
00234 return FALSE;
00235 }
00236
00237 if (!_dbus_create_file_exclusively (&keyring->filename_lock,
00238 &error))
00239 {
00240 _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n",
00241 error.message);
00242 dbus_error_free (&error);
00243 return FALSE;
00244 }
00245 }
00246
00247 return TRUE;
00248 }
00249
00250 static void
00251 _dbus_keyring_unlock (DBusKeyring *keyring)
00252 {
00253 DBusError error;
00254 dbus_error_init (&error);
00255 if (!_dbus_delete_file (&keyring->filename_lock, &error))
00256 {
00257 _dbus_warn ("Failed to delete lock file: %s\n",
00258 error.message);
00259 dbus_error_free (&error);
00260 }
00261 }
00262
00263 static DBusKey*
00264 find_key_by_id (DBusKey *keys,
00265 int n_keys,
00266 int id)
00267 {
00268 int i;
00269
00270 i = 0;
00271 while (i < n_keys)
00272 {
00273 if (keys[i].id == id)
00274 return &keys[i];
00275
00276 ++i;
00277 }
00278
00279 return NULL;
00280 }
00281
00282 static dbus_bool_t
00283 add_new_key (DBusKey **keys_p,
00284 int *n_keys_p,
00285 DBusError *error)
00286 {
00287 DBusKey *new;
00288 DBusString bytes;
00289 int id;
00290 long timestamp;
00291 const unsigned char *s;
00292 dbus_bool_t retval;
00293 DBusKey *keys;
00294 int n_keys;
00295
00296 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00297
00298 if (!_dbus_string_init (&bytes))
00299 {
00300 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00301 return FALSE;
00302 }
00303
00304 keys = *keys_p;
00305 n_keys = *n_keys_p;
00306 retval = FALSE;
00307
00308
00309 retry:
00310
00311 if (!_dbus_generate_random_bytes (&bytes, 4))
00312 {
00313 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00314 goto out;
00315 }
00316
00317 s = (const unsigned char*) _dbus_string_get_const_data (&bytes);
00318
00319 id = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
00320 if (id < 0)
00321 id = - id;
00322 _dbus_assert (id >= 0);
00323
00324 if (find_key_by_id (keys, n_keys, id) != NULL)
00325 {
00326 _dbus_string_set_length (&bytes, 0);
00327 _dbus_verbose ("Key ID %d already existed, trying another one\n",
00328 id);
00329 goto retry;
00330 }
00331
00332 _dbus_verbose ("Creating key with ID %d\n", id);
00333
00334 #define KEY_LENGTH_BYTES 24
00335 _dbus_string_set_length (&bytes, 0);
00336 if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES))
00337 {
00338 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00339 goto out;
00340 }
00341
00342 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
00343 if (new == NULL)
00344 {
00345 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00346 goto out;
00347 }
00348
00349 keys = new;
00350 *keys_p = keys;
00351 n_keys += 1;
00352
00353 if (!_dbus_string_init (&keys[n_keys-1].secret))
00354 {
00355 n_keys -= 1;
00356 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00357 goto out;
00358 }
00359
00360 _dbus_get_current_time (×tamp, NULL);
00361
00362 keys[n_keys-1].id = id;
00363 keys[n_keys-1].creation_time = timestamp;
00364 if (!_dbus_string_move (&bytes, 0,
00365 &keys[n_keys-1].secret,
00366 0))
00367 {
00368 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00369 _dbus_string_free (&keys[n_keys-1].secret);
00370 n_keys -= 1;
00371 goto out;
00372 }
00373
00374 retval = TRUE;
00375
00376 out:
00377 *n_keys_p = n_keys;
00378
00379 _dbus_string_free (&bytes);
00380 return retval;
00381 }
00382
00397 static dbus_bool_t
00398 _dbus_keyring_reload (DBusKeyring *keyring,
00399 dbus_bool_t add_new,
00400 DBusError *error)
00401 {
00402 DBusString contents;
00403 DBusString line;
00404 dbus_bool_t retval;
00405 dbus_bool_t have_lock;
00406 DBusKey *keys;
00407 int n_keys;
00408 int i;
00409 long now;
00410 DBusError tmp_error;
00411
00412 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00413
00414 if (!_dbus_check_dir_is_private_to_user (&keyring->directory, error))
00415 return FALSE;
00416
00417 if (!_dbus_string_init (&contents))
00418 {
00419 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00420 return FALSE;
00421 }
00422
00423 if (!_dbus_string_init (&line))
00424 {
00425 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00426 _dbus_string_free (&contents);
00427 return FALSE;
00428 }
00429
00430 keys = NULL;
00431 n_keys = 0;
00432 retval = FALSE;
00433 have_lock = FALSE;
00434
00435 _dbus_get_current_time (&now, NULL);
00436
00437 if (add_new)
00438 {
00439 if (!_dbus_keyring_lock (keyring))
00440 {
00441 dbus_set_error (error, DBUS_ERROR_FAILED,
00442 "Could not lock keyring file to add to it");
00443 goto out;
00444 }
00445
00446 have_lock = TRUE;
00447 }
00448
00449 dbus_error_init (&tmp_error);
00450 if (!_dbus_file_get_contents (&contents,
00451 &keyring->filename,
00452 &tmp_error))
00453 {
00454 _dbus_verbose ("Failed to load keyring file: %s\n",
00455 tmp_error.message);
00456
00457 dbus_error_free (&tmp_error);
00458 }
00459
00460 if (!_dbus_string_validate_ascii (&contents, 0,
00461 _dbus_string_get_length (&contents)))
00462 {
00463 _dbus_warn ("Secret keyring file contains non-ASCII! Ignoring existing contents\n");
00464 _dbus_string_set_length (&contents, 0);
00465 }
00466
00467
00468
00469
00470 while (_dbus_string_pop_line (&contents, &line))
00471 {
00472 int next;
00473 long val;
00474 int id;
00475 long timestamp;
00476 int len;
00477 int end;
00478 DBusKey *new;
00479
00480
00481 if (n_keys >= (add_new ? MAX_KEYS_IN_FILE - 1 : MAX_KEYS_IN_FILE))
00482 break;
00483
00484 next = 0;
00485 if (!_dbus_string_parse_int (&line, 0, &val, &next))
00486 {
00487 _dbus_verbose ("could not parse secret key ID at start of line\n");
00488 continue;
00489 }
00490
00491 if (val > _DBUS_INT32_MAX || val < 0)
00492 {
00493 _dbus_verbose ("invalid secret key ID at start of line\n");
00494 continue;
00495 }
00496
00497 id = val;
00498
00499 _dbus_string_skip_blank (&line, next, &next);
00500
00501 if (!_dbus_string_parse_int (&line, next, ×tamp, &next))
00502 {
00503 _dbus_verbose ("could not parse secret key timestamp\n");
00504 continue;
00505 }
00506
00507 if (timestamp < 0 ||
00508 (now + MAX_TIME_TRAVEL_SECONDS) < timestamp ||
00509 (now - EXPIRE_KEYS_TIMEOUT_SECONDS) > timestamp)
00510 {
00511 _dbus_verbose ("dropping/ignoring %ld-seconds old key with timestamp %ld as current time is %ld\n",
00512 now - timestamp, timestamp, now);
00513 continue;
00514 }
00515
00516 _dbus_string_skip_blank (&line, next, &next);
00517
00518 len = _dbus_string_get_length (&line);
00519
00520 if ((len - next) == 0)
00521 {
00522 _dbus_verbose ("no secret key after ID and timestamp\n");
00523 continue;
00524 }
00525
00526
00527 new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1));
00528 if (new == NULL)
00529 {
00530 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00531 goto out;
00532 }
00533
00534 keys = new;
00535 n_keys += 1;
00536
00537 if (!_dbus_string_init (&keys[n_keys-1].secret))
00538 {
00539 n_keys -= 1;
00540 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00541 goto out;
00542 }
00543
00544 keys[n_keys-1].id = id;
00545 keys[n_keys-1].creation_time = timestamp;
00546 if (!_dbus_string_hex_decode (&line, next, &end,
00547 &keys[n_keys-1].secret, 0))
00548 {
00549 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00550 goto out;
00551 }
00552
00553 if (_dbus_string_get_length (&line) != end)
00554 {
00555 _dbus_verbose ("invalid hex encoding in keyring file\n");
00556 _dbus_string_free (&keys[n_keys - 1].secret);
00557 n_keys -= 1;
00558 continue;
00559 }
00560 }
00561
00562 _dbus_verbose ("Successfully loaded %d existing keys\n",
00563 n_keys);
00564
00565 if (add_new)
00566 {
00567 if (!add_new_key (&keys, &n_keys, error))
00568 {
00569 _dbus_verbose ("Failed to generate new key: %s\n",
00570 error ? error->message : "(unknown)");
00571 goto out;
00572 }
00573
00574 _dbus_string_set_length (&contents, 0);
00575
00576 i = 0;
00577 while (i < n_keys)
00578 {
00579 if (!_dbus_string_append_int (&contents,
00580 keys[i].id))
00581 goto nomem;
00582
00583 if (!_dbus_string_append_byte (&contents, ' '))
00584 goto nomem;
00585
00586 if (!_dbus_string_append_int (&contents,
00587 keys[i].creation_time))
00588 goto nomem;
00589
00590 if (!_dbus_string_append_byte (&contents, ' '))
00591 goto nomem;
00592
00593 if (!_dbus_string_hex_encode (&keys[i].secret, 0,
00594 &contents,
00595 _dbus_string_get_length (&contents)))
00596 goto nomem;
00597
00598 if (!_dbus_string_append_byte (&contents, '\n'))
00599 goto nomem;
00600
00601 ++i;
00602 continue;
00603
00604 nomem:
00605 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00606 goto out;
00607 }
00608
00609 if (!_dbus_string_save_to_file (&contents, &keyring->filename,
00610 error))
00611 goto out;
00612 }
00613
00614 if (keyring->keys)
00615 free_keys (keyring->keys, keyring->n_keys);
00616 keyring->keys = keys;
00617 keyring->n_keys = n_keys;
00618 keys = NULL;
00619 n_keys = 0;
00620
00621 retval = TRUE;
00622
00623 out:
00624 if (have_lock)
00625 _dbus_keyring_unlock (keyring);
00626
00627 if (! ((retval == TRUE && (error == NULL || error->name == NULL)) ||
00628 (retval == FALSE && (error == NULL || error->name != NULL))))
00629 {
00630 if (error && error->name)
00631 _dbus_verbose ("error is %s: %s\n", error->name, error->message);
00632 _dbus_warn ("returning %d but error pointer %p name %s\n",
00633 retval, error, error->name ? error->name : "(none)");
00634 _dbus_assert_not_reached ("didn't handle errors properly");
00635 }
00636
00637 if (keys != NULL)
00638 {
00639 i = 0;
00640 while (i < n_keys)
00641 {
00642 _dbus_string_zero (&keys[i].secret);
00643 _dbus_string_free (&keys[i].secret);
00644 ++i;
00645 }
00646
00647 dbus_free (keys);
00648 }
00649
00650 _dbus_string_free (&contents);
00651 _dbus_string_free (&line);
00652
00653 return retval;
00654 }
00655
00657
00670 DBusKeyring *
00671 _dbus_keyring_ref (DBusKeyring *keyring)
00672 {
00673 keyring->refcount += 1;
00674
00675 return keyring;
00676 }
00677
00684 void
00685 _dbus_keyring_unref (DBusKeyring *keyring)
00686 {
00687 keyring->refcount -= 1;
00688
00689 if (keyring->refcount == 0)
00690 {
00691 if (keyring->credentials)
00692 _dbus_credentials_unref (keyring->credentials);
00693
00694 _dbus_string_free (&keyring->filename);
00695 _dbus_string_free (&keyring->filename_lock);
00696 _dbus_string_free (&keyring->directory);
00697 free_keys (keyring->keys, keyring->n_keys);
00698 dbus_free (keyring);
00699 }
00700 }
00701
00712 DBusKeyring*
00713 _dbus_keyring_new_for_credentials (DBusCredentials *credentials,
00714 const DBusString *context,
00715 DBusError *error)
00716 {
00717 DBusString ringdir;
00718 DBusKeyring *keyring;
00719 dbus_bool_t error_set;
00720 DBusError tmp_error;
00721 DBusCredentials *our_credentials;
00722
00723 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00724
00725 keyring = NULL;
00726 error_set = FALSE;
00727 our_credentials = NULL;
00728
00729 if (!_dbus_string_init (&ringdir))
00730 {
00731 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00732 return NULL;
00733 }
00734
00735 if (credentials != NULL)
00736 {
00737 our_credentials = _dbus_credentials_copy (credentials);
00738 }
00739 else
00740 {
00741 our_credentials = _dbus_credentials_new_from_current_process ();
00742 }
00743
00744 if (our_credentials == NULL)
00745 goto failed;
00746
00747 if (_dbus_credentials_are_anonymous (our_credentials))
00748 {
00749 if (!_dbus_credentials_add_from_current_process (our_credentials))
00750 goto failed;
00751 }
00752
00753 if (!_dbus_append_keyring_directory_for_credentials (&ringdir,
00754 our_credentials))
00755 goto failed;
00756
00757 keyring = _dbus_keyring_new ();
00758 if (keyring == NULL)
00759 goto failed;
00760
00761 _dbus_assert (keyring->credentials == NULL);
00762 keyring->credentials = our_credentials;
00763 our_credentials = NULL;
00764
00765
00766 if (!_dbus_keyring_validate_context (context))
00767 {
00768 error_set = TRUE;
00769 dbus_set_error_const (error,
00770 DBUS_ERROR_FAILED,
00771 "Invalid context in keyring creation");
00772 goto failed;
00773 }
00774
00775
00776 if (!_dbus_string_copy (&ringdir, 0,
00777 &keyring->directory, 0))
00778 goto failed;
00779
00780
00781 if (!_dbus_string_copy (&keyring->directory, 0,
00782 &keyring->filename, 0))
00783 goto failed;
00784
00785 if (!_dbus_concat_dir_and_file (&keyring->filename,
00786 context))
00787 goto failed;
00788
00789
00790 if (!_dbus_string_copy (&keyring->filename, 0,
00791 &keyring->filename_lock, 0))
00792 goto failed;
00793
00794 if (!_dbus_string_append (&keyring->filename_lock, ".lock"))
00795 goto failed;
00796
00797
00798 dbus_error_init (&tmp_error);
00799 if (!_dbus_keyring_reload (keyring, FALSE, &tmp_error))
00800 {
00801 _dbus_verbose ("didn't load an existing keyring: %s\n",
00802 tmp_error.message);
00803 dbus_error_free (&tmp_error);
00804 }
00805
00806
00807
00808
00809
00810 dbus_error_init (&tmp_error);
00811 if (!_dbus_create_directory (&keyring->directory,
00812 &tmp_error))
00813 {
00814 _dbus_verbose ("Creating keyring directory: %s\n",
00815 tmp_error.message);
00816 dbus_error_free (&tmp_error);
00817 }
00818
00819 _dbus_string_free (&ringdir);
00820
00821 return keyring;
00822
00823 failed:
00824 if (!error_set)
00825 dbus_set_error_const (error,
00826 DBUS_ERROR_NO_MEMORY,
00827 NULL);
00828 if (our_credentials)
00829 _dbus_credentials_unref (our_credentials);
00830 if (keyring)
00831 _dbus_keyring_unref (keyring);
00832 _dbus_string_free (&ringdir);
00833 return NULL;
00834
00835 }
00836
00849 dbus_bool_t
00850 _dbus_keyring_validate_context (const DBusString *context)
00851 {
00852 if (_dbus_string_get_length (context) == 0)
00853 {
00854 _dbus_verbose ("context is zero-length\n");
00855 return FALSE;
00856 }
00857
00858 if (!_dbus_string_validate_ascii (context, 0,
00859 _dbus_string_get_length (context)))
00860 {
00861 _dbus_verbose ("context not valid ascii\n");
00862 return FALSE;
00863 }
00864
00865
00866 if (_dbus_string_find (context, 0, "/", NULL))
00867 {
00868 _dbus_verbose ("context contains a slash\n");
00869 return FALSE;
00870 }
00871
00872 if (_dbus_string_find (context, 0, "\\", NULL))
00873 {
00874 _dbus_verbose ("context contains a backslash\n");
00875 return FALSE;
00876 }
00877
00878
00879
00880
00881 if (_dbus_string_find (context, 0, ".", NULL))
00882 {
00883 _dbus_verbose ("context contains a dot\n");
00884 return FALSE;
00885 }
00886
00887
00888 if (_dbus_string_find_blank (context, 0, NULL))
00889 {
00890 _dbus_verbose ("context contains a blank\n");
00891 return FALSE;
00892 }
00893
00894 if (_dbus_string_find (context, 0, "\n", NULL))
00895 {
00896 _dbus_verbose ("context contains a newline\n");
00897 return FALSE;
00898 }
00899
00900 if (_dbus_string_find (context, 0, "\r", NULL))
00901 {
00902 _dbus_verbose ("context contains a carriage return\n");
00903 return FALSE;
00904 }
00905
00906 return TRUE;
00907 }
00908
00909 static DBusKey*
00910 find_recent_key (DBusKeyring *keyring)
00911 {
00912 int i;
00913 long tv_sec, tv_usec;
00914
00915 _dbus_get_current_time (&tv_sec, &tv_usec);
00916
00917 i = 0;
00918 while (i < keyring->n_keys)
00919 {
00920 DBusKey *key = &keyring->keys[i];
00921
00922 _dbus_verbose ("Key %d is %ld seconds old\n",
00923 i, tv_sec - key->creation_time);
00924
00925 if ((tv_sec - NEW_KEY_TIMEOUT_SECONDS) < key->creation_time)
00926 return key;
00927
00928 ++i;
00929 }
00930
00931 return NULL;
00932 }
00933
00945 int
00946 _dbus_keyring_get_best_key (DBusKeyring *keyring,
00947 DBusError *error)
00948 {
00949 DBusKey *key;
00950
00951 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00952
00953 key = find_recent_key (keyring);
00954 if (key)
00955 return key->id;
00956
00957
00958
00959
00960 if (!_dbus_keyring_reload (keyring, TRUE,
00961 error))
00962 return -1;
00963
00964 key = find_recent_key (keyring);
00965 if (key)
00966 return key->id;
00967 else
00968 {
00969 dbus_set_error_const (error,
00970 DBUS_ERROR_FAILED,
00971 "No recent-enough key found in keyring, and unable to create a new key");
00972 return -1;
00973 }
00974 }
00975
00984 dbus_bool_t
00985 _dbus_keyring_is_for_credentials (DBusKeyring *keyring,
00986 DBusCredentials *credentials)
00987 {
00988 return _dbus_credentials_same_user (keyring->credentials,
00989 credentials);
00990 }
00991
01003 dbus_bool_t
01004 _dbus_keyring_get_hex_key (DBusKeyring *keyring,
01005 int key_id,
01006 DBusString *hex_key)
01007 {
01008 DBusKey *key;
01009
01010 key = find_key_by_id (keyring->keys,
01011 keyring->n_keys,
01012 key_id);
01013 if (key == NULL)
01014 return TRUE;
01015
01016 return _dbus_string_hex_encode (&key->secret, 0,
01017 hex_key,
01018 _dbus_string_get_length (hex_key));
01019 }
01020
01022
01023 #ifdef DBUS_BUILD_TESTS
01024 #include "dbus-test.h"
01025 #include <stdio.h>
01026
01027 dbus_bool_t
01028 _dbus_keyring_test (void)
01029 {
01030 DBusString context;
01031 DBusKeyring *ring1;
01032 DBusKeyring *ring2;
01033 int id;
01034 DBusError error;
01035 int i;
01036
01037 ring1 = NULL;
01038 ring2 = NULL;
01039
01040
01041
01042 _dbus_string_init_const (&context, "foo");
01043 _dbus_assert (_dbus_keyring_validate_context (&context));
01044 _dbus_string_init_const (&context, "org_freedesktop_blah");
01045 _dbus_assert (_dbus_keyring_validate_context (&context));
01046
01047 _dbus_string_init_const (&context, "");
01048 _dbus_assert (!_dbus_keyring_validate_context (&context));
01049 _dbus_string_init_const (&context, ".foo");
01050 _dbus_assert (!_dbus_keyring_validate_context (&context));
01051 _dbus_string_init_const (&context, "bar.foo");
01052 _dbus_assert (!_dbus_keyring_validate_context (&context));
01053 _dbus_string_init_const (&context, "bar/foo");
01054 _dbus_assert (!_dbus_keyring_validate_context (&context));
01055 _dbus_string_init_const (&context, "bar\\foo");
01056 _dbus_assert (!_dbus_keyring_validate_context (&context));
01057 _dbus_string_init_const (&context, "foo\xfa\xf0");
01058 _dbus_assert (!_dbus_keyring_validate_context (&context));
01059 _dbus_string_init_const (&context, "foo\x80");
01060 _dbus_assert (!_dbus_keyring_validate_context (&context));
01061 _dbus_string_init_const (&context, "foo\x7f");
01062 _dbus_assert (_dbus_keyring_validate_context (&context));
01063 _dbus_string_init_const (&context, "foo bar");
01064 _dbus_assert (!_dbus_keyring_validate_context (&context));
01065
01066 if (!_dbus_string_init (&context))
01067 _dbus_assert_not_reached ("no memory");
01068 if (!_dbus_string_append_byte (&context, '\0'))
01069 _dbus_assert_not_reached ("no memory");
01070 _dbus_assert (!_dbus_keyring_validate_context (&context));
01071 _dbus_string_free (&context);
01072
01073
01074
01075
01076
01077 _dbus_string_init_const (&context, "org_freedesktop_dbus_testsuite");
01078 dbus_error_init (&error);
01079 ring1 = _dbus_keyring_new_for_credentials (NULL, &context,
01080 &error);
01081 _dbus_assert (ring1);
01082 _dbus_assert (error.name == NULL);
01083
01084 id = _dbus_keyring_get_best_key (ring1, &error);
01085 if (id < 0)
01086 {
01087 fprintf (stderr, "Could not load keyring: %s\n", error.message);
01088 dbus_error_free (&error);
01089 goto failure;
01090 }
01091
01092 ring2 = _dbus_keyring_new_for_credentials (NULL, &context, &error);
01093 _dbus_assert (ring2);
01094 _dbus_assert (error.name == NULL);
01095
01096 if (ring1->n_keys != ring2->n_keys)
01097 {
01098 fprintf (stderr, "Different number of keys in keyrings\n");
01099 goto failure;
01100 }
01101
01102
01103
01104
01105 i = 0;
01106 while (i < ring1->n_keys)
01107 {
01108 if (ring1->keys[i].id != ring2->keys[i].id)
01109 {
01110 fprintf (stderr, "Keyring 1 has first key ID %d and keyring 2 has %d\n",
01111 ring1->keys[i].id, ring2->keys[i].id);
01112 goto failure;
01113 }
01114
01115 if (ring1->keys[i].creation_time != ring2->keys[i].creation_time)
01116 {
01117 fprintf (stderr, "Keyring 1 has first key time %ld and keyring 2 has %ld\n",
01118 ring1->keys[i].creation_time, ring2->keys[i].creation_time);
01119 goto failure;
01120 }
01121
01122 if (!_dbus_string_equal (&ring1->keys[i].secret,
01123 &ring2->keys[i].secret))
01124 {
01125 fprintf (stderr, "Keyrings 1 and 2 have different secrets for same ID/timestamp\n");
01126 goto failure;
01127 }
01128
01129 ++i;
01130 }
01131
01132 printf (" %d keys in test\n", ring1->n_keys);
01133
01134
01135 _dbus_keyring_ref (ring1);
01136 _dbus_keyring_ref (ring2);
01137 _dbus_keyring_unref (ring1);
01138 _dbus_keyring_unref (ring2);
01139
01140
01141
01142 _dbus_keyring_unref (ring1);
01143 _dbus_keyring_unref (ring2);
01144
01145 return TRUE;
01146
01147 failure:
01148 if (ring1)
01149 _dbus_keyring_unref (ring1);
01150 if (ring2)
01151 _dbus_keyring_unref (ring2);
01152
01153 return FALSE;
01154 }
01155
01156 #endif
01157