Home | History | Annotate | Download | only in ssl
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "net/ssl/ssl_config_service.h"
      6 
      7 #include "base/lazy_instance.h"
      8 #include "base/memory/ref_counted.h"
      9 #include "base/synchronization/lock.h"
     10 #include "net/cert/crl_set.h"
     11 #include "net/ssl/ssl_config_service_defaults.h"
     12 
     13 #if defined(USE_OPENSSL)
     14 #include <openssl/ssl.h>
     15 #endif
     16 
     17 namespace net {
     18 
     19 static uint16 g_default_version_min = SSL_PROTOCOL_VERSION_SSL3;
     20 
     21 static uint16 g_default_version_max =
     22 #if defined(USE_OPENSSL)
     23 #if defined(SSL_OP_NO_TLSv1_2)
     24     SSL_PROTOCOL_VERSION_TLS1_2;
     25 #elif defined(SSL_OP_NO_TLSv1_1)
     26     SSL_PROTOCOL_VERSION_TLS1_1;
     27 #else
     28     SSL_PROTOCOL_VERSION_TLS1;
     29 #endif
     30 #else
     31     SSL_PROTOCOL_VERSION_TLS1_2;
     32 #endif
     33 
     34 SSLConfig::CertAndStatus::CertAndStatus() : cert_status(0) {}
     35 
     36 SSLConfig::CertAndStatus::~CertAndStatus() {}
     37 
     38 SSLConfig::SSLConfig()
     39     : rev_checking_enabled(false),
     40       rev_checking_required_local_anchors(false),
     41       version_min(g_default_version_min),
     42       version_max(g_default_version_max),
     43       cached_info_enabled(false),
     44       channel_id_enabled(true),
     45       false_start_enabled(false),
     46       unrestricted_ssl3_fallback_enabled(false),
     47       send_client_cert(false),
     48       verify_ev_cert(false),
     49       version_fallback(false),
     50       cert_io_enabled(true) {
     51 }
     52 
     53 SSLConfig::~SSLConfig() {
     54 }
     55 
     56 bool SSLConfig::IsAllowedBadCert(X509Certificate* cert,
     57                                  CertStatus* cert_status) const {
     58   std::string der_cert;
     59   if (!X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_cert))
     60     return false;
     61   return IsAllowedBadCert(der_cert, cert_status);
     62 }
     63 
     64 bool SSLConfig::IsAllowedBadCert(const base::StringPiece& der_cert,
     65                                  CertStatus* cert_status) const {
     66   for (size_t i = 0; i < allowed_bad_certs.size(); ++i) {
     67     if (der_cert == allowed_bad_certs[i].der_cert) {
     68       if (cert_status)
     69         *cert_status = allowed_bad_certs[i].cert_status;
     70       return true;
     71     }
     72   }
     73   return false;
     74 }
     75 
     76 SSLConfigService::SSLConfigService()
     77     : observer_list_(ObserverList<Observer>::NOTIFY_EXISTING_ONLY) {
     78 }
     79 
     80 static bool g_cached_info_enabled = false;
     81 
     82 // GlobalCRLSet holds a reference to the global CRLSet. It simply wraps a lock
     83 // around a scoped_refptr so that getting a reference doesn't race with
     84 // updating the CRLSet.
     85 class GlobalCRLSet {
     86  public:
     87   void Set(const scoped_refptr<CRLSet>& new_crl_set) {
     88     base::AutoLock locked(lock_);
     89     crl_set_ = new_crl_set;
     90   }
     91 
     92   scoped_refptr<CRLSet> Get() const {
     93     base::AutoLock locked(lock_);
     94     return crl_set_;
     95   }
     96 
     97  private:
     98   scoped_refptr<CRLSet> crl_set_;
     99   mutable base::Lock lock_;
    100 };
    101 
    102 base::LazyInstance<GlobalCRLSet>::Leaky g_crl_set = LAZY_INSTANCE_INITIALIZER;
    103 
    104 // static
    105 void SSLConfigService::SetCRLSet(scoped_refptr<CRLSet> crl_set) {
    106   // Note: this can be called concurently with GetCRLSet().
    107   g_crl_set.Get().Set(crl_set);
    108 }
    109 
    110 // static
    111 scoped_refptr<CRLSet> SSLConfigService::GetCRLSet() {
    112   return g_crl_set.Get().Get();
    113 }
    114 
    115 void SSLConfigService::EnableCachedInfo() {
    116   g_cached_info_enabled = true;
    117 }
    118 
    119 // static
    120 bool SSLConfigService::cached_info_enabled() {
    121   return g_cached_info_enabled;
    122 }
    123 
    124 // static
    125 uint16 SSLConfigService::default_version_min() {
    126   return g_default_version_min;
    127 }
    128 
    129 // static
    130 uint16 SSLConfigService::default_version_max() {
    131   return g_default_version_max;
    132 }
    133 
    134 void SSLConfigService::AddObserver(Observer* observer) {
    135   observer_list_.AddObserver(observer);
    136 }
    137 
    138 void SSLConfigService::RemoveObserver(Observer* observer) {
    139   observer_list_.RemoveObserver(observer);
    140 }
    141 
    142 void SSLConfigService::NotifySSLConfigChange() {
    143   FOR_EACH_OBSERVER(Observer, observer_list_, OnSSLConfigChanged());
    144 }
    145 
    146 SSLConfigService::~SSLConfigService() {
    147 }
    148 
    149 // static
    150 void SSLConfigService::SetSSLConfigFlags(SSLConfig* ssl_config) {
    151   ssl_config->cached_info_enabled = g_cached_info_enabled;
    152 }
    153 
    154 void SSLConfigService::ProcessConfigUpdate(const SSLConfig& orig_config,
    155                                            const SSLConfig& new_config) {
    156   bool config_changed =
    157       (orig_config.rev_checking_enabled != new_config.rev_checking_enabled) ||
    158       (orig_config.rev_checking_required_local_anchors !=
    159        new_config.rev_checking_required_local_anchors) ||
    160       (orig_config.version_min != new_config.version_min) ||
    161       (orig_config.version_max != new_config.version_max) ||
    162       (orig_config.disabled_cipher_suites !=
    163        new_config.disabled_cipher_suites) ||
    164       (orig_config.channel_id_enabled != new_config.channel_id_enabled) ||
    165       (orig_config.false_start_enabled != new_config.false_start_enabled) ||
    166       (orig_config.unrestricted_ssl3_fallback_enabled !=
    167        new_config.unrestricted_ssl3_fallback_enabled);
    168 
    169   if (config_changed)
    170     NotifySSLConfigChange();
    171 }
    172 
    173 // static
    174 bool SSLConfigService::IsSNIAvailable(SSLConfigService* service) {
    175   if (!service)
    176     return false;
    177 
    178   SSLConfig ssl_config;
    179   service->GetSSLConfig(&ssl_config);
    180   return ssl_config.version_max >= SSL_PROTOCOL_VERSION_TLS1;
    181 }
    182 
    183 }  // namespace net
    184