OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 9 #include "chrome/browser/chrome_thread.h" |
| 10 #include "chrome/common/notification_service.h" |
| 11 #include "chrome/common/notification_type.h" |
| 12 |
| 13 namespace chromeos { |
| 14 |
| 15 OwnerManager::OwnerManager() |
| 16 : private_key_(NULL), |
| 17 public_key_(NULL), |
| 18 utils_(OwnerKeyUtils::Create()) { |
| 19 } |
| 20 |
| 21 OwnerManager::~OwnerManager() {} |
| 22 |
| 23 bool OwnerManager::IsAlreadyOwned() { |
| 24 return file_util::PathExists(utils_->GetOwnerKeyFilePath()); |
| 25 } |
| 26 |
| 27 bool OwnerManager::StartLoadOwnerKeyAttempt() { |
| 28 if (!IsAlreadyOwned()) { |
| 29 LOG(ERROR) << "Device not yet owned"; |
| 30 return false; |
| 31 } |
| 32 ChromeThread::PostTask( |
| 33 ChromeThread::FILE, FROM_HERE, |
| 34 NewRunnableMethod(this, &OwnerManager::LoadOwnerKey)); |
| 35 return true; |
| 36 } |
| 37 |
| 38 bool OwnerManager::StartTakeOwnershipAttempt() { |
| 39 if (IsAlreadyOwned()) { |
| 40 LOG(ERROR) << "Device is already owned"; |
| 41 return false; |
| 42 } |
| 43 ChromeThread::PostTask( |
| 44 ChromeThread::FILE, FROM_HERE, |
| 45 NewRunnableMethod(this, &OwnerManager::GenerateKeysAndExportPublic)); |
| 46 return true; |
| 47 } |
| 48 |
| 49 bool OwnerManager::StartSigningAttempt(const std::string& data, Delegate* d) { |
| 50 if (!IsAlreadyOwned()) { |
| 51 LOG(ERROR) << "Device not yet owned"; |
| 52 return false; |
| 53 } |
| 54 ChromeThread::ID thread_id; |
| 55 if (!ChromeThread::GetCurrentThreadIdentifier(&thread_id)) |
| 56 thread_id = ChromeThread::UI; |
| 57 ChromeThread::PostTask( |
| 58 ChromeThread::FILE, FROM_HERE, |
| 59 NewRunnableMethod(this, &OwnerManager::Sign, thread_id, data, d)); |
| 60 return true; |
| 61 } |
| 62 |
| 63 bool OwnerManager::StartVerifyAttempt(const std::string& data, |
| 64 const std::string& signature, |
| 65 Delegate* d) { |
| 66 if (!IsAlreadyOwned()) { |
| 67 LOG(ERROR) << "Device not yet owned"; |
| 68 return false; |
| 69 } |
| 70 ChromeThread::ID thread_id; |
| 71 if (!ChromeThread::GetCurrentThreadIdentifier(&thread_id)) |
| 72 thread_id = ChromeThread::UI; |
| 73 ChromeThread::PostTask( |
| 74 ChromeThread::FILE, FROM_HERE, |
| 75 NewRunnableMethod(this, |
| 76 &OwnerManager::Verify, |
| 77 thread_id, |
| 78 data, |
| 79 signature, |
| 80 d)); |
| 81 return true; |
| 82 } |
| 83 |
| 84 void OwnerManager::LoadOwnerKey() { |
| 85 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 86 public_key_ = utils_->ImportPublicKey(utils_->GetOwnerKeyFilePath()); |
| 87 |
| 88 // Whether we loaded the public key or not, send a notification indicating |
| 89 // that we're done with this attempt. We send along the key if we |
| 90 // got it, NULL if not. |
| 91 ChromeThread::PostTask( |
| 92 ChromeThread::UI, FROM_HERE, |
| 93 NewRunnableMethod(this, |
| 94 &OwnerManager::SendNotification, |
| 95 NotificationType::OWNER_KEY_FETCH_ATTEMPT_COMPLETE, |
| 96 Details<SECKEYPublicKey*>(&public_key_))); |
| 97 } |
| 98 |
| 99 void OwnerManager::GenerateKeysAndExportPublic() { |
| 100 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 101 public_key_ = NULL; |
| 102 private_key_ = NULL; |
| 103 |
| 104 if (utils_->GenerateKeyPair(&private_key_, &public_key_)) { |
| 105 // If we generated the keys successfully, export them. |
| 106 ChromeThread::PostTask( |
| 107 ChromeThread::UI, FROM_HERE, |
| 108 NewRunnableMethod(this, &OwnerManager::ExportKey)); |
| 109 } else { |
| 110 // If we didn't generate the key, send along NULL with the notification |
| 111 // that we're done with this attempt. |
| 112 ChromeThread::PostTask( |
| 113 ChromeThread::UI, FROM_HERE, |
| 114 NewRunnableMethod(this, |
| 115 &OwnerManager::SendNotification, |
| 116 NotificationType::OWNER_KEY_FETCH_ATTEMPT_COMPLETE, |
| 117 Details<SECKEYPublicKey*>(&public_key_))); |
| 118 } |
| 119 } |
| 120 |
| 121 void OwnerManager::ExportKey() { |
| 122 if (!utils_->ExportPublicKeyViaDbus(public_key_)) { |
| 123 utils_->DestroyKeys(private_key_, public_key_); |
| 124 private_key_ = NULL; |
| 125 public_key_ = NULL; |
| 126 } |
| 127 |
| 128 // Whether we generated the keys or not, send a notification indicating |
| 129 // that we're done with this attempt. We send along the public key if we |
| 130 // got it, NULL if not. |
| 131 ChromeThread::PostTask( |
| 132 ChromeThread::UI, FROM_HERE, |
| 133 NewRunnableMethod(this, |
| 134 &OwnerManager::SendNotification, |
| 135 NotificationType::OWNER_KEY_FETCH_ATTEMPT_COMPLETE, |
| 136 Details<SECKEYPublicKey*>(&public_key_))); |
| 137 } |
| 138 |
| 139 bool OwnerManager::EnsurePublicKey() { |
| 140 if (!public_key_) |
| 141 LoadOwnerKey(); |
| 142 |
| 143 return public_key_ != NULL; |
| 144 } |
| 145 |
| 146 bool OwnerManager::EnsurePrivateKey() { |
| 147 if (!EnsurePublicKey()) |
| 148 return false; |
| 149 |
| 150 if (!private_key_) |
| 151 private_key_ = utils_->FindPrivateKey(public_key_); |
| 152 |
| 153 return private_key_ != NULL; |
| 154 } |
| 155 |
| 156 void OwnerManager::Sign(const ChromeThread::ID thread_id, |
| 157 const std::string& data, |
| 158 Delegate* d) { |
| 159 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 160 |
| 161 // If it's not the case that we can get both keys... |
| 162 if (!(EnsurePublicKey() && EnsurePrivateKey())) { |
| 163 ChromeThread::PostTask( |
| 164 thread_id, FROM_HERE, |
| 165 NewRunnableMethod(this, |
| 166 &OwnerManager::CallDelegate, |
| 167 d, KEY_UNAVAILABLE, std::string())); |
| 168 return; |
| 169 } |
| 170 |
| 171 // TODO(cmasone): Sign |data| with |private_key_|, return |
| 172 // appropriate errors via CallDelegate. |
| 173 ChromeThread::PostTask( |
| 174 thread_id, FROM_HERE, |
| 175 NewRunnableMethod(this, |
| 176 &OwnerManager::CallDelegate, |
| 177 d, SUCCESS, data)); |
| 178 } |
| 179 |
| 180 void OwnerManager::Verify(const ChromeThread::ID thread_id, |
| 181 const std::string& data, |
| 182 const std::string& signature, |
| 183 Delegate* d) { |
| 184 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 185 |
| 186 if (!EnsurePublicKey()) { |
| 187 ChromeThread::PostTask( |
| 188 thread_id, FROM_HERE, |
| 189 NewRunnableMethod(this, |
| 190 &OwnerManager::CallDelegate, |
| 191 d, KEY_UNAVAILABLE, std::string())); |
| 192 return; |
| 193 } |
| 194 |
| 195 LOG(INFO) << "Starting verify attempt"; |
| 196 // TODO(cmasone): Verify |signature| over |data| with |public_key_|, return |
| 197 // appropriate errors via CallDelegate. |
| 198 ChromeThread::PostTask( |
| 199 thread_id, FROM_HERE, |
| 200 NewRunnableMethod(this, |
| 201 &OwnerManager::CallDelegate, |
| 202 d, SUCCESS, std::string())); |
| 203 } |
| 204 |
| 205 void OwnerManager::SendNotification(NotificationType type, |
| 206 const NotificationDetails& details) { |
| 207 NotificationService::current()->Notify( |
| 208 type, |
| 209 NotificationService::AllSources(), |
| 210 details); |
| 211 } |
| 212 |
| 213 } // namespace chromeos |
OLD | NEW |