OLD | NEW |
(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/exact_match_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::IPEndPoint& ip_endpoint, |
| 24 const scoped_refptr<net::X509Certificate>& cert, |
| 25 net::NetLog* net_log) |
| 26 : TCPClientTransport(ip_endpoint, net_log), |
| 27 ip_endpoint_(ip_endpoint), |
| 28 cert_verifier_(cert) {} |
| 29 |
| 30 SSLClientTransport::~SSLClientTransport() {} |
| 31 |
| 32 const std::string SSLClientTransport::GetName() const { |
| 33 return "SSL"; |
| 34 } |
| 35 |
| 36 void SSLClientTransport::OnTCPConnectComplete(int result) { |
| 37 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 38 |
| 39 scoped_ptr<net::StreamSocket> tcp_socket = TCPClientTransport::TakeSocket(); |
| 40 |
| 41 DVLOG(1) << "TCP connection result=" << result; |
| 42 if (result != net::OK) { |
| 43 OnConnectComplete(result); |
| 44 return; |
| 45 } |
| 46 |
| 47 // Construct arguments to use for the SSL socket factory. |
| 48 scoped_ptr<net::ClientSocketHandle> socket_handle( |
| 49 new net::ClientSocketHandle); |
| 50 socket_handle->SetSocket(std::move(tcp_socket)); |
| 51 |
| 52 net::HostPortPair host_port_pair = |
| 53 net::HostPortPair::FromIPEndPoint(ip_endpoint_); |
| 54 |
| 55 net::SSLClientSocketContext create_context; |
| 56 create_context.cert_verifier = &cert_verifier_; |
| 57 create_context.transport_security_state = &transport_security_state_; |
| 58 |
| 59 scoped_ptr<net::StreamSocket> ssl_socket( |
| 60 socket_factory()->CreateSSLClientSocket(std::move(socket_handle), |
| 61 host_port_pair, net::SSLConfig(), |
| 62 create_context)); |
| 63 |
| 64 if (!ssl_socket) { |
| 65 OnConnectComplete(net::ERR_SSL_PROTOCOL_ERROR); |
| 66 return; |
| 67 } |
| 68 |
| 69 result = ssl_socket->Connect(base::Bind( |
| 70 &SSLClientTransport::OnSSLConnectComplete, base::Unretained(this))); |
| 71 SetSocket(std::move(ssl_socket)); |
| 72 |
| 73 if (result == net::ERR_IO_PENDING) { |
| 74 // SSL connection will complete asynchronously. |
| 75 return; |
| 76 } |
| 77 |
| 78 OnSSLConnectComplete(result); |
| 79 } |
| 80 |
| 81 void SSLClientTransport::OnSSLConnectComplete(int result) { |
| 82 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 83 DVLOG(1) << "SSL connection result=" << result; |
| 84 |
| 85 OnConnectComplete(result); |
| 86 } |
| 87 |
| 88 } // namespace blimp |
OLD | NEW |