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_OPENSSLADAPTER_H_ |
| 12 | #define RTC_BASE_OPENSSLADAPTER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 14 | #include <openssl/ossl_typ.h> |
| 15 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 16 | #include <map> |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 17 | #include <memory> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | #include <string> |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 21 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/buffer.h" |
| 23 | #include "rtc_base/messagehandler.h" |
| 24 | #include "rtc_base/messagequeue.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 25 | #include "rtc_base/opensslcertificate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/opensslidentity.h" |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 27 | #include "rtc_base/opensslsessioncache.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/ssladapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 30 | namespace rtc { |
| 31 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 32 | class OpenSSLAdapter : public SSLAdapter, public MessageHandler { |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 33 | public: |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 34 | static bool InitializeSSL(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 35 | static bool CleanupSSL(); |
| 36 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 37 | // Creating an OpenSSLAdapter requires a socket to bind to, an optional |
| 38 | // session cache if you wish to improve performance by caching sessions for |
| 39 | // hostnames you have previously connected to and an optional |
| 40 | // SSLCertificateVerifier which can override any existing trusted roots to |
| 41 | // validate a peer certificate. The cache and verifier are effectively |
| 42 | // immutable after the the SSL connection starts. |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 43 | explicit OpenSSLAdapter(AsyncSocket* socket, |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 44 | OpenSSLSessionCache* ssl_session_cache = nullptr, |
| 45 | SSLCertificateVerifier* ssl_cert_verifier = nullptr); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 46 | ~OpenSSLAdapter() override; |
| 47 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 48 | void SetSSLConfig(const SSLConfig& ssl_config) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | void SetMode(SSLMode mode) override; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 50 | void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) override; |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 51 | void SetIdentity(SSLIdentity* identity) override; |
| 52 | void SetRole(SSLRole role) override; |
| 53 | AsyncSocket* Accept(SocketAddress* paddr) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 54 | int StartSSL(const char* hostname, bool restartable) override; |
| 55 | int Send(const void* pv, size_t cb) override; |
| 56 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; |
| 57 | int Recv(void* pv, size_t cb, int64_t* timestamp) override; |
| 58 | int RecvFrom(void* pv, |
| 59 | size_t cb, |
| 60 | SocketAddress* paddr, |
| 61 | int64_t* timestamp) override; |
| 62 | int Close() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | // Note that the socket returns ST_CONNECTING while SSL is being negotiated. |
| 64 | ConnState GetState() const override; |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 65 | bool IsResumedSession() override; |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 66 | // Creates a new SSL_CTX object, configured for client-to-server usage |
| 67 | // with SSLMode |mode|, and if |enable_cache| is true, with support for |
| 68 | // storing successful sessions so that they can be later resumed. |
| 69 | // OpenSSLAdapterFactory will call this method to create its own internal |
| 70 | // SSL_CTX, and OpenSSLAdapter will also call this when used without a |
| 71 | // factory. |
| 72 | static SSL_CTX* CreateContext(SSLMode mode, bool enable_cache); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 73 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 74 | protected: |
| 75 | void OnConnectEvent(AsyncSocket* socket) override; |
| 76 | void OnReadEvent(AsyncSocket* socket) override; |
| 77 | void OnWriteEvent(AsyncSocket* socket) override; |
| 78 | void OnCloseEvent(AsyncSocket* socket, int err) override; |
| 79 | |
| 80 | private: |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 81 | enum SSLState { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 82 | SSL_NONE, |
| 83 | SSL_WAIT, |
| 84 | SSL_CONNECTING, |
| 85 | SSL_CONNECTED, |
| 86 | SSL_ERROR |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | enum { MSG_TIMEOUT }; |
| 90 | |
| 91 | int BeginSSL(); |
| 92 | int ContinueSSL(); |
| 93 | void Error(const char* context, int err, bool signal = true); |
| 94 | void Cleanup(); |
| 95 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 96 | // If true, the server certificate need not match the configured hostname. |
| 97 | bool ShouldIgnoreBadCert(); |
| 98 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | // Return value and arguments have the same meanings as for Send; |error| is |
| 100 | // an output parameter filled with the result of SSL_get_error. |
| 101 | int DoSslWrite(const void* pv, size_t cb, int* error); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 102 | void OnMessage(Message* msg) override; |
Benjamin Wright | 9201d1a | 2018-04-05 12:12:26 -0700 | [diff] [blame] | 103 | bool SSLPostConnectionCheck(SSL* ssl, const std::string& host); |
| 104 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 105 | #if !defined(NDEBUG) |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 106 | // In debug builds, logs info about the state of the SSL connection. |
| 107 | static void SSLInfoCallback(const SSL* ssl, int where, int ret); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | #endif |
| 109 | static int SSLVerifyCallback(int ok, X509_STORE_CTX* store); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | friend class OpenSSLStreamAdapter; // for custom_verify_callback_; |
| 111 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 112 | // If the SSL_CTX was created with |enable_cache| set to true, this callback |
| 113 | // will be called when a SSL session has been successfully established, |
| 114 | // to allow its SSL_SESSION* to be cached for later resumption. |
| 115 | static int NewSSLSessionCallback(SSL* ssl, SSL_SESSION* session); |
| 116 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 117 | // Optional SSL Shared session cache to improve performance. |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 118 | OpenSSLSessionCache* ssl_session_cache_ = nullptr; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 119 | // Optional SSL Certificate verifier which can be set by a third party. |
| 120 | SSLCertificateVerifier* ssl_cert_verifier_ = nullptr; |
| 121 | // The current connection state of the (d)TLS connection. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 122 | SSLState state_; |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 123 | std::unique_ptr<OpenSSLIdentity> identity_; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 124 | // Indicates whethere this is a client or a server. |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 125 | SSLRole role_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 126 | bool ssl_read_needs_write_; |
| 127 | bool ssl_write_needs_read_; |
| 128 | // If true, socket will retain SSL configuration after Close. |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 129 | // TODO(juberti): Remove this unused flag. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 130 | bool restartable_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 131 | // This buffer is used if SSL_write fails with SSL_ERROR_WANT_WRITE, which |
| 132 | // means we need to keep retrying with *the same exact data* until it |
| 133 | // succeeds. Afterwards it will be cleared. |
| 134 | Buffer pending_data_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 135 | SSL* ssl_; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 136 | // Holds the SSL context, which may be shared if an session cache is provided. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 137 | SSL_CTX* ssl_ctx_; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 138 | // Hostname of server that is being connected, used for SNI. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 139 | std::string ssl_host_name_; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 140 | // Set the adapter to DTLS or TLS mode before creating the context. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 141 | SSLMode ssl_mode_; |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 142 | // SSL configuration for this session. |
| 143 | SSLConfig ssl_config_; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 144 | // Holds the result of the call to run of the ssl_cert_verify_->Verify() |
| 145 | bool custom_cert_verifier_status_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 148 | // The OpenSSLAdapterFactory is responsbile for creating multiple new |
| 149 | // OpenSSLAdapters with a shared SSL_CTX and a shared SSL_SESSION cache. The |
| 150 | // SSL_SESSION cache allows existing SSL_SESSIONS to be reused instead of |
| 151 | // recreating them leading to a significant performance improvement. |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 152 | class OpenSSLAdapterFactory : public SSLAdapterFactory { |
| 153 | public: |
| 154 | OpenSSLAdapterFactory(); |
| 155 | ~OpenSSLAdapterFactory() override; |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 156 | // Set the SSL Mode to use with this factory. This should only be set before |
| 157 | // the first adapter is created with the factory. If it is called after it |
| 158 | // will DCHECK. |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 159 | void SetMode(SSLMode mode) override; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 160 | // Set a custom certificate verifier to be passed down to each instance |
| 161 | // created with this factory. This should only ever be set before the first |
| 162 | // call to the factory and cannot be changed after the fact. |
| 163 | void SetCertVerifier(SSLCertificateVerifier* ssl_cert_verifier) override; |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 164 | // Constructs a new socket using the shared OpenSSLSessionCache. This means |
| 165 | // existing SSLSessions already in the cache will be reused instead of |
| 166 | // re-created for improved performance. |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 167 | OpenSSLAdapter* CreateAdapter(AsyncSocket* socket) override; |
Henrik Kjellander | c036276 | 2017-06-29 08:03:04 +0200 | [diff] [blame] | 168 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 169 | private: |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 170 | // Holds the SSLMode (DTLS,TLS) that will be used to set the session cache. |
| 171 | SSLMode ssl_mode_ = SSL_MODE_TLS; |
| 172 | // Holds a cache of existing SSL Sessions. |
| 173 | std::unique_ptr<OpenSSLSessionCache> ssl_session_cache_; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 174 | // Provides an optional custom callback for verifying SSL certificates, this |
| 175 | // in currently only used for TLS-TURN connections. |
| 176 | SSLCertificateVerifier* ssl_cert_verifier_ = nullptr; |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 177 | // TODO(benwright): Remove this when context is moved to OpenSSLCommon. |
| 178 | // Hold a friend class to the OpenSSLAdapter to retrieve the context. |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 179 | friend class OpenSSLAdapter; |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 182 | std::string TransformAlpnProtocols(const std::vector<std::string>& protos); |
| 183 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 184 | } // namespace rtc |
| 185 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 186 | #endif // RTC_BASE_OPENSSLADAPTER_H_ |