Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: chrome/browser/extensions/api/socket/tls_socket.cc

Issue 76403004: An implementation of chrome.socket.secure(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Another round of responses to Renaud. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 {
Ryan Sleevi 2013/12/17 00:37:40 style nit: line break
lally 2014/01/09 18:47:16 Done.
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) {
Ryan Sleevi 2013/12/17 00:37:40 Generally speaking, it's better to detect and hand
lally 2014/01/09 18:47:16 Done.
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.
Ryan Sleevi 2013/12/17 00:37:40 comment nit: This'll -> This will
lally 2014/01/09 18:47:16 Done.
59 delete ssl_socket;
60 callback.Run(NULL, result);
61 }
62 }
63 } // namespace
Ryan Sleevi 2013/12/17 00:37:40 style nit: Line break
lally 2014/01/09 18:47:16 Done.
64
65 namespace extensions {
66
67 const char kTLSSocketTypeInvalidError[] =
68 "Cannot listen on a socket that's already connected.";
69
70 TLSSocket::TLSSocket(net::StreamSocket* tls_socket,
71 const std::string& owner_extension_id)
Ryan Sleevi 2013/12/17 00:37:40 style nit: unnecessary extra space
lally 2014/01/09 18:47:16 Done.
72 : ResumableTCPSocket(owner_extension_id),
73 tls_socket_(tls_socket) {
74 }
75
76 TLSSocket::~TLSSocket() {
77 Disconnect();
78 }
79
80 void TLSSocket::Connect(const std::string& address,
81 int port,
82 const CompletionCallback& callback) {
83 callback.Run(net::ERR_CONNECTION_FAILED);
84 }
85
86 void TLSSocket::Disconnect() {
87 if (tls_socket_) {
88 tls_socket_->Disconnect();
89 tls_socket_.reset();
90 }
91 }
92
93 void TLSSocket::Read(int count,
94 const ReadCompletionCallback& callback) {
95 DCHECK(!callback.is_null());
96
97 if (!read_callback_.is_null()) {
98 callback.Run(net::ERR_IO_PENDING, NULL);
99 return;
100 }
101
102 if (count <= 0) {
103 callback.Run(net::ERR_INVALID_ARGUMENT, NULL);
104 return;
105 }
106
107 if (!tls_socket_.get() || !IsConnected()) {
108 callback.Run(net::ERR_SOCKET_NOT_CONNECTED, NULL);
109 return;
110 }
111
112 read_callback_ = callback;
113 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(count);
114 int result = tls_socket_->Read(
115 io_buffer.get(), count, base::Bind(&TLSSocket::OnReadComplete,
116 base::Unretained(this),
117 io_buffer));
118
119 if (result != net::ERR_IO_PENDING)
120 OnReadComplete(io_buffer, result);
121 }
122
123 void TLSSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer,
124 int result) {
125 DCHECK(!read_callback_.is_null());
126 ReadCompletionCallback read_callback = read_callback_;
127 read_callback_.Reset();
Ryan Sleevi 2013/12/17 00:37:40 STYLE: simplify this as base::ResetAndReturn(&rea
lally 2014/01/09 18:47:16 Done.
128 // Don't touch 'this' after the read callback.
129 read_callback.Run(result, io_buffer);
130 }
131
132 int TLSSocket::WriteImpl(net::IOBuffer* io_buffer,
133 int io_buffer_size,
134 const net::CompletionCallback& callback) {
135 if (!IsConnected())
136 return net::ERR_SOCKET_NOT_CONNECTED;
137 else
138 return tls_socket_->Write(io_buffer, io_buffer_size, callback);
Ryan Sleevi 2013/12/17 00:37:40 style: indent
lally 2014/01/09 18:47:16 Done.
139 }
140
141 bool TLSSocket::SetKeepAlive(bool enable, int delay) {
142 return false;
143 }
144
145 bool TLSSocket::SetNoDelay(bool no_delay) {
146 return false;
147 }
148
149 int TLSSocket::Listen(const std::string& address, int port, int backlog,
150 std::string* error_msg) {
151 *error_msg = kTLSSocketTypeInvalidError;
152 return net::ERR_NOT_IMPLEMENTED;
153 }
154
155 void TLSSocket::Accept(const AcceptCompletionCallback &callback) {
156 callback.Run(net::ERR_FAILED, NULL);
157 }
158
159 bool TLSSocket::IsConnected() {
160 return tls_socket_.get() && tls_socket_->IsConnected();
161 }
162
163 bool TLSSocket::GetPeerAddress(net::IPEndPoint* address) {
164 return IsConnected() && tls_socket_->GetPeerAddress(address);
165 }
166
167 bool TLSSocket::GetLocalAddress(net::IPEndPoint* address) {
168 return IsConnected() && tls_socket_->GetLocalAddress(address);
169 }
170
171 Socket::SocketType TLSSocket::GetSocketType() const {
172 return Socket::TYPE_TLS;
173 }
174
175 // static
176 void TLSSocket::SecureTCPSocket(
177 Socket* socket,
178 Profile* profile,
179 net::URLRequestContextGetter* url_request_getter,
180 const std::string& extension_id,
181 api::socket::SecureOptions* options,
182 SecureCallback callback) {
183 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
184 DCHECK(profile);
185
186 TCPSocket* tcp_socket = static_cast<TCPSocket*>(socket);
187
188 if (!socket || socket->GetSocketType() != Socket::TYPE_TCP
189 || tcp_socket->ClientStream() == NULL || !socket->IsConnected()) {
Ryan Sleevi 2013/12/17 00:37:40 style: inproper binary operator wrapping - http://
lally 2014/01/09 18:47:16 Done.
190 TlsConnectDone(NULL, extension_id, callback, net::ERR_INVALID_ARGUMENT);
191 return;
192 }
193
194 net::IPEndPoint dest_host_port_pair;
195 socket->GetPeerAddress(&dest_host_port_pair);
Ryan Sleevi 2013/12/17 00:37:40 BUG: GetPeerAddress returns a result. Check it.
lally 2014/01/09 18:47:16 Done.
196 net::HostPortPair host_port(socket->hostname(), dest_host_port_pair.port());
197
198 // Modeled after P2PSocketHostTcpBase::StartTls()
199 scoped_ptr<net::ClientSocketHandle> socket_handle(
200 new net::ClientSocketHandle());
201
202 // Set the socket handle.
203 socket_handle->SetSocket(scoped_ptr<net::StreamSocket>(
204 tcp_socket->ClientStream()));
205
206 // Have the old socket release its hold on the client stream.
207 tcp_socket->Release();
208
209 net::SSLClientSocketContext context;
210 net::URLRequestContext* url_context =
211 url_request_getter->GetURLRequestContext();
212
213 url_request_getter->Release();
Ryan Sleevi 2013/12/17 00:37:40 BUG: ?!? Seems incredibly dangerous here. You shou
lally 2014/01/09 18:47:16 I moved lifetime responsibility to the caller. No
214
215 DCHECK(url_context);
216 context.cert_verifier = url_context->cert_verifier();
217 context.transport_security_state = url_context->transport_security_state();
218 DCHECK(context.transport_security_state);
219
220 // Fill in the SSL socket params.
221 net::SSLConfig ssl_config;
222 profile->GetSSLConfigService()->GetSSLConfig(&ssl_config);
223 if (options && options->tls_version.get()) {
224 uint16 version_min = 0, version_max = 0;
225 api::socket::TLSVersionConstraints* versions = options->tls_version.get();
226 if (versions->min.get()) {
227 version_min = SSLProtocolVersionFromString(*versions->min.get());
228 }
229 if (versions->max.get()) {
230 version_max = SSLProtocolVersionFromString(*versions->max.get());
231 }
232 if (version_min) {
233 ssl_config.version_min = version_min;
234 }
235 if (version_max) {
236 ssl_config.version_max = version_max;
237 }
238 }
239 net::ClientSocketFactory* socket_factory =
240 net::ClientSocketFactory::GetDefaultFactory();
241
242 // Create the socket.
243 net::SSLClientSocket* ssl_socket = socket_factory->CreateSSLClientSocket(
244 socket_handle.Pass(), host_port, ssl_config, context).release();
Ryan Sleevi 2013/12/17 00:37:40 DESIGN: Make your ownership explicit here. For ex
lally 2014/01/09 18:47:16 Done.
245
246 // And try to connect.
247 int status = ssl_socket->Connect(
248 base::Bind(&TlsConnectDone, ssl_socket, extension_id, callback));
249
250 if (status != net::ERR_IO_PENDING) {
251 // Note: this can't recurse -- if 'socket' is already a connected
252 // TLSSocket, it will return TYPE_TLS instead of TYPE_TCP, failing before
253 // we get here. If the connection failed, the socket is closed below
254 // before the callback. If the caller tries to recurse into another
255 // secure() call, they must have either created a prior socket to try, or
256 // will have to go through an asynchronous Connect() call first.
257 TlsConnectDone(ssl_socket, extension_id, callback, status);
258 }
259 }
260
261 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698