cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/os_crypt/key_storage_linux.h" |
| 6 | |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 7 | #include "base/bind.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 8 | #include "base/environment.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "base/nix/xdg_util.h" |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 11 | #include "base/sequenced_task_runner.h" |
| 12 | #include "base/synchronization/waitable_event.h" |
| 13 | #include "base/task_runner_util.h" |
| 14 | #include "base/threading/thread_restrictions.h" |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 15 | #include "components/os_crypt/key_storage_config_linux.h" |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 16 | #include "components/os_crypt/key_storage_util_linux.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 17 | |
| 18 | #if defined(USE_LIBSECRET) |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 19 | #include "components/os_crypt/key_storage_libsecret.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 20 | #endif |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 21 | #if defined(USE_KEYRING) |
| 22 | #include "components/os_crypt/key_storage_keyring.h" |
| 23 | #endif |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 24 | #if defined(USE_KWALLET) |
| 25 | #include "components/os_crypt/key_storage_kwallet.h" |
| 26 | #endif |
| 27 | |
thestig | c0bfd64 | 2016-08-22 18:10:35 | [diff] [blame] | 28 | #if defined(GOOGLE_CHROME_BUILD) |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 29 | const char KeyStorageLinux::kFolderName[] = "Chrome Keys"; |
| 30 | const char KeyStorageLinux::kKey[] = "Chrome Safe Storage"; |
| 31 | #else |
| 32 | const char KeyStorageLinux::kFolderName[] = "Chromium Keys"; |
| 33 | const char KeyStorageLinux::kKey[] = "Chromium Safe Storage"; |
| 34 | #endif |
| 35 | |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 36 | namespace { |
| 37 | |
| 38 | // Copies the password value from |result| to |password| and notifies on |
| 39 | // |on_password_received| that the result is ready. |
| 40 | void OnPasswordReceived(base::WaitableEvent* on_password_received, |
| 41 | std::string* password, |
| 42 | const std::string& result) { |
| 43 | *password = result; |
| 44 | if (on_password_received) |
| 45 | on_password_received->Signal(); |
| 46 | } |
| 47 | |
| 48 | // Copies the initialisation result from |result| to |success| and notifies on |
| 49 | // |on_initialized| that the result is ready. |
| 50 | void OnInitialized(base::WaitableEvent* on_initialized, |
| 51 | bool* success, |
| 52 | const bool& result) { |
| 53 | *success = result; |
| 54 | if (on_initialized) |
| 55 | on_initialized->Signal(); |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 60 | // static |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 61 | std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService( |
| 62 | const os_crypt::Config& config) { |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 63 | #if defined(USE_LIBSECRET) || defined(USE_KEYRING) || defined(USE_KWALLET) |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 64 | // Select a backend. |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 65 | bool use_backend = !config.should_use_preference || |
| 66 | os_crypt::GetBackendUse(config.user_data_path); |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 67 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 68 | base::nix::DesktopEnvironment desktop_env = |
| 69 | base::nix::GetDesktopEnvironment(env.get()); |
| 70 | os_crypt::SelectedLinuxBackend selected_backend = |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 71 | os_crypt::SelectBackend(config.store, use_backend, desktop_env); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 72 | |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame] | 73 | // TODO(crbug.com/782851) Schedule the initialisation on each backend's |
| 74 | // favourite thread. |
| 75 | |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 76 | // Try initializing the selected backend. |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 77 | // In case of GNOME_ANY, prefer Libsecret |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 78 | std::unique_ptr<KeyStorageLinux> key_storage; |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 79 | |
| 80 | #if defined(USE_LIBSECRET) |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 81 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 82 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) { |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 83 | key_storage.reset(new KeyStorageLibsecret()); |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 84 | if (key_storage->WaitForInitOnTaskRunner()) { |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 85 | VLOG(1) << "OSCrypt using Libsecret as backend."; |
| 86 | return key_storage; |
| 87 | } |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 88 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 89 | #endif // defined(USE_LIBSECRET) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 90 | |
| 91 | #if defined(USE_KEYRING) |
| 92 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 93 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_KEYRING) { |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 94 | key_storage.reset(new KeyStorageKeyring(config.main_thread_runner)); |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 95 | if (key_storage->WaitForInitOnTaskRunner()) { |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 96 | VLOG(1) << "OSCrypt using Keyring as backend."; |
| 97 | return key_storage; |
| 98 | } |
| 99 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 100 | #endif // defined(USE_KEYRING) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 101 | |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 102 | #if defined(USE_KWALLET) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 103 | if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET || |
| 104 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) { |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 105 | DCHECK(!config.product_name.empty()); |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 106 | base::nix::DesktopEnvironment used_desktop_env = |
| 107 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET |
| 108 | ? base::nix::DESKTOP_ENVIRONMENT_KDE4 |
| 109 | : base::nix::DESKTOP_ENVIRONMENT_KDE5; |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 110 | key_storage.reset( |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 111 | new KeyStorageKWallet(used_desktop_env, config.product_name)); |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 112 | if (key_storage->WaitForInitOnTaskRunner()) { |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 113 | VLOG(1) << "OSCrypt using KWallet as backend."; |
| 114 | return key_storage; |
| 115 | } |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 116 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 117 | #endif // defined(USE_KWALLET) |
| 118 | #endif // defined(USE_LIBSECRET) || defined(USE_KEYRING) || |
| 119 | // defined(USE_KWALLET) |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 120 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 121 | // The appropriate store was not available. |
cfroussios | 6b340f81 | 2017-07-06 15:05:10 | [diff] [blame] | 122 | VLOG(1) << "OSCrypt did not initialize a backend."; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 123 | return nullptr; |
| 124 | } |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame] | 125 | |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 126 | bool KeyStorageLinux::WaitForInitOnTaskRunner() { |
| 127 | base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_sync_primitives; |
| 128 | base::SequencedTaskRunner* task_runner = GetTaskRunner(); |
| 129 | |
| 130 | // We don't need to change threads if the backend has no preference or if we |
| 131 | // are already on the right thread. |
| 132 | if (!task_runner || task_runner->RunsTasksInCurrentSequence()) |
| 133 | return Init(); |
| 134 | |
| 135 | base::WaitableEvent initialized( |
| 136 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 137 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 138 | bool success; |
| 139 | PostTaskAndReplyWithResult( |
| 140 | task_runner, FROM_HERE, |
| 141 | base::BindOnce(&KeyStorageLinux::Init, base::Unretained(this)), |
| 142 | base::BindOnce(&OnInitialized, &initialized, &success)); |
| 143 | initialized.Wait(); |
| 144 | return success; |
| 145 | } |
| 146 | |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame] | 147 | std::string KeyStorageLinux::GetKey() { |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame^] | 148 | base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_sync_primitives; |
| 149 | base::SequencedTaskRunner* task_runner = GetTaskRunner(); |
| 150 | |
| 151 | // We don't need to change threads if the backend has no preference or if we |
| 152 | // are already on the right thread. |
| 153 | if (!task_runner || task_runner->RunsTasksInCurrentSequence()) |
| 154 | return GetKeyImpl(); |
| 155 | |
| 156 | base::WaitableEvent password_loaded( |
| 157 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 158 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 159 | std::string password; |
| 160 | PostTaskAndReplyWithResult( |
| 161 | task_runner, FROM_HERE, |
| 162 | base::BindOnce(&KeyStorageLinux::GetKeyImpl, base::Unretained(this)), |
| 163 | base::BindOnce(&OnPasswordReceived, &password_loaded, &password)); |
| 164 | password_loaded.Wait(); |
| 165 | return password; |
| 166 | } |
| 167 | |
| 168 | base::SequencedTaskRunner* KeyStorageLinux::GetTaskRunner() { |
| 169 | return nullptr; |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame] | 170 | } |