| Index: blimp/net/ssl_client_transport.cc
|
| diff --git a/blimp/net/ssl_client_transport.cc b/blimp/net/ssl_client_transport.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6ba867780f5fb9e249618acea3c5c134a2d54192
|
| --- /dev/null
|
| +++ b/blimp/net/ssl_client_transport.cc
|
| @@ -0,0 +1,135 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "blimp/net/ssl_client_transport.h"
|
| +
|
| +#include "base/callback.h"
|
| +#include "base/callback_helpers.h"
|
| +#include "blimp/net/blimp_cert_verifier.h"
|
| +#include "blimp/net/stream_socket_connection.h"
|
| +#include "net/base/host_port_pair.h"
|
| +#include "net/cert/x509_certificate.h"
|
| +#include "net/socket/client_socket_factory.h"
|
| +#include "net/socket/client_socket_handle.h"
|
| +#include "net/socket/ssl_client_socket.h"
|
| +#include "net/socket/stream_socket.h"
|
| +#include "net/socket/tcp_client_socket.h"
|
| +#include "net/ssl/ssl_config.h"
|
| +
|
| +namespace blimp {
|
| +
|
| +SSLClientTransport::SSLClientTransport(
|
| + const net::AddressList& addresses,
|
| + scoped_refptr<net::X509Certificate> assigned_cert,
|
| + net::NetLog* net_log)
|
| + : addresses_(addresses),
|
| + net_log_(net_log),
|
| + cert_verifier_(new BlimpCertVerifier(assigned_cert)),
|
| + socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {}
|
| +
|
| +SSLClientTransport::~SSLClientTransport() {}
|
| +
|
| +void SSLClientTransport::Connect(const net::CompletionCallback& callback) {
|
| + DCHECK(!tcp_socket_);
|
| + DCHECK(!ssl_socket_);
|
| + DCHECK(!callback.is_null());
|
| + DCHECK(connect_callback_.is_null());
|
| +
|
| + tcp_socket_ = socket_factory_->CreateTransportClientSocket(
|
| + addresses_, net_log_, net::NetLog::Source());
|
| + connect_callback_ = callback;
|
| + net::CompletionCallback completion_callback = base::Bind(
|
| + &SSLClientTransport::OnTCPConnectComplete, base::Unretained(this));
|
| +
|
| + int result = tcp_socket_->Connect(completion_callback);
|
| + if (result == net::ERR_IO_PENDING) {
|
| + // Connection will complete asynchronously.
|
| + return;
|
| + }
|
| +
|
| + OnTCPConnectComplete(result);
|
| +}
|
| +
|
| +scoped_ptr<BlimpConnection> SSLClientTransport::TakeConnection() {
|
| + DCHECK(connect_callback_.is_null());
|
| + DCHECK(ssl_socket_);
|
| + return make_scoped_ptr(new StreamSocketConnection(std::move(ssl_socket_)));
|
| +}
|
| +
|
| +const std::string SSLClientTransport::GetName() const {
|
| + return "SSL";
|
| +}
|
| +
|
| +void SSLClientTransport::SetClientSocketFactoryForTest(
|
| + net::ClientSocketFactory* factory) {
|
| + DCHECK(factory);
|
| + socket_factory_ = factory;
|
| +}
|
| +
|
| +void SSLClientTransport::OnTCPConnectComplete(int result) {
|
| + DCHECK_NE(net::ERR_IO_PENDING, result);
|
| + DCHECK(tcp_socket_);
|
| + DCHECK(!ssl_socket_);
|
| + DCHECK(!connect_callback_.is_null());
|
| +
|
| + DVLOG(1) << "TCP connection result=" << result;
|
| + if (result != net::OK) {
|
| + tcp_socket_ = nullptr;
|
| + base::ResetAndReturn(&connect_callback_).Run(result);
|
| + return;
|
| + }
|
| +
|
| + // Forward connection details to the SSL layer.
|
| + net::IPEndPoint connected_endpoint;
|
| + result = tcp_socket_->GetPeerAddress(&connected_endpoint);
|
| + if (result != net::OK) {
|
| + tcp_socket_ = nullptr;
|
| + base::ResetAndReturn(&connect_callback_).Run(result);
|
| + return;
|
| + }
|
| +
|
| + // Construct arguments to use for the SSL socket factory.
|
| + scoped_ptr<net::ClientSocketHandle> socket_handle(
|
| + new net::ClientSocketHandle);
|
| + socket_handle->SetSocket(std::move(tcp_socket_));
|
| +
|
| + net::HostPortPair host_port_pair =
|
| + net::HostPortPair::FromIPEndPoint(connected_endpoint);
|
| +
|
| + net::SSLClientSocketContext create_context;
|
| + create_context.cert_verifier = cert_verifier_.get();
|
| + create_context.transport_security_state = &transport_security_state_;
|
| +
|
| + ssl_socket_ = socket_factory_->CreateSSLClientSocket(
|
| + std::move(socket_handle), host_port_pair, net::SSLConfig(),
|
| + create_context);
|
| +
|
| + if (!ssl_socket_) {
|
| + base::ResetAndReturn(&connect_callback_).Run(net::ERR_SSL_PROTOCOL_ERROR);
|
| + return;
|
| + }
|
| +
|
| + result = ssl_socket_->Connect(base::Bind(
|
| + &SSLClientTransport::OnSSLConnectComplete, base::Unretained(this)));
|
| + if (result == net::ERR_IO_PENDING) {
|
| + // SSL connection will complete asynchronously.
|
| + return;
|
| + }
|
| +
|
| + OnSSLConnectComplete(result);
|
| +}
|
| +
|
| +void SSLClientTransport::OnSSLConnectComplete(int result) {
|
| + DCHECK(ssl_socket_);
|
| +
|
| + DVLOG(1) << "SSL connection result=" << result;
|
| +
|
| + if (result != net::OK) {
|
| + ssl_socket_ = nullptr;
|
| + }
|
| +
|
| + base::ResetAndReturn(&connect_callback_).Run(result);
|
| +}
|
| +
|
| +} // namespace blimp
|
|
|