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