xref: /aosp_15_r20/external/cronet/net/ssl/ssl_config_service.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_SSL_SSL_CONFIG_SERVICE_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_SSL_SSL_CONFIG_SERVICE_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <optional>
9*6777b538SAndroid Build Coastguard Worker #include <string_view>
10*6777b538SAndroid Build Coastguard Worker #include <vector>
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker #include "base/observer_list.h"
13*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
14*6777b538SAndroid Build Coastguard Worker #include "net/ssl/ssl_config.h"
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker namespace net {
17*6777b538SAndroid Build Coastguard Worker 
18*6777b538SAndroid Build Coastguard Worker struct NET_EXPORT SSLContextConfig {
19*6777b538SAndroid Build Coastguard Worker   SSLContextConfig();
20*6777b538SAndroid Build Coastguard Worker   SSLContextConfig(const SSLContextConfig&);
21*6777b538SAndroid Build Coastguard Worker   SSLContextConfig(SSLContextConfig&&);
22*6777b538SAndroid Build Coastguard Worker   ~SSLContextConfig();
23*6777b538SAndroid Build Coastguard Worker   SSLContextConfig& operator=(const SSLContextConfig&);
24*6777b538SAndroid Build Coastguard Worker   SSLContextConfig& operator=(SSLContextConfig&&);
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker   bool operator==(const SSLContextConfig&) const;
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker   // Returns whether post-quantum key agreement is enabled in TLS handshakes.
29*6777b538SAndroid Build Coastguard Worker   bool PostQuantumKeyAgreementEnabled() const;
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker   // The minimum and maximum protocol versions that are enabled.
32*6777b538SAndroid Build Coastguard Worker   // (Use the SSL_PROTOCOL_VERSION_xxx enumerators defined in ssl_config.h.)
33*6777b538SAndroid Build Coastguard Worker   // SSL 2.0/3.0 and TLS 1.0/1.1 are not supported. If version_max <
34*6777b538SAndroid Build Coastguard Worker   // version_min, it means no protocol versions are enabled.
35*6777b538SAndroid Build Coastguard Worker   uint16_t version_min = kDefaultSSLVersionMin;
36*6777b538SAndroid Build Coastguard Worker   uint16_t version_max = kDefaultSSLVersionMax;
37*6777b538SAndroid Build Coastguard Worker 
38*6777b538SAndroid Build Coastguard Worker   // A list of cipher suites which should be explicitly prevented from being
39*6777b538SAndroid Build Coastguard Worker   // used in addition to those disabled by the net built-in policy.
40*6777b538SAndroid Build Coastguard Worker   //
41*6777b538SAndroid Build Coastguard Worker   // Though cipher suites are sent in TLS as "uint8_t CipherSuite[2]", in
42*6777b538SAndroid Build Coastguard Worker   // big-endian form, they should be declared in host byte order, with the
43*6777b538SAndroid Build Coastguard Worker   // first uint8_t occupying the most significant byte.
44*6777b538SAndroid Build Coastguard Worker   // Ex: To disable TLS_RSA_WITH_RC4_128_MD5, specify 0x0004, while to
45*6777b538SAndroid Build Coastguard Worker   // disable TLS_ECDH_ECDSA_WITH_RC4_128_SHA, specify 0xC002.
46*6777b538SAndroid Build Coastguard Worker   std::vector<uint16_t> disabled_cipher_suites;
47*6777b538SAndroid Build Coastguard Worker 
48*6777b538SAndroid Build Coastguard Worker   // If specified, controls whether post-quantum key agreement in TLS
49*6777b538SAndroid Build Coastguard Worker   // connections is allowed. If `std::nullopt`, this is determined by feature
50*6777b538SAndroid Build Coastguard Worker   // flags.
51*6777b538SAndroid Build Coastguard Worker   std::optional<bool> post_quantum_override;
52*6777b538SAndroid Build Coastguard Worker 
53*6777b538SAndroid Build Coastguard Worker   // Controls whether ECH is enabled.
54*6777b538SAndroid Build Coastguard Worker   bool ech_enabled = true;
55*6777b538SAndroid Build Coastguard Worker };
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker // The interface for retrieving global SSL configuration.  This interface
58*6777b538SAndroid Build Coastguard Worker // does not cover setting the SSL configuration, as on some systems, the
59*6777b538SAndroid Build Coastguard Worker // SSLConfigService objects may not have direct access to the configuration, or
60*6777b538SAndroid Build Coastguard Worker // live longer than the configuration preferences.
61*6777b538SAndroid Build Coastguard Worker class NET_EXPORT SSLConfigService {
62*6777b538SAndroid Build Coastguard Worker  public:
63*6777b538SAndroid Build Coastguard Worker   // Observer is notified when SSL config settings have changed.
64*6777b538SAndroid Build Coastguard Worker   class NET_EXPORT Observer {
65*6777b538SAndroid Build Coastguard Worker    public:
66*6777b538SAndroid Build Coastguard Worker     // Notify observers if SSL settings have changed.
67*6777b538SAndroid Build Coastguard Worker     virtual void OnSSLContextConfigChanged() = 0;
68*6777b538SAndroid Build Coastguard Worker 
69*6777b538SAndroid Build Coastguard Worker    protected:
70*6777b538SAndroid Build Coastguard Worker     virtual ~Observer() = default;
71*6777b538SAndroid Build Coastguard Worker   };
72*6777b538SAndroid Build Coastguard Worker 
73*6777b538SAndroid Build Coastguard Worker   SSLConfigService();
74*6777b538SAndroid Build Coastguard Worker   virtual ~SSLConfigService();
75*6777b538SAndroid Build Coastguard Worker 
76*6777b538SAndroid Build Coastguard Worker   // May not be thread-safe, should only be called on the IO thread.
77*6777b538SAndroid Build Coastguard Worker   virtual SSLContextConfig GetSSLContextConfig() = 0;
78*6777b538SAndroid Build Coastguard Worker 
79*6777b538SAndroid Build Coastguard Worker   // Returns true if connections to |hostname| can reuse, or are permitted to
80*6777b538SAndroid Build Coastguard Worker   // reuse, connections on which a client cert has been negotiated. Note that
81*6777b538SAndroid Build Coastguard Worker   // this must return true for both hostnames being pooled - that is to say this
82*6777b538SAndroid Build Coastguard Worker   // function must return true for both the hostname of the existing connection
83*6777b538SAndroid Build Coastguard Worker   // and the potential hostname to pool before allowing the connection to be
84*6777b538SAndroid Build Coastguard Worker   // reused.
85*6777b538SAndroid Build Coastguard Worker   //
86*6777b538SAndroid Build Coastguard Worker   // NOTE: Pooling connections with ambient authority can create security issues
87*6777b538SAndroid Build Coastguard Worker   // with that ambient authority and privacy issues in that embedders (and
88*6777b538SAndroid Build Coastguard Worker   // users) may not have been consulted to send a client cert to |hostname|.
89*6777b538SAndroid Build Coastguard Worker   // Implementations of this method should only return true if they have
90*6777b538SAndroid Build Coastguard Worker   // received affirmative consent (e.g. through preferences or Enterprise
91*6777b538SAndroid Build Coastguard Worker   // policy).
92*6777b538SAndroid Build Coastguard Worker   //
93*6777b538SAndroid Build Coastguard Worker   // NOTE: For Web Platform clients, this violates the Fetch Standard's policies
94*6777b538SAndroid Build Coastguard Worker   // around connection pools: https://fetch.spec.whatwg.org/#connections.
95*6777b538SAndroid Build Coastguard Worker   // Implementations that return true should take steps to limit the Web
96*6777b538SAndroid Build Coastguard Worker   // Platform visibility of this, such as only allowing it to be used for
97*6777b538SAndroid Build Coastguard Worker   // Enterprise or internal configurations.
98*6777b538SAndroid Build Coastguard Worker   //
99*6777b538SAndroid Build Coastguard Worker   // DEPRECATED: For the reasons above, this method is temporary and will be
100*6777b538SAndroid Build Coastguard Worker   // removed in a future release. Please leave a comment on
101*6777b538SAndroid Build Coastguard Worker   // https://crbug.com/855690 if you believe this is needed.
102*6777b538SAndroid Build Coastguard Worker   virtual bool CanShareConnectionWithClientCerts(
103*6777b538SAndroid Build Coastguard Worker       std::string_view hostname) const = 0;
104*6777b538SAndroid Build Coastguard Worker 
105*6777b538SAndroid Build Coastguard Worker   // Add an observer of this service.
106*6777b538SAndroid Build Coastguard Worker   void AddObserver(Observer* observer);
107*6777b538SAndroid Build Coastguard Worker 
108*6777b538SAndroid Build Coastguard Worker   // Remove an observer of this service.
109*6777b538SAndroid Build Coastguard Worker   void RemoveObserver(Observer* observer);
110*6777b538SAndroid Build Coastguard Worker 
111*6777b538SAndroid Build Coastguard Worker   // Calls the OnSSLContextConfigChanged method of registered observers. Should
112*6777b538SAndroid Build Coastguard Worker   // only be called on the IO thread.
113*6777b538SAndroid Build Coastguard Worker   void NotifySSLContextConfigChange();
114*6777b538SAndroid Build Coastguard Worker 
115*6777b538SAndroid Build Coastguard Worker  protected:
116*6777b538SAndroid Build Coastguard Worker   // Process before/after config update. If |force_notification| is true,
117*6777b538SAndroid Build Coastguard Worker   // NotifySSLContextConfigChange will be called regardless of whether
118*6777b538SAndroid Build Coastguard Worker   // |orig_config| and |new_config| are equal.
119*6777b538SAndroid Build Coastguard Worker   void ProcessConfigUpdate(const SSLContextConfig& orig_config,
120*6777b538SAndroid Build Coastguard Worker                            const SSLContextConfig& new_config,
121*6777b538SAndroid Build Coastguard Worker                            bool force_notification);
122*6777b538SAndroid Build Coastguard Worker 
123*6777b538SAndroid Build Coastguard Worker  private:
124*6777b538SAndroid Build Coastguard Worker   base::ObserverList<Observer>::Unchecked observer_list_;
125*6777b538SAndroid Build Coastguard Worker };
126*6777b538SAndroid Build Coastguard Worker 
127*6777b538SAndroid Build Coastguard Worker }  // namespace net
128*6777b538SAndroid Build Coastguard Worker 
129*6777b538SAndroid Build Coastguard Worker #endif  // NET_SSL_SSL_CONFIG_SERVICE_H_
130