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_libsecret.h" |
| 6 | |
| 7 | #include "base/base64.h" |
| 8 | #include "base/rand_util.h" |
| 9 | #include "base/strings/string_number_conversions.h" |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame^] | 10 | #include "build/branding_buildflags.h" |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 11 | #include "components/os_crypt/libsecret_util_linux.h" |
| 12 | |
| 13 | namespace { |
| 14 | |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame^] | 15 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 16 | const char kApplicationName[] = "chrome"; |
| 17 | #else |
| 18 | const char kApplicationName[] = "chromium"; |
| 19 | #endif |
| 20 | |
| 21 | // Deprecated in M55 (crbug.com/639298) |
| 22 | const SecretSchema kKeystoreSchemaV1 = { |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 23 | "chrome_libsecret_os_crypt_password", |
| 24 | SECRET_SCHEMA_NONE, |
| 25 | { |
| 26 | {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 27 | }}; |
| 28 | |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 29 | const SecretSchema kKeystoreSchemaV2 = { |
| 30 | "chrome_libsecret_os_crypt_password_v2", |
| 31 | SECRET_SCHEMA_DONT_MATCH_NAME, |
| 32 | { |
| 33 | {"application", SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 34 | {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 35 | }}; |
| 36 | |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 37 | // From a search result, extracts a SecretValue, asserting that there was at |
| 38 | // most one result. Returns nullptr if there are no results. |
| 39 | SecretValue* ToSingleSecret(GList* secret_items) { |
| 40 | GList* first = g_list_first(secret_items); |
| 41 | if (first == nullptr) |
| 42 | return nullptr; |
| 43 | if (g_list_next(first) != nullptr) { |
| 44 | VLOG(1) << "OSCrypt found more than one encryption keys."; |
| 45 | } |
| 46 | SecretItem* secret_item = static_cast<SecretItem*>(first->data); |
| 47 | SecretValue* secret_value = |
| 48 | LibsecretLoader::secret_item_get_secret(secret_item); |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 49 | return secret_value; |
| 50 | } |
| 51 | |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 52 | } // namespace |
| 53 | |
| 54 | std::string KeyStorageLibsecret::AddRandomPasswordInLibsecret() { |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 55 | std::string password; |
| 56 | base::Base64Encode(base::RandBytesAsString(16), &password); |
| 57 | GError* error = nullptr; |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 58 | bool success = LibsecretLoader::secret_password_store_sync( |
| 59 | &kKeystoreSchemaV2, nullptr, KeyStorageLinux::kKey, password.c_str(), |
| 60 | nullptr, &error, "application", kApplicationName, nullptr); |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 61 | if (error) { |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 62 | VLOG(1) << "Libsecret lookup failed: " << error->message; |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 63 | g_error_free(error); |
| 64 | return std::string(); |
| 65 | } |
| 66 | if (!success) { |
| 67 | VLOG(1) << "Libsecret lookup failed."; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 68 | return std::string(); |
| 69 | } |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 70 | |
| 71 | VLOG(1) << "OSCrypt generated a new password."; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 72 | return password; |
| 73 | } |
| 74 | |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame] | 75 | std::string KeyStorageLibsecret::GetKeyImpl() { |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 76 | LibsecretAttributesBuilder attrs; |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 77 | attrs.Append("application", kApplicationName); |
erikchen | e3e1bc7 | 2018-01-18 16:31:29 | [diff] [blame] | 78 | |
| 79 | LibsecretLoader::SearchHelper helper; |
| 80 | helper.Search(&kKeystoreSchemaV2, attrs.Get(), |
| 81 | SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS); |
| 82 | if (!helper.success()) { |
| 83 | VLOG(1) << "Libsecret lookup failed: " << helper.error()->message; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 84 | return std::string(); |
| 85 | } |
erikchen | e3e1bc7 | 2018-01-18 16:31:29 | [diff] [blame] | 86 | |
| 87 | SecretValue* password_libsecret = ToSingleSecret(helper.results()); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 88 | if (!password_libsecret) { |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 89 | std::string password = Migrate(); |
| 90 | if (!password.empty()) |
| 91 | return password; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 92 | return AddRandomPasswordInLibsecret(); |
| 93 | } |
| 94 | std::string password( |
| 95 | LibsecretLoader::secret_value_get_text(password_libsecret)); |
| 96 | LibsecretLoader::secret_value_unref(password_libsecret); |
| 97 | return password; |
| 98 | } |
| 99 | |
| 100 | bool KeyStorageLibsecret::Init() { |
cfroussios | f3a8dfe | 2016-10-28 15:30:32 | [diff] [blame] | 101 | bool loaded = LibsecretLoader::EnsureLibsecretLoaded(); |
| 102 | if (loaded) |
| 103 | LibsecretLoader::EnsureKeyringUnlocked(); |
| 104 | return loaded; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 105 | } |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 106 | |
| 107 | std::string KeyStorageLibsecret::Migrate() { |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 108 | LibsecretAttributesBuilder attrs; |
| 109 | |
| 110 | // Detect old entry. |
erikchen | e3e1bc7 | 2018-01-18 16:31:29 | [diff] [blame] | 111 | LibsecretLoader::SearchHelper helper; |
| 112 | helper.Search(&kKeystoreSchemaV1, attrs.Get(), |
| 113 | SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS); |
| 114 | if (!helper.success()) |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 115 | return std::string(); |
erikchen | e3e1bc7 | 2018-01-18 16:31:29 | [diff] [blame] | 116 | |
| 117 | SecretValue* password_libsecret = ToSingleSecret(helper.results()); |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 118 | if (!password_libsecret) |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 119 | return std::string(); |
| 120 | |
| 121 | VLOG(1) << "OSCrypt detected a deprecated password in Libsecret."; |
| 122 | std::string password( |
| 123 | LibsecretLoader::secret_value_get_text(password_libsecret)); |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 124 | LibsecretLoader::secret_value_unref(password_libsecret); |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 125 | |
| 126 | // Create new entry. |
erikchen | e3e1bc7 | 2018-01-18 16:31:29 | [diff] [blame] | 127 | GError* error = nullptr; |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 128 | bool success = LibsecretLoader::secret_password_store_sync( |
| 129 | &kKeystoreSchemaV2, nullptr, KeyStorageLinux::kKey, password.c_str(), |
| 130 | nullptr, &error, "application", kApplicationName, nullptr); |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 131 | if (error) { |
| 132 | VLOG(1) << "Failed to store migrated password. " << error->message; |
| 133 | g_error_free(error); |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 134 | return std::string(); |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 135 | } |
| 136 | if (!success) { |
| 137 | VLOG(1) << "Failed to store migrated password."; |
| 138 | return std::string(); |
| 139 | } |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 140 | |
| 141 | // Delete old entry. |
| 142 | // Even if deletion failed, we have to use the password that we created. |
| 143 | success = LibsecretLoader::secret_password_clear_sync( |
| 144 | &kKeystoreSchemaV1, nullptr, &error, nullptr); |
cfroussios | 51e9b14 | 2016-10-24 14:28:31 | [diff] [blame] | 145 | if (error) { |
| 146 | VLOG(1) << "OSCrypt failed to delete deprecated password. " |
| 147 | << error->message; |
| 148 | g_error_free(error); |
| 149 | } |
cfroussios | 02da78d | 2016-08-30 09:27:05 | [diff] [blame] | 150 | |
| 151 | VLOG(1) << "OSCrypt migrated from deprecated password."; |
| 152 | |
| 153 | return password; |
| 154 | } |