OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/os_crypt/key_storage_libsecret.h" | 5 #include "components/os_crypt/key_storage_libsecret.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "components/os_crypt/libsecret_util_linux.h" | 10 #include "components/os_crypt/libsecret_util_linux.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 #if defined(OFFICIAL_BUILD) | |
15 const char kKeyStorageEntryName[] = "Chrome Safe Storage"; | |
16 #else | |
17 const char kKeyStorageEntryName[] = "Chromium Safe Storage"; | |
18 #endif | |
19 | |
20 const SecretSchema kKeystoreSchema = { | 14 const SecretSchema kKeystoreSchema = { |
21 "chrome_libsecret_os_crypt_password", | 15 "chrome_libsecret_os_crypt_password", |
22 SECRET_SCHEMA_NONE, | 16 SECRET_SCHEMA_NONE, |
23 { | 17 { |
24 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, | 18 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
25 }}; | 19 }}; |
26 | 20 |
27 std::string AddRandomPasswordInLibsecret() { | 21 } // namespace |
| 22 |
| 23 std::string KeyStorageLibsecret::AddRandomPasswordInLibsecret() { |
28 std::string password; | 24 std::string password; |
29 base::Base64Encode(base::RandBytesAsString(16), &password); | 25 base::Base64Encode(base::RandBytesAsString(16), &password); |
30 GError* error = nullptr; | 26 GError* error = nullptr; |
31 LibsecretLoader::secret_password_store_sync( | 27 LibsecretLoader::secret_password_store_sync( |
32 &kKeystoreSchema, nullptr, kKeyStorageEntryName, password.c_str(), | 28 &kKeystoreSchema, nullptr, KeyStorageLinux::kKey, password.c_str(), |
33 nullptr, &error, nullptr); | 29 nullptr, &error, nullptr); |
34 | 30 |
35 if (error) { | 31 if (error) { |
36 VLOG(1) << "Libsecret lookup failed: " << error->message; | 32 VLOG(1) << "Libsecret lookup failed: " << error->message; |
37 return std::string(); | 33 return std::string(); |
38 } | 34 } |
39 return password; | 35 return password; |
40 } | 36 } |
41 | 37 |
42 } // namespace | |
43 | |
44 std::string KeyStorageLibsecret::GetKey() { | 38 std::string KeyStorageLibsecret::GetKey() { |
45 GError* error = nullptr; | 39 GError* error = nullptr; |
46 LibsecretAttributesBuilder attrs; | 40 LibsecretAttributesBuilder attrs; |
47 SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( | 41 SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( |
48 nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error); | 42 nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error); |
49 | 43 |
50 if (error) { | 44 if (error) { |
51 VLOG(1) << "Libsecret lookup failed: " << error->message; | 45 VLOG(1) << "Libsecret lookup failed: " << error->message; |
52 g_error_free(error); | 46 g_error_free(error); |
53 return std::string(); | 47 return std::string(); |
54 } | 48 } |
55 if (!password_libsecret) { | 49 if (!password_libsecret) { |
56 return AddRandomPasswordInLibsecret(); | 50 return AddRandomPasswordInLibsecret(); |
57 } | 51 } |
58 std::string password( | 52 std::string password( |
59 LibsecretLoader::secret_value_get_text(password_libsecret)); | 53 LibsecretLoader::secret_value_get_text(password_libsecret)); |
60 LibsecretLoader::secret_value_unref(password_libsecret); | 54 LibsecretLoader::secret_value_unref(password_libsecret); |
61 return password; | 55 return password; |
62 } | 56 } |
63 | 57 |
64 bool KeyStorageLibsecret::Init() { | 58 bool KeyStorageLibsecret::Init() { |
65 return LibsecretLoader::EnsureLibsecretLoaded(); | 59 return LibsecretLoader::EnsureLibsecretLoaded(); |
66 } | 60 } |
OLD | NEW |