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 | |
| 42 | base::LazyInstance<Configuration> g_config = LAZY_INSTANCE_INITIALIZER; |
| 43 | |
| 44 | } // namespace |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 45 | |
| 46 | // static |
| 47 | void KeyStorageLinux::SetStore(const std::string& store_type) { |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 48 | g_config.Get().store = store_type; |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 49 | VLOG(1) << "OSCrypt store set to " << store_type; |
| 50 | } |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 51 | |
| 52 | // static |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 53 | void KeyStorageLinux::SetProductName(const std::string& product_name) { |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 54 | g_config.Get().product_name = product_name; |
| 55 | } |
| 56 | |
| 57 | // static |
| 58 | void KeyStorageLinux::SetMainThreadRunner( |
| 59 | scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner) { |
| 60 | g_config.Get().main_thread_runner = main_thread_runner; |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // static |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 64 | std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 65 | // Select a backend. |
| 66 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 67 | base::nix::DesktopEnvironment desktop_env = |
| 68 | base::nix::GetDesktopEnvironment(env.get()); |
| 69 | os_crypt::SelectedLinuxBackend selected_backend = |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 70 | os_crypt::SelectBackend(g_config.Get().store, desktop_env); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 71 | |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 72 | // Try initializing the selected backend. |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame^] | 73 | // In case of GNOME_ANY, prefer Libsecret |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 74 | std::unique_ptr<KeyStorageLinux> key_storage; |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame^] | 75 | |
| 76 | #if defined(USE_LIBSECRET) |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 77 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 78 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) { |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 79 | key_storage.reset(new KeyStorageLibsecret()); |
| 80 | if (key_storage->Init()) { |
| 81 | VLOG(1) << "OSCrypt using Libsecret as backend."; |
| 82 | return key_storage; |
| 83 | } |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame^] | 84 | } |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 85 | #endif |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame^] | 86 | |
| 87 | #if defined(USE_KEYRING) |
| 88 | if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 89 | selected_backend == os_crypt::SelectedLinuxBackend::GNOME_KEYRING) { |
| 90 | key_storage.reset(new KeyStorageKeyring(g_config.Get().main_thread_runner)); |
| 91 | if (key_storage->Init()) { |
| 92 | VLOG(1) << "OSCrypt using Keyring as backend."; |
| 93 | return key_storage; |
| 94 | } |
| 95 | } |
| 96 | #endif |
| 97 | |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 98 | #if defined(USE_KWALLET) |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame^] | 99 | if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET || |
| 100 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) { |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 101 | DCHECK(!g_config.Get().product_name.empty()); |
cfroussios | 2f05d7f | 2016-08-17 15:58:50 | [diff] [blame] | 102 | base::nix::DesktopEnvironment used_desktop_env = |
| 103 | selected_backend == os_crypt::SelectedLinuxBackend::KWALLET |
| 104 | ? base::nix::DESKTOP_ENVIRONMENT_KDE4 |
| 105 | : base::nix::DESKTOP_ENVIRONMENT_KDE5; |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 106 | key_storage.reset( |
cfroussios | c51d7ae19 | 2016-08-19 12:01:14 | [diff] [blame] | 107 | new KeyStorageKWallet(used_desktop_env, g_config.Get().product_name)); |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 108 | if (key_storage->Init()) { |
| 109 | VLOG(1) << "OSCrypt using KWallet as backend."; |
| 110 | return key_storage; |
| 111 | } |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 112 | } |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame^] | 113 | #endif |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 114 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 115 | // The appropriate store was not available. |
| 116 | VLOG(1) << "OSCrypt could not initialize a backend."; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 117 | return nullptr; |
| 118 | } |