henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_SSLADAPTER_H_ |
| 12 | #define RTC_BASE_SSLADAPTER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/asyncsocket.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 18 | #include "rtc_base/sslcertificate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/sslstreamadapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | namespace rtc { |
| 22 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 23 | class SSLAdapter; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame^] | 25 | // TLS certificate policy. |
| 26 | enum class TlsCertPolicy { |
| 27 | // For TLS based protocols, ensure the connection is secure by not |
| 28 | // circumventing certificate validation. |
| 29 | TLS_CERT_POLICY_SECURE, |
| 30 | // For TLS based protocols, disregard security completely by skipping |
| 31 | // certificate validation. This is insecure and should never be used unless |
| 32 | // security is irrelevant in that particular context. |
| 33 | // Do not set to this value in production code. |
| 34 | // TODO(juberti): Remove the opportunistic encryption mechanism in |
| 35 | // BasicPacketSocketFactory that uses this value. |
| 36 | TLS_CERT_POLICY_INSECURE_NO_CHECK, |
| 37 | }; |
| 38 | |
| 39 | // SSL configuration options. |
| 40 | struct SSLConfig final { |
| 41 | SSLConfig(); |
| 42 | SSLConfig(const SSLConfig&); |
| 43 | ~SSLConfig(); |
| 44 | |
| 45 | bool operator==(const SSLConfig& o) const { |
| 46 | return enable_ocsp_stapling == o.enable_ocsp_stapling && |
| 47 | enable_signed_cert_timestamp == o.enable_signed_cert_timestamp && |
| 48 | enable_tls_channel_id == o.enable_tls_channel_id && |
| 49 | enable_grease == o.enable_grease && |
| 50 | max_ssl_version == o.max_ssl_version && |
| 51 | tls_alpn_protocols == o.tls_alpn_protocols && |
| 52 | tls_elliptic_curves == o.tls_elliptic_curves; |
| 53 | } |
| 54 | bool operator!=(const SSLConfig& o) const { return !(*this == o); } |
| 55 | |
| 56 | // If true, enables the (unused) OCSP stapling TLS extension. |
| 57 | bool enable_ocsp_stapling = true; |
| 58 | // If true, enables the (unused) signed certificate timestamp TLS extension. |
| 59 | bool enable_signed_cert_timestamp = true; |
| 60 | // If true, enables the (unused) channel ID TLS extension. |
| 61 | bool enable_tls_channel_id = false; |
| 62 | // If true, enables the (unused) GREASE TLS extension. |
| 63 | bool enable_grease = false; |
| 64 | // Indicates how to process incoming certificates. |
| 65 | TlsCertPolicy tls_cert_policy = TlsCertPolicy::TLS_CERT_POLICY_SECURE; |
| 66 | // If set, indicates the highest supported SSL version. |
| 67 | absl::optional<int> max_ssl_version; |
| 68 | // If set, indicates the list of protocols to be used in the TLS ALPN |
| 69 | // extension. |
| 70 | absl::optional<std::vector<std::string>> tls_alpn_protocols; |
| 71 | // If set, indicates the list of curves to be used in the TLS elliptic curves |
| 72 | // extension. |
| 73 | absl::optional<std::vector<std::string>> tls_elliptic_curves; |
| 74 | }; |
| 75 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 76 | // Class for creating SSL adapters with shared state, e.g., a session cache, |
| 77 | // which allows clients to resume SSL sessions to previously-contacted hosts. |
| 78 | // Clients should create the factory using Create(), set up the factory as |
| 79 | // needed using SetMode, and then call CreateAdapter to create adapters when |
| 80 | // needed. |
| 81 | class SSLAdapterFactory { |
| 82 | public: |
| 83 | virtual ~SSLAdapterFactory() {} |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 84 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 85 | // Specifies whether TLS or DTLS is to be used for the SSL adapters. |
| 86 | virtual void SetMode(SSLMode mode) = 0; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 87 | |
| 88 | // Specify a custom certificate verifier for SSL. |
| 89 | virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0; |
| 90 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 91 | // Creates a new SSL adapter, but from a shared context. |
| 92 | virtual SSLAdapter* CreateAdapter(AsyncSocket* socket) = 0; |
| 93 | |
| 94 | static SSLAdapterFactory* Create(); |
| 95 | }; |
| 96 | |
| 97 | // Class that abstracts a client-to-server SSL session. It can be created |
| 98 | // standalone, via SSLAdapter::Create, or through a factory as described above, |
| 99 | // in which case it will share state with other SSLAdapters created from the |
| 100 | // same factory. |
| 101 | // After creation, call StartSSL to initiate the SSL handshake to the server. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 102 | class SSLAdapter : public AsyncSocketAdapter { |
| 103 | public: |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 104 | explicit SSLAdapter(AsyncSocket* socket) : AsyncSocketAdapter(socket) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 105 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame^] | 106 | // Sets the SSL configuration for this session. |
| 107 | virtual void SetSSLConfig(const SSLConfig& ssl_config) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | |
| 109 | // Do DTLS or TLS (default is TLS, if unspecified) |
| 110 | virtual void SetMode(SSLMode mode) = 0; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 111 | // Specify a custom certificate verifier for SSL. |
| 112 | virtual void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 113 | |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 114 | // Set the certificate this socket will present to incoming clients. |
| 115 | virtual void SetIdentity(SSLIdentity* identity) = 0; |
| 116 | |
| 117 | // Choose whether the socket acts as a server socket or client socket. |
| 118 | virtual void SetRole(SSLRole role) = 0; |
| 119 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 120 | // StartSSL returns 0 if successful. |
| 121 | // If StartSSL is called while the socket is closed or connecting, the SSL |
| 122 | // negotiation will begin as soon as the socket connects. |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 123 | // TODO(juberti): Remove |restartable|. |
| 124 | virtual int StartSSL(const char* hostname, bool restartable = false) = 0; |
| 125 | |
| 126 | // When an SSLAdapterFactory is used, an SSLAdapter may be used to resume |
| 127 | // a previous SSL session, which results in an abbreviated handshake. |
| 128 | // This method, if called after SSL has been established for this adapter, |
| 129 | // indicates whether the current session is a resumption of a previous |
| 130 | // session. |
| 131 | virtual bool IsResumedSession() = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 132 | |
| 133 | // Create the default SSL adapter for this platform. On failure, returns null |
| 134 | // and deletes |socket|. Otherwise, the returned SSLAdapter takes ownership |
| 135 | // of |socket|. |
| 136 | static SSLAdapter* Create(AsyncSocket* socket); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 141 | // Call this on the main thread, before using SSL. |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 142 | // Call CleanupSSL when finished with SSL. |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 143 | bool InitializeSSL(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 144 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 145 | // Call to cleanup additional threads, and also the main thread. |
| 146 | bool CleanupSSL(); |
| 147 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 148 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 149 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 150 | #endif // RTC_BASE_SSLADAPTER_H_ |