blob: ded176374a327825a5f138fc1ee2c645dcd48711 [file] [log] [blame]
cfroussios3b5a4e42016-05-31 11:02:181// 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
cfroussios3ea4c692016-07-18 19:15:147#include "base/environment.h"
cfroussios3ea4c692016-07-18 19:15:148#include "base/logging.h"
9#include "base/nix/xdg_util.h"
Christos Froussios494196d2017-07-14 10:10:0410#include "components/os_crypt/key_storage_config_linux.h"
cfroussios2f05d7f2016-08-17 15:58:5011#include "components/os_crypt/key_storage_util_linux.h"
cfroussios3ea4c692016-07-18 19:15:1412
13#if defined(USE_LIBSECRET)
cfroussios3b5a4e42016-05-31 11:02:1814#include "components/os_crypt/key_storage_libsecret.h"
cfroussios3ea4c692016-07-18 19:15:1415#endif
cfroussiosb013c15b2016-09-03 01:10:1616#if defined(USE_KEYRING)
17#include "components/os_crypt/key_storage_keyring.h"
18#endif
cfroussios2e6729a42016-07-26 09:18:1219#if defined(USE_KWALLET)
20#include "components/os_crypt/key_storage_kwallet.h"
21#endif
22
thestigc0bfd642016-08-22 18:10:3523#if defined(GOOGLE_CHROME_BUILD)
cfroussios2e6729a42016-07-26 09:18:1224const char KeyStorageLinux::kFolderName[] = "Chrome Keys";
25const char KeyStorageLinux::kKey[] = "Chrome Safe Storage";
26#else
27const char KeyStorageLinux::kFolderName[] = "Chromium Keys";
28const char KeyStorageLinux::kKey[] = "Chromium Safe Storage";
29#endif
30
cfroussios3ea4c692016-07-18 19:15:1431// static
Christos Froussios494196d2017-07-14 10:10:0432std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService(
33 const os_crypt::Config& config) {
slana881a862016-09-09 21:36:0734#if defined(USE_LIBSECRET) || defined(USE_KEYRING) || defined(USE_KWALLET)
cfroussios2f05d7f2016-08-17 15:58:5035 // Select a backend.
Christos Froussios494196d2017-07-14 10:10:0436 bool use_backend = !config.should_use_preference ||
37 os_crypt::GetBackendUse(config.user_data_path);
cfroussios2f05d7f2016-08-17 15:58:5038 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 Froussios494196d2017-07-14 10:10:0442 os_crypt::SelectBackend(config.store, use_backend, desktop_env);
cfroussios3b5a4e42016-05-31 11:02:1843
Christos Froussios985d1aac2017-11-09 11:01:0744 // TODO(crbug.com/782851) Schedule the initialisation on each backend's
45 // favourite thread.
46
cfroussios2f05d7f2016-08-17 15:58:5047 // Try initializing the selected backend.
cfroussiosb013c15b2016-09-03 01:10:1648 // In case of GNOME_ANY, prefer Libsecret
cfroussios3ea4c692016-07-18 19:15:1449 std::unique_ptr<KeyStorageLinux> key_storage;
cfroussiosb013c15b2016-09-03 01:10:1650
51#if defined(USE_LIBSECRET)
cfroussios2f05d7f2016-08-17 15:58:5052 if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY ||
53 selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) {
cfroussios3ea4c692016-07-18 19:15:1454 key_storage.reset(new KeyStorageLibsecret());
55 if (key_storage->Init()) {
56 VLOG(1) << "OSCrypt using Libsecret as backend.";
57 return key_storage;
58 }
cfroussiosb013c15b2016-09-03 01:10:1659 }
slana881a862016-09-09 21:36:0760#endif // defined(USE_LIBSECRET)
cfroussiosb013c15b2016-09-03 01:10:1661
62#if defined(USE_KEYRING)
63 if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY ||
64 selected_backend == os_crypt::SelectedLinuxBackend::GNOME_KEYRING) {
Christos Froussios494196d2017-07-14 10:10:0465 key_storage.reset(new KeyStorageKeyring(config.main_thread_runner));
cfroussiosb013c15b2016-09-03 01:10:1666 if (key_storage->Init()) {
67 VLOG(1) << "OSCrypt using Keyring as backend.";
68 return key_storage;
69 }
70 }
slana881a862016-09-09 21:36:0771#endif // defined(USE_KEYRING)
cfroussiosb013c15b2016-09-03 01:10:1672
cfroussios2e6729a42016-07-26 09:18:1273#if defined(USE_KWALLET)
cfroussiosb013c15b2016-09-03 01:10:1674 if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET ||
75 selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) {
Christos Froussios494196d2017-07-14 10:10:0476 DCHECK(!config.product_name.empty());
cfroussios2f05d7f2016-08-17 15:58:5077 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;
cfroussios2e6729a42016-07-26 09:18:1281 key_storage.reset(
Christos Froussios494196d2017-07-14 10:10:0482 new KeyStorageKWallet(used_desktop_env, config.product_name));
cfroussios2e6729a42016-07-26 09:18:1283 if (key_storage->Init()) {
84 VLOG(1) << "OSCrypt using KWallet as backend.";
85 return key_storage;
86 }
cfroussios3ea4c692016-07-18 19:15:1487 }
slana881a862016-09-09 21:36:0788#endif // defined(USE_KWALLET)
89#endif // defined(USE_LIBSECRET) || defined(USE_KEYRING) ||
90 // defined(USE_KWALLET)
cfroussios3b5a4e42016-05-31 11:02:1891
cfroussios3ea4c692016-07-18 19:15:1492 // The appropriate store was not available.
cfroussios6b340f812017-07-06 15:05:1093 VLOG(1) << "OSCrypt did not initialize a backend.";
cfroussios3b5a4e42016-05-31 11:02:1894 return nullptr;
95}
Christos Froussios985d1aac2017-11-09 11:01:0796
97std::string KeyStorageLinux::GetKey() {
98 // TODO(crbug.com/782851) Schedule this operation on the backend's favourite
99 // thread.
100 return GetKeyImpl();
101}