Chromium Code Reviews| Index: components/os_crypt/key_storage_libsecret.cc |
| diff --git a/components/os_crypt/key_storage_libsecret.cc b/components/os_crypt/key_storage_libsecret.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c5264bdecf32e89533c70d899cc2bc13819d7c3e |
| --- /dev/null |
| +++ b/components/os_crypt/key_storage_libsecret.cc |
| @@ -0,0 +1,65 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/os_crypt/key_storage_libsecret.h" |
| + |
| +#include "base/base64.h" |
| +#include "base/rand_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "components/os_crypt/libsecret_util_linux.h" |
| + |
| +namespace { |
| + |
| +#if defined(OFFICIAL_BUILD) |
| +const char kKeyStorageEntryName[] = "Chrome Safe Storage"; |
| +#else |
| +const char kKeyStorageEntryName[] = "Chromium Safe Storage"; |
| +#endif |
| + |
| +const SecretSchema kKeystoreSchema = { |
| + "chrome_libsecret_os_crypt_password", |
| + SECRET_SCHEMA_NONE, |
| + { |
| + {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| + }}; |
| + |
| +std::string AddRandomPasswordInLibsecret() { |
| + std::string password; |
| + base::Base64Encode(base::RandBytesAsString(128 / 8), &password); |
| + GError* error = nullptr; |
| + LibsecretLoader::secret_password_store_sync( |
| + &kKeystoreSchema, nullptr, kKeyStorageEntryName, password.c_str(), |
| + nullptr, &error, nullptr); |
| + |
| + if (error) { |
| + VLOG(1) << "Libsecret lookup failed: " << error->message; |
| + return std::string(); |
| + } |
| + return password; |
| +} |
| + |
| +} // namespace |
| + |
| +std::string KeyStorageLibsecret::GetKey() { |
| + GError* error = nullptr; |
| + LibsecretAttributesBuilder attrs; |
| + SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( |
| + nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error); |
| + |
| + if (error) { |
| + VLOG(1) << "Libsecret lookup failed: " << error->message; |
| + g_error_free(error); |
| + return std::string(); |
| + } else if (!password_libsecret) { |
|
Lei Zhang
2016/05/19 22:45:38
There's still an else-if after a return.
cfroussios
2016/05/20 16:44:52
Done.
|
| + return AddRandomPasswordInLibsecret(); |
| + } |
| + std::string password( |
| + LibsecretLoader::secret_value_get_text(password_libsecret)); |
| + LibsecretLoader::secret_value_unref(password_libsecret); |
| + return password; |
| +} |
| + |
| +bool KeyStorageLibsecret::Init() { |
| + return LibsecretLoader::EnsureLibsecretLoaded(); |
| +} |