OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/cryptauth/secure_channel.h" | 5 #include "components/cryptauth/secure_channel.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "components/cryptauth/cryptauth_service.h" |
9 #include "components/cryptauth/wire_message.h" | 10 #include "components/cryptauth/wire_message.h" |
10 #include "components/proximity_auth/logging/logging.h" | 11 #include "components/proximity_auth/logging/logging.h" |
11 | 12 |
12 namespace cryptauth { | 13 namespace cryptauth { |
13 | 14 |
14 // static | 15 // static |
15 SecureChannel::Factory* SecureChannel::Factory::factory_instance_ = nullptr; | 16 SecureChannel::Factory* SecureChannel::Factory::factory_instance_ = nullptr; |
16 | 17 |
17 // static | 18 // static |
18 std::unique_ptr<SecureChannel> SecureChannel::Factory::NewInstance( | 19 std::unique_ptr<SecureChannel> SecureChannel::Factory::NewInstance( |
19 std::unique_ptr<Connection> connection, | 20 std::unique_ptr<Connection> connection, |
20 std::unique_ptr<Delegate> delegate) { | 21 CryptAuthService* cryptauth_service) { |
21 if (!factory_instance_) { | 22 if (!factory_instance_) { |
22 factory_instance_ = new Factory(); | 23 factory_instance_ = new Factory(); |
23 } | 24 } |
24 return factory_instance_->BuildInstance(std::move(connection), | 25 return factory_instance_->BuildInstance(std::move(connection), |
25 std::move(delegate)); | 26 cryptauth_service); |
26 } | 27 } |
27 | 28 |
28 // static | 29 // static |
29 void SecureChannel::Factory::SetInstanceForTesting(Factory* factory) { | 30 void SecureChannel::Factory::SetInstanceForTesting(Factory* factory) { |
30 factory_instance_ = factory; | 31 factory_instance_ = factory; |
31 } | 32 } |
32 | 33 |
33 std::unique_ptr<SecureChannel> SecureChannel::Factory::BuildInstance( | 34 std::unique_ptr<SecureChannel> SecureChannel::Factory::BuildInstance( |
34 std::unique_ptr<Connection> connection, | 35 std::unique_ptr<Connection> connection, |
35 std::unique_ptr<Delegate> delegate) { | 36 CryptAuthService* cryptauth_service) { |
36 return base::WrapUnique( | 37 return base::WrapUnique( |
37 new SecureChannel(std::move(connection), std::move(delegate))); | 38 new SecureChannel(std::move(connection), cryptauth_service)); |
38 } | 39 } |
39 | 40 |
40 // static | 41 // static |
41 std::string SecureChannel::StatusToString(const Status& status) { | 42 std::string SecureChannel::StatusToString(const Status& status) { |
42 switch (status) { | 43 switch (status) { |
43 case Status::DISCONNECTED: | 44 case Status::DISCONNECTED: |
44 return "[disconnected]"; | 45 return "[disconnected]"; |
45 case Status::CONNECTING: | 46 case Status::CONNECTING: |
46 return "[connecting]"; | 47 return "[connecting]"; |
47 case Status::CONNECTED: | 48 case Status::CONNECTED: |
48 return "[connected]"; | 49 return "[connected]"; |
49 case Status::AUTHENTICATING: | 50 case Status::AUTHENTICATING: |
50 return "[authenticating]"; | 51 return "[authenticating]"; |
51 case Status::AUTHENTICATED: | 52 case Status::AUTHENTICATED: |
52 return "[authenticated]"; | 53 return "[authenticated]"; |
53 default: | 54 default: |
54 return "[unknown status]"; | 55 return "[unknown status]"; |
55 } | 56 } |
56 } | 57 } |
57 | 58 |
58 SecureChannel::Delegate::~Delegate() {} | |
59 | |
60 SecureChannel::PendingMessage::PendingMessage() {} | 59 SecureChannel::PendingMessage::PendingMessage() {} |
61 | 60 |
62 SecureChannel::PendingMessage::PendingMessage( | 61 SecureChannel::PendingMessage::PendingMessage( |
63 const std::string& feature, const std::string& payload) | 62 const std::string& feature, const std::string& payload) |
64 : feature(feature), payload(payload) {} | 63 : feature(feature), payload(payload) {} |
65 | 64 |
66 SecureChannel::PendingMessage::~PendingMessage() {} | 65 SecureChannel::PendingMessage::~PendingMessage() {} |
67 | 66 |
68 SecureChannel::SecureChannel(std::unique_ptr<Connection> connection, | 67 SecureChannel::SecureChannel(std::unique_ptr<Connection> connection, |
69 std::unique_ptr<Delegate> delegate) | 68 CryptAuthService* cryptauth_service) |
70 : status_(Status::DISCONNECTED), | 69 : status_(Status::DISCONNECTED), |
71 connection_(std::move(connection)), | 70 connection_(std::move(connection)), |
72 delegate_(std::move(delegate)), | 71 cryptauth_service_(cryptauth_service), |
73 weak_ptr_factory_(this) { | 72 weak_ptr_factory_(this) { |
74 DCHECK(connection_); | 73 DCHECK(connection_); |
75 DCHECK(!connection_->IsConnected()); | 74 DCHECK(!connection_->IsConnected()); |
76 DCHECK(!connection_->remote_device().user_id.empty()); | 75 DCHECK(!connection_->remote_device().user_id.empty()); |
77 DCHECK(delegate_); | 76 DCHECK(cryptauth_service); |
78 | 77 |
79 connection_->AddObserver(this); | 78 connection_->AddObserver(this); |
80 } | 79 } |
81 | 80 |
82 SecureChannel::~SecureChannel() { | 81 SecureChannel::~SecureChannel() { |
83 connection_->RemoveObserver(this); | 82 connection_->RemoveObserver(this); |
84 } | 83 } |
85 | 84 |
86 void SecureChannel::Initialize() { | 85 void SecureChannel::Initialize() { |
87 DCHECK(status_ == Status::DISCONNECTED); | 86 DCHECK(status_ == Status::DISCONNECTED); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 for (auto& observer : observer_list_) { | 193 for (auto& observer : observer_list_) { |
195 observer.OnSecureChannelStatusChanged(this, old_status, status_); | 194 observer.OnSecureChannelStatusChanged(this, old_status, status_); |
196 } | 195 } |
197 } | 196 } |
198 | 197 |
199 void SecureChannel::Authenticate() { | 198 void SecureChannel::Authenticate() { |
200 DCHECK(status_ == Status::CONNECTED); | 199 DCHECK(status_ == Status::CONNECTED); |
201 DCHECK(!authenticator_); | 200 DCHECK(!authenticator_); |
202 | 201 |
203 authenticator_ = DeviceToDeviceAuthenticator::Factory::NewInstance( | 202 authenticator_ = DeviceToDeviceAuthenticator::Factory::NewInstance( |
204 connection_.get(), | 203 connection_.get(), connection_->remote_device().user_id, |
205 connection_->remote_device().user_id, | 204 cryptauth_service_->CreateSecureMessageDelegate()); |
206 delegate_->CreateSecureMessageDelegate()); | |
207 authenticator_->Authenticate( | 205 authenticator_->Authenticate( |
208 base::Bind(&SecureChannel::OnAuthenticationResult, | 206 base::Bind(&SecureChannel::OnAuthenticationResult, |
209 weak_ptr_factory_.GetWeakPtr())); | 207 weak_ptr_factory_.GetWeakPtr())); |
210 | 208 |
211 TransitionToStatus(Status::AUTHENTICATING); | 209 TransitionToStatus(Status::AUTHENTICATING); |
212 } | 210 } |
213 | 211 |
214 void SecureChannel::ProcessMessageQueue() { | 212 void SecureChannel::ProcessMessageQueue() { |
215 if (pending_message_ || queued_messages_.empty()) { | 213 if (pending_message_ || queued_messages_.empty()) { |
216 return; | 214 return; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 << connection_->remote_device().GetTruncatedDeviceIdForLogs(); | 261 << connection_->remote_device().GetTruncatedDeviceIdForLogs(); |
264 Disconnect(); | 262 Disconnect(); |
265 return; | 263 return; |
266 } | 264 } |
267 | 265 |
268 secure_context_ = std::move(secure_context); | 266 secure_context_ = std::move(secure_context); |
269 TransitionToStatus(Status::AUTHENTICATED); | 267 TransitionToStatus(Status::AUTHENTICATED); |
270 } | 268 } |
271 | 269 |
272 } // namespace cryptauth | 270 } // namespace cryptauth |
OLD | NEW |