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 const SecretSchema kKeystoreSchema = { | 14 #if defined(GOOGLE_CHROME_BUILD) |
| 15 const char kApplicationName[] = "chrome"; |
| 16 #else |
| 17 const char kApplicationName[] = "chromium"; |
| 18 #endif |
| 19 |
| 20 // Deprecated in M55 (crbug.com/639298) |
| 21 const SecretSchema kKeystoreSchemaV1 = { |
15 "chrome_libsecret_os_crypt_password", | 22 "chrome_libsecret_os_crypt_password", |
16 SECRET_SCHEMA_NONE, | 23 SECRET_SCHEMA_NONE, |
17 { | 24 { |
18 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, | 25 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
19 }}; | 26 }}; |
20 | 27 |
| 28 const SecretSchema kKeystoreSchemaV2 = { |
| 29 "chrome_libsecret_os_crypt_password_v2", |
| 30 SECRET_SCHEMA_DONT_MATCH_NAME, |
| 31 { |
| 32 {"application", SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 33 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 34 }}; |
| 35 |
21 } // namespace | 36 } // namespace |
22 | 37 |
23 std::string KeyStorageLibsecret::AddRandomPasswordInLibsecret() { | 38 std::string KeyStorageLibsecret::AddRandomPasswordInLibsecret() { |
24 std::string password; | 39 std::string password; |
25 base::Base64Encode(base::RandBytesAsString(16), &password); | 40 base::Base64Encode(base::RandBytesAsString(16), &password); |
26 GError* error = nullptr; | 41 GError* error = nullptr; |
27 LibsecretLoader::secret_password_store_sync( | 42 bool success = LibsecretLoader::secret_password_store_sync( |
28 &kKeystoreSchema, nullptr, KeyStorageLinux::kKey, password.c_str(), | 43 &kKeystoreSchemaV2, nullptr, KeyStorageLinux::kKey, password.c_str(), |
29 nullptr, &error, nullptr); | 44 nullptr, &error, "application", kApplicationName, nullptr); |
30 | 45 if (error || !success) { |
31 if (error) { | |
32 VLOG(1) << "Libsecret lookup failed: " << error->message; | 46 VLOG(1) << "Libsecret lookup failed: " << error->message; |
33 return std::string(); | 47 return std::string(); |
34 } | 48 } |
| 49 |
| 50 VLOG(1) << "OSCrypt generated a new password."; |
35 return password; | 51 return password; |
36 } | 52 } |
37 | 53 |
38 std::string KeyStorageLibsecret::GetKey() { | 54 std::string KeyStorageLibsecret::GetKey() { |
39 GError* error = nullptr; | 55 GError* error = nullptr; |
40 LibsecretAttributesBuilder attrs; | 56 LibsecretAttributesBuilder attrs; |
| 57 attrs.Append("application", kApplicationName); |
41 SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( | 58 SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( |
42 nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error); | 59 nullptr, &kKeystoreSchemaV2, attrs.Get(), nullptr, &error); |
43 | |
44 if (error) { | 60 if (error) { |
45 VLOG(1) << "Libsecret lookup failed: " << error->message; | 61 VLOG(1) << "Libsecret lookup failed: " << error->message; |
46 g_error_free(error); | 62 g_error_free(error); |
47 return std::string(); | 63 return std::string(); |
48 } | 64 } |
49 if (!password_libsecret) { | 65 if (!password_libsecret) { |
| 66 std::string password = Migrate(); |
| 67 if (!password.empty()) |
| 68 return password; |
50 return AddRandomPasswordInLibsecret(); | 69 return AddRandomPasswordInLibsecret(); |
51 } | 70 } |
52 std::string password( | 71 std::string password( |
53 LibsecretLoader::secret_value_get_text(password_libsecret)); | 72 LibsecretLoader::secret_value_get_text(password_libsecret)); |
54 LibsecretLoader::secret_value_unref(password_libsecret); | 73 LibsecretLoader::secret_value_unref(password_libsecret); |
55 return password; | 74 return password; |
56 } | 75 } |
57 | 76 |
58 bool KeyStorageLibsecret::Init() { | 77 bool KeyStorageLibsecret::Init() { |
59 return LibsecretLoader::EnsureLibsecretLoaded(); | 78 return LibsecretLoader::EnsureLibsecretLoaded(); |
60 } | 79 } |
| 80 |
| 81 std::string KeyStorageLibsecret::Migrate() { |
| 82 GError* error = nullptr; |
| 83 LibsecretAttributesBuilder attrs; |
| 84 |
| 85 // Detect old entry. |
| 86 SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync( |
| 87 nullptr, &kKeystoreSchemaV1, attrs.Get(), nullptr, &error); |
| 88 if (error || !password_libsecret) |
| 89 return std::string(); |
| 90 |
| 91 VLOG(1) << "OSCrypt detected a deprecated password in Libsecret."; |
| 92 std::string password( |
| 93 LibsecretLoader::secret_value_get_text(password_libsecret)); |
| 94 |
| 95 // Create new entry. |
| 96 bool success = LibsecretLoader::secret_password_store_sync( |
| 97 &kKeystoreSchemaV2, nullptr, KeyStorageLinux::kKey, password.c_str(), |
| 98 nullptr, &error, "application", kApplicationName, nullptr); |
| 99 if (error || !success) |
| 100 return std::string(); |
| 101 |
| 102 // Delete old entry. |
| 103 // Even if deletion failed, we have to use the password that we created. |
| 104 success = LibsecretLoader::secret_password_clear_sync( |
| 105 &kKeystoreSchemaV1, nullptr, &error, nullptr); |
| 106 |
| 107 VLOG(1) << "OSCrypt migrated from deprecated password."; |
| 108 |
| 109 return password; |
| 110 } |
OLD | NEW |