Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_linux.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_posix.h" | |
| 11 | |
| 12 #ifdef OFFICIAL_BUILD | |
|
vabr (Chromium)
2016/05/13 15:10:17
I'm not sure we need to differentiate the name her
cfroussios
2016/05/13 17:09:11
This name is the name Seahorse uses to display the
| |
| 13 const char kKeyStorageEntryName[] = "Chrome Safe Storage"; | |
| 14 #else | |
| 15 const char kKeyStorageEntryName[] = "Chromium Safe Storage"; | |
| 16 #endif | |
| 17 | |
| 18 // static | |
| 19 std::unique_ptr<KeyStorage> KeyStorage::FindService() { | |
|
vabr (Chromium)
2016/05/13 15:10:17
nit: Here "Find" is a bit confusing. It sounds lik
cfroussios
2016/05/13 17:09:11
Done.
| |
| 20 std::unique_ptr<KeyStorage> key_storage; | |
| 21 | |
| 22 key_storage.reset(new KeyStorageLibsecret()); | |
|
vabr (Chromium)
2016/05/13 15:10:17
nit: You can merge lines 20 and 22.
cfroussios
2016/05/13 17:09:11
I added this redundant expression to be symmetrica
| |
| 23 if (key_storage->Init()) | |
| 24 return key_storage; | |
| 25 | |
| 26 return nullptr; | |
| 27 } | |
| 28 | |
| 29 const SecretSchema kKeystoreSchema = { | |
| 30 "chrome_libsecret_os_crypt_password", | |
| 31 SECRET_SCHEMA_NONE, | |
| 32 { | |
| 33 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, | |
| 34 }}; | |
| 35 | |
| 36 std::string AddRandomPasswordInLibsecret() { | |
|
vabr (Chromium)
2016/05/13 15:10:17
nit: Please put local (=not exported beyond this .
cfroussios
2016/05/13 17:09:11
Done.
| |
| 37 std::string password; | |
| 38 base::Base64Encode(base::RandBytesAsString(128 / 8), &password); | |
| 39 GError* error = nullptr; | |
| 40 LibsecretLoader::secret_password_store_sync( | |
| 41 &kKeystoreSchema, nullptr, kKeyStorageEntryName, password.c_str(), | |
| 42 nullptr, &error, nullptr); | |
| 43 | |
| 44 if (error) { | |
| 45 VLOG(1) << "Libsecret lookup failed: " << error->message; | |
| 46 return std::string(); | |
| 47 } | |
| 48 return password; | |
| 49 } | |
| 50 | |
| 51 std::string KeyStorageLibsecret::GetKey() { | |
| 52 GError* error = nullptr; | |
| 53 LibsecretAttributesBuilder attrs; | |
| 54 SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( | |
| 55 nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error); | |
| 56 | |
| 57 if (error) { | |
| 58 VLOG(1) << "Libsecret lookup failed: " << error->message; | |
| 59 g_error_free(error); | |
| 60 return ""; | |
|
vabr (Chromium)
2016/05/13 15:10:17
"" -> std::string()
cfroussios
2016/05/13 17:09:11
Done.
| |
| 61 } else if (password_libsecret == nullptr) { | |
| 62 return AddRandomPasswordInLibsecret(); | |
| 63 } else { | |
| 64 std::string password( | |
| 65 LibsecretLoader::secret_value_get_text(password_libsecret)); | |
| 66 LibsecretLoader::secret_value_unref(password_libsecret); | |
| 67 return password; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 bool KeyStorageLibsecret::Init() { | |
| 72 return LibsecretLoader::EnsureLibsecretLoaded(); | |
| 73 } | |
| 74 | |
| 75 KeyStorageMock::KeyStorageMock(std::string in_key) : key(in_key) {} | |
|
vabr (Chromium)
2016/05/13 15:10:17
Either use std::move(in_key) or change the type of
cfroussios
2016/05/13 17:09:11
Done.
| |
| 76 | |
| 77 std::string KeyStorageMock::GetKey() { | |
| 78 if (key == "") | |
|
vabr (Chromium)
2016/05/13 15:10:17
if (key_.empty())
(empty() is more efficient than
cfroussios
2016/05/13 17:09:11
Done.
| |
| 79 base::Base64Encode(base::RandBytesAsString(128 / 8), &key); | |
| 80 return key; | |
| 81 } | |
| 82 | |
| 83 bool KeyStorageMock::Init() { | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 void KeyStorageMock::ResetTo(std::string in_key) { | |
| 88 key = std::move(in_key); | |
| 89 } | |
| OLD | NEW |