| 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 |
| 11 | #ifndef RTC_BASE_OPENSSL_ADAPTER_H_ |
| 12 | #define RTC_BASE_OPENSSL_ADAPTER_H_ |
| 13 |
| 14 | #include <openssl/ossl_typ.h> |
| 15 | #include <stddef.h> |
| 16 | #include <stdint.h> |
| 17 |
| 18 | #include <memory> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 |
| 22 | #include "absl/strings/string_view.h" |
| 23 | #include "api/task_queue/pending_task_safety_flag.h" |
| 24 | #include "rtc_base/buffer.h" |
| 25 | #ifdef OPENSSL_IS_BORINGSSL |
| 26 | #include "rtc_base/boringssl_identity.h" |
| 27 | #else |
| 28 | #include "rtc_base/openssl_identity.h" |
| 29 | #endif |
| 30 | #include "rtc_base/openssl_session_cache.h" |
| 31 | #include "rtc_base/socket.h" |
| 32 | #include "rtc_base/socket_address.h" |
| 33 | #include "rtc_base/ssl_adapter.h" |
| 34 | #include "rtc_base/ssl_certificate.h" |
| 35 | #include "rtc_base/ssl_identity.h" |
| 36 | #include "rtc_base/ssl_stream_adapter.h" |
| 37 |
| 38 | namespace rtc { |
| 39 |
| 40 | class OpenSSLAdapter final : public SSLAdapter { |
| 41 | public: |
| 42 | static bool InitializeSSL(); |
| 43 | static bool CleanupSSL(); |
| 44 |
| 45 | // Creating an OpenSSLAdapter requires a socket to bind to, an optional |
| 46 | // session cache if you wish to improve performance by caching sessions for |
| 47 | // hostnames you have previously connected to and an optional |
| 48 | // SSLCertificateVerifier which can override any existing trusted roots to |
| 49 | // validate a peer certificate. The cache and verifier are effectively |
| 50 | // immutable after the the SSL connection starts. |
| 51 | explicit OpenSSLAdapter(Socket* socket, |
| 52 | OpenSSLSessionCache* ssl_session_cache = nullptr, |
| 53 | SSLCertificateVerifier* ssl_cert_verifier = nullptr); |
| 54 | ~OpenSSLAdapter() override; |
| 55 |
| 56 | void SetIgnoreBadCert(bool ignore) override; |
| 57 | void SetAlpnProtocols(const std::vector<std::string>& protos) override; |
| 58 | void SetEllipticCurves(const std::vector<std::string>& curves) override; |
| 59 | void SetMode(SSLMode mode) override; |
| 60 | void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) override; |
| 61 | void SetIdentity(std::unique_ptr<SSLIdentity> identity) override; |
| 62 | void SetRole(SSLRole role) override; |
| 63 | int StartSSL(absl::string_view hostname) override; |
| 64 | int Send(const void* pv, size_t cb) override; |
| 65 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; |
| 66 | int Recv(void* pv, size_t cb, int64_t* timestamp) override; |
| 67 | int RecvFrom(void* pv, |
| 68 | size_t cb, |
| 69 | SocketAddress* paddr, |
| 70 | int64_t* timestamp) override; |
| 71 | int Close() override; |
| 72 | // Note that the socket returns ST_CONNECTING while SSL is being negotiated. |
| 73 | ConnState GetState() const override; |
| 74 | bool IsResumedSession() override; |
| 75 | // Creates a new SSL_CTX object, configured for client-to-server usage |
| 76 | // with SSLMode `mode`, and if `enable_cache` is true, with support for |
| 77 | // storing successful sessions so that they can be later resumed. |
| 78 | // OpenSSLAdapterFactory will call this method to create its own internal |
| 79 | // SSL_CTX, and OpenSSLAdapter will also call this when used without a |
| 80 | // factory. |
| 81 | static SSL_CTX* CreateContext(SSLMode mode, bool enable_cache); |
| 82 |
| 83 | protected: |
| 84 | void OnConnectEvent(Socket* socket) override; |
| 85 | void OnReadEvent(Socket* socket) override; |
| 86 | void OnWriteEvent(Socket* socket) override; |
| 87 | void OnCloseEvent(Socket* socket, int err) override; |
| 88 |
| 89 | private: |
| 90 | class EarlyExitCatcher { |
| 91 | public: |
| 92 | EarlyExitCatcher(OpenSSLAdapter& adapter_ptr); |
| 93 | void disable(); |
| 94 | ~EarlyExitCatcher(); |
| 95 |
| 96 | private: |
| 97 | bool disabled_ = false; |
| 98 | OpenSSLAdapter& adapter_ptr_; |
| 99 | }; |
| 100 | enum SSLState { |
| 101 | SSL_NONE, |
| 102 | SSL_WAIT, |
| 103 | SSL_CONNECTING, |
| 104 | SSL_CONNECTED, |
| 105 | SSL_ERROR |
| 106 | }; |
| 107 |
| 108 | int BeginSSL(); |
| 109 | int ContinueSSL(); |
| 110 | void Error(absl::string_view context, int err, bool signal = true); |
| 111 | void Cleanup(); |
| 112 | void OnTimeout(); |
| 113 |
| 114 | // Return value and arguments have the same meanings as for Send; `error` is |
| 115 | // an output parameter filled with the result of SSL_get_error. |
| 116 | int DoSslWrite(const void* pv, size_t cb, int* error); |
| 117 | bool SSLPostConnectionCheck(SSL* ssl, absl::string_view host); |
| 118 |
| 119 | // Logs info about the state of the SSL connection. |
| 120 | static void SSLInfoCallback(const SSL* ssl, int where, int ret); |
| 121 |
| 122 | #if defined(OPENSSL_IS_BORINGSSL) && \ |
| 123 | defined(WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS) |
| 124 | static enum ssl_verify_result_t SSLVerifyCallback(SSL* ssl, |
| 125 | uint8_t* out_alert); |
| 126 | enum ssl_verify_result_t SSLVerifyInternal(SSL* ssl, uint8_t* out_alert); |
| 127 | #else |
| 128 | static int SSLVerifyCallback(int ok, X509_STORE_CTX* store); |
| 129 | // Call a custom verifier, if installed. |
| 130 | // Returns 1 on success, `status_on_error` on error or verification failure. |
| 131 | int SSLVerifyInternal(int status_on_error, SSL* ssl, X509_STORE_CTX* store); |
| 132 | #endif |
| 133 | friend class OpenSSLStreamAdapter; // for custom_verify_callback_; |
| 134 |
| 135 | // If the SSL_CTX was created with `enable_cache` set to true, this callback |
| 136 | // will be called when a SSL session has been successfully established, |
| 137 | // to allow its SSL_SESSION* to be cached for later resumption. |
| 138 | static int NewSSLSessionCallback(SSL* ssl, SSL_SESSION* session); |
| 139 |
| 140 | // Optional SSL Shared session cache to improve performance. |
| 141 | OpenSSLSessionCache* ssl_session_cache_ = nullptr; |
| 142 | // Optional SSL Certificate verifier which can be set by a third party. |
| 143 | SSLCertificateVerifier* ssl_cert_verifier_ = nullptr; |
| 144 | // The current connection state of the (d)TLS connection. |
| 145 | SSLState state_; |
| 146 |
| 147 | #ifdef OPENSSL_IS_BORINGSSL |
| 148 | std::unique_ptr<BoringSSLIdentity> identity_; |
| 149 | #else |
| 150 | std::unique_ptr<OpenSSLIdentity> identity_; |
| 151 | #endif |
| 152 | // Indicates whethere this is a client or a server. |
| 153 | SSLRole role_; |
| 154 | bool ssl_read_needs_write_; |
| 155 | bool ssl_write_needs_read_; |
| 156 | // This buffer is used if SSL_write fails with SSL_ERROR_WANT_WRITE, which |
| 157 | // means we need to keep retrying with *the same exact data* until it |
| 158 | // succeeds. Afterwards it will be cleared. |
| 159 | Buffer pending_data_; |
| 160 | SSL* ssl_; |
| 161 | // Holds the SSL context, which may be shared if an session cache is provided. |
| 162 | SSL_CTX* ssl_ctx_; |
| 163 | // Hostname of server that is being connected, used for SNI. |
| 164 | std::string ssl_host_name_; |
| 165 | // Set the adapter to DTLS or TLS mode before creating the context. |
| 166 | SSLMode ssl_mode_; |
| 167 | // If true, the server certificate need not match the configured hostname. |
| 168 | bool ignore_bad_cert_; |
| 169 | // List of protocols to be used in the TLS ALPN extension. |
| 170 | std::vector<std::string> alpn_protocols_; |
| 171 | // List of elliptic curves to be used in the TLS elliptic curves extension. |
| 172 | std::vector<std::string> elliptic_curves_; |
| 173 | // Holds the result of the call to run of the ssl_cert_verify_->Verify() |
| 174 | bool custom_cert_verifier_status_; |
| 175 | // Flag to cancel pending timeout task. |
| 176 | webrtc::ScopedTaskSafety timer_; |
| 177 | }; |
| 178 |
| 179 | // The OpenSSLAdapterFactory is responsbile for creating multiple new |
| 180 | // OpenSSLAdapters with a shared SSL_CTX and a shared SSL_SESSION cache. The |
| 181 | // SSL_SESSION cache allows existing SSL_SESSIONS to be reused instead of |
| 182 | // recreating them leading to a significant performance improvement. |
| 183 | class OpenSSLAdapterFactory : public SSLAdapterFactory { |
| 184 | public: |
| 185 | OpenSSLAdapterFactory(); |
| 186 | ~OpenSSLAdapterFactory() override; |
| 187 | // Set the SSL Mode to use with this factory. This should only be set before |
| 188 | // the first adapter is created with the factory. If it is called after it |
| 189 | // will DCHECK. |
| 190 | void SetMode(SSLMode mode) override; |
| 191 |
| 192 | // Set a custom certificate verifier to be passed down to each instance |
| 193 | // created with this factory. This should only ever be set before the first |
| 194 | // call to the factory and cannot be changed after the fact. |
| 195 | void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) override; |
| 196 |
| 197 | void SetIdentity(std::unique_ptr<SSLIdentity> identity) override; |
| 198 |
| 199 | // Choose whether the socket acts as a server socket or client socket. |
| 200 | void SetRole(SSLRole role) override; |
| 201 |
| 202 | // Methods that control server certificate verification, used in unit tests. |
| 203 | // Do not call these methods in production code. |
| 204 | void SetIgnoreBadCert(bool ignore) override; |
| 205 |
| 206 | // Constructs a new socket using the shared OpenSSLSessionCache. This means |
| 207 | // existing SSLSessions already in the cache will be reused instead of |
| 208 | // re-created for improved performance. |
| 209 | OpenSSLAdapter* CreateAdapter(Socket* socket) override; |
| 210 |
| 211 | private: |
| 212 | // Holds the SSLMode (DTLS,TLS) that will be used to set the session cache. |
| 213 | SSLMode ssl_mode_ = SSL_MODE_TLS; |
| 214 | SSLRole ssl_role_ = SSL_CLIENT; |
| 215 | bool ignore_bad_cert_ = false; |
| 216 |
| 217 | std::unique_ptr<SSLIdentity> identity_; |
| 218 |
| 219 | // Holds a cache of existing SSL Sessions. |
| 220 | std::unique_ptr<OpenSSLSessionCache> ssl_session_cache_; |
| 221 | // Provides an optional custom callback for verifying SSL certificates, this |
| 222 | // in currently only used for TLS-TURN connections. |
| 223 | SSLCertificateVerifier* ssl_cert_verifier_ = nullptr; |
| 224 | // TODO(benwright): Remove this when context is moved to OpenSSLCommon. |
| 225 | // Hold a friend class to the OpenSSLAdapter to retrieve the context. |
| 226 | friend class OpenSSLAdapter; |
| 227 | }; |
| 228 |
| 229 | // The EarlyExitCatcher is responsible for calling OpenSSLAdapter::Cleanup on |
| 230 | // destruction. By doing this we have scoped cleanup which can be disabled if |
| 231 | // there were no errors, aka early exits. |
| 232 |
| 233 | std::string TransformAlpnProtocols(const std::vector<std::string>& protos); |
| 234 |
| 235 | } // namespace rtc |
| 236 |
| 237 | #endif // RTC_BASE_OPENSSL_ADAPTER_H_ |
| 238 |