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_keyring.h" |
| 6 |
| 7 #include <gnome-keyring.h> |
| 8 |
| 9 #include "base/base64.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/rand_util.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "components/os_crypt/keyring_util_linux.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 #if defined(GOOGLE_CHROME_BUILD) |
| 21 const char kApplicationName[] = "chrome"; |
| 22 #else |
| 23 const char kApplicationName[] = "chromium"; |
| 24 #endif |
| 25 |
| 26 const GnomeKeyringPasswordSchema kSchema = { |
| 27 GNOME_KEYRING_ITEM_GENERIC_SECRET, |
| 28 {{"application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING}, {nullptr}}}; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 KeyStorageKeyring::KeyStorageKeyring( |
| 33 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner) |
| 34 : main_thread_runner_(main_thread_runner) {} |
| 35 |
| 36 KeyStorageKeyring::~KeyStorageKeyring() {} |
| 37 |
| 38 bool KeyStorageKeyring::Init() { |
| 39 return GnomeKeyringLoader::LoadGnomeKeyring(); |
| 40 } |
| 41 |
| 42 std::string KeyStorageKeyring::GetKey() { |
| 43 std::string password; |
| 44 |
| 45 // Ensure GetKeyDelegate() is executed on the main thread. |
| 46 if (main_thread_runner_->BelongsToCurrentThread()) { |
| 47 GetKeyDelegate(&password, nullptr); |
| 48 } else { |
| 49 base::WaitableEvent password_loaded( |
| 50 base::WaitableEvent::ResetPolicy::MANUAL, |
| 51 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 52 main_thread_runner_->PostTask( |
| 53 FROM_HERE, |
| 54 base::Bind(&KeyStorageKeyring::GetKeyDelegate, base::Unretained(this), |
| 55 &password, &password_loaded)); |
| 56 password_loaded.Wait(); |
| 57 } |
| 58 |
| 59 return password; |
| 60 } |
| 61 |
| 62 void KeyStorageKeyring::GetKeyDelegate( |
| 63 std::string* password_ptr, |
| 64 base::WaitableEvent* password_loaded_ptr) { |
| 65 gchar* password = nullptr; |
| 66 GnomeKeyringResult result = |
| 67 GnomeKeyringLoader::gnome_keyring_find_password_sync_ptr( |
| 68 &kSchema, &password, "application", kApplicationName, nullptr); |
| 69 if (result == GNOME_KEYRING_RESULT_OK) { |
| 70 *password_ptr = password; |
| 71 GnomeKeyringLoader::gnome_keyring_free_password_ptr(password); |
| 72 } else if (result == GNOME_KEYRING_RESULT_NO_MATCH) { |
| 73 *password_ptr = KeyStorageKeyring::AddRandomPasswordInKeyring(); |
| 74 VLOG(1) << "OSCrypt generated a new password"; |
| 75 } else { |
| 76 password_ptr->clear(); |
| 77 VLOG(1) << "OSCrypt failed to use gnome-keyring"; |
| 78 } |
| 79 |
| 80 if (password_loaded_ptr) |
| 81 password_loaded_ptr->Signal(); |
| 82 } |
| 83 |
| 84 std::string KeyStorageKeyring::AddRandomPasswordInKeyring() { |
| 85 // Generate password |
| 86 std::string password; |
| 87 base::Base64Encode(base::RandBytesAsString(16), &password); |
| 88 |
| 89 // Store generated password |
| 90 GnomeKeyringResult result = |
| 91 GnomeKeyringLoader::gnome_keyring_store_password_sync_ptr( |
| 92 &kSchema, nullptr /* default keyring */, KeyStorageLinux::kKey, |
| 93 password.c_str(), "application", kApplicationName, nullptr); |
| 94 if (result != GNOME_KEYRING_RESULT_OK) { |
| 95 VLOG(1) << "Failed to store generated password to gnome-keyring"; |
| 96 return std::string(); |
| 97 } |
| 98 |
| 99 return password; |
| 100 } |
OLD | NEW |