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_SSL_STREAM_ADAPTER_H_ |
| 12 | #define RTC_BASE_SSL_STREAM_ADAPTER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 20 | |
Tommi | 59574ca | 2023-09-05 07:21:57 | [diff] [blame] | 21 | #include "absl/functional/any_invocable.h" |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 22 | #include "absl/memory/memory.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 23 | #include "absl/strings/string_view.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 24 | #include "rtc_base/ssl_certificate.h" |
| 25 | #include "rtc_base/ssl_identity.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "rtc_base/stream.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 27 | |
| 28 | namespace rtc { |
| 29 | |
| 30 | // Constants for SSL profile. |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 31 | constexpr int kTlsNullWithNullNull = 0; |
| 32 | constexpr int kSslCipherSuiteMaxValue = 0xFFFF; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | |
Mirko Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 34 | // Constants for SRTP profiles. |
| 35 | constexpr int kSrtpInvalidCryptoSuite = 0; |
| 36 | constexpr int kSrtpAes128CmSha1_80 = 0x0001; |
| 37 | constexpr int kSrtpAes128CmSha1_32 = 0x0002; |
| 38 | constexpr int kSrtpAeadAes128Gcm = 0x0007; |
| 39 | constexpr int kSrtpAeadAes256Gcm = 0x0008; |
| 40 | constexpr int kSrtpCryptoSuiteMaxValue = 0xFFFF; |
| 41 | |
Philipp Hancke | 36e4dd2 | 2023-09-28 09:02:21 | [diff] [blame] | 42 | // Constants for SSL signature algorithms. |
| 43 | constexpr int kSslSignatureAlgorithmUnknown = 0; |
| 44 | constexpr int kSslSignatureAlgorithmMaxValue = 0xFFFF; |
| 45 | |
Björn Terelius | e71fa4e | 2024-06-25 09:55:12 | [diff] [blame] | 46 | // Names of SRTP profiles listed above. |
| 47 | // 128-bit AES with 80-bit SHA-1 HMAC. |
| 48 | extern const char kCsAesCm128HmacSha1_80[]; |
| 49 | // 128-bit AES with 32-bit SHA-1 HMAC. |
| 50 | extern const char kCsAesCm128HmacSha1_32[]; |
| 51 | // 128-bit AES GCM with 16 byte AEAD auth tag. |
| 52 | extern const char kCsAeadAes128Gcm[]; |
| 53 | // 256-bit AES GCM with 16 byte AEAD auth tag. |
| 54 | extern const char kCsAeadAes256Gcm[]; |
| 55 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 56 | // Given the DTLS-SRTP protection profile ID, as defined in |
| 57 | // https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile |
| 58 | // name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2. |
| 59 | std::string SrtpCryptoSuiteToName(int crypto_suite); |
| 60 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 61 | // Get key length and salt length for given crypto suite. Returns true for |
| 62 | // valid suites, otherwise false. |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 63 | bool GetSrtpKeyAndSaltLengths(int crypto_suite, |
| 64 | int* key_length, |
| 65 | int* salt_length); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 66 | |
| 67 | // Returns true if the given crypto suite id uses a GCM cipher. |
| 68 | bool IsGcmCryptoSuite(int crypto_suite); |
| 69 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 70 | // SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS. |
| 71 | // After SSL has been started, the stream will only open on successful |
| 72 | // SSL verification of certificates, and the communication is |
| 73 | // encrypted of course. |
| 74 | // |
| 75 | // This class was written with SSLAdapter as a starting point. It |
| 76 | // offers a similar interface, with two differences: there is no |
| 77 | // support for a restartable SSL connection, and this class has a |
| 78 | // peer-to-peer mode. |
| 79 | // |
| 80 | // The SSL library requires initialization and cleanup. Static method |
| 81 | // for doing this are in SSLAdapter. They should possibly be moved out |
| 82 | // to a neutral class. |
| 83 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 84 | enum SSLRole { SSL_CLIENT, SSL_SERVER }; |
| 85 | enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS }; |
Harald Alvestrand | 1379913 | 2020-03-09 18:39:36 | [diff] [blame] | 86 | |
Philipp Hancke | ed18014 | 2024-06-11 16:42:51 | [diff] [blame] | 87 | // TODO bugs.webrtc.org/40644300 remove unused legacy constants. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 88 | enum SSLProtocolVersion { |
Harald Alvestrand | 5cb7807 | 2019-10-28 08:51:17 | [diff] [blame] | 89 | SSL_PROTOCOL_NOT_GIVEN = -1, |
Philipp Hancke | ed18014 | 2024-06-11 16:42:51 | [diff] [blame] | 90 | SSL_PROTOCOL_TLS_10 = 0, // Deprecated and no longer supported. |
| 91 | SSL_PROTOCOL_TLS_11 = 1, // Deprecated and no longer supported. |
| 92 | SSL_PROTOCOL_TLS_12 = 2, |
| 93 | SSL_PROTOCOL_DTLS_10 = 1, // Deprecated and no longer supported. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 94 | SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12, |
| 95 | }; |
| 96 | enum class SSLPeerCertificateDigestError { |
| 97 | NONE, |
| 98 | UNKNOWN_ALGORITHM, |
| 99 | INVALID_LENGTH, |
| 100 | VERIFICATION_FAILED, |
| 101 | }; |
| 102 | |
| 103 | // Errors for Read -- in the high range so no conflict with OpenSSL. |
| 104 | enum { SSE_MSG_TRUNC = 0xff0001 }; |
| 105 | |
| 106 | // Used to send back UMA histogram value. Logged when Dtls handshake fails. |
| 107 | enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE }; |
| 108 | |
Tommi | 48df56e | 2023-09-05 11:16:47 | [diff] [blame] | 109 | class SSLStreamAdapter : public StreamInterface { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 110 | public: |
| 111 | // Instantiate an SSLStreamAdapter wrapping the given stream, |
| 112 | // (using the selected implementation for the platform). |
| 113 | // Caller is responsible for freeing the returned object. |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 114 | static std::unique_ptr<SSLStreamAdapter> Create( |
Tommi | 59574ca | 2023-09-05 07:21:57 | [diff] [blame] | 115 | std::unique_ptr<StreamInterface> stream, |
| 116 | absl::AnyInvocable<void(SSLHandshakeError)> handshake_error = nullptr); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 117 | |
Niels Möller | 0131a4d | 2021-04-16 07:16:21 | [diff] [blame] | 118 | SSLStreamAdapter() = default; |
| 119 | ~SSLStreamAdapter() override = default; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 120 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 121 | // Specify our SSL identity: key and certificate. SSLStream takes ownership |
| 122 | // of the SSLIdentity object and will free it when appropriate. Should be |
| 123 | // called no more than once on a given SSLStream instance. |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 124 | virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0; |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 125 | virtual SSLIdentity* GetIdentityForTesting() const = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 126 | |
| 127 | // Call this to indicate that we are to play the server role (or client role, |
| 128 | // if the default argument is replaced by SSL_CLIENT). |
| 129 | // The default argument is for backward compatibility. |
| 130 | // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function |
| 131 | virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0; |
| 132 | |
| 133 | // Do DTLS or TLS. |
| 134 | virtual void SetMode(SSLMode mode) = 0; |
| 135 | |
| 136 | // Set maximum supported protocol version. The highest version supported by |
| 137 | // both ends will be used for the connection, i.e. if one party supports |
| 138 | // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used. |
| 139 | // If requested version is not supported by underlying crypto library, the |
| 140 | // next lower will be used. |
| 141 | virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0; |
| 142 | |
| 143 | // Set the initial retransmission timeout for DTLS messages. When the timeout |
| 144 | // expires, the message gets retransmitted and the timeout is exponentially |
| 145 | // increased. |
| 146 | // This should only be called before StartSSL(). |
| 147 | virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0; |
| 148 | |
| 149 | // StartSSL starts negotiation with a peer, whose certificate is verified |
| 150 | // using the certificate digest. Generally, SetIdentity() and possibly |
| 151 | // SetServerRole() should have been called before this. |
| 152 | // SetPeerCertificateDigest() must also be called. It may be called after |
| 153 | // StartSSLWithPeer() but must be called before the underlying stream opens. |
| 154 | // |
| 155 | // Use of the stream prior to calling StartSSL will pass data in clear text. |
| 156 | // Calling StartSSL causes SSL negotiation to begin as soon as possible: right |
| 157 | // away if the underlying wrapped stream is already opened, or else as soon as |
| 158 | // it opens. |
| 159 | // |
| 160 | // StartSSL returns a negative error code on failure. Returning 0 means |
| 161 | // success so far, but negotiation is probably not complete and will continue |
| 162 | // asynchronously. In that case, the exposed stream will open after |
| 163 | // successful negotiation and verification, or an SE_CLOSE event will be |
| 164 | // raised if negotiation fails. |
| 165 | virtual int StartSSL() = 0; |
| 166 | |
| 167 | // Specify the digest of the certificate that our peer is expected to use. |
| 168 | // Only this certificate will be accepted during SSL verification. The |
| 169 | // certificate is assumed to have been obtained through some other secure |
| 170 | // channel (such as the signaling channel). This must specify the terminal |
| 171 | // certificate, not just a CA. SSLStream makes a copy of the digest value. |
| 172 | // |
| 173 | // Returns true if successful. |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 174 | // `error` is optional and provides more information about the failure. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 175 | virtual bool SetPeerCertificateDigest( |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 176 | absl::string_view digest_alg, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 177 | const unsigned char* digest_val, |
| 178 | size_t digest_len, |
| 179 | SSLPeerCertificateDigestError* error = nullptr) = 0; |
| 180 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 181 | // Retrieves the peer's certificate chain including leaf certificate, if a |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 182 | // connection has been established. |
| 183 | virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0; |
| 184 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 185 | // Retrieves the IANA registration id of the cipher suite used for the |
| 186 | // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA"). |
| 187 | virtual bool GetSslCipherSuite(int* cipher_suite); |
| 188 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 08:51:17 | [diff] [blame] | 189 | // Retrieves the enum value for SSL version. |
| 190 | // Will return -1 until the version has been negotiated. |
Philipp Hancke | ed18014 | 2024-06-11 16:42:51 | [diff] [blame] | 191 | [[deprecated("Use GetSslVersionBytes")]] virtual SSLProtocolVersion |
| 192 | GetSslVersion() const = 0; |
Harald Alvestrand | 5cb7807 | 2019-10-28 08:51:17 | [diff] [blame] | 193 | // Retrieves the 2-byte version from the TLS protocol. |
| 194 | // Will return false until the version has been negotiated. |
| 195 | virtual bool GetSslVersionBytes(int* version) const = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 196 | |
| 197 | // Key Exporter interface from RFC 5705 |
| 198 | // Arguments are: |
| 199 | // label -- the exporter label. |
| 200 | // part of the RFC defining each exporter |
| 201 | // usage (IN) |
| 202 | // context/context_len -- a context to bind to for this connection; |
| 203 | // optional, can be null, 0 (IN) |
| 204 | // use_context -- whether to use the context value |
| 205 | // (needed to distinguish no context from |
| 206 | // zero-length ones). |
| 207 | // result -- where to put the computed value |
| 208 | // result_len -- the length of the computed value |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 209 | virtual bool ExportKeyingMaterial(absl::string_view label, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 210 | const uint8_t* context, |
| 211 | size_t context_len, |
| 212 | bool use_context, |
| 213 | uint8_t* result, |
| 214 | size_t result_len); |
| 215 | |
Philipp Hancke | 36e4dd2 | 2023-09-28 09:02:21 | [diff] [blame] | 216 | // Returns the signature algorithm or 0 if not applicable. |
| 217 | virtual uint16_t GetPeerSignatureAlgorithm() const = 0; |
| 218 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 219 | // DTLS-SRTP interface |
| 220 | virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites); |
| 221 | virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite); |
| 222 | |
| 223 | // Returns true if a TLS connection has been established. |
| 224 | // The only difference between this and "GetState() == SE_OPEN" is that if |
| 225 | // the peer certificate digest hasn't been verified, the state will still be |
| 226 | // SS_OPENING but IsTlsConnected should return true. |
| 227 | virtual bool IsTlsConnected() = 0; |
| 228 | |
| 229 | // Capabilities testing. |
| 230 | // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now |
| 231 | // that's assumed. |
| 232 | static bool IsBoringSsl(); |
| 233 | |
| 234 | // Returns true iff the supplied cipher is deemed to be strong. |
| 235 | // TODO(torbjorng): Consider removing the KeyType argument. |
| 236 | static bool IsAcceptableCipher(int cipher, KeyType key_type); |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 237 | static bool IsAcceptableCipher(absl::string_view cipher, KeyType key_type); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 238 | |
| 239 | // TODO(guoweis): Move this away from a static class method. Currently this is |
| 240 | // introduced such that any caller could depend on sslstreamadapter.h without |
| 241 | // depending on specific SSL implementation. |
| 242 | static std::string SslCipherSuiteToName(int cipher_suite); |
| 243 | |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 244 | //////////////////////////////////////////////////////////////////////////// |
| 245 | // Testing only member functions |
| 246 | //////////////////////////////////////////////////////////////////////////// |
| 247 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 248 | // Use our timeutils.h source of timing in BoringSSL, allowing us to test |
| 249 | // using a fake clock. |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 250 | static void EnableTimeCallbackForTesting(); |
| 251 | |
| 252 | // Deprecated. Do not use this API outside of testing. |
| 253 | // Do not set this to false outside of testing. |
| 254 | void SetClientAuthEnabledForTesting(bool enabled) { |
| 255 | client_auth_enabled_ = enabled; |
| 256 | } |
| 257 | |
| 258 | // Deprecated. Do not use this API outside of testing. |
| 259 | // Returns true by default, else false if explicitly set to disable client |
| 260 | // authentication. |
| 261 | bool GetClientAuthEnabled() const { return client_auth_enabled_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 262 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 263 | private: |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 264 | // If true (default), the client is required to provide a certificate during |
| 265 | // handshake. If no certificate is given, handshake fails. This applies to |
| 266 | // server mode only. |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 267 | bool client_auth_enabled_ = true; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 271 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 272 | #endif // RTC_BASE_SSL_STREAM_ADAPTER_H_ |