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_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 thread_checker_.DetachFromThread(); | |
| 36 } | |
| 37 | |
| 38 KeyStorageKeyring::~KeyStorageKeyring() {} | |
| 39 | |
| 40 bool KeyStorageKeyring::Init() { | |
| 41 return GnomeKeyringLoader::LoadGnomeKeyring(); | |
| 42 } | |
| 43 | |
| 44 std::string KeyStorageKeyring::GetKey() { | |
| 45 std::string password; | |
| 46 | |
| 47 // Ensure GetKeyDelegate() is executed on the main thread. | |
| 48 if (main_thread_runner_->BelongsToCurrentThread()) { | |
| 49 GetKeyDelegate(&password, nullptr); | |
| 50 } else { | |
| 51 base::WaitableEvent password_loaded( | |
| 52 base::WaitableEvent::ResetPolicy::MANUAL, | |
| 53 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 54 main_thread_runner_->PostTask( | |
| 55 FROM_HERE, | |
| 56 base::Bind(&KeyStorageKeyring::GetKeyDelegate, base::Unretained(this), | |
| 57 &password, &password_loaded)); | |
| 58 password_loaded.Wait(); | |
| 59 } | |
| 60 | |
| 61 return password; | |
| 62 } | |
| 63 | |
| 64 void KeyStorageKeyring::GetKeyDelegate( | |
|
Lei Zhang
2016/09/01 07:56:31
Can these be inside an anonymous namespace, instea
cfroussios
2016/09/01 10:58:16
AddRandomPasswordInKeyring() needs access to prote
| |
| 65 std::string* password_ptr, | |
| 66 base::WaitableEvent* password_loaded_ptr) { | |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 68 | |
| 69 gchar* password = nullptr; | |
| 70 GnomeKeyringResult result = | |
| 71 GnomeKeyringLoader::gnome_keyring_find_password_sync_ptr( | |
| 72 &kSchema, &password, "application", kApplicationName, nullptr); | |
| 73 if (result == GNOME_KEYRING_RESULT_OK) { | |
| 74 *password_ptr = password; | |
| 75 GnomeKeyringLoader::gnome_keyring_free_password_ptr(password); | |
| 76 } else if (result == GNOME_KEYRING_RESULT_NO_MATCH) { | |
| 77 *password_ptr = KeyStorageKeyring::AddRandomPasswordInKeyring(); | |
| 78 VLOG(1) << "OSCrypt generated a new password"; | |
| 79 } else { | |
| 80 password_ptr->clear(); | |
| 81 VLOG(1) << "OSCrypt failed to use gnome-keyring"; | |
| 82 } | |
| 83 | |
| 84 if (password_loaded_ptr) | |
|
Lei Zhang
2016/09/01 07:56:31
Is this ever false?
cfroussios
2016/09/01 10:58:16
When GetKey() is called on the main thread.
Lei Zhang
2016/09/01 17:16:40
Right, that's what I get for doing late night revi
| |
| 85 password_loaded_ptr->Signal(); | |
| 86 } | |
| 87 | |
| 88 std::string KeyStorageKeyring::AddRandomPasswordInKeyring() { | |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 90 | |
| 91 // Generate password | |
| 92 std::string password; | |
| 93 base::Base64Encode(base::RandBytesAsString(16), &password); | |
| 94 | |
| 95 // Store generated password | |
| 96 GnomeKeyringResult result = | |
| 97 GnomeKeyringLoader::gnome_keyring_store_password_sync_ptr( | |
| 98 &kSchema, nullptr /* default keyring */, KeyStorageLinux::kKey, | |
| 99 password.c_str(), "application", kApplicationName, nullptr); | |
| 100 if (result != GNOME_KEYRING_RESULT_OK) { | |
| 101 VLOG(1) << "Failed to store generated password to gnome-keyring"; | |
| 102 return std::string(); | |
| 103 } | |
| 104 | |
| 105 return password; | |
| 106 } | |
| OLD | NEW |