OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/login/owner_manager.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/file_path.h" | |
12 #include "base/file_util.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/chromeos/boot_times_loader.h" | |
15 #include "chrome/browser/chromeos/login/signed_settings_cache.h" | |
16 #include "chrome/common/chrome_notification_types.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "content/public/browser/notification_service.h" | |
19 | |
20 using content::BrowserThread; | |
21 | |
22 namespace chromeos { | |
23 | |
24 OwnerManager::OwnerManager() | |
25 : private_key_(NULL), | |
26 public_key_(0), | |
27 utils_(OwnerKeyUtils::Create()) { | |
28 } | |
29 | |
30 void OwnerManager::UpdateOwnerKey(const BrowserThread::ID thread_id, | |
31 const std::vector<uint8>& key, | |
32 KeyUpdateDelegate* d) { | |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
34 | |
35 public_key_ = key; | |
36 | |
37 BrowserThread::PostTask( | |
38 thread_id, FROM_HERE, | |
39 base::Bind(&OwnerManager::CallKeyUpdateDelegate, this, d)); | |
40 } | |
41 | |
42 void OwnerManager::LoadOwnerKey() { | |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
44 VLOG(1) << "Loading owner key"; | |
45 int result = chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED; | |
46 | |
47 // If |public_key_| isn't empty, we already have the key, so don't | |
48 // try to import again. | |
49 if (public_key_.empty() && | |
50 !utils_->ImportPublicKey(utils_->GetOwnerKeyFilePath(), &public_key_)) { | |
51 result = chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_FAILED; | |
52 } | |
53 | |
54 // Whether we loaded the public key or not, send a notification indicating | |
55 // that we're done with this attempt. | |
56 BrowserThread::PostTask( | |
57 BrowserThread::UI, FROM_HERE, | |
58 base::Bind(&OwnerManager::SendNotification, this, result, | |
59 content::NotificationService::NoDetails())); | |
60 } | |
61 | |
62 bool OwnerManager::EnsurePublicKey() { | |
63 if (public_key_.empty()) | |
64 LoadOwnerKey(); | |
65 | |
66 return !public_key_.empty(); | |
67 } | |
68 | |
69 bool OwnerManager::EnsurePrivateKey() { | |
70 if (!EnsurePublicKey()) | |
71 return false; | |
72 | |
73 if (!private_key_.get()) | |
74 private_key_.reset(utils_->FindPrivateKey(public_key_)); | |
75 | |
76 return private_key_.get() != NULL; | |
77 } | |
78 | |
79 void OwnerManager::Sign(const BrowserThread::ID thread_id, | |
80 const std::string& data, | |
81 Delegate* d) { | |
82 BootTimesLoader::Get()->AddLoginTimeMarker("SignStart", false); | |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
84 | |
85 // If it's not the case that we can get both keys... | |
86 if (!(EnsurePublicKey() && EnsurePrivateKey())) { | |
87 BrowserThread::PostTask( | |
88 thread_id, FROM_HERE, | |
89 base::Bind(&OwnerManager::CallDelegate, this, d, KEY_UNAVAILABLE, | |
90 std::vector<uint8>())); | |
91 BootTimesLoader::Get()->AddLoginTimeMarker("SignEnd", false); | |
92 return; | |
93 } | |
94 | |
95 VLOG(1) << "Starting signing attempt"; | |
96 KeyOpCode return_code = SUCCESS; | |
97 std::vector<uint8> signature; | |
98 if (!utils_->Sign(data, &signature, private_key_.get())) { | |
99 return_code = OPERATION_FAILED; | |
100 } | |
101 | |
102 BrowserThread::PostTask( | |
103 thread_id, FROM_HERE, | |
104 base::Bind(&OwnerManager::CallDelegate, this, d, return_code, signature)); | |
105 BootTimesLoader::Get()->AddLoginTimeMarker("SignEnd", false); | |
106 } | |
107 | |
108 void OwnerManager::Verify(const BrowserThread::ID thread_id, | |
109 const std::string& data, | |
110 const std::vector<uint8>& signature, | |
111 Delegate* d) { | |
112 BootTimesLoader::Get()->AddLoginTimeMarker("VerifyStart", false); | |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
114 | |
115 if (!EnsurePublicKey()) { | |
116 BrowserThread::PostTask( | |
117 thread_id, FROM_HERE, | |
118 base::Bind(&OwnerManager::CallDelegate, this, d, KEY_UNAVAILABLE, | |
119 std::vector<uint8>())); | |
120 BootTimesLoader::Get()->AddLoginTimeMarker("VerifyEnd", false); | |
121 return; | |
122 } | |
123 | |
124 VLOG(1) << "Starting verify attempt"; | |
125 KeyOpCode return_code = SUCCESS; | |
126 if (!utils_->Verify(data, signature, public_key_)) { | |
127 return_code = OPERATION_FAILED; | |
128 } | |
129 BrowserThread::PostTask( | |
130 thread_id, FROM_HERE, | |
131 base::Bind(&OwnerManager::CallDelegate, this, d, return_code, | |
132 std::vector<uint8>())); | |
133 BootTimesLoader::Get()->AddLoginTimeMarker("VerifyEnd", false); | |
134 } | |
135 | |
136 OwnerManager::~OwnerManager() {} | |
137 | |
138 void OwnerManager::SendNotification( | |
139 int type, | |
140 const content::NotificationDetails& details) { | |
141 content::NotificationService::current()->Notify( | |
142 type, | |
143 content::NotificationService::AllSources(), | |
144 details); | |
145 } | |
146 | |
147 } // namespace chromeos | |
OLD | NEW |