| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/os_crypt/key_storage_linux.h" | 5 #include "components/os_crypt/key_storage_linux.h" |
| 6 | 6 |
| 7 #include "base/environment.h" | 7 #include "base/environment.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/nix/xdg_util.h" | 10 #include "base/nix/xdg_util.h" |
| 11 #include "components/os_crypt/key_storage_util_linux.h" | 11 #include "components/os_crypt/key_storage_util_linux.h" |
| 12 | 12 |
| 13 #if defined(USE_LIBSECRET) | 13 #if defined(USE_LIBSECRET) |
| 14 #include "components/os_crypt/key_storage_libsecret.h" | 14 #include "components/os_crypt/key_storage_libsecret.h" |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 #if defined(USE_KWALLET) | 17 #if defined(USE_KWALLET) |
| 18 #include "components/os_crypt/key_storage_kwallet.h" | 18 #include "components/os_crypt/key_storage_kwallet.h" |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 #if defined(OFFICIAL_BUILD) | 21 #if defined(OFFICIAL_BUILD) |
| 22 const char KeyStorageLinux::kFolderName[] = "Chrome Keys"; | 22 const char KeyStorageLinux::kFolderName[] = "Chrome Keys"; |
| 23 const char KeyStorageLinux::kKey[] = "Chrome Safe Storage"; | 23 const char KeyStorageLinux::kKey[] = "Chrome Safe Storage"; |
| 24 #else | 24 #else |
| 25 const char KeyStorageLinux::kFolderName[] = "Chromium Keys"; | 25 const char KeyStorageLinux::kFolderName[] = "Chromium Keys"; |
| 26 const char KeyStorageLinux::kKey[] = "Chromium Safe Storage"; | 26 const char KeyStorageLinux::kKey[] = "Chromium Safe Storage"; |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 base::LazyInstance<std::string> g_store = LAZY_INSTANCE_INITIALIZER; | 29 namespace { |
| 30 base::LazyInstance<std::string> g_product_name = LAZY_INSTANCE_INITIALIZER; | 30 |
| 31 // Parameters to OSCrypt, which are set before the first call to OSCrypt, are |
| 32 // stored here. |
| 33 struct Configuration { |
| 34 std::string store; |
| 35 std::string product_name; |
| 36 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner; |
| 37 }; |
| 38 |
| 39 base::LazyInstance<Configuration> g_config = LAZY_INSTANCE_INITIALIZER; |
| 40 |
| 41 } // namespace |
| 31 | 42 |
| 32 // static | 43 // static |
| 33 void KeyStorageLinux::SetStore(const std::string& store_type) { | 44 void KeyStorageLinux::SetStore(const std::string& store_type) { |
| 34 g_store.Get() = store_type; | 45 g_config.Get().store = store_type; |
| 35 VLOG(1) << "OSCrypt store set to " << store_type; | 46 VLOG(1) << "OSCrypt store set to " << store_type; |
| 36 } | 47 } |
| 37 | 48 |
| 38 // static | 49 // static |
| 39 void KeyStorageLinux::SetProductName(const std::string& product_name) { | 50 void KeyStorageLinux::SetProductName(const std::string& product_name) { |
| 40 g_product_name.Get() = product_name; | 51 g_config.Get().product_name = product_name; |
| 41 } | 52 } |
| 42 | 53 |
| 43 // static | 54 // static |
| 55 void KeyStorageLinux::SetMainThreadRunner( |
| 56 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner) { |
| 57 g_config.Get().main_thread_runner = main_thread_runner; |
| 58 } |
| 59 |
| 60 // static |
| 44 std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { | 61 std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService() { |
| 45 // Select a backend. | 62 // Select a backend. |
| 46 std::unique_ptr<base::Environment> env(base::Environment::Create()); | 63 std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 47 base::nix::DesktopEnvironment desktop_env = | 64 base::nix::DesktopEnvironment desktop_env = |
| 48 base::nix::GetDesktopEnvironment(env.get()); | 65 base::nix::GetDesktopEnvironment(env.get()); |
| 49 os_crypt::SelectedLinuxBackend selected_backend = | 66 os_crypt::SelectedLinuxBackend selected_backend = |
| 50 os_crypt::SelectBackend(g_store.Get(), desktop_env); | 67 os_crypt::SelectBackend(g_config.Get().store, desktop_env); |
| 51 | 68 |
| 52 // Try initializing the selected backend. | 69 // Try initializing the selected backend. |
| 53 std::unique_ptr<KeyStorageLinux> key_storage; | 70 std::unique_ptr<KeyStorageLinux> key_storage; |
| 54 if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || | 71 if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY || |
| 55 selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) { | 72 selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) { |
| 56 #if defined(USE_LIBSECRET) | 73 #if defined(USE_LIBSECRET) |
| 57 key_storage.reset(new KeyStorageLibsecret()); | 74 key_storage.reset(new KeyStorageLibsecret()); |
| 58 if (key_storage->Init()) { | 75 if (key_storage->Init()) { |
| 59 VLOG(1) << "OSCrypt using Libsecret as backend."; | 76 VLOG(1) << "OSCrypt using Libsecret as backend."; |
| 60 return key_storage; | 77 return key_storage; |
| 61 } | 78 } |
| 62 #endif | 79 #endif |
| 63 } else if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET || | 80 } else if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET || |
| 64 selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) { | 81 selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) { |
| 65 #if defined(USE_KWALLET) | 82 #if defined(USE_KWALLET) |
| 66 DCHECK(!g_product_name.Get().empty()); | 83 DCHECK(!g_config.Get().product_name.empty()); |
| 67 base::nix::DesktopEnvironment used_desktop_env = | 84 base::nix::DesktopEnvironment used_desktop_env = |
| 68 selected_backend == os_crypt::SelectedLinuxBackend::KWALLET | 85 selected_backend == os_crypt::SelectedLinuxBackend::KWALLET |
| 69 ? base::nix::DESKTOP_ENVIRONMENT_KDE4 | 86 ? base::nix::DESKTOP_ENVIRONMENT_KDE4 |
| 70 : base::nix::DESKTOP_ENVIRONMENT_KDE5; | 87 : base::nix::DESKTOP_ENVIRONMENT_KDE5; |
| 71 key_storage.reset( | 88 key_storage.reset( |
| 72 new KeyStorageKWallet(used_desktop_env, g_product_name.Get())); | 89 new KeyStorageKWallet(used_desktop_env, g_config.Get().product_name)); |
| 73 if (key_storage->Init()) { | 90 if (key_storage->Init()) { |
| 74 VLOG(1) << "OSCrypt using KWallet as backend."; | 91 VLOG(1) << "OSCrypt using KWallet as backend."; |
| 75 return key_storage; | 92 return key_storage; |
| 76 } | 93 } |
| 77 #endif | 94 #endif |
| 78 } | 95 } |
| 79 | 96 |
| 80 // The appropriate store was not available. | 97 // The appropriate store was not available. |
| 81 VLOG(1) << "OSCrypt could not initialize a backend."; | 98 VLOG(1) << "OSCrypt could not initialize a backend."; |
| 82 return nullptr; | 99 return nullptr; |
| 83 } | 100 } |
| OLD | NEW |