| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/messenger_impl.h" | 5 #include "components/proximity_auth/messenger_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64url.h" | 9 #include "base/base64url.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 message.SetString(kTypeKey, kMessageTypeLocalEvent); | 108 message.SetString(kTypeKey, kMessageTypeLocalEvent); |
| 109 message.SetString(kNameKey, kUnlockEventName); | 109 message.SetString(kNameKey, kUnlockEventName); |
| 110 queued_messages_.push_back(PendingMessage(message)); | 110 queued_messages_.push_back(PendingMessage(message)); |
| 111 ProcessMessageQueue(); | 111 ProcessMessageQueue(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void MessengerImpl::RequestDecryption(const std::string& challenge) { | 114 void MessengerImpl::RequestDecryption(const std::string& challenge) { |
| 115 if (!SupportsSignIn()) { | 115 if (!SupportsSignIn()) { |
| 116 PA_LOG(WARNING) << "Dropping decryption request, as remote device " | 116 PA_LOG(WARNING) << "Dropping decryption request, as remote device " |
| 117 << "does not support protocol v3.1."; | 117 << "does not support protocol v3.1."; |
| 118 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 118 for (auto& observer : observers_) |
| 119 OnDecryptResponse(std::string())); | 119 observer.OnDecryptResponse(std::string()); |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 | 122 |
| 123 const std::string encrypted_message_data = challenge; | 123 const std::string encrypted_message_data = challenge; |
| 124 std::string encrypted_message_data_base64; | 124 std::string encrypted_message_data_base64; |
| 125 base::Base64UrlEncode(encrypted_message_data, | 125 base::Base64UrlEncode(encrypted_message_data, |
| 126 base::Base64UrlEncodePolicy::INCLUDE_PADDING, | 126 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 127 &encrypted_message_data_base64); | 127 &encrypted_message_data_base64); |
| 128 | 128 |
| 129 base::DictionaryValue message; | 129 base::DictionaryValue message; |
| 130 message.SetString(kTypeKey, kMessageTypeDecryptRequest); | 130 message.SetString(kTypeKey, kMessageTypeDecryptRequest); |
| 131 message.SetString(kEncryptedDataKey, encrypted_message_data_base64); | 131 message.SetString(kEncryptedDataKey, encrypted_message_data_base64); |
| 132 queued_messages_.push_back(PendingMessage(message)); | 132 queued_messages_.push_back(PendingMessage(message)); |
| 133 ProcessMessageQueue(); | 133 ProcessMessageQueue(); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void MessengerImpl::RequestUnlock() { | 136 void MessengerImpl::RequestUnlock() { |
| 137 if (!SupportsSignIn()) { | 137 if (!SupportsSignIn()) { |
| 138 PA_LOG(WARNING) << "Dropping unlock request, as remote device does not " | 138 PA_LOG(WARNING) << "Dropping unlock request, as remote device does not " |
| 139 << "support protocol v3.1."; | 139 << "support protocol v3.1."; |
| 140 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnUnlockResponse(false)); | 140 for (auto& observer : observers_) |
| 141 observer.OnUnlockResponse(false); |
| 141 return; | 142 return; |
| 142 } | 143 } |
| 143 | 144 |
| 144 base::DictionaryValue message; | 145 base::DictionaryValue message; |
| 145 message.SetString(kTypeKey, kMessageTypeUnlockRequest); | 146 message.SetString(kTypeKey, kMessageTypeUnlockRequest); |
| 146 queued_messages_.push_back(PendingMessage(message)); | 147 queued_messages_.push_back(PendingMessage(message)); |
| 147 ProcessMessageQueue(); | 148 ProcessMessageQueue(); |
| 148 } | 149 } |
| 149 | 150 |
| 150 SecureContext* MessengerImpl::GetSecureContext() const { | 151 SecureContext* MessengerImpl::GetSecureContext() const { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 183 |
| 183 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { | 184 void MessengerImpl::OnMessageDecoded(const std::string& decoded_message) { |
| 184 // TODO(tengs): Unify the iOS status update protocol with the existing Android | 185 // TODO(tengs): Unify the iOS status update protocol with the existing Android |
| 185 // protocol, so we don't have this special case. | 186 // protocol, so we don't have this special case. |
| 186 if (decoded_message == kScreenUnlocked || decoded_message == kScreenLocked) { | 187 if (decoded_message == kScreenUnlocked || decoded_message == kScreenLocked) { |
| 187 RemoteStatusUpdate update; | 188 RemoteStatusUpdate update; |
| 188 update.user_presence = | 189 update.user_presence = |
| 189 (decoded_message == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); | 190 (decoded_message == kScreenUnlocked ? USER_PRESENT : USER_ABSENT); |
| 190 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; | 191 update.secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED; |
| 191 update.trust_agent_state = TRUST_AGENT_ENABLED; | 192 update.trust_agent_state = TRUST_AGENT_ENABLED; |
| 192 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 193 for (auto& observer : observers_) |
| 193 OnRemoteStatusUpdate(update)); | 194 observer.OnRemoteStatusUpdate(update); |
| 194 pending_message_.reset(); | 195 pending_message_.reset(); |
| 195 ProcessMessageQueue(); | 196 ProcessMessageQueue(); |
| 196 return; | 197 return; |
| 197 } | 198 } |
| 198 | 199 |
| 199 // The decoded message should be a JSON string. | 200 // The decoded message should be a JSON string. |
| 200 std::unique_ptr<base::Value> message_value = | 201 std::unique_ptr<base::Value> message_value = |
| 201 base::JSONReader::Read(decoded_message); | 202 base::JSONReader::Read(decoded_message); |
| 202 if (!message_value || !message_value->IsType(base::Value::TYPE_DICTIONARY)) { | 203 if (!message_value || !message_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 203 PA_LOG(ERROR) << "Unable to parse message as JSON:\n" << decoded_message; | 204 PA_LOG(ERROR) << "Unable to parse message as JSON:\n" << decoded_message; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 | 257 |
| 257 void MessengerImpl::HandleRemoteStatusUpdateMessage( | 258 void MessengerImpl::HandleRemoteStatusUpdateMessage( |
| 258 const base::DictionaryValue& message) { | 259 const base::DictionaryValue& message) { |
| 259 std::unique_ptr<RemoteStatusUpdate> status_update = | 260 std::unique_ptr<RemoteStatusUpdate> status_update = |
| 260 RemoteStatusUpdate::Deserialize(message); | 261 RemoteStatusUpdate::Deserialize(message); |
| 261 if (!status_update) { | 262 if (!status_update) { |
| 262 PA_LOG(ERROR) << "Unexpected remote status update: " << message; | 263 PA_LOG(ERROR) << "Unexpected remote status update: " << message; |
| 263 return; | 264 return; |
| 264 } | 265 } |
| 265 | 266 |
| 266 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 267 for (auto& observer : observers_) |
| 267 OnRemoteStatusUpdate(*status_update)); | 268 observer.OnRemoteStatusUpdate(*status_update); |
| 268 } | 269 } |
| 269 | 270 |
| 270 void MessengerImpl::HandleDecryptResponseMessage( | 271 void MessengerImpl::HandleDecryptResponseMessage( |
| 271 const base::DictionaryValue& message) { | 272 const base::DictionaryValue& message) { |
| 272 std::string base64_data; | 273 std::string base64_data; |
| 273 std::string decrypted_data; | 274 std::string decrypted_data; |
| 274 if (!message.GetString(kDataKey, &base64_data) || base64_data.empty()) { | 275 if (!message.GetString(kDataKey, &base64_data) || base64_data.empty()) { |
| 275 PA_LOG(ERROR) << "Decrypt response missing '" << kDataKey << "' value."; | 276 PA_LOG(ERROR) << "Decrypt response missing '" << kDataKey << "' value."; |
| 276 } else if (!base::Base64UrlDecode( | 277 } else if (!base::Base64UrlDecode( |
| 277 base64_data, base::Base64UrlDecodePolicy::REQUIRE_PADDING, | 278 base64_data, base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 278 &decrypted_data)) { | 279 &decrypted_data)) { |
| 279 PA_LOG(ERROR) << "Unable to base64-decode decrypt response."; | 280 PA_LOG(ERROR) << "Unable to base64-decode decrypt response."; |
| 280 } | 281 } |
| 281 | 282 |
| 282 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 283 for (auto& observer : observers_) |
| 283 OnDecryptResponse(decrypted_data)); | 284 observer.OnDecryptResponse(decrypted_data); |
| 284 } | 285 } |
| 285 | 286 |
| 286 void MessengerImpl::HandleUnlockResponseMessage( | 287 void MessengerImpl::HandleUnlockResponseMessage( |
| 287 const base::DictionaryValue& message) { | 288 const base::DictionaryValue& message) { |
| 288 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnUnlockResponse(true)); | 289 for (auto& observer : observers_) |
| 290 observer.OnUnlockResponse(true); |
| 289 } | 291 } |
| 290 | 292 |
| 291 void MessengerImpl::PollScreenStateForIOS() { | 293 void MessengerImpl::PollScreenStateForIOS() { |
| 292 if (!connection_->IsConnected()) | 294 if (!connection_->IsConnected()) |
| 293 return; | 295 return; |
| 294 | 296 |
| 295 // Sends message requesting screen state. | 297 // Sends message requesting screen state. |
| 296 queued_messages_.push_back(PendingMessage(std::string(kPollScreenState))); | 298 queued_messages_.push_back(PendingMessage(std::string(kPollScreenState))); |
| 297 ProcessMessageQueue(); | 299 ProcessMessageQueue(); |
| 298 | 300 |
| 299 // Schedules the next message in |kPollingIntervalSeconds|. | 301 // Schedules the next message in |kPollingIntervalSeconds|. |
| 300 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 302 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 301 FROM_HERE, base::Bind(&MessengerImpl::PollScreenStateForIOS, | 303 FROM_HERE, base::Bind(&MessengerImpl::PollScreenStateForIOS, |
| 302 weak_ptr_factory_.GetWeakPtr()), | 304 weak_ptr_factory_.GetWeakPtr()), |
| 303 base::TimeDelta::FromSeconds(kIOSPollingIntervalSeconds)); | 305 base::TimeDelta::FromSeconds(kIOSPollingIntervalSeconds)); |
| 304 } | 306 } |
| 305 | 307 |
| 306 void MessengerImpl::OnConnectionStatusChanged(Connection* connection, | 308 void MessengerImpl::OnConnectionStatusChanged(Connection* connection, |
| 307 Connection::Status old_status, | 309 Connection::Status old_status, |
| 308 Connection::Status new_status) { | 310 Connection::Status new_status) { |
| 309 DCHECK_EQ(connection, connection_.get()); | 311 DCHECK_EQ(connection, connection_.get()); |
| 310 if (new_status == Connection::DISCONNECTED) { | 312 if (new_status == Connection::DISCONNECTED) { |
| 311 PA_LOG(INFO) << "Secure channel disconnected..."; | 313 PA_LOG(INFO) << "Secure channel disconnected..."; |
| 312 connection_->RemoveObserver(this); | 314 connection_->RemoveObserver(this); |
| 313 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnDisconnected()); | 315 for (auto& observer : observers_) |
| 316 observer.OnDisconnected(); |
| 314 // TODO(isherman): Determine whether it's also necessary/appropriate to fire | 317 // TODO(isherman): Determine whether it's also necessary/appropriate to fire |
| 315 // this notification from the destructor. | 318 // this notification from the destructor. |
| 316 } | 319 } |
| 317 } | 320 } |
| 318 | 321 |
| 319 void MessengerImpl::OnMessageReceived(const Connection& connection, | 322 void MessengerImpl::OnMessageReceived(const Connection& connection, |
| 320 const WireMessage& wire_message) { | 323 const WireMessage& wire_message) { |
| 321 secure_context_->Decode(wire_message.payload(), | 324 secure_context_->Decode(wire_message.payload(), |
| 322 base::Bind(&MessengerImpl::OnMessageDecoded, | 325 base::Bind(&MessengerImpl::OnMessageDecoded, |
| 323 weak_ptr_factory_.GetWeakPtr())); | 326 weak_ptr_factory_.GetWeakPtr())); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 335 // Don't wait if the message could not be sent, as there won't ever be a | 338 // Don't wait if the message could not be sent, as there won't ever be a |
| 336 // response in that case. Likewise, don't wait for a response to local | 339 // response in that case. Likewise, don't wait for a response to local |
| 337 // event messages, as there is no response for such messages. | 340 // event messages, as there is no response for such messages. |
| 338 if (success && pending_message_->type != kMessageTypeLocalEvent) | 341 if (success && pending_message_->type != kMessageTypeLocalEvent) |
| 339 return; | 342 return; |
| 340 | 343 |
| 341 // Notify observer of failure if sending the message fails. | 344 // Notify observer of failure if sending the message fails. |
| 342 // For local events, we don't expect a response, so on success, we | 345 // For local events, we don't expect a response, so on success, we |
| 343 // notify observers right away. | 346 // notify observers right away. |
| 344 if (pending_message_->type == kMessageTypeDecryptRequest) { | 347 if (pending_message_->type == kMessageTypeDecryptRequest) { |
| 345 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 348 for (auto& observer : observers_) |
| 346 OnDecryptResponse(std::string())); | 349 observer.OnDecryptResponse(std::string()); |
| 347 } else if (pending_message_->type == kMessageTypeUnlockRequest) { | 350 } else if (pending_message_->type == kMessageTypeUnlockRequest) { |
| 348 FOR_EACH_OBSERVER(MessengerObserver, observers_, OnUnlockResponse(false)); | 351 for (auto& observer : observers_) |
| 352 observer.OnUnlockResponse(false); |
| 349 } else if (pending_message_->type == kMessageTypeLocalEvent) { | 353 } else if (pending_message_->type == kMessageTypeLocalEvent) { |
| 350 FOR_EACH_OBSERVER(MessengerObserver, observers_, | 354 for (auto& observer : observers_) |
| 351 OnUnlockEventSent(success)); | 355 observer.OnUnlockEventSent(success); |
| 352 } else { | 356 } else { |
| 353 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type | 357 PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type |
| 354 << "' sent."; | 358 << "' sent."; |
| 355 } | 359 } |
| 356 | 360 |
| 357 pending_message_.reset(); | 361 pending_message_.reset(); |
| 358 ProcessMessageQueue(); | 362 ProcessMessageQueue(); |
| 359 } | 363 } |
| 360 | 364 |
| 361 } // namespace proximity_auth | 365 } // namespace proximity_auth |
| OLD | NEW |