| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/device_to_device_authenticator.h" | 5 #include "components/proximity_auth/device_to_device_authenticator.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // Create the [Hello] message to send to the remote device. | 85 // Create the [Hello] message to send to the remote device. |
| 86 state_ = State::SENDING_HELLO; | 86 state_ = State::SENDING_HELLO; |
| 87 DeviceToDeviceInitiatorOperations::CreateHelloMessage( | 87 DeviceToDeviceInitiatorOperations::CreateHelloMessage( |
| 88 public_key, connection_->remote_device().persistent_symmetric_key, | 88 public_key, connection_->remote_device().persistent_symmetric_key, |
| 89 secure_message_delegate_.get(), | 89 secure_message_delegate_.get(), |
| 90 base::Bind(&DeviceToDeviceAuthenticator::OnHelloMessageCreated, | 90 base::Bind(&DeviceToDeviceAuthenticator::OnHelloMessageCreated, |
| 91 weak_ptr_factory_.GetWeakPtr())); | 91 weak_ptr_factory_.GetWeakPtr())); |
| 92 } | 92 } |
| 93 | 93 |
| 94 std::unique_ptr<base::Timer> DeviceToDeviceAuthenticator::CreateTimer() { | 94 std::unique_ptr<base::Timer> DeviceToDeviceAuthenticator::CreateTimer() { |
| 95 return base::WrapUnique(new base::OneShotTimer()); | 95 return base::MakeUnique<base::OneShotTimer>(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void DeviceToDeviceAuthenticator::OnHelloMessageCreated( | 98 void DeviceToDeviceAuthenticator::OnHelloMessageCreated( |
| 99 const std::string& message) { | 99 const std::string& message) { |
| 100 DCHECK(state_ == State::SENDING_HELLO); | 100 DCHECK(state_ == State::SENDING_HELLO); |
| 101 if (message.empty()) { | 101 if (message.empty()) { |
| 102 Fail("Failed to create [Hello]"); | 102 Fail("Failed to create [Hello]"); |
| 103 return; | 103 return; |
| 104 } | 104 } |
| 105 | 105 |
| 106 // Add a timeout for receiving the [Responder Auth] message as a guard. | 106 // Add a timeout for receiving the [Responder Auth] message as a guard. |
| 107 timer_ = CreateTimer(); | 107 timer_ = CreateTimer(); |
| 108 timer_->Start( | 108 timer_->Start( |
| 109 FROM_HERE, base::TimeDelta::FromSeconds(kResponderAuthTimeoutSeconds), | 109 FROM_HERE, base::TimeDelta::FromSeconds(kResponderAuthTimeoutSeconds), |
| 110 base::Bind(&DeviceToDeviceAuthenticator::OnResponderAuthTimedOut, | 110 base::Bind(&DeviceToDeviceAuthenticator::OnResponderAuthTimedOut, |
| 111 weak_ptr_factory_.GetWeakPtr())); | 111 weak_ptr_factory_.GetWeakPtr())); |
| 112 | 112 |
| 113 // Send the [Hello] message to the remote device. | 113 // Send the [Hello] message to the remote device. |
| 114 state_ = State::SENT_HELLO; | 114 state_ = State::SENT_HELLO; |
| 115 hello_message_ = message; | 115 hello_message_ = message; |
| 116 std::string permit_id = kPermitIdPrefix + account_id_; | 116 std::string permit_id = kPermitIdPrefix + account_id_; |
| 117 connection_->SendMessage( | 117 connection_->SendMessage( |
| 118 base::WrapUnique(new WireMessage(hello_message_, permit_id))); | 118 base::MakeUnique<WireMessage>(hello_message_, permit_id)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void DeviceToDeviceAuthenticator::OnResponderAuthTimedOut() { | 121 void DeviceToDeviceAuthenticator::OnResponderAuthTimedOut() { |
| 122 DCHECK(state_ == State::SENT_HELLO); | 122 DCHECK(state_ == State::SENT_HELLO); |
| 123 Fail("Timed out waiting for [Responder Auth]"); | 123 Fail("Timed out waiting for [Responder Auth]"); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void DeviceToDeviceAuthenticator::OnResponderAuthValidated( | 126 void DeviceToDeviceAuthenticator::OnResponderAuthValidated( |
| 127 bool validated, | 127 bool validated, |
| 128 const std::string& session_symmetric_key) { | 128 const std::string& session_symmetric_key) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 147 | 147 |
| 148 void DeviceToDeviceAuthenticator::OnInitiatorAuthCreated( | 148 void DeviceToDeviceAuthenticator::OnInitiatorAuthCreated( |
| 149 const std::string& message) { | 149 const std::string& message) { |
| 150 DCHECK(state_ == State::VALIDATED_RESPONDER_AUTH); | 150 DCHECK(state_ == State::VALIDATED_RESPONDER_AUTH); |
| 151 if (message.empty()) { | 151 if (message.empty()) { |
| 152 Fail("Failed to create [Initiator Auth]"); | 152 Fail("Failed to create [Initiator Auth]"); |
| 153 return; | 153 return; |
| 154 } | 154 } |
| 155 | 155 |
| 156 state_ = State::SENT_INITIATOR_AUTH; | 156 state_ = State::SENT_INITIATOR_AUTH; |
| 157 connection_->SendMessage(base::WrapUnique(new WireMessage(message))); | 157 connection_->SendMessage(base::MakeUnique<WireMessage>(message)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void DeviceToDeviceAuthenticator::Fail(const std::string& error_message) { | 160 void DeviceToDeviceAuthenticator::Fail(const std::string& error_message) { |
| 161 Fail(error_message, Result::FAILURE); | 161 Fail(error_message, Result::FAILURE); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void DeviceToDeviceAuthenticator::Fail(const std::string& error_message, | 164 void DeviceToDeviceAuthenticator::Fail(const std::string& error_message, |
| 165 Result result) { | 165 Result result) { |
| 166 DCHECK(result != Result::SUCCESS); | 166 DCHECK(result != Result::SUCCESS); |
| 167 PA_LOG(WARNING) << "Authentication failed: " << error_message; | 167 PA_LOG(WARNING) << "Authentication failed: " << error_message; |
| 168 state_ = State::AUTHENTICATION_FAILURE; | 168 state_ = State::AUTHENTICATION_FAILURE; |
| 169 weak_ptr_factory_.InvalidateWeakPtrs(); | 169 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 170 connection_->RemoveObserver(this); | 170 connection_->RemoveObserver(this); |
| 171 timer_.reset(); | 171 timer_.reset(); |
| 172 callback_.Run(result, nullptr); | 172 callback_.Run(result, nullptr); |
| 173 } | 173 } |
| 174 | 174 |
| 175 void DeviceToDeviceAuthenticator::Succeed() { | 175 void DeviceToDeviceAuthenticator::Succeed() { |
| 176 DCHECK(state_ == State::SENT_INITIATOR_AUTH); | 176 DCHECK(state_ == State::SENT_INITIATOR_AUTH); |
| 177 DCHECK(!session_symmetric_key_.empty()); | 177 DCHECK(!session_symmetric_key_.empty()); |
| 178 PA_LOG(INFO) << "Authentication succeeded!"; | 178 PA_LOG(INFO) << "Authentication succeeded!"; |
| 179 | 179 |
| 180 state_ = State::AUTHENTICATION_SUCCESS; | 180 state_ = State::AUTHENTICATION_SUCCESS; |
| 181 connection_->RemoveObserver(this); | 181 connection_->RemoveObserver(this); |
| 182 callback_.Run( | 182 callback_.Run( |
| 183 Result::SUCCESS, | 183 Result::SUCCESS, |
| 184 base::WrapUnique(new DeviceToDeviceSecureContext( | 184 base::MakeUnique<DeviceToDeviceSecureContext>( |
| 185 std::move(secure_message_delegate_), session_symmetric_key_, | 185 std::move(secure_message_delegate_), session_symmetric_key_, |
| 186 responder_auth_message_, SecureContext::PROTOCOL_VERSION_THREE_ONE))); | 186 responder_auth_message_, SecureContext::PROTOCOL_VERSION_THREE_ONE)); |
| 187 } | 187 } |
| 188 | 188 |
| 189 void DeviceToDeviceAuthenticator::OnConnectionStatusChanged( | 189 void DeviceToDeviceAuthenticator::OnConnectionStatusChanged( |
| 190 Connection* connection, | 190 Connection* connection, |
| 191 Connection::Status old_status, | 191 Connection::Status old_status, |
| 192 Connection::Status new_status) { | 192 Connection::Status new_status) { |
| 193 // We do not expect the connection to drop during authentication. | 193 // We do not expect the connection to drop during authentication. |
| 194 if (new_status == Connection::DISCONNECTED) { | 194 if (new_status == Connection::DISCONNECTED) { |
| 195 Fail("Disconnected while authentication is in progress", | 195 Fail("Disconnected while authentication is in progress", |
| 196 Result::DISCONNECTED); | 196 Result::DISCONNECTED); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 Succeed(); | 230 Succeed(); |
| 231 else | 231 else |
| 232 Fail("Failed to send [Initiator Auth]"); | 232 Fail("Failed to send [Initiator Auth]"); |
| 233 } else if (!success && state_ == State::SENT_HELLO) { | 233 } else if (!success && state_ == State::SENT_HELLO) { |
| 234 DCHECK(message.payload() == hello_message_); | 234 DCHECK(message.payload() == hello_message_); |
| 235 Fail("Failed to send [Hello]"); | 235 Fail("Failed to send [Hello]"); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 } // namespace proximity_auth | 239 } // namespace proximity_auth |
| OLD | NEW |