blob: 48810de20eb7d855f39670b451226253ca87d5ee [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
Christos Froussios4e170cb2017-12-01 09:42:337#include "base/bind.h"
cfroussios3ea4c692016-07-18 19:15:148#include "base/environment.h"
cfroussios3ea4c692016-07-18 19:15:149#include "base/logging.h"
10#include "base/nix/xdg_util.h"
Christos Froussios4e170cb2017-12-01 09:42:3311#include "base/sequenced_task_runner.h"
12#include "base/synchronization/waitable_event.h"
13#include "base/task_runner_util.h"
14#include "base/threading/thread_restrictions.h"
Nico Weber356b3042019-08-23 15:30:4115#include "build/branding_buildflags.h"
Christos Froussios494196d2017-07-14 10:10:0416#include "components/os_crypt/key_storage_config_linux.h"
cfroussios2f05d7f2016-08-17 15:58:5017#include "components/os_crypt/key_storage_util_linux.h"
cfroussios3ea4c692016-07-18 19:15:1418
19#if defined(USE_LIBSECRET)
cfroussios3b5a4e42016-05-31 11:02:1820#include "components/os_crypt/key_storage_libsecret.h"
cfroussios3ea4c692016-07-18 19:15:1421#endif
cfroussiosb013c15b2016-09-03 01:10:1622#if defined(USE_KEYRING)
23#include "components/os_crypt/key_storage_keyring.h"
24#endif
cfroussios2e6729a42016-07-26 09:18:1225#if defined(USE_KWALLET)
26#include "components/os_crypt/key_storage_kwallet.h"
27#endif
28
Nico Weber356b3042019-08-23 15:30:4129#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
cfroussios2e6729a42016-07-26 09:18:1230const char KeyStorageLinux::kFolderName[] = "Chrome Keys";
31const char KeyStorageLinux::kKey[] = "Chrome Safe Storage";
32#else
33const char KeyStorageLinux::kFolderName[] = "Chromium Keys";
34const char KeyStorageLinux::kKey[] = "Chromium Safe Storage";
35#endif
36
cfroussios3ea4c692016-07-18 19:15:1437// static
Christos Froussios494196d2017-07-14 10:10:0438std::unique_ptr<KeyStorageLinux> KeyStorageLinux::CreateService(
39 const os_crypt::Config& config) {
slana881a862016-09-09 21:36:0740#if defined(USE_LIBSECRET) || defined(USE_KEYRING) || defined(USE_KWALLET)
cfroussios2f05d7f2016-08-17 15:58:5041 // Select a backend.
Christos Froussios494196d2017-07-14 10:10:0442 bool use_backend = !config.should_use_preference ||
43 os_crypt::GetBackendUse(config.user_data_path);
cfroussios2f05d7f2016-08-17 15:58:5044 std::unique_ptr<base::Environment> env(base::Environment::Create());
45 base::nix::DesktopEnvironment desktop_env =
46 base::nix::GetDesktopEnvironment(env.get());
47 os_crypt::SelectedLinuxBackend selected_backend =
Christos Froussios494196d2017-07-14 10:10:0448 os_crypt::SelectBackend(config.store, use_backend, desktop_env);
cfroussios3b5a4e42016-05-31 11:02:1849
Christos Froussios985d1aac2017-11-09 11:01:0750 // TODO(crbug.com/782851) Schedule the initialisation on each backend's
51 // favourite thread.
52
cfroussios2f05d7f2016-08-17 15:58:5053 // Try initializing the selected backend.
cfroussiosb013c15b2016-09-03 01:10:1654 // In case of GNOME_ANY, prefer Libsecret
cfroussios3ea4c692016-07-18 19:15:1455 std::unique_ptr<KeyStorageLinux> key_storage;
cfroussiosb013c15b2016-09-03 01:10:1656
57#if defined(USE_LIBSECRET)
cfroussios2f05d7f2016-08-17 15:58:5058 if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY ||
59 selected_backend == os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET) {
Christos Froussios70cc95b2017-12-14 21:13:2360 key_storage.reset(new KeyStorageLibsecret());
Christos Froussios4e170cb2017-12-01 09:42:3361 if (key_storage->WaitForInitOnTaskRunner()) {
cfroussios3ea4c692016-07-18 19:15:1462 VLOG(1) << "OSCrypt using Libsecret as backend.";
63 return key_storage;
64 }
cfroussiosb013c15b2016-09-03 01:10:1665 }
slana881a862016-09-09 21:36:0766#endif // defined(USE_LIBSECRET)
cfroussiosb013c15b2016-09-03 01:10:1667
68#if defined(USE_KEYRING)
69 if (selected_backend == os_crypt::SelectedLinuxBackend::GNOME_ANY ||
70 selected_backend == os_crypt::SelectedLinuxBackend::GNOME_KEYRING) {
Christos Froussios70cc95b2017-12-14 21:13:2371 key_storage.reset(new KeyStorageKeyring(config.main_thread_runner));
Christos Froussios4e170cb2017-12-01 09:42:3372 if (key_storage->WaitForInitOnTaskRunner()) {
cfroussiosb013c15b2016-09-03 01:10:1673 VLOG(1) << "OSCrypt using Keyring as backend.";
74 return key_storage;
75 }
76 }
slana881a862016-09-09 21:36:0777#endif // defined(USE_KEYRING)
cfroussiosb013c15b2016-09-03 01:10:1678
cfroussios2e6729a42016-07-26 09:18:1279#if defined(USE_KWALLET)
cfroussiosb013c15b2016-09-03 01:10:1680 if (selected_backend == os_crypt::SelectedLinuxBackend::KWALLET ||
81 selected_backend == os_crypt::SelectedLinuxBackend::KWALLET5) {
Christos Froussios494196d2017-07-14 10:10:0482 DCHECK(!config.product_name.empty());
cfroussios2f05d7f2016-08-17 15:58:5083 base::nix::DesktopEnvironment used_desktop_env =
84 selected_backend == os_crypt::SelectedLinuxBackend::KWALLET
85 ? base::nix::DESKTOP_ENVIRONMENT_KDE4
86 : base::nix::DESKTOP_ENVIRONMENT_KDE5;
Christos Froussios70cc95b2017-12-14 21:13:2387 key_storage.reset(
88 new KeyStorageKWallet(used_desktop_env, config.product_name));
Christos Froussios4e170cb2017-12-01 09:42:3389 if (key_storage->WaitForInitOnTaskRunner()) {
cfroussios2e6729a42016-07-26 09:18:1290 VLOG(1) << "OSCrypt using KWallet as backend.";
91 return key_storage;
92 }
cfroussios3ea4c692016-07-18 19:15:1493 }
slana881a862016-09-09 21:36:0794#endif // defined(USE_KWALLET)
95#endif // defined(USE_LIBSECRET) || defined(USE_KEYRING) ||
96 // defined(USE_KWALLET)
cfroussios3b5a4e42016-05-31 11:02:1897
cfroussios3ea4c692016-07-18 19:15:1498 // The appropriate store was not available.
cfroussios6b340f812017-07-06 15:05:1099 VLOG(1) << "OSCrypt did not initialize a backend.";
cfroussios3b5a4e42016-05-31 11:02:18100 return nullptr;
101}
Christos Froussios985d1aac2017-11-09 11:01:07102
Christos Froussios4e170cb2017-12-01 09:42:33103bool KeyStorageLinux::WaitForInitOnTaskRunner() {
104 base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_sync_primitives;
105 base::SequencedTaskRunner* task_runner = GetTaskRunner();
106
107 // We don't need to change threads if the backend has no preference or if we
108 // are already on the right thread.
109 if (!task_runner || task_runner->RunsTasksInCurrentSequence())
110 return Init();
111
112 base::WaitableEvent initialized(
113 base::WaitableEvent::ResetPolicy::MANUAL,
114 base::WaitableEvent::InitialState::NOT_SIGNALED);
115 bool success;
Christos Froussios57fe5742017-12-07 21:16:49116 task_runner->PostTask(
117 FROM_HERE,
118 base::BindOnce(&KeyStorageLinux::BlockOnInitThenSignal,
119 base::Unretained(this), &initialized, &success));
Christos Froussios4e170cb2017-12-01 09:42:33120 initialized.Wait();
121 return success;
122}
123
Christos Froussios985d1aac2017-11-09 11:01:07124std::string KeyStorageLinux::GetKey() {
Christos Froussios4e170cb2017-12-01 09:42:33125 base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_sync_primitives;
126 base::SequencedTaskRunner* task_runner = GetTaskRunner();
127
128 // We don't need to change threads if the backend has no preference or if we
129 // are already on the right thread.
130 if (!task_runner || task_runner->RunsTasksInCurrentSequence())
131 return GetKeyImpl();
132
133 base::WaitableEvent password_loaded(
134 base::WaitableEvent::ResetPolicy::MANUAL,
135 base::WaitableEvent::InitialState::NOT_SIGNALED);
136 std::string password;
Christos Froussios57fe5742017-12-07 21:16:49137 task_runner->PostTask(
138 FROM_HERE,
139 base::BindOnce(&KeyStorageLinux::BlockOnGetKeyImplThenSignal,
140 base::Unretained(this), &password_loaded, &password));
Christos Froussios4e170cb2017-12-01 09:42:33141 password_loaded.Wait();
142 return password;
143}
144
145base::SequencedTaskRunner* KeyStorageLinux::GetTaskRunner() {
146 return nullptr;
Christos Froussios985d1aac2017-11-09 11:01:07147}
Christos Froussios57fe5742017-12-07 21:16:49148
149void KeyStorageLinux::BlockOnGetKeyImplThenSignal(
150 base::WaitableEvent* on_password_received,
151 std::string* password) {
152 *password = GetKeyImpl();
153 on_password_received->Signal();
154}
155
156void KeyStorageLinux::BlockOnInitThenSignal(base::WaitableEvent* on_inited,
157 bool* success) {
158 *success = Init();
159 on_inited->Signal();
160}