blob: 6a21c6bed4887e733a4f6b7f8999ff55bcc5243b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 09:11:00 -080011#ifndef RTC_BASE_SSL_STREAM_ADAPTER_H_
12#define RTC_BASE_SSL_STREAM_ADAPTER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <memory>
17#include <string>
18#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010020#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/ssl_certificate.h"
22#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/stream.h"
Sam Zackrissonb298f742020-10-06 11:40:30 +000024#include "rtc_base/third_party/sigslot/sigslot.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26namespace rtc {
27
28// Constants for SSL profile.
Mirko Bonadei7750d802021-07-26 17:27:42 +020029constexpr int kTlsNullWithNullNull = 0;
30constexpr int kSslCipherSuiteMaxValue = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031
Mirko Bonadei7750d802021-07-26 17:27:42 +020032// TODO(bugs.webrtc.org/12997): Start.
33// Remove the following constants which violate the style guide and can
34// potentially clash with OpenSSL macros.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035#ifndef SRTP_AES128_CM_SHA1_80
36const int SRTP_AES128_CM_SHA1_80 = 0x0001;
37#endif
38#ifndef SRTP_AES128_CM_SHA1_32
39const int SRTP_AES128_CM_SHA1_32 = 0x0002;
40#endif
41#ifndef SRTP_AEAD_AES_128_GCM
42const int SRTP_AEAD_AES_128_GCM = 0x0007;
43#endif
44#ifndef SRTP_AEAD_AES_256_GCM
45const int SRTP_AEAD_AES_256_GCM = 0x0008;
46#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047// Names of SRTP profiles listed above.
48// 128-bit AES with 80-bit SHA-1 HMAC.
49extern const char CS_AES_CM_128_HMAC_SHA1_80[];
50// 128-bit AES with 32-bit SHA-1 HMAC.
51extern const char CS_AES_CM_128_HMAC_SHA1_32[];
52// 128-bit AES GCM with 16 byte AEAD auth tag.
53extern const char CS_AEAD_AES_128_GCM[];
54// 256-bit AES GCM with 16 byte AEAD auth tag.
55extern const char CS_AEAD_AES_256_GCM[];
Mirko Bonadei7750d802021-07-26 17:27:42 +020056// TODO(bugs.webrtc.org/12997): End.
57
58// Constants for SRTP profiles.
59constexpr int kSrtpInvalidCryptoSuite = 0;
60constexpr int kSrtpAes128CmSha1_80 = 0x0001;
61constexpr int kSrtpAes128CmSha1_32 = 0x0002;
62constexpr int kSrtpAeadAes128Gcm = 0x0007;
63constexpr int kSrtpAeadAes256Gcm = 0x0008;
64constexpr int kSrtpCryptoSuiteMaxValue = 0xFFFF;
65
66// Names of SRTP profiles listed above.
67// 128-bit AES with 80-bit SHA-1 HMAC.
68extern const char kCsAesCm128HmacSha1_80[];
69// 128-bit AES with 32-bit SHA-1 HMAC.
70extern const char kCsAesCm128HmacSha1_32[];
71// 128-bit AES GCM with 16 byte AEAD auth tag.
72extern const char kCsAeadAes128Gcm[];
73// 256-bit AES GCM with 16 byte AEAD auth tag.
74extern const char kCsAeadAes256Gcm[];
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075
76// Given the DTLS-SRTP protection profile ID, as defined in
77// https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile
78// name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2.
79std::string SrtpCryptoSuiteToName(int crypto_suite);
80
81// The reverse of above conversion.
82int SrtpCryptoSuiteFromName(const std::string& crypto_suite);
83
84// Get key length and salt length for given crypto suite. Returns true for
85// valid suites, otherwise false.
Jian Cui0a8798b2017-11-16 16:58:02 -080086bool GetSrtpKeyAndSaltLengths(int crypto_suite,
87 int* key_length,
88 int* salt_length);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089
90// Returns true if the given crypto suite id uses a GCM cipher.
91bool IsGcmCryptoSuite(int crypto_suite);
92
93// Returns true if the given crypto suite name uses a GCM cipher.
94bool IsGcmCryptoSuiteName(const std::string& crypto_suite);
95
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
97// After SSL has been started, the stream will only open on successful
98// SSL verification of certificates, and the communication is
99// encrypted of course.
100//
101// This class was written with SSLAdapter as a starting point. It
102// offers a similar interface, with two differences: there is no
103// support for a restartable SSL connection, and this class has a
104// peer-to-peer mode.
105//
106// The SSL library requires initialization and cleanup. Static method
107// for doing this are in SSLAdapter. They should possibly be moved out
108// to a neutral class.
109
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110enum SSLRole { SSL_CLIENT, SSL_SERVER };
111enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Harald Alvestrand13799132020-03-09 19:39:36 +0100112
Guido Urdanetaae2e8642020-10-26 09:55:26 +0100113// Note: TLS_10, TLS_11, and DTLS_10 will all be ignored, and only DTLS1_2 will
114// be accepted unless the trial flag WebRTC-LegacyTlsProtocols/Enabled/ is
115// passed in or an explicit override is used. Support for the legacy protocol
116// versions will be completely removed in the future.
117// See https://bugs.webrtc.org/10261.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118enum SSLProtocolVersion {
Harald Alvestrand5cb78072019-10-28 09:51:17 +0100119 SSL_PROTOCOL_NOT_GIVEN = -1,
120 SSL_PROTOCOL_TLS_10 = 0,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121 SSL_PROTOCOL_TLS_11,
122 SSL_PROTOCOL_TLS_12,
123 SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11,
124 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
125};
126enum class SSLPeerCertificateDigestError {
127 NONE,
128 UNKNOWN_ALGORITHM,
129 INVALID_LENGTH,
130 VERIFICATION_FAILED,
131};
132
133// Errors for Read -- in the high range so no conflict with OpenSSL.
134enum { SSE_MSG_TRUNC = 0xff0001 };
135
136// Used to send back UMA histogram value. Logged when Dtls handshake fails.
137enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
138
Niels Möller0131a4d2021-04-16 09:16:21 +0200139class SSLStreamAdapter : public StreamInterface, public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140 public:
141 // Instantiate an SSLStreamAdapter wrapping the given stream,
142 // (using the selected implementation for the platform).
143 // Caller is responsible for freeing the returned object.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100144 static std::unique_ptr<SSLStreamAdapter> Create(
145 std::unique_ptr<StreamInterface> stream);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200146
Niels Möller0131a4d2021-04-16 09:16:21 +0200147 SSLStreamAdapter() = default;
148 ~SSLStreamAdapter() override = default;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200149
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150 // Specify our SSL identity: key and certificate. SSLStream takes ownership
151 // of the SSLIdentity object and will free it when appropriate. Should be
152 // called no more than once on a given SSLStream instance.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100153 virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100154 virtual SSLIdentity* GetIdentityForTesting() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200155
156 // Call this to indicate that we are to play the server role (or client role,
157 // if the default argument is replaced by SSL_CLIENT).
158 // The default argument is for backward compatibility.
159 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
160 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
161
162 // Do DTLS or TLS.
163 virtual void SetMode(SSLMode mode) = 0;
164
165 // Set maximum supported protocol version. The highest version supported by
166 // both ends will be used for the connection, i.e. if one party supports
167 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
168 // If requested version is not supported by underlying crypto library, the
169 // next lower will be used.
170 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
171
172 // Set the initial retransmission timeout for DTLS messages. When the timeout
173 // expires, the message gets retransmitted and the timeout is exponentially
174 // increased.
175 // This should only be called before StartSSL().
176 virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0;
177
178 // StartSSL starts negotiation with a peer, whose certificate is verified
179 // using the certificate digest. Generally, SetIdentity() and possibly
180 // SetServerRole() should have been called before this.
181 // SetPeerCertificateDigest() must also be called. It may be called after
182 // StartSSLWithPeer() but must be called before the underlying stream opens.
183 //
184 // Use of the stream prior to calling StartSSL will pass data in clear text.
185 // Calling StartSSL causes SSL negotiation to begin as soon as possible: right
186 // away if the underlying wrapped stream is already opened, or else as soon as
187 // it opens.
188 //
189 // StartSSL returns a negative error code on failure. Returning 0 means
190 // success so far, but negotiation is probably not complete and will continue
191 // asynchronously. In that case, the exposed stream will open after
192 // successful negotiation and verification, or an SE_CLOSE event will be
193 // raised if negotiation fails.
194 virtual int StartSSL() = 0;
195
196 // Specify the digest of the certificate that our peer is expected to use.
197 // Only this certificate will be accepted during SSL verification. The
198 // certificate is assumed to have been obtained through some other secure
199 // channel (such as the signaling channel). This must specify the terminal
200 // certificate, not just a CA. SSLStream makes a copy of the digest value.
201 //
202 // Returns true if successful.
203 // |error| is optional and provides more information about the failure.
204 virtual bool SetPeerCertificateDigest(
205 const std::string& digest_alg,
206 const unsigned char* digest_val,
207 size_t digest_len,
208 SSLPeerCertificateDigestError* error = nullptr) = 0;
209
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800210 // Retrieves the peer's certificate chain including leaf certificate, if a
Jian Cui0a8798b2017-11-16 16:58:02 -0800211 // connection has been established.
212 virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0;
213
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200214 // Retrieves the IANA registration id of the cipher suite used for the
215 // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA").
216 virtual bool GetSslCipherSuite(int* cipher_suite);
217
Harald Alvestrand5cb78072019-10-28 09:51:17 +0100218 // Retrieves the enum value for SSL version.
219 // Will return -1 until the version has been negotiated.
220 virtual SSLProtocolVersion GetSslVersion() const = 0;
221 // Retrieves the 2-byte version from the TLS protocol.
222 // Will return false until the version has been negotiated.
223 virtual bool GetSslVersionBytes(int* version) const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200224
225 // Key Exporter interface from RFC 5705
226 // Arguments are:
227 // label -- the exporter label.
228 // part of the RFC defining each exporter
229 // usage (IN)
230 // context/context_len -- a context to bind to for this connection;
231 // optional, can be null, 0 (IN)
232 // use_context -- whether to use the context value
233 // (needed to distinguish no context from
234 // zero-length ones).
235 // result -- where to put the computed value
236 // result_len -- the length of the computed value
237 virtual bool ExportKeyingMaterial(const std::string& label,
238 const uint8_t* context,
239 size_t context_len,
240 bool use_context,
241 uint8_t* result,
242 size_t result_len);
243
244 // DTLS-SRTP interface
245 virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites);
246 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite);
247
248 // Returns true if a TLS connection has been established.
249 // The only difference between this and "GetState() == SE_OPEN" is that if
250 // the peer certificate digest hasn't been verified, the state will still be
251 // SS_OPENING but IsTlsConnected should return true.
252 virtual bool IsTlsConnected() = 0;
253
254 // Capabilities testing.
255 // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now
256 // that's assumed.
257 static bool IsBoringSsl();
258
259 // Returns true iff the supplied cipher is deemed to be strong.
260 // TODO(torbjorng): Consider removing the KeyType argument.
261 static bool IsAcceptableCipher(int cipher, KeyType key_type);
262 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
263
264 // TODO(guoweis): Move this away from a static class method. Currently this is
265 // introduced such that any caller could depend on sslstreamadapter.h without
266 // depending on specific SSL implementation.
267 static std::string SslCipherSuiteToName(int cipher_suite);
268
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700269 ////////////////////////////////////////////////////////////////////////////
270 // Testing only member functions
271 ////////////////////////////////////////////////////////////////////////////
272
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200273 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
274 // using a fake clock.
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700275 static void EnableTimeCallbackForTesting();
276
277 // Deprecated. Do not use this API outside of testing.
278 // Do not set this to false outside of testing.
279 void SetClientAuthEnabledForTesting(bool enabled) {
280 client_auth_enabled_ = enabled;
281 }
282
283 // Deprecated. Do not use this API outside of testing.
284 // Returns true by default, else false if explicitly set to disable client
285 // authentication.
286 bool GetClientAuthEnabled() const { return client_auth_enabled_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200287
Sam Zackrissonb298f742020-10-06 11:40:30 +0000288 sigslot::signal1<SSLHandshakeError> SignalSSLHandshakeError;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200289
290 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200291 // If true (default), the client is required to provide a certificate during
292 // handshake. If no certificate is given, handshake fails. This applies to
293 // server mode only.
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700294 bool client_auth_enabled_ = true;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200295};
296
297} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298
Steve Anton10542f22019-01-11 09:11:00 -0800299#endif // RTC_BASE_SSL_STREAM_ADAPTER_H_