blob: ade687454a99efa329aa354f086e5ab3cc5e87c7 [file] [log] [blame]
[email protected]80c75f682012-05-26 16:22:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]f7984fc62009-06-22 23:26:445#include "net/socket/client_socket_factory.h"
initial.commit586acc5fe2008-07-26 22:42:526
[email protected]625332e02010-12-14 07:48:497#include "base/lazy_instance.h"
[email protected]53998282012-06-06 22:08:528#include "base/thread_task_runner_handle.h"
9#include "base/threading/thread.h"
[email protected]68bf9152008-09-25 19:47:3010#include "build/build_config.h"
[email protected]62635c72011-03-10 04:16:2511#include "net/base/cert_database.h"
[email protected]e60e47a2010-07-14 03:37:1812#include "net/socket/client_socket_handle.h"
[email protected]68bf9152008-09-25 19:47:3013#if defined(OS_WIN)
[email protected]2380f372011-02-23 21:35:1914#include "net/socket/ssl_client_socket_nss.h"
[email protected]f7984fc62009-06-22 23:26:4415#include "net/socket/ssl_client_socket_win.h"
[email protected]d518cd92010-09-29 12:27:4416#elif defined(USE_OPENSSL)
17#include "net/socket/ssl_client_socket_openssl.h"
[email protected]1a157302010-01-29 03:36:4518#elif defined(USE_NSS)
[email protected]f7984fc62009-06-22 23:26:4419#include "net/socket/ssl_client_socket_nss.h"
[email protected]b75523f2008-10-17 14:49:0720#elif defined(OS_MACOSX)
[email protected]2380f372011-02-23 21:35:1921#include "net/socket/ssl_client_socket_mac.h"
[email protected]fd4f139f2010-06-11 17:02:2022#include "net/socket/ssl_client_socket_nss.h"
[email protected]68bf9152008-09-25 19:47:3023#endif
[email protected]d0672be2010-10-20 16:30:1924#include "net/socket/ssl_host_info.h"
[email protected]f7984fc62009-06-22 23:26:4425#include "net/socket/tcp_client_socket.h"
[email protected]98b0e582011-06-22 14:31:4126#include "net/udp/udp_client_socket.h"
initial.commit586acc5fe2008-07-26 22:42:5227
28namespace net {
29
[email protected]62635c72011-03-10 04:16:2530class X509Certificate;
31
[email protected]abe48d32010-02-03 02:09:3632namespace {
33
[email protected]2380f372011-02-23 21:35:1934bool g_use_system_ssl = false;
[email protected]abe48d32010-02-03 02:09:3635
[email protected]53998282012-06-06 22:08:5236// ChromeOS uses a hardware TPM module that may cause NSS operations to
37// block for upwards of several seconds. To avoid blocking all network and
38// IPC activity, run NSS SSL functions on a dedicated thread.
39#if defined(OS_CHROMEOS)
40bool g_use_dedicated_nss_thread = true;
41#else
42bool g_use_dedicated_nss_thread = false;
43#endif
44
[email protected]62635c72011-03-10 04:16:2545class DefaultClientSocketFactory : public ClientSocketFactory,
46 public CertDatabase::Observer {
initial.commit586acc5fe2008-07-26 22:42:5247 public:
[email protected]62635c72011-03-10 04:16:2548 DefaultClientSocketFactory() {
[email protected]53998282012-06-06 22:08:5249 if (g_use_dedicated_nss_thread) {
50 nss_thread_.reset(new base::Thread("NSS SSL Thread"));
51 if (nss_thread_->Start())
52 nss_thread_task_runner_ = nss_thread_->message_loop_proxy();
53 }
54
[email protected]62635c72011-03-10 04:16:2555 CertDatabase::AddObserver(this);
56 }
57
58 virtual ~DefaultClientSocketFactory() {
[email protected]53998282012-06-06 22:08:5259 // Note: This code never runs, as the factory is defined as a Leaky
60 // singleton.
[email protected]62635c72011-03-10 04:16:2561 CertDatabase::RemoveObserver(this);
62 }
63
[email protected]c940d372011-04-13 17:20:1864 virtual void OnUserCertAdded(const X509Certificate* cert) {
65 ClearSSLSessionCache();
66 }
67
68 virtual void OnCertTrustChanged(const X509Certificate* cert) {
69 // Per wtc, we actually only need to flush when trust is reduced.
70 // Always flush now because OnCertTrustChanged does not tell us this.
71 // See comments in ClientSocketPoolManager::OnCertTrustChanged.
[email protected]62635c72011-03-10 04:16:2572 ClearSSLSessionCache();
73 }
74
[email protected]98b0e582011-06-22 14:31:4175 virtual DatagramClientSocket* CreateDatagramClientSocket(
[email protected]5370c012011-06-29 03:47:0476 DatagramSocket::BindType bind_type,
77 const RandIntCallback& rand_int_cb,
[email protected]98b0e582011-06-22 14:31:4178 NetLog* net_log,
79 const NetLog::Source& source) {
[email protected]5370c012011-06-29 03:47:0480 return new UDPClientSocket(bind_type, rand_int_cb, net_log, source);
[email protected]98b0e582011-06-22 14:31:4181 }
82
[email protected]3268023f2011-05-05 00:08:1083 virtual StreamSocket* CreateTransportClientSocket(
[email protected]0a0b7682010-08-25 17:08:0784 const AddressList& addresses,
85 NetLog* net_log,
86 const NetLog::Source& source) {
87 return new TCPClientSocket(addresses, net_log, source);
initial.commit586acc5fe2008-07-26 22:42:5288 }
89
[email protected]aaead502008-10-15 00:20:1190 virtual SSLClientSocket* CreateSSLClientSocket(
[email protected]e60e47a2010-07-14 03:37:1891 ClientSocketHandle* transport_socket,
[email protected]4f4de7e62010-11-12 19:55:2792 const HostPortPair& host_and_port,
[email protected]7ab5bbd12010-10-19 13:33:2193 const SSLConfig& ssl_config,
[email protected]d8fbf582010-11-04 21:51:1294 SSLHostInfo* ssl_host_info,
[email protected]feb79bcd2011-07-21 16:55:1795 const SSLClientSocketContext& context) {
[email protected]2380f372011-02-23 21:35:1996 scoped_ptr<SSLHostInfo> shi(ssl_host_info);
[email protected]feb79bcd2011-07-21 16:55:1797
[email protected]53998282012-06-06 22:08:5298 // nss_thread_task_runner_ may be NULL if g_use_dedicated_nss_thread is
99 // false or if the dedicated NSS thread failed to start. If so, cause NSS
100 // functions to execute on the current task runner.
101 //
102 // Note: The current task runner is obtained on each call due to unit
103 // tests, which may create and tear down the current thread's TaskRunner
104 // between each test. Because the DefaultClientSocketFactory is leaky, it
105 // may span multiple tests, and thus the current task runner may change
106 // from call to call.
107 scoped_refptr<base::SingleThreadTaskRunner> nss_task_runner(
108 nss_thread_task_runner_);
109 if (!nss_task_runner)
110 nss_task_runner = base::ThreadTaskRunnerHandle::Get();
111
112#if defined(USE_OPENSSL)
113 return new SSLClientSocketOpenSSL(transport_socket, host_and_port,
114 ssl_config, context);
115#elif defined(USE_NSS)
116 return new SSLClientSocketNSS(nss_task_runner, transport_socket,
117 host_and_port, ssl_config, shi.release(),
118 context);
119#elif defined(OS_WIN)
[email protected]2380f372011-02-23 21:35:19120 if (g_use_system_ssl) {
121 return new SSLClientSocketWin(transport_socket, host_and_port,
[email protected]feb79bcd2011-07-21 16:55:17122 ssl_config, context);
[email protected]2380f372011-02-23 21:35:19123 }
[email protected]53998282012-06-06 22:08:52124 return new SSLClientSocketNSS(nss_task_runner, transport_socket,
125 host_and_port, ssl_config, shi.release(),
126 context);
[email protected]2380f372011-02-23 21:35:19127#elif defined(OS_MACOSX)
128 if (g_use_system_ssl) {
129 return new SSLClientSocketMac(transport_socket, host_and_port,
[email protected]feb79bcd2011-07-21 16:55:17130 ssl_config, context);
[email protected]2380f372011-02-23 21:35:19131 }
[email protected]53998282012-06-06 22:08:52132 return new SSLClientSocketNSS(nss_task_runner, transport_socket,
133 host_and_port, ssl_config, shi.release(),
134 context);
[email protected]2380f372011-02-23 21:35:19135#else
136 NOTIMPLEMENTED();
137 return NULL;
138#endif
initial.commit586acc5fe2008-07-26 22:42:52139 }
[email protected]25f47352011-02-25 16:31:59140
[email protected]25f47352011-02-25 16:31:59141 void ClearSSLSessionCache() {
[email protected]c3456bb2011-12-12 22:22:19142 SSLClientSocket::ClearSessionCache();
[email protected]25f47352011-02-25 16:31:59143 }
144
[email protected]53998282012-06-06 22:08:52145 private:
146 scoped_ptr<base::Thread> nss_thread_;
147 scoped_refptr<base::SingleThreadTaskRunner> nss_thread_task_runner_;
initial.commit586acc5fe2008-07-26 22:42:52148};
149
[email protected]53998282012-06-06 22:08:52150static base::LazyInstance<DefaultClientSocketFactory>::Leaky
[email protected]6de0fd1d2011-11-15 13:31:49151 g_default_client_socket_factory = LAZY_INSTANCE_INITIALIZER;
[email protected]625332e02010-12-14 07:48:49152
[email protected]abe48d32010-02-03 02:09:36153} // namespace
154
[email protected]3268023f2011-05-05 00:08:10155// Deprecated function (http://crbug.com/37810) that takes a StreamSocket.
[email protected]e60e47a2010-07-14 03:37:18156SSLClientSocket* ClientSocketFactory::CreateSSLClientSocket(
[email protected]3268023f2011-05-05 00:08:10157 StreamSocket* transport_socket,
[email protected]4f4de7e62010-11-12 19:55:27158 const HostPortPair& host_and_port,
[email protected]7ab5bbd12010-10-19 13:33:21159 const SSLConfig& ssl_config,
[email protected]822581d2010-12-16 17:27:15160 SSLHostInfo* ssl_host_info,
[email protected]feb79bcd2011-07-21 16:55:17161 const SSLClientSocketContext& context) {
[email protected]e60e47a2010-07-14 03:37:18162 ClientSocketHandle* socket_handle = new ClientSocketHandle();
163 socket_handle->set_socket(transport_socket);
[email protected]4f4de7e62010-11-12 19:55:27164 return CreateSSLClientSocket(socket_handle, host_and_port, ssl_config,
[email protected]feb79bcd2011-07-21 16:55:17165 ssl_host_info, context);
[email protected]e60e47a2010-07-14 03:37:18166}
167
[email protected]d100e44f2011-01-26 22:47:11168// static
169ClientSocketFactory* ClientSocketFactory::GetDefaultFactory() {
170 return g_default_client_socket_factory.Pointer();
171}
172
173// static
[email protected]2380f372011-02-23 21:35:19174void ClientSocketFactory::UseSystemSSL() {
175 g_use_system_ssl = true;
[email protected]80c75f682012-05-26 16:22:17176
177#if defined(OS_WIN)
178 // Reflect the capability of SSLClientSocketWin.
179 SSLConfigService::SetDefaultVersionMax(SSL_PROTOCOL_VERSION_TLS1);
180#elif defined(OS_MACOSX)
181 // Reflect the capability of SSLClientSocketMac.
182 SSLConfigService::SetDefaultVersionMax(SSL_PROTOCOL_VERSION_TLS1);
183#endif
[email protected]d100e44f2011-01-26 22:47:11184}
185
initial.commit586acc5fe2008-07-26 22:42:52186} // namespace net