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 #ifndef COMPONENTS_CRYPTAUTH_SECURE_CHANNEL_H_ | 5 #ifndef COMPONENTS_CRYPTAUTH_SECURE_CHANNEL_H_ |
6 #define COMPONENTS_CRYPTAUTH_SECURE_CHANNEL_H_ | 6 #define COMPONENTS_CRYPTAUTH_SECURE_CHANNEL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "components/cryptauth/authenticator.h" | 12 #include "components/cryptauth/authenticator.h" |
13 #include "components/cryptauth/connection.h" | 13 #include "components/cryptauth/connection.h" |
14 #include "components/cryptauth/connection_observer.h" | 14 #include "components/cryptauth/connection_observer.h" |
15 #include "components/cryptauth/device_to_device_authenticator.h" | 15 #include "components/cryptauth/device_to_device_authenticator.h" |
16 #include "components/cryptauth/remote_device.h" | 16 #include "components/cryptauth/remote_device.h" |
17 #include "components/cryptauth/secure_context.h" | 17 #include "components/cryptauth/secure_context.h" |
18 #include "components/cryptauth/secure_message_delegate.h" | 18 #include "components/cryptauth/secure_message_delegate.h" |
19 | 19 |
20 namespace cryptauth { | 20 namespace cryptauth { |
21 | 21 |
| 22 class CryptAuthService; |
| 23 |
22 // An authenticated bi-directional channel for exchanging messages with remote | 24 // An authenticated bi-directional channel for exchanging messages with remote |
23 // devices. |SecureChannel| manages a |Connection| by initializing it and | 25 // devices. |SecureChannel| manages a |Connection| by initializing it and |
24 // authenticating it via a security handshake once the connection has occurred. | 26 // authenticating it via a security handshake once the connection has occurred. |
25 // Once the channel has been authenticated, messages sent are automatically | 27 // Once the channel has been authenticated, messages sent are automatically |
26 // encrypted and messages received are automatically decrypted. | 28 // encrypted and messages received are automatically decrypted. |
27 class SecureChannel : public ConnectionObserver { | 29 class SecureChannel : public ConnectionObserver { |
28 public: | 30 public: |
29 // Enumeration of possible states of connecting to a remote device. | 31 // Enumeration of possible states of connecting to a remote device. |
30 // DISCONNECTED: There is no connection to the device, nor is there a | 32 // DISCONNECTED: There is no connection to the device, nor is there a |
31 // pending connection attempt. | 33 // pending connection attempt. |
(...skipping 20 matching lines...) Expand all Loading... |
52 SecureChannel* secure_channel, | 54 SecureChannel* secure_channel, |
53 const Status& old_status, | 55 const Status& old_status, |
54 const Status& new_status) = 0; | 56 const Status& new_status) = 0; |
55 | 57 |
56 virtual void OnMessageReceived( | 58 virtual void OnMessageReceived( |
57 SecureChannel* secure_channel, | 59 SecureChannel* secure_channel, |
58 const std::string& feature, | 60 const std::string& feature, |
59 const std::string& payload) = 0; | 61 const std::string& payload) = 0; |
60 }; | 62 }; |
61 | 63 |
62 class Delegate { | |
63 public: | |
64 virtual ~Delegate(); | |
65 | |
66 virtual std::unique_ptr<SecureMessageDelegate> | |
67 CreateSecureMessageDelegate() = 0; | |
68 }; | |
69 | |
70 class Factory { | 64 class Factory { |
71 public: | 65 public: |
72 static std::unique_ptr<SecureChannel> NewInstance( | 66 static std::unique_ptr<SecureChannel> NewInstance( |
73 std::unique_ptr<Connection> connection, | 67 std::unique_ptr<Connection> connection, |
74 std::unique_ptr<Delegate> delegate); | 68 CryptAuthService* cryptauth_service); |
75 | 69 |
76 static void SetInstanceForTesting(Factory* factory); | 70 static void SetInstanceForTesting(Factory* factory); |
77 | 71 |
78 protected: | 72 protected: |
79 virtual std::unique_ptr<SecureChannel> BuildInstance( | 73 virtual std::unique_ptr<SecureChannel> BuildInstance( |
80 std::unique_ptr<Connection> connection, | 74 std::unique_ptr<Connection> connection, |
81 std::unique_ptr<Delegate> delegate); | 75 CryptAuthService* cryptauth_service); |
82 | 76 |
83 private: | 77 private: |
84 static Factory* factory_instance_; | 78 static Factory* factory_instance_; |
85 }; | 79 }; |
86 | 80 |
87 ~SecureChannel() override; | 81 ~SecureChannel() override; |
88 | 82 |
89 virtual void Initialize(); | 83 virtual void Initialize(); |
90 | 84 |
91 virtual void SendMessage(const std::string& feature, | 85 virtual void SendMessage(const std::string& feature, |
(...skipping 13 matching lines...) Expand all Loading... |
105 Connection::Status old_status, | 99 Connection::Status old_status, |
106 Connection::Status new_status) override; | 100 Connection::Status new_status) override; |
107 void OnMessageReceived(const Connection& connection, | 101 void OnMessageReceived(const Connection& connection, |
108 const WireMessage& wire_message) override; | 102 const WireMessage& wire_message) override; |
109 void OnSendCompleted(const cryptauth::Connection& connection, | 103 void OnSendCompleted(const cryptauth::Connection& connection, |
110 const cryptauth::WireMessage& wire_message, | 104 const cryptauth::WireMessage& wire_message, |
111 bool success) override; | 105 bool success) override; |
112 | 106 |
113 protected: | 107 protected: |
114 SecureChannel(std::unique_ptr<Connection> connection, | 108 SecureChannel(std::unique_ptr<Connection> connection, |
115 std::unique_ptr<Delegate> delegate); | 109 CryptAuthService* cryptauth_service); |
116 | 110 |
117 Status status_; | 111 Status status_; |
118 | 112 |
119 private: | 113 private: |
120 // Message waiting to be sent. Note that this is *not* the message that will | 114 // Message waiting to be sent. Note that this is *not* the message that will |
121 // end up being sent over the wire; before that can be done, the payload must | 115 // end up being sent over the wire; before that can be done, the payload must |
122 // be encrypted. | 116 // be encrypted. |
123 struct PendingMessage { | 117 struct PendingMessage { |
124 PendingMessage(); | 118 PendingMessage(); |
125 PendingMessage(const std::string& feature, const std::string& payload); | 119 PendingMessage(const std::string& feature, const std::string& payload); |
126 virtual ~PendingMessage(); | 120 virtual ~PendingMessage(); |
127 | 121 |
128 const std::string feature; | 122 const std::string feature; |
129 const std::string payload; | 123 const std::string payload; |
130 }; | 124 }; |
131 | 125 |
132 void TransitionToStatus(const Status& new_status); | 126 void TransitionToStatus(const Status& new_status); |
133 void Authenticate(); | 127 void Authenticate(); |
134 void ProcessMessageQueue(); | 128 void ProcessMessageQueue(); |
135 void OnMessageEncoded( | 129 void OnMessageEncoded( |
136 const std::string& feature, const std::string& encoded_message); | 130 const std::string& feature, const std::string& encoded_message); |
137 void OnMessageDecoded( | 131 void OnMessageDecoded( |
138 const std::string& feature, const std::string& decoded_message); | 132 const std::string& feature, const std::string& decoded_message); |
139 void OnAuthenticationResult( | 133 void OnAuthenticationResult( |
140 Authenticator::Result result, | 134 Authenticator::Result result, |
141 std::unique_ptr<SecureContext> secure_context); | 135 std::unique_ptr<SecureContext> secure_context); |
142 | 136 |
143 std::unique_ptr<Connection> connection_; | 137 std::unique_ptr<Connection> connection_; |
144 std::unique_ptr<Delegate> delegate_; | 138 CryptAuthService* cryptauth_service_; // Outlives this instance. |
145 std::unique_ptr<Authenticator> authenticator_; | 139 std::unique_ptr<Authenticator> authenticator_; |
146 std::unique_ptr<SecureContext> secure_context_; | 140 std::unique_ptr<SecureContext> secure_context_; |
147 std::deque<PendingMessage> queued_messages_; | 141 std::deque<PendingMessage> queued_messages_; |
148 std::unique_ptr<PendingMessage> pending_message_; | 142 std::unique_ptr<PendingMessage> pending_message_; |
149 base::ObserverList<Observer> observer_list_; | 143 base::ObserverList<Observer> observer_list_; |
150 base::WeakPtrFactory<SecureChannel> weak_ptr_factory_; | 144 base::WeakPtrFactory<SecureChannel> weak_ptr_factory_; |
151 | 145 |
152 DISALLOW_COPY_AND_ASSIGN(SecureChannel); | 146 DISALLOW_COPY_AND_ASSIGN(SecureChannel); |
153 }; | 147 }; |
154 | 148 |
155 } // namespace cryptauth | 149 } // namespace cryptauth |
156 | 150 |
157 #endif // COMPONENTS_CRYPTAUTH_SECURE_CHANNEL_H_ | 151 #endif // COMPONENTS_CRYPTAUTH_SECURE_CHANNEL_H_ |
OLD | NEW |