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 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 7 | #include "base/environment.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 8 | #include "base/logging.h" |
| 9 | #include "base/nix/xdg_util.h" |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 10 | #include "components/os_crypt/key_storage_config_linux.h" |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 11 | #include "components/os_crypt/key_storage_util_linux.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 12 | |
| 13 | #if defined(USE_LIBSECRET) |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 14 | #include "components/os_crypt/key_storage_libsecret.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 15 | #endif |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 16 | #if defined(USE_KEYRING) |
| 17 | #include "components/os_crypt/key_storage_keyring.h" |
| 18 | #endif |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 19 | #if defined(USE_KWALLET) |
| 20 | #include "components/os_crypt/key_storage_kwallet.h" |
| 21 | #endif |
| 22 | |
thestig | c0bfd64 | 2016-08-22 18:10:35 | [diff] [blame] | 23 | #if defined(GOOGLE_CHROME_BUILD) |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 24 | const char KeyStorageLinux::kFolderName[] = "Chrome Keys"; |
| 25 | const char KeyStorageLinux::kKey[] = "Chrome Safe Storage"; |
| 26 | #else |
| 27 | const char KeyStorageLinux::kFolderName[] = "Chromium Keys"; |
| 28 | const char KeyStorageLinux::kKey[] = "Chromium Safe Storage"; |
| 29 | #endif |
| 30 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 31 | // static |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 32 | std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService( |
| 33 | const os_crypt::Config& config) { |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 34 | #if defined(USE_LIBSECRET) || defined(USE_KEYRING) || defined(USE_KWALLET) |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 35 | // Select a backend. |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 36 | bool use_backend = !config.should_use_preference || |
| 37 | os_crypt::GetBackendUse(config.user_data_path); |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 38 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 39 | base::nix::DesktopEnvironment desktop_env = |
| 40 | base::nix::GetDesktopEnvironment(env.get()); |
| 41 | os_crypt::SelectedLinuxBackend selected_backend = |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 42 | os_crypt::SelectBackend(config.store, use_backend, desktop_env); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 43 | |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame^] | 44 | // TODO(crbug.com/782851) Schedule the initialisation on each backend's |
| 45 | // favourite thread. |
| 46 | |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 47 | // Try initializing the selected backend. |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 48 | // In case of GNOME_ANY, prefer Libsecret |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 49 | std::unique_ptr<KeyStorageLinux> key_storage; |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 50 | |
| 51 | #if defined(USE_LIBSECRET) |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 52 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 53 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) { |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 54 | key_storage.reset(new KeyStorageLibsecret()); |
| 55 | if (key_storage->Init()) { |
| 56 | VLOG(1) << "OSCrypt using Libsecret as backend."; |
| 57 | return key_storage; |
| 58 | } |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 59 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 60 | #endif // defined(USE_LIBSECRET) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 61 | |
| 62 | #if defined(USE_KEYRING) |
| 63 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 64 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_KEYRING) { |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 65 | key_storage.reset(new KeyStorageKeyring(config.main_thread_runner)); |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 66 | if (key_storage->Init()) { |
| 67 | VLOG(1) << "OSCrypt using Keyring as backend."; |
| 68 | return key_storage; |
| 69 | } |
| 70 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 71 | #endif // defined(USE_KEYRING) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 72 | |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 73 | #if defined(USE_KWALLET) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 74 | if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET || |
| 75 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) { |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 76 | DCHECK(!config.product_name.empty()); |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 77 | base::nix::DesktopEnvironment used_desktop_env = |
| 78 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET |
| 79 | ? base::nix::DESKTOP_ENVIRONMENT_KDE4 |
| 80 | : base::nix::DESKTOP_ENVIRONMENT_KDE5; |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 81 | key_storage.reset( |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 82 | new KeyStorageKWallet(used_desktop_env, config.product_name)); |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 83 | if (key_storage->Init()) { |
| 84 | VLOG(1) << "OSCrypt using KWallet as backend."; |
| 85 | return key_storage; |
| 86 | } |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 87 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 88 | #endif // defined(USE_KWALLET) |
| 89 | #endif // defined(USE_LIBSECRET) || defined(USE_KEYRING) || |
| 90 | // defined(USE_KWALLET) |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 91 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 92 | // The appropriate store was not available. |
cfroussios | 6b340f81 | 2017-07-06 15:05:10 | [diff] [blame] | 93 | VLOG(1) << "OSCrypt did not initialize a backend."; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 94 | return nullptr; |
| 95 | } |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame^] | 96 | |
| 97 | std::string KeyStorageLinux::GetKey() { |
| 98 | // TODO(crbug.com/782851) Schedule this operation on the backend's favourite |
| 99 | // thread. |
| 100 | return GetKeyImpl(); |
| 101 | } |