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/strings/string_number_conversions.h" | |
13 #include "base/threading/thread.h" | |
Lei Zhang
2016/08/30 23:19:24
Given there's some threading here, do we want to u
cfroussios
2016/08/31 12:18:41
I implemented it, but I think it's a bit meh. The
Lei Zhang
2016/09/01 07:56:31
I actually wasn't that interested in checking GetK
cfroussios
2016/09/01 10:58:16
I think there might be a misunderstanding about th
| |
14 #include "components/os_crypt/keyring_util_linux.h" | |
15 | |
16 namespace { | |
17 | |
18 #if defined(GOOGLE_CHROME_BUILD) | |
19 const char kApplicationName[] = "chrome"; | |
20 #else | |
21 const char kApplicationName[] = "chromium"; | |
22 #endif | |
23 | |
24 const GnomeKeyringPasswordSchema kSchema = { | |
25 GNOME_KEYRING_ITEM_GENERIC_SECRET, | |
26 {{"application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING}, {nullptr}}}; | |
27 | |
28 } // namespace | |
29 | |
30 KeyStorageKeyring::KeyStorageKeyring( | |
31 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner) { | |
32 main_thread_runner_ = main_thread_runner; | |
Lei Zhang
2016/08/30 23:19:24
Can we use the initializer list?
cfroussios
2016/08/31 12:18:41
Done.
| |
33 } | |
34 | |
35 KeyStorageKeyring::~KeyStorageKeyring() {} | |
36 | |
37 bool KeyStorageKeyring::Init() { | |
38 return GnomeKeyringLoader::LoadGnomeKeyring(); | |
39 } | |
40 | |
41 std::string KeyStorageKeyring::GetKey() { | |
42 std::string password; | |
43 | |
44 // Ensure GetKeyDelegate() is executed on the main thread. | |
45 if (main_thread_runner_->BelongsToCurrentThread()) { | |
46 GetKeyDelegate(&password, nullptr); | |
47 } else { | |
48 base::WaitableEvent password_loaded( | |
49 base::WaitableEvent::ResetPolicy::MANUAL, | |
50 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
51 main_thread_runner_->PostTask( | |
52 FROM_HERE, base::Bind(&GetKeyDelegate, &password, &password_loaded)); | |
53 password_loaded.Wait(); | |
54 } | |
55 | |
56 return password; | |
57 } | |
58 | |
59 // static | |
60 void KeyStorageKeyring::GetKeyDelegate( | |
61 std::string* password_ptr, | |
62 base::WaitableEvent* password_loaded_ptr) { | |
63 gchar* password = nullptr; | |
64 GnomeKeyringResult result = | |
65 GnomeKeyringLoader::gnome_keyring_find_password_sync_ptr( | |
66 &kSchema, &password, "application", kApplicationName, nullptr); | |
67 if (result == GNOME_KEYRING_RESULT_OK) { | |
68 *password_ptr = password; | |
69 GnomeKeyringLoader::gnome_keyring_free_password_ptr(password); | |
70 } else if (result == GNOME_KEYRING_RESULT_NO_MATCH) { | |
71 *password_ptr = KeyStorageKeyring::AddRandomPasswordInKeyring(); | |
72 VLOG(1) << "OSCrypt generated a new password"; | |
73 } else { | |
74 password_ptr->clear(); | |
75 VLOG(1) << "OSCrypt failed to use gnome-keyring"; | |
76 } | |
77 | |
78 if (password_loaded_ptr) | |
79 password_loaded_ptr->Signal(); | |
80 } | |
81 | |
82 // static | |
83 std::string KeyStorageKeyring::AddRandomPasswordInKeyring() { | |
84 // Generate password | |
85 std::string password; | |
86 base::Base64Encode(base::RandBytesAsString(16), &password); | |
87 | |
88 // Store generated password | |
89 GnomeKeyringResult result = | |
90 GnomeKeyringLoader::gnome_keyring_store_password_sync_ptr( | |
91 &kSchema, nullptr /* default keyring */, KeyStorageLinux::kKey, | |
92 password.c_str(), "application", kApplicationName, nullptr); | |
93 if (result != GNOME_KEYRING_RESULT_OK) { | |
94 VLOG(1) << "Failed to store generated password to gnome-keyring"; | |
95 return std::string(); | |
96 } | |
97 | |
98 return password; | |
99 } | |
OLD | NEW |