blob: b6efe1214c6f6cfc55bce552fca5ccab12ce29a2 [file] [log] [blame]
[email protected]c940d372011-04-13 17:20:181// Copyright (c) 2011 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]68bf9152008-09-25 19:47:308#include "build/build_config.h"
[email protected]62635c72011-03-10 04:16:259#include "net/base/cert_database.h"
[email protected]e60e47a2010-07-14 03:37:1810#include "net/socket/client_socket_handle.h"
[email protected]68bf9152008-09-25 19:47:3011#if defined(OS_WIN)
[email protected]2380f372011-02-23 21:35:1912#include "net/socket/ssl_client_socket_nss.h"
[email protected]f7984fc62009-06-22 23:26:4413#include "net/socket/ssl_client_socket_win.h"
[email protected]d518cd92010-09-29 12:27:4414#elif defined(USE_OPENSSL)
15#include "net/socket/ssl_client_socket_openssl.h"
[email protected]1a157302010-01-29 03:36:4516#elif defined(USE_NSS)
[email protected]f7984fc62009-06-22 23:26:4417#include "net/socket/ssl_client_socket_nss.h"
[email protected]b75523f2008-10-17 14:49:0718#elif defined(OS_MACOSX)
[email protected]2380f372011-02-23 21:35:1919#include "net/socket/ssl_client_socket_mac.h"
[email protected]fd4f139f2010-06-11 17:02:2020#include "net/socket/ssl_client_socket_nss.h"
[email protected]68bf9152008-09-25 19:47:3021#endif
[email protected]d0672be2010-10-20 16:30:1922#include "net/socket/ssl_host_info.h"
[email protected]f7984fc62009-06-22 23:26:4423#include "net/socket/tcp_client_socket.h"
[email protected]98b0e582011-06-22 14:31:4124#include "net/udp/udp_client_socket.h"
initial.commit586acc5fe2008-07-26 22:42:5225
26namespace net {
27
[email protected]62635c72011-03-10 04:16:2528class X509Certificate;
29
[email protected]abe48d32010-02-03 02:09:3630namespace {
31
[email protected]2380f372011-02-23 21:35:1932bool g_use_system_ssl = false;
[email protected]abe48d32010-02-03 02:09:3633
[email protected]62635c72011-03-10 04:16:2534class DefaultClientSocketFactory : public ClientSocketFactory,
35 public CertDatabase::Observer {
initial.commit586acc5fe2008-07-26 22:42:5236 public:
[email protected]62635c72011-03-10 04:16:2537 DefaultClientSocketFactory() {
38 CertDatabase::AddObserver(this);
39 }
40
41 virtual ~DefaultClientSocketFactory() {
42 CertDatabase::RemoveObserver(this);
43 }
44
[email protected]c940d372011-04-13 17:20:1845 virtual void OnUserCertAdded(const X509Certificate* cert) {
46 ClearSSLSessionCache();
47 }
48
49 virtual void OnCertTrustChanged(const X509Certificate* cert) {
50 // Per wtc, we actually only need to flush when trust is reduced.
51 // Always flush now because OnCertTrustChanged does not tell us this.
52 // See comments in ClientSocketPoolManager::OnCertTrustChanged.
[email protected]62635c72011-03-10 04:16:2553 ClearSSLSessionCache();
54 }
55
[email protected]98b0e582011-06-22 14:31:4156 virtual DatagramClientSocket* CreateDatagramClientSocket(
57 NetLog* net_log,
58 const NetLog::Source& source) {
59 return new UDPClientSocket(net_log, source);
60 }
61
[email protected]3268023f2011-05-05 00:08:1062 virtual StreamSocket* CreateTransportClientSocket(
[email protected]0a0b7682010-08-25 17:08:0763 const AddressList& addresses,
64 NetLog* net_log,
65 const NetLog::Source& source) {
66 return new TCPClientSocket(addresses, net_log, source);
initial.commit586acc5fe2008-07-26 22:42:5267 }
68
[email protected]aaead502008-10-15 00:20:1169 virtual SSLClientSocket* CreateSSLClientSocket(
[email protected]e60e47a2010-07-14 03:37:1870 ClientSocketHandle* transport_socket,
[email protected]4f4de7e62010-11-12 19:55:2771 const HostPortPair& host_and_port,
[email protected]7ab5bbd12010-10-19 13:33:2172 const SSLConfig& ssl_config,
[email protected]d8fbf582010-11-04 21:51:1273 SSLHostInfo* ssl_host_info,
[email protected]822581d2010-12-16 17:27:1574 CertVerifier* cert_verifier,
[email protected]345c613b2010-11-22 19:33:1875 DnsCertProvenanceChecker* dns_cert_checker) {
[email protected]2380f372011-02-23 21:35:1976 scoped_ptr<SSLHostInfo> shi(ssl_host_info);
77#if defined(OS_WIN)
78 if (g_use_system_ssl) {
79 return new SSLClientSocketWin(transport_socket, host_and_port,
80 ssl_config, cert_verifier);
81 }
82 return new SSLClientSocketNSS(transport_socket, host_and_port, ssl_config,
83 shi.release(), cert_verifier,
84 dns_cert_checker);
85#elif defined(USE_OPENSSL)
86 return new SSLClientSocketOpenSSL(transport_socket, host_and_port,
87 ssl_config, cert_verifier);
88#elif defined(USE_NSS)
89 return new SSLClientSocketNSS(transport_socket, host_and_port, ssl_config,
90 shi.release(), cert_verifier,
91 dns_cert_checker);
92#elif defined(OS_MACOSX)
93 if (g_use_system_ssl) {
94 return new SSLClientSocketMac(transport_socket, host_and_port,
95 ssl_config, cert_verifier);
96 }
97 return new SSLClientSocketNSS(transport_socket, host_and_port, ssl_config,
98 shi.release(), cert_verifier,
99 dns_cert_checker);
100#else
101 NOTIMPLEMENTED();
102 return NULL;
103#endif
initial.commit586acc5fe2008-07-26 22:42:52104 }
[email protected]25f47352011-02-25 16:31:59105
106 // TODO(rch): This is only implemented for the NSS SSL library, which is the
107 /// default for Windows, Mac and Linux, but we should implement it everywhere.
108 void ClearSSLSessionCache() {
109#if defined(OS_WIN)
110 if (!g_use_system_ssl)
111 SSLClientSocketNSS::ClearSessionCache();
112#elif defined(USE_OPENSSL)
113 // no-op
114#elif defined(USE_NSS)
115 SSLClientSocketNSS::ClearSessionCache();
116#elif defined(OS_MACOSX)
117 if (!g_use_system_ssl)
118 SSLClientSocketNSS::ClearSessionCache();
119#else
120 NOTIMPLEMENTED();
121#endif
122 }
123
initial.commit586acc5fe2008-07-26 22:42:52124};
125
[email protected]625332e02010-12-14 07:48:49126static base::LazyInstance<DefaultClientSocketFactory>
127 g_default_client_socket_factory(base::LINKER_INITIALIZED);
128
[email protected]abe48d32010-02-03 02:09:36129} // namespace
130
[email protected]3268023f2011-05-05 00:08:10131// Deprecated function (http://crbug.com/37810) that takes a StreamSocket.
[email protected]e60e47a2010-07-14 03:37:18132SSLClientSocket* ClientSocketFactory::CreateSSLClientSocket(
[email protected]3268023f2011-05-05 00:08:10133 StreamSocket* transport_socket,
[email protected]4f4de7e62010-11-12 19:55:27134 const HostPortPair& host_and_port,
[email protected]7ab5bbd12010-10-19 13:33:21135 const SSLConfig& ssl_config,
[email protected]822581d2010-12-16 17:27:15136 SSLHostInfo* ssl_host_info,
137 CertVerifier* cert_verifier) {
[email protected]e60e47a2010-07-14 03:37:18138 ClientSocketHandle* socket_handle = new ClientSocketHandle();
139 socket_handle->set_socket(transport_socket);
[email protected]4f4de7e62010-11-12 19:55:27140 return CreateSSLClientSocket(socket_handle, host_and_port, ssl_config,
[email protected]822581d2010-12-16 17:27:15141 ssl_host_info, cert_verifier,
[email protected]345c613b2010-11-22 19:33:18142 NULL /* DnsCertProvenanceChecker */);
[email protected]e60e47a2010-07-14 03:37:18143}
144
[email protected]d100e44f2011-01-26 22:47:11145// static
146ClientSocketFactory* ClientSocketFactory::GetDefaultFactory() {
147 return g_default_client_socket_factory.Pointer();
148}
149
150// static
[email protected]2380f372011-02-23 21:35:19151void ClientSocketFactory::UseSystemSSL() {
152 g_use_system_ssl = true;
[email protected]d100e44f2011-01-26 22:47:11153}
154
initial.commit586acc5fe2008-07-26 22:42:52155} // namespace net