OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // extension that opened the socket. | 68 // extension that opened the socket. |
69 CastSocket(const std::string& owner_extension_id, | 69 CastSocket(const std::string& owner_extension_id, |
70 const GURL& url, | 70 const GURL& url, |
71 CastSocket::Delegate* delegate, | 71 CastSocket::Delegate* delegate, |
72 net::NetLog* net_log); | 72 net::NetLog* net_log); |
73 virtual ~CastSocket(); | 73 virtual ~CastSocket(); |
74 | 74 |
75 // The URL for the channel. | 75 // The URL for the channel. |
76 const GURL& url() const; | 76 const GURL& url() const; |
77 | 77 |
78 // True if the protocol is casts: | 78 // Whether to perform receiver authentication. |
79 bool is_secure() const { return is_secure_; } | 79 bool auth_required() const { return auth_required_; } |
80 | 80 |
81 // Channel id for the ApiResourceManager. | 81 // Channel id for the ApiResourceManager. |
82 long id() const { return channel_id_; } | 82 long id() const { return channel_id_; } |
83 | 83 |
84 // Sets the channel id. | 84 // Sets the channel id. |
85 void set_id(long channel_id) { channel_id_ = channel_id; } | 85 void set_id(long channel_id) { channel_id_ = channel_id; } |
86 | 86 |
87 // Returns the state of the channel. | 87 // Returns the state of the channel. |
88 const ReadyState& ready_state() const { return ready_state_; } | 88 const ReadyState& ready_state() const { return ready_state_; } |
89 | 89 |
(...skipping 19 matching lines...) Expand all Loading... |
109 | 109 |
110 protected: | 110 protected: |
111 // Creates an instance of TCPClientSocket. | 111 // Creates an instance of TCPClientSocket. |
112 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); | 112 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); |
113 // Creates an instance of SSLClientSocket. | 113 // Creates an instance of SSLClientSocket. |
114 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket(); | 114 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket(); |
115 // Extracts peer certificate from SSLClientSocket instance when the socket | 115 // Extracts peer certificate from SSLClientSocket instance when the socket |
116 // is in cert error state. | 116 // is in cert error state. |
117 // Returns whether certificate is successfully extracted. | 117 // Returns whether certificate is successfully extracted. |
118 virtual bool ExtractPeerCert(std::string* cert); | 118 virtual bool ExtractPeerCert(std::string* cert); |
| 119 // Sends a challenge request to the receiver. |
| 120 virtual int SendAuthChallenge(); |
| 121 // Reads auth challenge reply from the receiver. |
| 122 virtual int ReadAuthChallengeReply(); |
| 123 // Verifies whether the challenge reply received from the peer is valid: |
| 124 // 1. Signature in the reply is valid. |
| 125 // 2. Certificate is rooted to a trusted CA. |
| 126 virtual bool VerifyChallengeReply(); |
119 | 127 |
120 private: | 128 private: |
121 friend class ApiResourceManager<CastSocket>; | 129 friend class ApiResourceManager<CastSocket>; |
| 130 friend class CastSocketTest; |
| 131 |
122 static const char* service_name() { | 132 static const char* service_name() { |
123 return "CastSocketManager"; | 133 return "CastSocketManager"; |
124 } | 134 } |
125 | 135 |
126 // Internal connection states. | 136 // Internal connection states. |
127 enum ConnectionState { | 137 enum ConnectionState { |
128 CONN_STATE_NONE, | 138 CONN_STATE_NONE, |
129 CONN_STATE_TCP_CONNECT, | 139 CONN_STATE_TCP_CONNECT, |
130 CONN_STATE_TCP_CONNECT_COMPLETE, | 140 CONN_STATE_TCP_CONNECT_COMPLETE, |
131 CONN_STATE_SSL_CONNECT, | 141 CONN_STATE_SSL_CONNECT, |
132 CONN_STATE_SSL_CONNECT_COMPLETE, | 142 CONN_STATE_SSL_CONNECT_COMPLETE, |
| 143 CONN_STATE_AUTH_CHALLENGE_SEND, |
| 144 CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE, |
| 145 CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE, |
133 }; | 146 }; |
134 | 147 |
135 ///////////////////////////////////////////////////////////////////////////// | 148 ///////////////////////////////////////////////////////////////////////////// |
136 // Following methods work together to implement the following flow: | 149 // Following methods work together to implement the following flow: |
137 // 1. Create a new TCP socket and connect to it | 150 // 1. Create a new TCP socket and connect to it |
138 // 2. Create a new SSL socket and try connecting to it | 151 // 2. Create a new SSL socket and try connecting to it |
139 // 3. If connection fails due to invalid cert authority, then extract the | 152 // 3. If connection fails due to invalid cert authority, then extract the |
140 // peer certificate from the error. | 153 // peer certificate from the error. |
141 // 4. Whitelist the peer certificate and try #1 and #2 again. | 154 // 4. Whitelist the peer certificate and try #1 and #2 again. |
| 155 // 5. If SSL socket is connected successfully, and if protocol is casts:// |
| 156 // then issue an auth challenge request. |
| 157 // 6. Validate the auth challenge response. |
142 | 158 |
143 // Main method that performs connection state transitions. | 159 // Main method that performs connection state transitions. |
144 int DoConnectLoop(int result); | 160 int DoConnectLoop(int result); |
145 // Each of the below Do* method is executed in the corresponding | 161 // Each of the below Do* method is executed in the corresponding |
146 // connection state. For e.g. when connection state is TCP_CONNECT | 162 // connection state. For e.g. when connection state is TCP_CONNECT |
147 // DoTcpConnect is called, and so on. | 163 // DoTcpConnect is called, and so on. |
148 int DoTcpConnect(); | 164 int DoTcpConnect(); |
149 int DoTcpConnectComplete(int result); | 165 int DoTcpConnectComplete(int result); |
150 int DoSslConnect(); | 166 int DoSslConnect(); |
151 int DoSslConnectComplete(int result); | 167 int DoSslConnectComplete(int result); |
152 int DoSslConnectRetry(); | 168 int DoAuthChallengeSend(); |
| 169 int DoAuthChallengeSendComplete(int result); |
| 170 int DoAuthChallengeReplyComplete(int result); |
153 ///////////////////////////////////////////////////////////////////////////// | 171 ///////////////////////////////////////////////////////////////////////////// |
154 | 172 |
155 // Callback method for callbacks from underlying sockets. | 173 // Callback method for callbacks from underlying sockets. |
156 void OnConnectComplete(int result); | 174 void OnConnectComplete(int result); |
157 | 175 |
| 176 // Callback method when a challenge request is sent or a reply is received. |
| 177 void OnChallengeEvent(int result); |
| 178 |
158 // Runs the external connection callback and resets it. | 179 // Runs the external connection callback and resets it. |
159 void DoConnectCallback(int result); | 180 void DoConnectCallback(int result); |
160 | 181 |
161 // Verifies that the URL is a valid cast:// or casts:// URL and sets url_ to | 182 // Verifies that the URL is a valid cast:// or casts:// URL and sets url_ to |
162 // the result. | 183 // the result. |
163 bool ParseChannelUrl(const GURL& url); | 184 bool ParseChannelUrl(const GURL& url); |
164 | 185 |
| 186 // Sends the given |message| and invokes the given callback when done. |
| 187 int SendMessageInternal(const CastMessage& message, |
| 188 const net::CompletionCallback& callback); |
| 189 |
165 // Writes data to the socket from the WriteRequest at the head of the queue. | 190 // Writes data to the socket from the WriteRequest at the head of the queue. |
166 // Calls OnWriteData() on completion. | 191 // Calls OnWriteData() on completion. |
167 void WriteData(); | 192 int WriteData(); |
168 void OnWriteData(int result); | 193 void OnWriteData(int result); |
169 | 194 |
170 // Reads data from the socket into one of the read buffers. Calls | 195 // Reads data from the socket into one of the read buffers. Calls |
171 // OnReadData() on completion. | 196 // OnReadData() on completion. |
172 void ReadData(); | 197 int ReadData(); |
173 void OnReadData(int result); | 198 void OnReadData(int result); |
174 | 199 |
175 // Processes the contents of header_read_buffer_ and returns true on success. | 200 // Processes the contents of header_read_buffer_ and returns true on success. |
176 bool ProcessHeader(); | 201 bool ProcessHeader(); |
177 // Processes the contents of body_read_buffer_ and returns true on success. | 202 // Processes the contents of body_read_buffer_ and returns true on success. |
178 bool ProcessBody(); | 203 bool ProcessBody(); |
179 // Parses the message held in body_read_buffer_ and notifies |delegate_| if a | 204 // Parses the message held in body_read_buffer_ and notifies |delegate_| if a |
180 // message was extracted from the buffer. Returns true on success. | 205 // message was extracted from the buffer. Returns true on success. |
181 bool ParseMessageFromBody(); | 206 bool ParseMessageFromBody(); |
182 | 207 |
183 // Serializes the content of message_proto (with a header) to |message_data|. | 208 // Serializes the content of message_proto (with a header) to |message_data|. |
184 static bool Serialize(const CastMessage& message_proto, | 209 static bool Serialize(const CastMessage& message_proto, |
185 std::string* message_data); | 210 std::string* message_data); |
186 | 211 |
187 // Closes the socket and sets |error_state_|. Also signals |error| via | 212 // Closes the socket and sets |error_state_|. Also signals |error| via |
188 // |delegate_|. | 213 // |delegate_|. |
189 void CloseWithError(ChannelError error); | 214 void CloseWithError(ChannelError error); |
190 | 215 |
191 // The id of the channel. | 216 // The id of the channel. |
192 long channel_id_; | 217 long channel_id_; |
193 | 218 |
194 // The URL of the peer (cast:// or casts://). | 219 // The URL of the peer (cast:// or casts://). |
195 GURL url_; | 220 GURL url_; |
196 // Delegate to inform of incoming messages and errors. | 221 // Delegate to inform of incoming messages and errors. |
197 Delegate* delegate_; | 222 Delegate* delegate_; |
198 // True if the channel is using a secure transport. | 223 // True if we should perform receiver authentication. |
199 bool is_secure_; | 224 bool auth_required_; |
200 // The IP endpoint of the peer. | 225 // The IP endpoint of the peer. |
201 net::IPEndPoint ip_endpoint_; | 226 net::IPEndPoint ip_endpoint_; |
202 // The last error encountered by the channel. | 227 // The last error encountered by the channel. |
203 ChannelError error_state_; | 228 ChannelError error_state_; |
204 // The current status of the channel. | 229 // The current status of the channel. |
205 ReadyState ready_state_; | 230 ReadyState ready_state_; |
206 | 231 |
207 // True when there is a write callback pending. | 232 // True when there is a write callback pending. |
208 bool write_callback_pending_; | 233 bool write_callback_pending_; |
209 // True when there is a read callback pending. | 234 // True when there is a read callback pending. |
(...skipping 17 matching lines...) Expand all Loading... |
227 ConnectionState next_state_; | 252 ConnectionState next_state_; |
228 // Owned ptr to the underlying TCP socket. | 253 // Owned ptr to the underlying TCP socket. |
229 scoped_ptr<net::TCPClientSocket> tcp_socket_; | 254 scoped_ptr<net::TCPClientSocket> tcp_socket_; |
230 // Owned ptr to the underlying SSL socket. | 255 // Owned ptr to the underlying SSL socket. |
231 scoped_ptr<net::SSLClientSocket> socket_; | 256 scoped_ptr<net::SSLClientSocket> socket_; |
232 // Certificate of the peer. This field may be empty if the peer | 257 // Certificate of the peer. This field may be empty if the peer |
233 // certificate is not yet fetched. | 258 // certificate is not yet fetched. |
234 std::string peer_cert_; | 259 std::string peer_cert_; |
235 scoped_ptr<net::CertVerifier> cert_verifier_; | 260 scoped_ptr<net::CertVerifier> cert_verifier_; |
236 scoped_ptr<net::TransportSecurityState> transport_security_state_; | 261 scoped_ptr<net::TransportSecurityState> transport_security_state_; |
| 262 // Reply received from the receiver to a challenge request. |
| 263 scoped_ptr<CastMessage> challenge_reply_; |
237 | 264 |
238 // Callback invoked when the socket is connected. | 265 // Callback invoked when the socket is connected. |
239 net::CompletionCallback connect_callback_; | 266 net::CompletionCallback connect_callback_; |
240 | 267 |
241 // Message header struct. If fields are added, be sure to update | 268 // Message header struct. If fields are added, be sure to update |
242 // kMessageHeaderSize in the .cc. | 269 // kMessageHeaderSize in the .cc. |
243 struct MessageHeader { | 270 struct MessageHeader { |
244 MessageHeader(); | 271 MessageHeader(); |
245 // Sets the message size. | 272 // Sets the message size. |
246 void SetMessageSize(size_t message_size); | 273 void SetMessageSize(size_t message_size); |
(...skipping 27 matching lines...) Expand all Loading... |
274 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); | 301 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); |
275 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); | 302 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); |
276 DISALLOW_COPY_AND_ASSIGN(CastSocket); | 303 DISALLOW_COPY_AND_ASSIGN(CastSocket); |
277 }; | 304 }; |
278 | 305 |
279 } // namespace cast_channel | 306 } // namespace cast_channel |
280 } // namespace api | 307 } // namespace api |
281 } // namespace extensions | 308 } // namespace extensions |
282 | 309 |
283 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ | 310 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |