OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/socket/tls_socket.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "chrome/browser/extensions/api/api_resource.h" | |
9 #include "net/base/address_list.h" | |
10 #include "net/base/ip_endpoint.h" | |
11 #include "net/base/net_errors.h" | |
12 #include "net/base/rand_callback.h" | |
13 #include "net/socket/client_socket_factory.h" | |
14 #include "net/socket/client_socket_handle.h" | |
15 #include "net/socket/tcp_client_socket.h" | |
16 #include "net/url_request/url_request_context.h" | |
17 #include "net/url_request/url_request_context_getter.h" | |
18 | |
19 namespace { | |
20 // Returns the SSL protocol version (as a uint16) represented by a string. | |
21 // Returns 0 if the string is invalid. Pulled from | |
22 // chrome/browser/net/ssl_config_service_manager_pref.cc. | |
23 uint16 SSLProtocolVersionFromString(const std::string& version_str) { | |
24 uint16 version = 0; // Invalid. | |
25 if (version_str == "ssl3") { | |
26 version = net::SSL_PROTOCOL_VERSION_SSL3; | |
27 } else if (version_str == "tls1") { | |
28 version = net::SSL_PROTOCOL_VERSION_TLS1; | |
29 } else if (version_str == "tls1.1") { | |
30 version = net::SSL_PROTOCOL_VERSION_TLS1_1; | |
31 } else if (version_str == "tls1.2") { | |
32 version = net::SSL_PROTOCOL_VERSION_TLS1_2; | |
33 } | |
34 return version; | |
35 } | |
36 | |
37 void TlsConnectDone(net::SSLClientSocket* ssl_socket, | |
38 const std::string& extension_id, | |
39 extensions::SecureCallback callback, int result) { | |
40 DVLOG(1) << "chrome.socket.secure: TlsConnectDone(): got back result " | |
41 << result; | |
42 | |
43 // No matter how the TLS connection attempt went, the underlying socket's | |
44 // no longer bound to the original TCPSocket. It belongs to ssl_socket_, | |
45 // which is promoted here to a new API-accessible socket (via a TLSSocket | |
46 // wrapper) or deleted. | |
47 if (result == net::OK) { | |
48 // Wrap the StreamSocket in a TLSSocket (which matches the extension | |
49 // socket API). Set the handle of the socket to the new value, so that | |
50 // it can be used for read/write/close/etc. | |
51 extensions::TLSSocket* wrapper = | |
52 new extensions::TLSSocket(ssl_socket, extension_id); | |
53 | |
54 // Caller will end up deleting the prior TCPSocket, once it calls | |
55 // SetSocket(..,wrapper). | |
56 callback.Run(wrapper, result); | |
57 } else { | |
58 // Failed TLS connection. This'll delete the underlying TCPClientSocket. | |
59 delete ssl_socket; | |
60 callback.Run(NULL, result); | |
61 } | |
62 } | |
63 } // namespace | |
64 | |
65 namespace extensions { | |
66 | |
67 const char kSocketNotFoundError[] = "Socket not found"; | |
rpaquay
2013/12/16 20:18:46
I think this is not used anymore in this file.
lally
2013/12/16 22:20:07
Good catch, thanks.
| |
68 const char kTLSSocketTypeInvalidError[] = | |
69 "Cannot listen on a socket that's already connected."; | |
70 | |
71 TLSSocket::TLSSocket(net::StreamSocket* tls_socket, | |
72 const std::string& owner_extension_id) | |
73 : ResumableTCPSocket(owner_extension_id), | |
74 tls_socket_(tls_socket) { | |
75 } | |
76 | |
77 TLSSocket::~TLSSocket() { | |
78 Disconnect(); | |
79 } | |
80 | |
81 void TLSSocket::Connect(const std::string& address, | |
82 int port, | |
83 const CompletionCallback& callback) { | |
84 callback.Run(net::ERR_CONNECTION_FAILED); | |
85 } | |
86 | |
87 void TLSSocket::Disconnect() { | |
88 if (tls_socket_) { | |
89 tls_socket_->Disconnect(); | |
90 tls_socket_.reset(); | |
91 } | |
92 } | |
93 | |
94 void TLSSocket::Read(int count, | |
95 const ReadCompletionCallback& callback) { | |
96 DCHECK(!callback.is_null()); | |
97 | |
98 if (!read_callback_.is_null()) { | |
99 callback.Run(net::ERR_IO_PENDING, NULL); | |
100 return; | |
101 } | |
102 | |
103 if (count <= 0) { | |
104 callback.Run(net::ERR_INVALID_ARGUMENT, NULL); | |
105 return; | |
106 } | |
107 | |
108 if (!tls_socket_.get() || !IsConnected()) { | |
109 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, NULL); | |
110 return; | |
111 } | |
112 | |
113 read_callback_ = callback; | |
114 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(count); | |
115 int result = tls_socket_->Read( | |
116 io_buffer.get(), count, base::Bind(&TLSSocket::OnReadComplete, | |
117 base::Unretained(this), | |
118 io_buffer)); | |
119 | |
120 if (result != net::ERR_IO_PENDING) | |
121 OnReadComplete(io_buffer, result); | |
122 } | |
123 | |
124 void TLSSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, | |
125 int result) { | |
126 DCHECK(!read_callback_.is_null()); | |
127 ReadCompletionCallback read_callback = read_callback_; | |
128 read_callback_.Reset(); | |
129 // Don't touch 'this' after the read callback. | |
130 read_callback.Run(result, io_buffer); | |
131 } | |
132 | |
133 int TLSSocket::WriteImpl(net::IOBuffer* io_buffer, | |
134 int io_buffer_size, | |
135 const net::CompletionCallback& callback) { | |
136 if (!IsConnected()) | |
137 return net::ERR_SOCKET_NOT_CONNECTED; | |
138 else | |
139 return tls_socket_->Write(io_buffer, io_buffer_size, callback); | |
140 } | |
141 | |
142 bool TLSSocket::SetKeepAlive(bool enable, int delay) { | |
143 return false; | |
144 } | |
145 | |
146 bool TLSSocket::SetNoDelay(bool no_delay) { | |
147 return false; | |
148 } | |
149 | |
150 int TLSSocket::Listen(const std::string& address, int port, int backlog, | |
151 std::string* error_msg) { | |
152 *error_msg = kTLSSocketTypeInvalidError; | |
153 return net::ERR_NOT_IMPLEMENTED; | |
154 } | |
155 | |
156 void TLSSocket::Accept(const AcceptCompletionCallback &callback) { | |
157 callback.Run(net::ERR_FAILED, NULL); | |
158 } | |
159 | |
160 bool TLSSocket::IsConnected() { | |
161 return tls_socket_.get() && tls_socket_->IsConnected(); | |
162 } | |
163 | |
164 bool TLSSocket::GetPeerAddress(net::IPEndPoint* address) { | |
165 return IsConnected() && tls_socket_->GetPeerAddress(address); | |
166 } | |
167 | |
168 bool TLSSocket::GetLocalAddress(net::IPEndPoint* address) { | |
169 return IsConnected() && tls_socket_->GetLocalAddress(address); | |
170 } | |
171 | |
172 Socket::SocketType TLSSocket::GetSocketType() const { | |
173 return Socket::TYPE_TLS; | |
174 } | |
175 | |
176 // static | |
177 void TLSSocket::SecureTCPSocket( | |
178 Socket* socket, | |
179 Profile* profile, | |
180 net::URLRequestContextGetter* url_request_getter, | |
181 const std::string& extension_id, | |
182 api::socket::SecureOptions* options, | |
183 SecureCallback callback) { | |
184 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
185 DCHECK(profile); | |
186 | |
187 TCPSocket* tcp_socket = static_cast<TCPSocket*>(socket); | |
188 | |
189 if (!socket || socket->GetSocketType() != Socket::TYPE_TCP | |
190 || tcp_socket->ClientStream() == NULL || !socket->IsConnected()) { | |
191 TlsConnectDone(NULL, extension_id, callback, net::ERR_INVALID_ARGUMENT); | |
192 return; | |
193 } | |
194 | |
195 net::IPEndPoint dest_host_port_pair; | |
196 socket->GetPeerAddress(&dest_host_port_pair); | |
197 net::HostPortPair host_port(socket->hostname(), dest_host_port_pair.port()); | |
198 | |
199 // Modeled after P2PSocketHostTcpBase::StartTls() | |
200 scoped_ptr<net::ClientSocketHandle> socket_handle( | |
201 new net::ClientSocketHandle()); | |
202 | |
203 // Set the socket handle. | |
204 socket_handle->SetSocket(scoped_ptr<net::StreamSocket>( | |
205 tcp_socket->ClientStream())); | |
206 | |
207 // Have the old socket release its hold on the client stream. | |
208 tcp_socket->Release(); | |
209 | |
210 net::SSLClientSocketContext context; | |
211 net::URLRequestContext* url_context = | |
212 url_request_getter->GetURLRequestContext(); | |
213 | |
214 url_request_getter->Release(); | |
215 | |
216 DCHECK(url_context); | |
217 context.cert_verifier = url_context->cert_verifier(); | |
218 context.transport_security_state = url_context->transport_security_state(); | |
219 DCHECK(context.transport_security_state); | |
220 | |
221 // Fill in the SSL socket params. | |
222 net::SSLConfig ssl_config; | |
223 profile->GetSSLConfigService()->GetSSLConfig(&ssl_config); | |
224 if (options && options->tls_version.get()) { | |
225 uint16 version_min = 0, version_max = 0; | |
226 api::socket::TLSVersionConstraints* versions = options->tls_version.get(); | |
227 if (versions->min.get()) { | |
228 version_min = SSLProtocolVersionFromString(*versions->min.get()); | |
229 } | |
230 if (versions->max.get()) { | |
231 version_max = SSLProtocolVersionFromString(*versions->max.get()); | |
232 } | |
233 if (version_min) { | |
234 ssl_config.version_min = version_min; | |
235 } | |
236 if (version_max) { | |
237 ssl_config.version_max = version_max; | |
238 } | |
239 } | |
240 net::ClientSocketFactory* socket_factory = | |
241 net::ClientSocketFactory::GetDefaultFactory(); | |
242 | |
243 // Create the socket. | |
244 net::SSLClientSocket* ssl_socket = socket_factory->CreateSSLClientSocket( | |
245 socket_handle.Pass(), host_port, ssl_config, context).release(); | |
246 | |
247 // And try to connect. | |
248 int status = ssl_socket->Connect( | |
249 base::Bind(&TlsConnectDone, ssl_socket, extension_id, callback)); | |
250 | |
251 if (status != net::ERR_IO_PENDING) { | |
252 // Note: this can't recurse -- if 'socket' is already a connected | |
253 // TLSSocket, it will return TYPE_TLS instead of TYPE_TCP, failing before | |
254 // we get here. If the connection failed, the socket is closed below | |
255 // before the callback. If the caller tries to recurse into another | |
256 // secure() call, they must have either created a prior socket to try, or | |
257 // will have to go through an asynchronous Connect() call first. | |
258 TlsConnectDone(ssl_socket, extension_id, callback, status); | |
259 } | |
260 } | |
261 | |
262 } // namespace extensions | |
OLD | NEW |