blob: 6b44c76455564ecb9e5c991ee54a99952e229140 [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.
29const int TLS_NULL_WITH_NULL_NULL = 0;
Qingsi Wang7fc821d2018-07-12 12:54:53 -070030const int SSL_CIPHER_SUITE_MAX_VALUE = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031
32// Constants for SRTP profiles.
33const int SRTP_INVALID_CRYPTO_SUITE = 0;
34#ifndef SRTP_AES128_CM_SHA1_80
35const int SRTP_AES128_CM_SHA1_80 = 0x0001;
36#endif
37#ifndef SRTP_AES128_CM_SHA1_32
38const int SRTP_AES128_CM_SHA1_32 = 0x0002;
39#endif
40#ifndef SRTP_AEAD_AES_128_GCM
41const int SRTP_AEAD_AES_128_GCM = 0x0007;
42#endif
43#ifndef SRTP_AEAD_AES_256_GCM
44const int SRTP_AEAD_AES_256_GCM = 0x0008;
45#endif
Qingsi Wang7fc821d2018-07-12 12:54:53 -070046const int SRTP_CRYPTO_SUITE_MAX_VALUE = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047
48// Names of SRTP profiles listed above.
49// 128-bit AES with 80-bit SHA-1 HMAC.
50extern const char CS_AES_CM_128_HMAC_SHA1_80[];
51// 128-bit AES with 32-bit SHA-1 HMAC.
52extern const char CS_AES_CM_128_HMAC_SHA1_32[];
53// 128-bit AES GCM with 16 byte AEAD auth tag.
54extern const char CS_AEAD_AES_128_GCM[];
55// 256-bit AES GCM with 16 byte AEAD auth tag.
56extern const char CS_AEAD_AES_256_GCM[];
57
58// Given the DTLS-SRTP protection profile ID, as defined in
59// https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile
60// name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2.
61std::string SrtpCryptoSuiteToName(int crypto_suite);
62
63// The reverse of above conversion.
64int SrtpCryptoSuiteFromName(const std::string& crypto_suite);
65
66// Get key length and salt length for given crypto suite. Returns true for
67// valid suites, otherwise false.
Jian Cui0a8798b2017-11-16 16:58:02 -080068bool GetSrtpKeyAndSaltLengths(int crypto_suite,
69 int* key_length,
70 int* salt_length);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071
72// Returns true if the given crypto suite id uses a GCM cipher.
73bool IsGcmCryptoSuite(int crypto_suite);
74
75// Returns true if the given crypto suite name uses a GCM cipher.
76bool IsGcmCryptoSuiteName(const std::string& crypto_suite);
77
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
79// After SSL has been started, the stream will only open on successful
80// SSL verification of certificates, and the communication is
81// encrypted of course.
82//
83// This class was written with SSLAdapter as a starting point. It
84// offers a similar interface, with two differences: there is no
85// support for a restartable SSL connection, and this class has a
86// peer-to-peer mode.
87//
88// The SSL library requires initialization and cleanup. Static method
89// for doing this are in SSLAdapter. They should possibly be moved out
90// to a neutral class.
91
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092enum SSLRole { SSL_CLIENT, SSL_SERVER };
93enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Harald Alvestrand13799132020-03-09 19:39:36 +010094
Guido Urdanetaae2e8642020-10-26 09:55:26 +010095// Note: TLS_10, TLS_11, and DTLS_10 will all be ignored, and only DTLS1_2 will
96// be accepted unless the trial flag WebRTC-LegacyTlsProtocols/Enabled/ is
97// passed in or an explicit override is used. Support for the legacy protocol
98// versions will be completely removed in the future.
99// See https://bugs.webrtc.org/10261.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100enum SSLProtocolVersion {
Harald Alvestrand5cb78072019-10-28 09:51:17 +0100101 SSL_PROTOCOL_NOT_GIVEN = -1,
102 SSL_PROTOCOL_TLS_10 = 0,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 SSL_PROTOCOL_TLS_11,
104 SSL_PROTOCOL_TLS_12,
105 SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11,
106 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
107};
108enum class SSLPeerCertificateDigestError {
109 NONE,
110 UNKNOWN_ALGORITHM,
111 INVALID_LENGTH,
112 VERIFICATION_FAILED,
113};
114
115// Errors for Read -- in the high range so no conflict with OpenSSL.
116enum { SSE_MSG_TRUNC = 0xff0001 };
117
118// Used to send back UMA histogram value. Logged when Dtls handshake fails.
119enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
120
Niels Möller0131a4d2021-04-16 09:16:21 +0200121class SSLStreamAdapter : public StreamInterface, public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 public:
123 // Instantiate an SSLStreamAdapter wrapping the given stream,
124 // (using the selected implementation for the platform).
125 // Caller is responsible for freeing the returned object.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100126 static std::unique_ptr<SSLStreamAdapter> Create(
127 std::unique_ptr<StreamInterface> stream);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128
Niels Möller0131a4d2021-04-16 09:16:21 +0200129 SSLStreamAdapter() = default;
130 ~SSLStreamAdapter() override = default;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132 // Specify our SSL identity: key and certificate. SSLStream takes ownership
133 // of the SSLIdentity object and will free it when appropriate. Should be
134 // called no more than once on a given SSLStream instance.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100135 virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100136 virtual SSLIdentity* GetIdentityForTesting() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137
138 // Call this to indicate that we are to play the server role (or client role,
139 // if the default argument is replaced by SSL_CLIENT).
140 // The default argument is for backward compatibility.
141 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
142 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
143
144 // Do DTLS or TLS.
145 virtual void SetMode(SSLMode mode) = 0;
146
147 // Set maximum supported protocol version. The highest version supported by
148 // both ends will be used for the connection, i.e. if one party supports
149 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
150 // If requested version is not supported by underlying crypto library, the
151 // next lower will be used.
152 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
153
154 // Set the initial retransmission timeout for DTLS messages. When the timeout
155 // expires, the message gets retransmitted and the timeout is exponentially
156 // increased.
157 // This should only be called before StartSSL().
158 virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0;
159
160 // StartSSL starts negotiation with a peer, whose certificate is verified
161 // using the certificate digest. Generally, SetIdentity() and possibly
162 // SetServerRole() should have been called before this.
163 // SetPeerCertificateDigest() must also be called. It may be called after
164 // StartSSLWithPeer() but must be called before the underlying stream opens.
165 //
166 // Use of the stream prior to calling StartSSL will pass data in clear text.
167 // Calling StartSSL causes SSL negotiation to begin as soon as possible: right
168 // away if the underlying wrapped stream is already opened, or else as soon as
169 // it opens.
170 //
171 // StartSSL returns a negative error code on failure. Returning 0 means
172 // success so far, but negotiation is probably not complete and will continue
173 // asynchronously. In that case, the exposed stream will open after
174 // successful negotiation and verification, or an SE_CLOSE event will be
175 // raised if negotiation fails.
176 virtual int StartSSL() = 0;
177
178 // Specify the digest of the certificate that our peer is expected to use.
179 // Only this certificate will be accepted during SSL verification. The
180 // certificate is assumed to have been obtained through some other secure
181 // channel (such as the signaling channel). This must specify the terminal
182 // certificate, not just a CA. SSLStream makes a copy of the digest value.
183 //
184 // Returns true if successful.
185 // |error| is optional and provides more information about the failure.
186 virtual bool SetPeerCertificateDigest(
187 const std::string& digest_alg,
188 const unsigned char* digest_val,
189 size_t digest_len,
190 SSLPeerCertificateDigestError* error = nullptr) = 0;
191
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800192 // Retrieves the peer's certificate chain including leaf certificate, if a
Jian Cui0a8798b2017-11-16 16:58:02 -0800193 // connection has been established.
194 virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0;
195
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200196 // Retrieves the IANA registration id of the cipher suite used for the
197 // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA").
198 virtual bool GetSslCipherSuite(int* cipher_suite);
199
Harald Alvestrand5cb78072019-10-28 09:51:17 +0100200 // Retrieves the enum value for SSL version.
201 // Will return -1 until the version has been negotiated.
202 virtual SSLProtocolVersion GetSslVersion() const = 0;
203 // Retrieves the 2-byte version from the TLS protocol.
204 // Will return false until the version has been negotiated.
205 virtual bool GetSslVersionBytes(int* version) const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200206
207 // Key Exporter interface from RFC 5705
208 // Arguments are:
209 // label -- the exporter label.
210 // part of the RFC defining each exporter
211 // usage (IN)
212 // context/context_len -- a context to bind to for this connection;
213 // optional, can be null, 0 (IN)
214 // use_context -- whether to use the context value
215 // (needed to distinguish no context from
216 // zero-length ones).
217 // result -- where to put the computed value
218 // result_len -- the length of the computed value
219 virtual bool ExportKeyingMaterial(const std::string& label,
220 const uint8_t* context,
221 size_t context_len,
222 bool use_context,
223 uint8_t* result,
224 size_t result_len);
225
226 // DTLS-SRTP interface
227 virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites);
228 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite);
229
230 // Returns true if a TLS connection has been established.
231 // The only difference between this and "GetState() == SE_OPEN" is that if
232 // the peer certificate digest hasn't been verified, the state will still be
233 // SS_OPENING but IsTlsConnected should return true.
234 virtual bool IsTlsConnected() = 0;
235
236 // Capabilities testing.
237 // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now
238 // that's assumed.
239 static bool IsBoringSsl();
240
241 // Returns true iff the supplied cipher is deemed to be strong.
242 // TODO(torbjorng): Consider removing the KeyType argument.
243 static bool IsAcceptableCipher(int cipher, KeyType key_type);
244 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
245
246 // TODO(guoweis): Move this away from a static class method. Currently this is
247 // introduced such that any caller could depend on sslstreamadapter.h without
248 // depending on specific SSL implementation.
249 static std::string SslCipherSuiteToName(int cipher_suite);
250
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700251 ////////////////////////////////////////////////////////////////////////////
252 // Testing only member functions
253 ////////////////////////////////////////////////////////////////////////////
254
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200255 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
256 // using a fake clock.
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700257 static void EnableTimeCallbackForTesting();
258
259 // Deprecated. Do not use this API outside of testing.
260 // Do not set this to false outside of testing.
261 void SetClientAuthEnabledForTesting(bool enabled) {
262 client_auth_enabled_ = enabled;
263 }
264
265 // Deprecated. Do not use this API outside of testing.
266 // Returns true by default, else false if explicitly set to disable client
267 // authentication.
268 bool GetClientAuthEnabled() const { return client_auth_enabled_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200269
Sam Zackrissonb298f742020-10-06 11:40:30 +0000270 sigslot::signal1<SSLHandshakeError> SignalSSLHandshakeError;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200271
272 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200273 // If true (default), the client is required to provide a certificate during
274 // handshake. If no certificate is given, handshake fails. This applies to
275 // server mode only.
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700276 bool client_auth_enabled_ = true;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200277};
278
279} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280
Steve Anton10542f22019-01-11 09:11:00 -0800281#endif // RTC_BASE_SSL_STREAM_ADAPTER_H_