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" |
| 10 | #include "components/os_crypt/libsecret_util_linux.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 14 | const SecretSchema kKeystoreSchema = { |
| 15 | "chrome_libsecret_os_crypt_password", |
| 16 | SECRET_SCHEMA_NONE, |
| 17 | { |
| 18 | {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 19 | }}; |
| 20 | |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame^] | 21 | } // namespace |
| 22 | |
| 23 | std::string KeyStorageLibsecret::AddRandomPasswordInLibsecret() { |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 24 | std::string password; |
| 25 | base::Base64Encode(base::RandBytesAsString(16), &password); |
| 26 | GError* error = nullptr; |
| 27 | LibsecretLoader::secret_password_store_sync( |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame^] | 28 | &kKeystoreSchema, nullptr, KeyStorageLinux::kKey, password.c_str(), |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 29 | nullptr, &error, nullptr); |
| 30 | |
| 31 | if (error) { |
| 32 | VLOG(1) << "Libsecret lookup failed: " << error->message; |
| 33 | return std::string(); |
| 34 | } |
| 35 | return password; |
| 36 | } |
| 37 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 38 | std::string KeyStorageLibsecret::GetKey() { |
| 39 | GError* error = nullptr; |
| 40 | LibsecretAttributesBuilder attrs; |
| 41 | SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( |
| 42 | nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error); |
| 43 | |
| 44 | if (error) { |
| 45 | VLOG(1) << "Libsecret lookup failed: " << error->message; |
| 46 | g_error_free(error); |
| 47 | return std::string(); |
| 48 | } |
| 49 | if (!password_libsecret) { |
| 50 | return AddRandomPasswordInLibsecret(); |
| 51 | } |
| 52 | std::string password( |
| 53 | LibsecretLoader::secret_value_get_text(password_libsecret)); |
| 54 | LibsecretLoader::secret_value_unref(password_libsecret); |
| 55 | return password; |
| 56 | } |
| 57 | |
| 58 | bool KeyStorageLibsecret::Init() { |
| 59 | return LibsecretLoader::EnsureLibsecretLoaded(); |
| 60 | } |