blob: fe115d6eb703aa2d3537fdd61b19ac8686564033 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_SSL_STREAM_ADAPTER_H_
12#define RTC_BASE_SSL_STREAM_ADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
15#include <stdint.h>
Ali Tofigh7fa90572022-03-17 14:47:4916
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <memory>
Philipp Hancke40607452024-11-26 18:50:2618#include <optional>
Henrik Kjellanderec78f1c2017-06-29 05:52:5019#include <string>
20#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:2621
Tommi59574ca2023-09-05 07:21:5722#include "absl/functional/any_invocable.h"
Ali Tofigh7fa90572022-03-17 14:47:4923#include "absl/strings/string_view.h"
Philipp Hancke40607452024-11-26 18:50:2624#include "api/array_view.h"
Jonas Oreland12574a32024-12-19 14:15:5025#include "api/field_trials_view.h"
Philipp Hancke40607452024-11-26 18:50:2626#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 17:11:0027#include "rtc_base/ssl_certificate.h"
28#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3129#include "rtc_base/stream.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5030
31namespace rtc {
32
33// Constants for SSL profile.
Mirko Bonadei7750d802021-07-26 15:27:4234constexpr int kTlsNullWithNullNull = 0;
35constexpr int kSslCipherSuiteMaxValue = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 05:52:5036
Mirko Bonadei7750d802021-07-26 15:27:4237// Constants for SRTP profiles.
38constexpr int kSrtpInvalidCryptoSuite = 0;
39constexpr int kSrtpAes128CmSha1_80 = 0x0001;
40constexpr int kSrtpAes128CmSha1_32 = 0x0002;
41constexpr int kSrtpAeadAes128Gcm = 0x0007;
42constexpr int kSrtpAeadAes256Gcm = 0x0008;
43constexpr int kSrtpCryptoSuiteMaxValue = 0xFFFF;
44
Philipp Hancke36e4dd22023-09-28 09:02:2145// Constants for SSL signature algorithms.
46constexpr int kSslSignatureAlgorithmUnknown = 0;
47constexpr int kSslSignatureAlgorithmMaxValue = 0xFFFF;
48
Björn Tereliuse71fa4e2024-06-25 09:55:1249// Names of SRTP profiles listed above.
50// 128-bit AES with 80-bit SHA-1 HMAC.
51extern const char kCsAesCm128HmacSha1_80[];
52// 128-bit AES with 32-bit SHA-1 HMAC.
53extern const char kCsAesCm128HmacSha1_32[];
54// 128-bit AES GCM with 16 byte AEAD auth tag.
55extern const char kCsAeadAes128Gcm[];
56// 256-bit AES GCM with 16 byte AEAD auth tag.
57extern const char kCsAeadAes256Gcm[];
58
Henrik Kjellanderec78f1c2017-06-29 05:52:5059// Given the DTLS-SRTP protection profile ID, as defined in
60// https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile
61// name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2.
62std::string SrtpCryptoSuiteToName(int crypto_suite);
63
Henrik Kjellanderec78f1c2017-06-29 05:52:5064// Get key length and salt length for given crypto suite. Returns true for
65// valid suites, otherwise false.
Jian Cui0a8798b2017-11-17 00:58:0266bool GetSrtpKeyAndSaltLengths(int crypto_suite,
67 int* key_length,
68 int* salt_length);
Henrik Kjellanderec78f1c2017-06-29 05:52:5069
70// Returns true if the given crypto suite id uses a GCM cipher.
71bool IsGcmCryptoSuite(int crypto_suite);
72
Henrik Kjellanderec78f1c2017-06-29 05:52:5073// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
74// After SSL has been started, the stream will only open on successful
75// SSL verification of certificates, and the communication is
76// encrypted of course.
77//
78// This class was written with SSLAdapter as a starting point. It
79// offers a similar interface, with two differences: there is no
80// support for a restartable SSL connection, and this class has a
81// peer-to-peer mode.
82//
83// The SSL library requires initialization and cleanup. Static method
84// for doing this are in SSLAdapter. They should possibly be moved out
85// to a neutral class.
86
Henrik Kjellanderec78f1c2017-06-29 05:52:5087enum SSLRole { SSL_CLIENT, SSL_SERVER };
88enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Harald Alvestrand13799132020-03-09 18:39:3689
Philipp Hanckeed180142024-06-11 16:42:5190// TODO bugs.webrtc.org/40644300 remove unused legacy constants.
Henrik Kjellanderec78f1c2017-06-29 05:52:5091enum SSLProtocolVersion {
Harald Alvestrand5cb78072019-10-28 08:51:1792 SSL_PROTOCOL_NOT_GIVEN = -1,
Philipp Hanckeed180142024-06-11 16:42:5193 SSL_PROTOCOL_TLS_10 = 0, // Deprecated and no longer supported.
94 SSL_PROTOCOL_TLS_11 = 1, // Deprecated and no longer supported.
95 SSL_PROTOCOL_TLS_12 = 2,
Jonas Orelanddcf0ffa2024-12-10 14:20:1196 SSL_PROTOCOL_TLS_13 = 3,
Philipp Hanckeed180142024-06-11 16:42:5197 SSL_PROTOCOL_DTLS_10 = 1, // Deprecated and no longer supported.
Henrik Kjellanderec78f1c2017-06-29 05:52:5098 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
Jonas Orelanddcf0ffa2024-12-10 14:20:1199 SSL_PROTOCOL_DTLS_13 = SSL_PROTOCOL_TLS_13,
Henrik Kjellanderec78f1c2017-06-29 05:52:50100};
Jonas Orelandac401852024-12-18 08:18:21101
102// Versions returned from BoringSSL.
103const uint16_t kDtls10VersionBytes = 0xfeff;
104const uint16_t kDtls12VersionBytes = 0xfefd;
105const uint16_t kDtls13VersionBytes = 0xfefc;
106
Henrik Kjellanderec78f1c2017-06-29 05:52:50107enum class SSLPeerCertificateDigestError {
108 NONE,
109 UNKNOWN_ALGORITHM,
110 INVALID_LENGTH,
111 VERIFICATION_FAILED,
112};
113
114// Errors for Read -- in the high range so no conflict with OpenSSL.
115enum { SSE_MSG_TRUNC = 0xff0001 };
116
117// Used to send back UMA histogram value. Logged when Dtls handshake fails.
118enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
119
Tommi48df56e2023-09-05 11:16:47120class SSLStreamAdapter : public StreamInterface {
Henrik Kjellanderec78f1c2017-06-29 05:52:50121 public:
122 // Instantiate an SSLStreamAdapter wrapping the given stream,
123 // (using the selected implementation for the platform).
124 // Caller is responsible for freeing the returned object.
Harald Alvestrand8515d5a2020-03-20 21:51:32125 static std::unique_ptr<SSLStreamAdapter> Create(
Tommi59574ca2023-09-05 07:21:57126 std::unique_ptr<StreamInterface> stream,
Jonas Oreland12574a32024-12-19 14:15:50127 absl::AnyInvocable<void(SSLHandshakeError)> handshake_error = nullptr,
128 const webrtc::FieldTrialsView* field_trials = nullptr);
Henrik Kjellanderec78f1c2017-06-29 05:52:50129
Niels Möller0131a4d2021-04-16 07:16:21130 SSLStreamAdapter() = default;
131 ~SSLStreamAdapter() override = default;
Henrik Kjellanderec78f1c2017-06-29 05:52:50132
Henrik Kjellanderec78f1c2017-06-29 05:52:50133 // Specify our SSL identity: key and certificate. SSLStream takes ownership
134 // of the SSLIdentity object and will free it when appropriate. Should be
135 // called no more than once on a given SSLStream instance.
Harald Alvestrand8515d5a2020-03-20 21:51:32136 virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
Harald Alvestrand8515d5a2020-03-20 21:51:32137 virtual SSLIdentity* GetIdentityForTesting() const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50138
139 // Call this to indicate that we are to play the server role (or client role,
140 // if the default argument is replaced by SSL_CLIENT).
141 // The default argument is for backward compatibility.
142 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
143 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
144
Philipp Hancke5d6fa7d2024-07-30 23:42:49145 [[deprecated("Only DTLS is supported by the stream adapter")]] virtual void
146 SetMode(SSLMode mode) = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50147
148 // Set maximum supported protocol version. The highest version supported by
149 // both ends will be used for the connection, i.e. if one party supports
150 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
151 // If requested version is not supported by underlying crypto library, the
152 // next lower will be used.
153 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
154
155 // Set the initial retransmission timeout for DTLS messages. When the timeout
156 // expires, the message gets retransmitted and the timeout is exponentially
157 // increased.
158 // This should only be called before StartSSL().
159 virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0;
160
161 // StartSSL starts negotiation with a peer, whose certificate is verified
162 // using the certificate digest. Generally, SetIdentity() and possibly
163 // SetServerRole() should have been called before this.
164 // SetPeerCertificateDigest() must also be called. It may be called after
165 // StartSSLWithPeer() but must be called before the underlying stream opens.
166 //
167 // Use of the stream prior to calling StartSSL will pass data in clear text.
168 // Calling StartSSL causes SSL negotiation to begin as soon as possible: right
169 // away if the underlying wrapped stream is already opened, or else as soon as
170 // it opens.
171 //
172 // StartSSL returns a negative error code on failure. Returning 0 means
173 // success so far, but negotiation is probably not complete and will continue
174 // asynchronously. In that case, the exposed stream will open after
175 // successful negotiation and verification, or an SE_CLOSE event will be
176 // raised if negotiation fails.
177 virtual int StartSSL() = 0;
178
179 // Specify the digest of the certificate that our peer is expected to use.
180 // Only this certificate will be accepted during SSL verification. The
181 // certificate is assumed to have been obtained through some other secure
182 // channel (such as the signaling channel). This must specify the terminal
183 // certificate, not just a CA. SSLStream makes a copy of the digest value.
184 //
Philipp Hancke40607452024-11-26 18:50:26185 // Returns SSLPeerCertificateDigestError::NONE if successful.
186 virtual SSLPeerCertificateDigestError SetPeerCertificateDigest(
Ali Tofigh7fa90572022-03-17 14:47:49187 absl::string_view digest_alg,
Florent Castelli1bda6a62024-11-27 12:28:57188 rtc::ArrayView<const uint8_t> digest_val) = 0;
Philipp Hancke40607452024-11-26 18:50:26189 [[deprecated(
190 "Use SetPeerCertificateDigest with ArrayView instead")]] virtual bool
191 SetPeerCertificateDigest(absl::string_view digest_alg,
192 const unsigned char* digest_val,
193 size_t digest_len,
194 SSLPeerCertificateDigestError* error = nullptr);
Henrik Kjellanderec78f1c2017-06-29 05:52:50195
Taylor Brandstetterc3928662018-02-23 21:04:51196 // Retrieves the peer's certificate chain including leaf certificate, if a
Jian Cui0a8798b2017-11-17 00:58:02197 // connection has been established.
198 virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0;
199
Henrik Kjellanderec78f1c2017-06-29 05:52:50200 // Retrieves the IANA registration id of the cipher suite used for the
201 // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA").
Philipp Hancke4f732f42024-09-30 18:58:25202 virtual bool GetSslCipherSuite(int* cipher_suite) const = 0;
Philipp Hancke18311842024-09-27 16:04:43203 // Returns the name of the cipher suite used for the DTLS transport,
204 // as defined in the "Description" column of the IANA cipher suite registry.
205 virtual std::optional<absl::string_view> GetTlsCipherSuiteName() const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50206
Harald Alvestrand5cb78072019-10-28 08:51:17207 // Retrieves the enum value for SSL version.
208 // Will return -1 until the version has been negotiated.
Philipp Hanckeed180142024-06-11 16:42:51209 [[deprecated("Use GetSslVersionBytes")]] virtual SSLProtocolVersion
210 GetSslVersion() const = 0;
Harald Alvestrand5cb78072019-10-28 08:51:17211 // Retrieves the 2-byte version from the TLS protocol.
212 // Will return false until the version has been negotiated.
213 virtual bool GetSslVersionBytes(int* version) const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50214
215 // Key Exporter interface from RFC 5705
Philipp Hancke6caca652024-10-10 17:33:25216 virtual bool ExportSrtpKeyingMaterial(
Philipp Hancke03b2c9f2024-10-14 12:19:03217 rtc::ZeroOnFreeBuffer<uint8_t>& keying_material) = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50218
Philipp Hancke36e4dd22023-09-28 09:02:21219 // Returns the signature algorithm or 0 if not applicable.
220 virtual uint16_t GetPeerSignatureAlgorithm() const = 0;
221
Henrik Kjellanderec78f1c2017-06-29 05:52:50222 // DTLS-SRTP interface
Philipp Hancke4f732f42024-09-30 18:58:25223 virtual bool SetDtlsSrtpCryptoSuites(
224 const std::vector<int>& crypto_suites) = 0;
225 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite) const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50226
227 // Returns true if a TLS connection has been established.
228 // The only difference between this and "GetState() == SE_OPEN" is that if
229 // the peer certificate digest hasn't been verified, the state will still be
230 // SS_OPENING but IsTlsConnected should return true.
231 virtual bool IsTlsConnected() = 0;
232
233 // Capabilities testing.
234 // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now
235 // that's assumed.
236 static bool IsBoringSsl();
237
238 // Returns true iff the supplied cipher is deemed to be strong.
239 // TODO(torbjorng): Consider removing the KeyType argument.
240 static bool IsAcceptableCipher(int cipher, KeyType key_type);
Ali Tofigh7fa90572022-03-17 14:47:49241 static bool IsAcceptableCipher(absl::string_view cipher, KeyType key_type);
Henrik Kjellanderec78f1c2017-06-29 05:52:50242
Benjamin Wrightb19b4972018-10-25 17:46:49243 ////////////////////////////////////////////////////////////////////////////
244 // Testing only member functions
245 ////////////////////////////////////////////////////////////////////////////
246
Henrik Kjellanderec78f1c2017-06-29 05:52:50247 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
248 // using a fake clock.
Benjamin Wrightb19b4972018-10-25 17:46:49249 static void EnableTimeCallbackForTesting();
250
Jonas Orelandac401852024-12-18 08:18:21251 // Return max DTLS SSLProtocolVersion supported by implementation.
252 static SSLProtocolVersion GetMaxSupportedDTLSProtocolVersion();
253
Benjamin Wrightb19b4972018-10-25 17:46:49254 // Deprecated. Do not use this API outside of testing.
255 // Do not set this to false outside of testing.
256 void SetClientAuthEnabledForTesting(bool enabled) {
257 client_auth_enabled_ = enabled;
258 }
259
260 // Deprecated. Do not use this API outside of testing.
261 // Returns true by default, else false if explicitly set to disable client
262 // authentication.
263 bool GetClientAuthEnabled() const { return client_auth_enabled_; }
Henrik Kjellanderec78f1c2017-06-29 05:52:50264
Henrik Kjellanderec78f1c2017-06-29 05:52:50265 private:
Henrik Kjellanderec78f1c2017-06-29 05:52:50266 // If true (default), the client is required to provide a certificate during
267 // handshake. If no certificate is given, handshake fails. This applies to
268 // server mode only.
Benjamin Wrightb19b4972018-10-25 17:46:49269 bool client_auth_enabled_ = true;
Henrik Kjellanderec78f1c2017-06-29 05:52:50270};
271
272} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26273
Steve Anton10542f22019-01-11 17:11:00274#endif // RTC_BASE_SSL_STREAM_ADAPTER_H_