| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #include "chrome/browser/net/ssl_config_service_manager.h" |
| 4 | 5 |
| 5 #include "base/message_loop.h" | 6 #include <algorithm> |
| 6 #include "chrome/browser/net/ssl_config_service_manager.h" | 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 7 #include "chrome/browser/prefs/pref_member.h" | 12 #include "chrome/browser/prefs/pref_member.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 11 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
| 17 #include "content/common/notification_details.h" |
| 18 #include "content/common/notification_source.h" |
| 19 #include "net/base/ssl_cipher_suite_names.h" |
| 12 #include "net/base/ssl_config_service.h" | 20 #include "net/base/ssl_config_service.h" |
| 13 | 21 |
| 22 namespace { |
| 23 |
| 24 // Converts a ListValue of StringValues into a vector of strings. Any Values |
| 25 // which cannot be converted will be skipped. |
| 26 std::vector<std::string> ListValueToStringVector(const ListValue* value) { |
| 27 std::vector<std::string> results; |
| 28 results.reserve(value->GetSize()); |
| 29 std::string s; |
| 30 for (ListValue::const_iterator it = value->begin(); it != value->end(); |
| 31 ++it) { |
| 32 if (!(*it)->GetAsString(&s)) |
| 33 continue; |
| 34 results.push_back(s); |
| 35 } |
| 36 return results; |
| 37 } |
| 38 |
| 39 // Parses a vector of cipher suite strings, returning a sorted vector |
| 40 // containing the underlying SSL/TLS cipher suites. Unrecognized/invalid |
| 41 // cipher suites will be ignored. |
| 42 std::vector<uint16> ParseCipherSuites( |
| 43 const std::vector<std::string>& cipher_strings) { |
| 44 std::vector<uint16> cipher_suites; |
| 45 cipher_suites.reserve(cipher_strings.size()); |
| 46 |
| 47 for (std::vector<std::string>::const_iterator it = cipher_strings.begin(); |
| 48 it != cipher_strings.end(); ++it) { |
| 49 uint16 cipher_suite = 0; |
| 50 if (!net::ParseSSLCipherString(*it, &cipher_suite)) { |
| 51 LOG(ERROR) << "Ignoring unrecognized or unparsable cipher suite: " |
| 52 << *it; |
| 53 continue; |
| 54 } |
| 55 cipher_suites.push_back(cipher_suite); |
| 56 } |
| 57 std::sort(cipher_suites.begin(), cipher_suites.end()); |
| 58 return cipher_suites; |
| 59 } |
| 60 |
| 61 } // namespace |
| 62 |
| 14 //////////////////////////////////////////////////////////////////////////////// | 63 //////////////////////////////////////////////////////////////////////////////// |
| 15 // SSLConfigServicePref | 64 // SSLConfigServicePref |
| 16 | 65 |
| 17 // An SSLConfigService which stores a cached version of the current SSLConfig | 66 // An SSLConfigService which stores a cached version of the current SSLConfig |
| 18 // prefs, which are updated by SSLConfigServiceManagerPref when the prefs | 67 // prefs, which are updated by SSLConfigServiceManagerPref when the prefs |
| 19 // change. | 68 // change. |
| 20 class SSLConfigServicePref : public net::SSLConfigService { | 69 class SSLConfigServicePref : public net::SSLConfigService { |
| 21 public: | 70 public: |
| 22 SSLConfigServicePref() {} | 71 SSLConfigServicePref() {} |
| 23 virtual ~SSLConfigServicePref() {} | 72 virtual ~SSLConfigServicePref() {} |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // Callback for preference changes. This will post the changes to the IO | 119 // Callback for preference changes. This will post the changes to the IO |
| 71 // thread with SetNewSSLConfig. | 120 // thread with SetNewSSLConfig. |
| 72 virtual void Observe(int type, | 121 virtual void Observe(int type, |
| 73 const NotificationSource& source, | 122 const NotificationSource& source, |
| 74 const NotificationDetails& details); | 123 const NotificationDetails& details); |
| 75 | 124 |
| 76 // Store SSL config settings in |config|, directly from the preferences. Must | 125 // Store SSL config settings in |config|, directly from the preferences. Must |
| 77 // only be called from UI thread. | 126 // only be called from UI thread. |
| 78 void GetSSLConfigFromPrefs(net::SSLConfig* config); | 127 void GetSSLConfigFromPrefs(net::SSLConfig* config); |
| 79 | 128 |
| 129 // Processes changes to the disabled cipher suites preference, updating the |
| 130 // cached list of parsed SSL/TLS cipher suites that are disabled. |
| 131 void OnDisabledCipherSuitesChange(PrefService* prefs); |
| 132 |
| 133 PrefChangeRegistrar pref_change_registrar_; |
| 134 |
| 80 // The prefs (should only be accessed from UI thread) | 135 // The prefs (should only be accessed from UI thread) |
| 81 BooleanPrefMember rev_checking_enabled_; | 136 BooleanPrefMember rev_checking_enabled_; |
| 82 BooleanPrefMember ssl3_enabled_; | 137 BooleanPrefMember ssl3_enabled_; |
| 83 BooleanPrefMember tls1_enabled_; | 138 BooleanPrefMember tls1_enabled_; |
| 84 | 139 |
| 140 // The cached list of disabled SSL cipher suites. |
| 141 std::vector<uint16> disabled_cipher_suites_; |
| 142 |
| 85 scoped_refptr<SSLConfigServicePref> ssl_config_service_; | 143 scoped_refptr<SSLConfigServicePref> ssl_config_service_; |
| 86 | 144 |
| 87 DISALLOW_COPY_AND_ASSIGN(SSLConfigServiceManagerPref); | 145 DISALLOW_COPY_AND_ASSIGN(SSLConfigServiceManagerPref); |
| 88 }; | 146 }; |
| 89 | 147 |
| 90 SSLConfigServiceManagerPref::SSLConfigServiceManagerPref( | 148 SSLConfigServiceManagerPref::SSLConfigServiceManagerPref( |
| 91 PrefService* local_state) | 149 PrefService* local_state) |
| 92 : ssl_config_service_(new SSLConfigServicePref()) { | 150 : ssl_config_service_(new SSLConfigServicePref()) { |
| 93 DCHECK(local_state); | 151 DCHECK(local_state); |
| 94 | 152 |
| 95 rev_checking_enabled_.Init(prefs::kCertRevocationCheckingEnabled, | 153 rev_checking_enabled_.Init(prefs::kCertRevocationCheckingEnabled, |
| 96 local_state, this); | 154 local_state, this); |
| 97 ssl3_enabled_.Init(prefs::kSSL3Enabled, local_state, this); | 155 ssl3_enabled_.Init(prefs::kSSL3Enabled, local_state, this); |
| 98 tls1_enabled_.Init(prefs::kTLS1Enabled, local_state, this); | 156 tls1_enabled_.Init(prefs::kTLS1Enabled, local_state, this); |
| 157 pref_change_registrar_.Init(local_state); |
| 158 pref_change_registrar_.Add(prefs::kCipherSuiteBlacklist, this); |
| 99 | 159 |
| 160 OnDisabledCipherSuitesChange(local_state); |
| 100 // Initialize from UI thread. This is okay as there shouldn't be anything on | 161 // Initialize from UI thread. This is okay as there shouldn't be anything on |
| 101 // the IO thread trying to access it yet. | 162 // the IO thread trying to access it yet. |
| 102 GetSSLConfigFromPrefs(&ssl_config_service_->cached_config_); | 163 GetSSLConfigFromPrefs(&ssl_config_service_->cached_config_); |
| 103 } | 164 } |
| 104 | 165 |
| 105 // static | 166 // static |
| 106 void SSLConfigServiceManagerPref::RegisterPrefs(PrefService* prefs) { | 167 void SSLConfigServiceManagerPref::RegisterPrefs(PrefService* prefs) { |
| 107 net::SSLConfig default_config; | 168 net::SSLConfig default_config; |
| 108 prefs->RegisterBooleanPref(prefs::kCertRevocationCheckingEnabled, | 169 prefs->RegisterBooleanPref(prefs::kCertRevocationCheckingEnabled, |
| 109 default_config.rev_checking_enabled); | 170 default_config.rev_checking_enabled); |
| 110 prefs->RegisterBooleanPref(prefs::kSSL3Enabled, | 171 prefs->RegisterBooleanPref(prefs::kSSL3Enabled, |
| 111 default_config.ssl3_enabled); | 172 default_config.ssl3_enabled); |
| 112 prefs->RegisterBooleanPref(prefs::kTLS1Enabled, | 173 prefs->RegisterBooleanPref(prefs::kTLS1Enabled, |
| 113 default_config.tls1_enabled); | 174 default_config.tls1_enabled); |
| 175 prefs->RegisterListPref(prefs::kCipherSuiteBlacklist); |
| 114 } | 176 } |
| 115 | 177 |
| 116 net::SSLConfigService* SSLConfigServiceManagerPref::Get() { | 178 net::SSLConfigService* SSLConfigServiceManagerPref::Get() { |
| 117 return ssl_config_service_; | 179 return ssl_config_service_; |
| 118 } | 180 } |
| 119 | 181 |
| 120 void SSLConfigServiceManagerPref::Observe(int type, | 182 void SSLConfigServiceManagerPref::Observe(int type, |
| 121 const NotificationSource& source, | 183 const NotificationSource& source, |
| 122 const NotificationDetails& details) { | 184 const NotificationDetails& details) { |
| 123 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | 185 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 187 std::string* pref_name_in = Details<std::string>(details).ptr(); |
| 188 PrefService* prefs = Source<PrefService>(source).ptr(); |
| 189 DCHECK(pref_name_in && prefs); |
| 190 if (*pref_name_in == prefs::kCipherSuiteBlacklist) |
| 191 OnDisabledCipherSuitesChange(prefs); |
| 192 |
| 125 net::SSLConfig new_config; | 193 net::SSLConfig new_config; |
| 126 GetSSLConfigFromPrefs(&new_config); | 194 GetSSLConfigFromPrefs(&new_config); |
| 127 | 195 |
| 128 // Post a task to |io_loop| with the new configuration, so it can | 196 // Post a task to |io_loop| with the new configuration, so it can |
| 129 // update |cached_config_|. | 197 // update |cached_config_|. |
| 130 BrowserThread::PostTask( | 198 BrowserThread::PostTask( |
| 131 BrowserThread::IO, | 199 BrowserThread::IO, |
| 132 FROM_HERE, | 200 FROM_HERE, |
| 133 NewRunnableMethod( | 201 NewRunnableMethod( |
| 134 ssl_config_service_.get(), | 202 ssl_config_service_.get(), |
| 135 &SSLConfigServicePref::SetNewSSLConfig, | 203 &SSLConfigServicePref::SetNewSSLConfig, |
| 136 new_config)); | 204 new_config)); |
| 137 } | 205 } |
| 138 } | 206 } |
| 139 | 207 |
| 140 void SSLConfigServiceManagerPref::GetSSLConfigFromPrefs( | 208 void SSLConfigServiceManagerPref::GetSSLConfigFromPrefs( |
| 141 net::SSLConfig* config) { | 209 net::SSLConfig* config) { |
| 142 config->rev_checking_enabled = rev_checking_enabled_.GetValue(); | 210 config->rev_checking_enabled = rev_checking_enabled_.GetValue(); |
| 143 config->ssl3_enabled = ssl3_enabled_.GetValue(); | 211 config->ssl3_enabled = ssl3_enabled_.GetValue(); |
| 144 config->tls1_enabled = tls1_enabled_.GetValue(); | 212 config->tls1_enabled = tls1_enabled_.GetValue(); |
| 213 config->disabled_cipher_suites = disabled_cipher_suites_; |
| 145 SSLConfigServicePref::SetSSLConfigFlags(config); | 214 SSLConfigServicePref::SetSSLConfigFlags(config); |
| 146 } | 215 } |
| 147 | 216 |
| 217 void SSLConfigServiceManagerPref::OnDisabledCipherSuitesChange( |
| 218 PrefService* prefs) { |
| 219 const ListValue* value = prefs->GetList(prefs::kCipherSuiteBlacklist); |
| 220 disabled_cipher_suites_ = ParseCipherSuites(ListValueToStringVector(value)); |
| 221 } |
| 222 |
| 148 //////////////////////////////////////////////////////////////////////////////// | 223 //////////////////////////////////////////////////////////////////////////////// |
| 149 // SSLConfigServiceManager | 224 // SSLConfigServiceManager |
| 150 | 225 |
| 151 // static | 226 // static |
| 152 SSLConfigServiceManager* SSLConfigServiceManager::CreateDefaultManager( | 227 SSLConfigServiceManager* SSLConfigServiceManager::CreateDefaultManager( |
| 153 PrefService* local_state) { | 228 PrefService* local_state) { |
| 154 return new SSLConfigServiceManagerPref(local_state); | 229 return new SSLConfigServiceManagerPref(local_state); |
| 155 } | 230 } |
| 156 | 231 |
| 157 // static | 232 // static |
| 158 void SSLConfigServiceManager::RegisterPrefs(PrefService* prefs) { | 233 void SSLConfigServiceManager::RegisterPrefs(PrefService* prefs) { |
| 159 SSLConfigServiceManagerPref::RegisterPrefs(prefs); | 234 SSLConfigServiceManagerPref::RegisterPrefs(prefs); |
| 160 } | 235 } |
| OLD | NEW |