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" |
| 8 | #include "base/lazy_instance.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/nix/xdg_util.h" |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 11 | #include "base/single_thread_task_runner.h" |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 12 | #include "components/os_crypt/key_storage_util_linux.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 13 | |
| 14 | #if defined(USE_LIBSECRET) |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 15 | #include "components/os_crypt/key_storage_libsecret.h" |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 16 | #endif |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 17 | #if defined(USE_KEYRING) |
| 18 | #include "components/os_crypt/key_storage_keyring.h" |
| 19 | #endif |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 20 | #if defined(USE_KWALLET) |
| 21 | #include "components/os_crypt/key_storage_kwallet.h" |
| 22 | #endif |
| 23 | |
thestig | c0bfd64 | 2016-08-22 18:10:35 | [diff] [blame] | 24 | #if defined(GOOGLE_CHROME_BUILD) |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 25 | const char KeyStorageLinux::kFolderName[] = "Chrome Keys"; |
| 26 | const char KeyStorageLinux::kKey[] = "Chrome Safe Storage"; |
| 27 | #else |
| 28 | const char KeyStorageLinux::kFolderName[] = "Chromium Keys"; |
| 29 | const char KeyStorageLinux::kKey[] = "Chromium Safe Storage"; |
| 30 | #endif |
| 31 | |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // Parameters to OSCrypt, which are set before the first call to OSCrypt, are |
| 35 | // stored here. |
| 36 | struct Configuration { |
| 37 | std::string store; |
| 38 | std::string product_name; |
| 39 | scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner; |
| 40 | }; |
| 41 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame^] | 42 | base::LazyInstance<Configuration>::DestructorAtExit g_config = |
| 43 | LAZY_INSTANCE_INITIALIZER; |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 44 | |
| 45 | } // namespace |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 46 | |
| 47 | // static |
| 48 | void KeyStorageLinux::SetStore(const std::string& store_type) { |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 49 | g_config.Get().store = store_type; |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 50 | VLOG(1) << "OSCrypt store set to " << store_type; |
| 51 | } |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 52 | |
| 53 | // static |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 54 | void KeyStorageLinux::SetProductName(const std::string& product_name) { |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 55 | g_config.Get().product_name = product_name; |
| 56 | } |
| 57 | |
| 58 | // static |
| 59 | void KeyStorageLinux::SetMainThreadRunner( |
| 60 | scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner) { |
| 61 | g_config.Get().main_thread_runner = main_thread_runner; |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // static |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 65 | std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 66 | #if defined(USE_LIBSECRET) || defined(USE_KEYRING) || defined(USE_KWALLET) |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 67 | // Select a backend. |
| 68 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 69 | base::nix::DesktopEnvironment desktop_env = |
| 70 | base::nix::GetDesktopEnvironment(env.get()); |
| 71 | os_crypt::SelectedLinuxBackend selected_backend = |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 72 | os_crypt::SelectBackend(g_config.Get().store, desktop_env); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 73 | |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 74 | // Try initializing the selected backend. |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 75 | // In case of GNOME_ANY, prefer Libsecret |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 76 | std::unique_ptr<KeyStorageLinux> key_storage; |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 77 | |
| 78 | #if defined(USE_LIBSECRET) |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 79 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 80 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) { |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 81 | key_storage.reset(new KeyStorageLibsecret()); |
| 82 | if (key_storage->Init()) { |
| 83 | VLOG(1) << "OSCrypt using Libsecret as backend."; |
| 84 | return key_storage; |
| 85 | } |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 86 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 87 | #endif // defined(USE_LIBSECRET) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 88 | |
| 89 | #if defined(USE_KEYRING) |
| 90 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 91 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_KEYRING) { |
| 92 | key_storage.reset(new KeyStorageKeyring(g_config.Get().main_thread_runner)); |
| 93 | if (key_storage->Init()) { |
| 94 | VLOG(1) << "OSCrypt using Keyring as backend."; |
| 95 | return key_storage; |
| 96 | } |
| 97 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 98 | #endif // defined(USE_KEYRING) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 99 | |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 100 | #if defined(USE_KWALLET) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 101 | if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET || |
| 102 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) { |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 103 | DCHECK(!g_config.Get().product_name.empty()); |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 104 | base::nix::DesktopEnvironment used_desktop_env = |
| 105 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET |
| 106 | ? base::nix::DESKTOP_ENVIRONMENT_KDE4 |
| 107 | : base::nix::DESKTOP_ENVIRONMENT_KDE5; |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 108 | key_storage.reset( |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 109 | new KeyStorageKWallet(used_desktop_env, g_config.Get().product_name)); |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 110 | if (key_storage->Init()) { |
| 111 | VLOG(1) << "OSCrypt using KWallet as backend."; |
| 112 | return key_storage; |
| 113 | } |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 114 | } |
slan | a881a86 | 2016-09-09 21:36:07 | [diff] [blame] | 115 | #endif // defined(USE_KWALLET) |
| 116 | #endif // defined(USE_LIBSECRET) || defined(USE_KEYRING) || |
| 117 | // defined(USE_KWALLET) |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 118 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 119 | // The appropriate store was not available. |
| 120 | VLOG(1) << "OSCrypt could not initialize a backend."; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 121 | return nullptr; |
| 122 | } |