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

Side by Side Diff: blimp/net/ssl_client_transport.cc

Issue 1696563002: Blimp: add support for SSL connections. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months 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
OLDNEW
(Empty)
1 // Copyright 2016 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 "blimp/net/ssl_client_transport.h"
6
7 #include "base/callback.h"
8 #include "base/callback_helpers.h"
9 #include "blimp/net/blimp_cert_verifier.h"
10 #include "blimp/net/stream_socket_connection.h"
11 #include "net/base/host_port_pair.h"
12 #include "net/cert/x509_certificate.h"
13 #include "net/socket/client_socket_factory.h"
14 #include "net/socket/client_socket_handle.h"
15 #include "net/socket/ssl_client_socket.h"
16 #include "net/socket/stream_socket.h"
17 #include "net/socket/tcp_client_socket.h"
18 #include "net/ssl/ssl_config.h"
19
20 namespace blimp {
21
22 SSLClientTransport::SSLClientTransport(
23 const net::AddressList& addresses,
24 scoped_refptr<net::X509Certificate> assigned_cert,
25 net::NetLog* net_log)
26 : addresses_(addresses),
27 net_log_(net_log),
28 cert_verifier_(new BlimpCertVerifier(assigned_cert)),
29 socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {}
30
31 SSLClientTransport::~SSLClientTransport() {}
32
33 void SSLClientTransport::Connect(const net::CompletionCallback& callback) {
34 DCHECK(!tcp_socket_);
35 DCHECK(!ssl_socket_);
36 DCHECK(!callback.is_null());
37 DCHECK(connect_callback_.is_null());
38
39 tcp_socket_ = socket_factory_->CreateTransportClientSocket(
40 addresses_, net_log_, net::NetLog::Source());
41 connect_callback_ = callback;
42 net::CompletionCallback completion_callback = base::Bind(
43 &SSLClientTransport::OnTCPConnectComplete, base::Unretained(this));
44
45 int result = tcp_socket_->Connect(completion_callback);
46 if (result == net::ERR_IO_PENDING) {
47 // Connection will complete asynchronously.
48 return;
49 }
50
51 OnTCPConnectComplete(result);
52 }
53
54 scoped_ptr<BlimpConnection> SSLClientTransport::TakeConnection() {
55 DCHECK(connect_callback_.is_null());
56 DCHECK(ssl_socket_);
57 return make_scoped_ptr(new StreamSocketConnection(std::move(ssl_socket_)));
58 }
59
60 const std::string SSLClientTransport::GetName() const {
61 return "SSL";
62 }
63
64 void SSLClientTransport::SetClientSocketFactoryForTest(
65 net::ClientSocketFactory* factory) {
66 DCHECK(factory);
67 socket_factory_ = factory;
68 }
69
70 void SSLClientTransport::OnTCPConnectComplete(int result) {
71 DCHECK_NE(net::ERR_IO_PENDING, result);
72 DCHECK(tcp_socket_);
73 DCHECK(!ssl_socket_);
74 DCHECK(!connect_callback_.is_null());
75
76 DVLOG(1) << "TCP connection result=" << result;
77 if (result != net::OK) {
78 tcp_socket_ = nullptr;
79 base::ResetAndReturn(&connect_callback_).Run(result);
80 return;
81 }
82
83 // Forward connection details to the SSL layer.
84 net::IPEndPoint connected_endpoint;
85 result = tcp_socket_->GetPeerAddress(&connected_endpoint);
86 if (result != net::OK) {
87 tcp_socket_ = nullptr;
88 base::ResetAndReturn(&connect_callback_).Run(result);
89 return;
90 }
91
92 // Construct arguments to use for the SSL socket factory.
93 scoped_ptr<net::ClientSocketHandle> socket_handle(
94 new net::ClientSocketHandle);
95 socket_handle->SetSocket(std::move(tcp_socket_));
96
97 net::HostPortPair host_port_pair =
98 net::HostPortPair::FromIPEndPoint(connected_endpoint);
99
100 net::SSLClientSocketContext create_context;
101 create_context.cert_verifier = cert_verifier_.get();
102 create_context.transport_security_state = &transport_security_state_;
103
104 ssl_socket_ = socket_factory_->CreateSSLClientSocket(
105 std::move(socket_handle), host_port_pair, net::SSLConfig(),
106 create_context);
107
108 if (!ssl_socket_) {
109 base::ResetAndReturn(&connect_callback_).Run(net::ERR_SSL_PROTOCOL_ERROR);
110 return;
111 }
112
113 result = ssl_socket_->Connect(base::Bind(
114 &SSLClientTransport::OnSSLConnectComplete, base::Unretained(this)));
115 if (result == net::ERR_IO_PENDING) {
116 // SSL connection will complete asynchronously.
117 return;
118 }
119
120 OnSSLConnectComplete(result);
121 }
122
123 void SSLClientTransport::OnSSLConnectComplete(int result) {
124 DCHECK(ssl_socket_);
125
126 DVLOG(1) << "SSL connection result=" << result;
127
128 if (result != net::OK) {
129 ssl_socket_ = nullptr;
130 }
131
132 base::ResetAndReturn(&connect_callback_).Run(result);
133 }
134
135 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698