[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 5 | #include "net/socket/client_socket_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame^] | 7 | #include <utility> |
| 8 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 9 | #include "base/lazy_instance.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 11 | #include "net/cert/cert_database.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 12 | #include "net/socket/client_socket_handle.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 13 | #include "net/socket/tcp_client_socket.h" |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 14 | #include "net/udp/udp_client_socket.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 15 | |
davidben | f72d28c | 2015-09-30 15:27:12 | [diff] [blame] | 16 | #if defined(USE_OPENSSL) |
| 17 | #include "net/socket/ssl_client_socket_openssl.h" |
| 18 | #else |
| 19 | #include "net/socket/ssl_client_socket_nss.h" |
| 20 | #endif |
| 21 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | namespace net { |
| 23 | |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 24 | class X509Certificate; |
| 25 | |
[email protected] | abe48d3 | 2010-02-03 02:09:36 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 28 | class DefaultClientSocketFactory : public ClientSocketFactory, |
| 29 | public CertDatabase::Observer { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | public: |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 31 | DefaultClientSocketFactory() { |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 32 | CertDatabase::GetInstance()->AddObserver(this); |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 33 | } |
| 34 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 35 | ~DefaultClientSocketFactory() override { |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 36 | // Note: This code never runs, as the factory is defined as a Leaky |
| 37 | // singleton. |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 38 | CertDatabase::GetInstance()->RemoveObserver(this); |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 39 | } |
| 40 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 41 | void OnCertAdded(const X509Certificate* cert) override { |
[email protected] | c940d37 | 2011-04-13 17:20:18 | [diff] [blame] | 42 | ClearSSLSessionCache(); |
| 43 | } |
| 44 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 45 | void OnCACertChanged(const X509Certificate* cert) override { |
[email protected] | c940d37 | 2011-04-13 17:20:18 | [diff] [blame] | 46 | // Per wtc, we actually only need to flush when trust is reduced. |
[email protected] | c157b2e2 | 2013-10-31 01:38:33 | [diff] [blame] | 47 | // Always flush now because OnCACertChanged does not tell us this. |
| 48 | // See comments in ClientSocketPoolManager::OnCACertChanged. |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 49 | ClearSSLSessionCache(); |
| 50 | } |
| 51 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 52 | scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 53 | DatagramSocket::BindType bind_type, |
| 54 | const RandIntCallback& rand_int_cb, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 55 | NetLog* net_log, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 56 | const NetLog::Source& source) override { |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 57 | return scoped_ptr<DatagramClientSocket>( |
| 58 | new UDPClientSocket(bind_type, rand_int_cb, net_log, source)); |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 59 | } |
| 60 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 61 | scoped_ptr<StreamSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 62 | const AddressList& addresses, |
| 63 | NetLog* net_log, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 64 | const NetLog::Source& source) override { |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 65 | return scoped_ptr<StreamSocket>( |
| 66 | new TCPClientSocket(addresses, net_log, source)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | } |
| 68 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 69 | scoped_ptr<SSLClientSocket> CreateSSLClientSocket( |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 70 | scoped_ptr<ClientSocketHandle> transport_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 71 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 72 | const SSLConfig& ssl_config, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 73 | const SSLClientSocketContext& context) override { |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 74 | #if defined(USE_OPENSSL) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame^] | 75 | return scoped_ptr<SSLClientSocket>(new SSLClientSocketOpenSSL( |
| 76 | std::move(transport_socket), host_and_port, ssl_config, context)); |
[email protected] | 2380f37 | 2011-02-23 21:35:19 | [diff] [blame] | 77 | #else |
davidben | f72d28c | 2015-09-30 15:27:12 | [diff] [blame] | 78 | return scoped_ptr<SSLClientSocket>(new SSLClientSocketNSS( |
| 79 | transport_socket.Pass(), host_and_port, ssl_config, context)); |
[email protected] | 2380f37 | 2011-02-23 21:35:19 | [diff] [blame] | 80 | #endif |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | } |
[email protected] | 25f4735 | 2011-02-25 16:31:59 | [diff] [blame] | 82 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 83 | void ClearSSLSessionCache() override { SSLClientSocket::ClearSessionCache(); } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | 5399828 | 2012-06-06 22:08:52 | [diff] [blame] | 86 | static base::LazyInstance<DefaultClientSocketFactory>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 87 | g_default_client_socket_factory = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 88 | |
[email protected] | abe48d3 | 2010-02-03 02:09:36 | [diff] [blame] | 89 | } // namespace |
| 90 | |
[email protected] | d100e44f | 2011-01-26 22:47:11 | [diff] [blame] | 91 | // static |
| 92 | ClientSocketFactory* ClientSocketFactory::GetDefaultFactory() { |
| 93 | return g_default_client_socket_factory.Pointer(); |
| 94 | } |
| 95 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 96 | } // namespace net |