Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "extensions/browser/api/socket/tls_socket.h" | |
| 6 | |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "extensions/browser/api/api_resource.h" | |
| 10 #include "net/base/address_list.h" | |
| 11 #include "net/base/ip_endpoint.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/base/rand_callback.h" | |
| 14 #include "net/socket/client_socket_factory.h" | |
| 15 #include "net/socket/client_socket_handle.h" | |
| 16 #include "net/socket/ssl_client_socket.h" | |
| 17 #include "net/socket/tcp_client_socket.h" | |
| 18 #include "url/url_canon.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Returns the SSL protocol version (as a uint16) represented by a string. | |
| 23 // Returns 0 if the string is invalid. | |
| 24 uint16 SSLProtocolVersionFromString(const std::string& version_str) { | |
| 25 uint16 version = 0; // Invalid. | |
| 26 if (version_str == "ssl3") { | |
| 27 version = net::SSL_PROTOCOL_VERSION_SSL3; | |
| 28 } else if (version_str == "tls1") { | |
| 29 version = net::SSL_PROTOCOL_VERSION_TLS1; | |
| 30 } else if (version_str == "tls1.1") { | |
| 31 version = net::SSL_PROTOCOL_VERSION_TLS1_1; | |
| 32 } else if (version_str == "tls1.2") { | |
| 33 version = net::SSL_PROTOCOL_VERSION_TLS1_2; | |
| 34 } | |
| 35 return version; | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 namespace extensions { | |
| 41 | |
| 42 const char kTLSSocketTypeInvalidError[] = | |
| 43 "Cannot listen on a socket that is already connected."; | |
| 44 | |
| 45 TLSSocket::TLSSocket(scoped_ptr<net::StreamSocket> tls_socket, | |
| 46 const std::string& owner_extension_id) | |
| 47 : ResumableTCPSocket(owner_extension_id), tls_socket_(tls_socket.Pass()) { | |
| 48 } | |
| 49 | |
| 50 TLSSocket::~TLSSocket() { | |
| 51 Disconnect(); | |
| 52 } | |
| 53 | |
| 54 void TLSSocket::Connect(const std::string& address, | |
| 55 int port, | |
| 56 const CompletionCallback& callback) { | |
| 57 callback.Run(net::ERR_CONNECTION_FAILED); | |
| 58 } | |
| 59 | |
| 60 void TLSSocket::Disconnect() { | |
| 61 if (tls_socket_) { | |
| 62 tls_socket_->Disconnect(); | |
| 63 tls_socket_.reset(); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void TLSSocket::Read(int count, const ReadCompletionCallback& callback) { | |
| 68 DCHECK(!callback.is_null()); | |
| 69 | |
| 70 if (!read_callback_.is_null()) { | |
| 71 callback.Run(net::ERR_IO_PENDING, NULL); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 if (count <= 0) { | |
| 76 callback.Run(net::ERR_INVALID_ARGUMENT, NULL); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 if (!tls_socket_.get() || !IsConnected()) { | |
| 81 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, NULL); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 read_callback_ = callback; | |
| 86 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(count)); | |
| 87 int result = tls_socket_->Read( | |
| 88 io_buffer.get(), | |
| 89 count, | |
| 90 base::Bind( | |
| 91 &TLSSocket::OnReadComplete, base::Unretained(this), io_buffer)); | |
| 92 | |
| 93 if (result != net::ERR_IO_PENDING) { | |
| 94 OnReadComplete(io_buffer, result); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void TLSSocket::OnReadComplete(const scoped_refptr<net::IOBuffer>& io_buffer, | |
| 99 int result) { | |
| 100 DCHECK(!read_callback_.is_null()); | |
| 101 base::ResetAndReturn(&read_callback_).Run(result, io_buffer); | |
| 102 } | |
| 103 | |
| 104 int TLSSocket::WriteImpl(net::IOBuffer* io_buffer, | |
| 105 int io_buffer_size, | |
| 106 const net::CompletionCallback& callback) { | |
| 107 if (!IsConnected()) { | |
| 108 return net::ERR_SOCKET_NOT_CONNECTED; | |
| 109 } | |
| 110 return tls_socket_->Write(io_buffer, io_buffer_size, callback); | |
| 111 } | |
| 112 | |
| 113 bool TLSSocket::SetKeepAlive(bool enable, int delay) { | |
| 114 return false; | |
| 115 } | |
| 116 | |
| 117 bool TLSSocket::SetNoDelay(bool no_delay) { | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 int TLSSocket::Listen(const std::string& address, | |
| 122 int port, | |
| 123 int backlog, | |
| 124 std::string* error_msg) { | |
| 125 *error_msg = kTLSSocketTypeInvalidError; | |
| 126 return net::ERR_NOT_IMPLEMENTED; | |
| 127 } | |
| 128 | |
| 129 void TLSSocket::Accept(const AcceptCompletionCallback& callback) { | |
| 130 callback.Run(net::ERR_FAILED, NULL); | |
| 131 } | |
| 132 | |
| 133 bool TLSSocket::IsConnected() { | |
| 134 return tls_socket_.get() && tls_socket_->IsConnected(); | |
| 135 } | |
| 136 | |
| 137 bool TLSSocket::GetPeerAddress(net::IPEndPoint* address) { | |
| 138 return IsConnected() && tls_socket_->GetPeerAddress(address); | |
| 139 } | |
| 140 | |
| 141 bool TLSSocket::GetLocalAddress(net::IPEndPoint* address) { | |
| 142 return IsConnected() && tls_socket_->GetLocalAddress(address); | |
| 143 } | |
| 144 | |
| 145 Socket::SocketType TLSSocket::GetSocketType() const { | |
| 146 return Socket::TYPE_TLS; | |
| 147 } | |
| 148 | |
| 149 // static | |
| 150 void TLSSocket::UpgradeSocketToTLS( | |
| 151 Socket* socket, | |
| 152 scoped_refptr<net::SSLConfigService> ssl_config_service, | |
| 153 net::CertVerifier* cert_verifier, | |
| 154 net::TransportSecurityState* transport_security_state, | |
| 155 const std::string& extension_id, | |
| 156 core_api::socket::SecureOptions* options, | |
| 157 const TLSSocket::SecureCallback& callback) { | |
| 158 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 159 TCPSocket* tcp_socket = static_cast<TCPSocket*>(socket); | |
| 160 scoped_ptr<net::SSLClientSocket> null_sock; | |
| 161 | |
| 162 if (!tcp_socket || tcp_socket->GetSocketType() != Socket::TYPE_TCP || | |
| 163 !tcp_socket->ClientStream() || !tcp_socket->IsConnected() || | |
| 164 tcp_socket->HasPendingRead()) { | |
| 165 DVLOG(1) << "Failing before trying. socket is " << tcp_socket; | |
| 166 if (tcp_socket) { | |
| 167 DVLOG(1) << "type: " << tcp_socket->GetSocketType() | |
| 168 << ", ClientStream is " << tcp_socket->ClientStream() | |
| 169 << ", IsConnected: " << tcp_socket->IsConnected() | |
| 170 << ", HasPendingRead: " << tcp_socket->HasPendingRead(); | |
| 171 } | |
| 172 TlsConnectDone( | |
| 173 null_sock.Pass(), extension_id, callback, net::ERR_INVALID_ARGUMENT); | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 net::IPEndPoint dest_host_port_pair; | |
| 178 if (!tcp_socket->GetPeerAddress(&dest_host_port_pair)) { | |
| 179 DVLOG(1) << "Could not get peer address."; | |
| 180 TlsConnectDone( | |
| 181 null_sock.Pass(), extension_id, callback, net::ERR_INVALID_ARGUMENT); | |
| 182 return; | |
| 183 } | |
| 184 | |
| 185 // Convert any U-LABELs to A-LABELs. | |
| 186 url::CanonHostInfo host_info; | |
| 187 std::string canon_host = | |
| 188 net::CanonicalizeHost(tcp_socket->hostname(), &host_info); | |
| 189 | |
| 190 // Canonicalization shouldn't fail: the socket is already connected with a | |
| 191 // host, using this hostname. | |
| 192 if (host_info.family == url::CanonHostInfo::BROKEN) { | |
| 193 DVLOG(1) << "Could not canonicalize hostname"; | |
| 194 TlsConnectDone( | |
| 195 null_sock.Pass(), extension_id, callback, net::ERR_INVALID_ARGUMENT); | |
| 196 return; | |
| 197 } | |
| 198 | |
| 199 net::HostPortPair host_and_port(canon_host, dest_host_port_pair.port()); | |
| 200 | |
| 201 scoped_ptr<net::ClientSocketHandle> socket_handle( | |
| 202 new net::ClientSocketHandle()); | |
| 203 | |
| 204 // Set the socket handle to the socket's client stream (that should be the | |
| 205 // only one active here). Then have the old socket release ownership on | |
| 206 // that client stream. | |
| 207 socket_handle->SetSocket( | |
| 208 scoped_ptr<net::StreamSocket>(tcp_socket->ClientStream())); | |
| 209 tcp_socket->Release(); | |
| 210 | |
| 211 DCHECK(transport_security_state); | |
| 212 net::SSLClientSocketContext context; | |
| 213 context.cert_verifier = cert_verifier; | |
| 214 context.transport_security_state = transport_security_state; | |
| 215 | |
| 216 // Fill in the SSL socket params. | |
| 217 net::SSLConfig ssl_config; | |
| 218 ssl_config_service->GetSSLConfig(&ssl_config); | |
| 219 if (options && options->tls_version.get()) { | |
| 220 uint16 version_min = 0, version_max = 0; | |
| 221 core_api::socket::TLSVersionConstraints* versions = | |
| 222 options->tls_version.get(); | |
| 223 if (versions->min.get()) { | |
| 224 version_min = SSLProtocolVersionFromString(*versions->min.get()); | |
| 225 } | |
| 226 if (versions->max.get()) { | |
| 227 version_max = SSLProtocolVersionFromString(*versions->max.get()); | |
| 228 } | |
| 229 if (version_min) { | |
| 230 ssl_config.version_min = version_min; | |
| 231 } | |
| 232 if (version_max) { | |
| 233 ssl_config.version_max = version_max; | |
| 234 } | |
| 235 } | |
| 236 net::ClientSocketFactory* socket_factory = | |
| 237 net::ClientSocketFactory::GetDefaultFactory(); | |
| 238 | |
| 239 // Create the socket. | |
| 240 scoped_ptr<net::SSLClientSocket> ssl_socket( | |
| 241 socket_factory->CreateSSLClientSocket( | |
| 242 socket_handle.Pass(), host_and_port, ssl_config, context)); | |
| 243 | |
| 244 DVLOG(1) << "Attempting to secure a connection to " << tcp_socket->hostname() | |
| 245 << ":" << dest_host_port_pair.port(); | |
| 246 | |
| 247 // We need the contents of |ssl_socket| in order to invoke its Connect() | |
| 248 // method. It belongs to |ssl_socket|, and we own that until our internal | |
| 249 // callback (|connect_cb|, below) is invoked. | |
| 250 net::SSLClientSocket* saved_ssl_socket = ssl_socket.get(); | |
| 251 | |
| 252 // Try establish a TLS connection. Pass ownership of |ssl_socket| to | |
| 253 // TlsConnectDone, which will pass it on to |callback|. |connect_cb| below | |
| 254 // is only for UpgradeSocketToTLS use, and not be confused with the | |
| 255 // argument |callback|, which gets invoked by TlsConnectDone() after | |
| 256 // Connect() below returns. | |
| 257 base::Callback<void(int)> connect_cb(base::Bind(&TLSSocket::TlsConnectDone, | |
| 258 base::Passed(&ssl_socket), | |
| 259 extension_id, | |
| 260 callback)); | |
| 261 int status = saved_ssl_socket->Connect(connect_cb); | |
| 262 saved_ssl_socket = NULL; | |
| 263 | |
| 264 // Connect failed, and won't call the callback. Call it now. | |
| 265 if (status != net::ERR_IO_PENDING && status != net::OK) { | |
|
Ryan Sleevi
2014/07/15 00:18:45
If Connect() synchronously returns net::OK, it's n
lally
2014/07/16 16:19:44
Unexpected behavior for Connect(). I've fixed as
| |
| 266 // Note: this can't recurse -- if |socket| is already a connected | |
| 267 // TLSSocket, it will return TYPE_TLS instead of TYPE_TCP, causing | |
| 268 // UpgradeSocketToTLS() to fail with an error above. If | |
| 269 // UpgradeSocketToTLS() is called on |socket| twice, the call to | |
| 270 // Release() on |socket| above causes the additional call to | |
| 271 // fail with an error above. | |
| 272 DVLOG(1) << "Status is not IO-pending: " << net::ErrorToString(status); | |
| 273 connect_cb.Run(status); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 // static | |
| 278 void TLSSocket::TlsConnectDone(scoped_ptr<net::SSLClientSocket> ssl_socket, | |
|
Ryan Sleevi
2014/07/15 00:18:45
This doesn't need to be a static class member, doe
lally
2014/07/16 16:19:44
Good point, moved up and declaration removed from
| |
| 279 const std::string& extension_id, | |
| 280 const TLSSocket::SecureCallback& callback, | |
| 281 int result) { | |
| 282 DVLOG(1) << "Got back result " << result << " " << net::ErrorToString(result); | |
| 283 | |
| 284 // No matter how the TLS connection attempt went, the underlying socket's | |
| 285 // no longer bound to the original TCPSocket. It belongs to |ssl_socket|, | |
| 286 // which is promoted here to a new API-accessible socket (via a TLSSocket | |
| 287 // wrapper), or deleted. | |
| 288 if (result != net::OK) { | |
| 289 callback.Run(scoped_ptr<TLSSocket>(), result); | |
| 290 return; | |
| 291 }; | |
| 292 | |
| 293 // Wrap the StreamSocket in a TLSSocket, which matches the extension socket | |
| 294 // API. Set the handle of the socket to the new value, so that it can be | |
| 295 // used for read/write/close/etc. | |
| 296 scoped_ptr<TLSSocket> wrapper( | |
| 297 new TLSSocket(ssl_socket.PassAs<net::StreamSocket>(), extension_id)); | |
| 298 | |
| 299 // Caller will end up deleting the prior TCPSocket, once it calls | |
| 300 // SetSocket(..,wrapper). | |
| 301 callback.Run(wrapper.Pass(), result); | |
| 302 } | |
| 303 | |
| 304 } // namespace extensions | |
| OLD | NEW |