henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_OPENSSL_STREAM_ADAPTER_H_ |
| 12 | #define RTC_BASE_OPENSSL_STREAM_ADAPTER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 14 | #include <openssl/ossl_typ.h> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 15 | #include <stddef.h> |
| 16 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 18 | #include <memory> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 19 | #include <string> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 20 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 21 | |
Guido Urdaneta | 14bba6e | 2020-09-25 14:00:51 | [diff] [blame] | 22 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "rtc_base/buffer.h" |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 24 | #ifdef OPENSSL_IS_BORINGSSL |
| 25 | #include "rtc_base/boringssl_identity.h" |
| 26 | #else |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 27 | #include "rtc_base/openssl_identity.h" |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 28 | #endif |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 29 | #include "rtc_base/ssl_identity.h" |
| 30 | #include "rtc_base/ssl_stream_adapter.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 31 | #include "rtc_base/stream.h" |
Guido Urdaneta | 14bba6e | 2020-09-25 14:00:51 | [diff] [blame] | 32 | #include "rtc_base/system/rtc_export.h" |
Tommi | 0448298 | 2020-10-05 12:43:53 | [diff] [blame] | 33 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
| 34 | #include "rtc_base/task_utils/repeating_task.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 35 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 36 | namespace rtc { |
| 37 | |
| 38 | // This class was written with OpenSSLAdapter (a socket adapter) as a |
| 39 | // starting point. It has similar structure and functionality, but uses a |
| 40 | // "peer-to-peer" mode, verifying the peer's certificate using a digest |
| 41 | // sent over a secure signaling channel. |
| 42 | // |
| 43 | // Static methods to initialize and deinit the SSL library are in |
| 44 | // OpenSSLAdapter. These should probably be moved out to a neutral class. |
| 45 | // |
| 46 | // In a few cases I have factored out some OpenSSLAdapter code into static |
| 47 | // methods so it can be reused from this class. Eventually that code should |
| 48 | // probably be moved to a common support class. Unfortunately there remain a |
| 49 | // few duplicated sections of code. I have not done more restructuring because |
| 50 | // I did not want to affect existing code that uses OpenSSLAdapter. |
| 51 | // |
| 52 | // This class does not support the SSL connection restart feature present in |
| 53 | // OpenSSLAdapter. I am not entirely sure how the feature is useful and I am |
| 54 | // not convinced that it works properly. |
| 55 | // |
| 56 | // This implementation is careful to disallow data exchange after an SSL error, |
| 57 | // and it has an explicit SSL_CLOSED state. It should not be possible to send |
| 58 | // any data in clear after one of the StartSSL methods has been called. |
| 59 | |
| 60 | // Look in sslstreamadapter.h for documentation of the methods. |
| 61 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 62 | class SSLCertChain; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 63 | |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | |
Guido Urdaneta | 14bba6e | 2020-09-25 14:00:51 | [diff] [blame] | 66 | // If |allow| has a value, its value determines if legacy TLS protocols are |
| 67 | // allowed, overriding the default configuration. |
| 68 | // If |allow| has no value, any previous override is removed and the default |
| 69 | // configuration is restored. |
| 70 | RTC_EXPORT void SetAllowLegacyTLSProtocols(const absl::optional<bool>& allow); |
| 71 | |
Benjamin Wright | 61c5cc8 | 2018-10-27 00:50:00 | [diff] [blame] | 72 | class OpenSSLStreamAdapter final : public SSLStreamAdapter { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 73 | public: |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 74 | explicit OpenSSLStreamAdapter(std::unique_ptr<StreamInterface> stream); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 75 | ~OpenSSLStreamAdapter() override; |
| 76 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 77 | void SetIdentity(std::unique_ptr<SSLIdentity> identity) override; |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 78 | SSLIdentity* GetIdentityForTesting() const override; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 79 | |
| 80 | // Default argument is for compatibility |
| 81 | void SetServerRole(SSLRole role = SSL_SERVER) override; |
| 82 | bool SetPeerCertificateDigest( |
| 83 | const std::string& digest_alg, |
| 84 | const unsigned char* digest_val, |
| 85 | size_t digest_len, |
| 86 | SSLPeerCertificateDigestError* error = nullptr) override; |
| 87 | |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 88 | std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const override; |
| 89 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 90 | // Goes from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT, depending |
| 91 | // on whether the underlying stream is already open or not. |
| 92 | int StartSSL() override; |
| 93 | void SetMode(SSLMode mode) override; |
| 94 | void SetMaxProtocolVersion(SSLProtocolVersion version) override; |
| 95 | void SetInitialRetransmissionTimeout(int timeout_ms) override; |
| 96 | |
| 97 | StreamResult Read(void* data, |
| 98 | size_t data_len, |
| 99 | size_t* read, |
| 100 | int* error) override; |
| 101 | StreamResult Write(const void* data, |
| 102 | size_t data_len, |
| 103 | size_t* written, |
| 104 | int* error) override; |
| 105 | void Close() override; |
| 106 | StreamState GetState() const override; |
| 107 | |
| 108 | // TODO(guoweis): Move this away from a static class method. |
| 109 | static std::string SslCipherSuiteToName(int crypto_suite); |
| 110 | |
| 111 | bool GetSslCipherSuite(int* cipher) override; |
| 112 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 08:51:17 | [diff] [blame] | 113 | SSLProtocolVersion GetSslVersion() const override; |
| 114 | bool GetSslVersionBytes(int* version) const override; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 115 | // Key Extractor interface |
| 116 | bool ExportKeyingMaterial(const std::string& label, |
| 117 | const uint8_t* context, |
| 118 | size_t context_len, |
| 119 | bool use_context, |
| 120 | uint8_t* result, |
| 121 | size_t result_len) override; |
| 122 | |
| 123 | // DTLS-SRTP interface |
| 124 | bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites) override; |
| 125 | bool GetDtlsSrtpCryptoSuite(int* crypto_suite) override; |
| 126 | |
| 127 | bool IsTlsConnected() override; |
| 128 | |
| 129 | // Capabilities interfaces. |
| 130 | static bool IsBoringSsl(); |
| 131 | |
| 132 | static bool IsAcceptableCipher(int cipher, KeyType key_type); |
| 133 | static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type); |
| 134 | |
| 135 | // Use our timeutils.h source of timing in BoringSSL, allowing us to test |
| 136 | // using a fake clock. |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 137 | static void EnableTimeCallbackForTesting(); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 138 | |
| 139 | protected: |
| 140 | void OnEvent(StreamInterface* stream, int events, int err) override; |
| 141 | |
| 142 | private: |
| 143 | enum SSLState { |
| 144 | // Before calling one of the StartSSL methods, data flows |
| 145 | // in clear text. |
| 146 | SSL_NONE, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 147 | SSL_WAIT, // waiting for the stream to open to start SSL negotiation |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 148 | SSL_CONNECTING, // SSL negotiation in progress |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 149 | SSL_CONNECTED, // SSL stream successfully established |
| 150 | SSL_ERROR, // some SSL error occurred, stream is closed |
| 151 | SSL_CLOSED // Clean close |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 152 | }; |
| 153 | |
Tommi | 0448298 | 2020-10-05 12:43:53 | [diff] [blame] | 154 | void PostEvent(int events, int err); |
| 155 | void SetTimeout(int delay_ms); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 156 | |
| 157 | // The following three methods return 0 on success and a negative |
| 158 | // error code on failure. The error code may be from OpenSSL or -1 |
| 159 | // on some other error cases, so it can't really be interpreted |
| 160 | // unfortunately. |
| 161 | |
| 162 | // Prepare SSL library, state is SSL_CONNECTING. |
| 163 | int BeginSSL(); |
| 164 | // Perform SSL negotiation steps. |
| 165 | int ContinueSSL(); |
| 166 | |
| 167 | // Error handler helper. signal is given as true for errors in |
| 168 | // asynchronous contexts (when an error method was not returned |
| 169 | // through some other method), and in that case an SE_CLOSE event is |
| 170 | // raised on the stream with the specified error. |
| 171 | // A 0 error means a graceful close, otherwise there is not really enough |
| 172 | // context to interpret the error code. |
| 173 | // |alert| indicates an alert description (one of the SSL_AD constants) to |
| 174 | // send to the remote endpoint when closing the association. If 0, a normal |
| 175 | // shutdown will be performed. |
| 176 | void Error(const char* context, int err, uint8_t alert, bool signal); |
| 177 | void Cleanup(uint8_t alert); |
| 178 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 179 | // Flush the input buffers by reading left bytes (for DTLS) |
| 180 | void FlushInput(unsigned int left); |
| 181 | |
| 182 | // SSL library configuration |
| 183 | SSL_CTX* SetupSSLContext(); |
| 184 | // Verify the peer certificate matches the signaled digest. |
| 185 | bool VerifyPeerCertificate(); |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 186 | |
| 187 | #ifdef OPENSSL_IS_BORINGSSL |
| 188 | // SSL certificate verification callback. See SSL_CTX_set_custom_verify. |
| 189 | static enum ssl_verify_result_t SSLVerifyCallback(SSL* ssl, |
| 190 | uint8_t* out_alert); |
| 191 | #else |
David Benjamin | dc24656 | 2017-09-29 16:14:08 | [diff] [blame] | 192 | // SSL certificate verification callback. See |
| 193 | // SSL_CTX_set_cert_verify_callback. |
| 194 | static int SSLVerifyCallback(X509_STORE_CTX* store, void* arg); |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 195 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 196 | |
Benjamin Wright | 5d35554 | 2018-10-27 00:57:00 | [diff] [blame] | 197 | bool WaitingToVerifyPeerCertificate() const { |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 198 | return GetClientAuthEnabled() && !peer_certificate_verified_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 199 | } |
| 200 | |
Benjamin Wright | 5d35554 | 2018-10-27 00:57:00 | [diff] [blame] | 201 | bool HasPeerCertificateDigest() const { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 202 | return !peer_certificate_digest_algorithm_.empty() && |
| 203 | !peer_certificate_digest_value_.empty(); |
| 204 | } |
| 205 | |
Tommi | 0448298 | 2020-10-05 12:43:53 | [diff] [blame] | 206 | rtc::Thread* const owner_; |
| 207 | webrtc::ScopedTaskSafety task_safety_; |
| 208 | webrtc::RepeatingTaskHandle timeout_task_; |
| 209 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 210 | SSLState state_; |
| 211 | SSLRole role_; |
| 212 | int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED |
| 213 | // Whether the SSL negotiation is blocked on needing to read or |
| 214 | // write to the wrapped stream. |
| 215 | bool ssl_read_needs_write_; |
| 216 | bool ssl_write_needs_read_; |
| 217 | |
| 218 | SSL* ssl_; |
| 219 | SSL_CTX* ssl_ctx_; |
| 220 | |
| 221 | // Our key and certificate. |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 222 | #ifdef OPENSSL_IS_BORINGSSL |
| 223 | std::unique_ptr<BoringSSLIdentity> identity_; |
| 224 | #else |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 225 | std::unique_ptr<OpenSSLIdentity> identity_; |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 226 | #endif |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 227 | // The certificate chain that the peer presented. Initially null, until the |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 228 | // connection is established. |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 229 | std::unique_ptr<SSLCertChain> peer_cert_chain_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 230 | bool peer_certificate_verified_ = false; |
| 231 | // The digest of the certificate that the peer must present. |
| 232 | Buffer peer_certificate_digest_value_; |
| 233 | std::string peer_certificate_digest_algorithm_; |
| 234 | |
| 235 | // The DtlsSrtp ciphers |
| 236 | std::string srtp_ciphers_; |
| 237 | |
| 238 | // Do DTLS or not |
| 239 | SSLMode ssl_mode_; |
| 240 | |
| 241 | // Max. allowed protocol version |
| 242 | SSLProtocolVersion ssl_max_version_; |
| 243 | |
| 244 | // A 50-ms initial timeout ensures rapid setup on fast connections, but may |
| 245 | // be too aggressive for low bandwidth links. |
| 246 | int dtls_handshake_timeout_ms_ = 50; |
Harald Alvestrand | 1379913 | 2020-03-09 18:39:36 | [diff] [blame] | 247 | |
| 248 | // TODO(https://bugs.webrtc.org/10261): Completely remove this option in M84. |
| 249 | const bool support_legacy_tls_protocols_flag_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | ///////////////////////////////////////////////////////////////////////////// |
| 253 | |
| 254 | } // namespace rtc |
| 255 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 256 | #endif // RTC_BASE_OPENSSL_STREAM_ADAPTER_H_ |