blob: 3da0b09469a67d4a39f4aa24e2856a66a52a73ee [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"
21#include "rtc_base/deprecation.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/ssl_certificate.h"
23#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/stream.h"
Sam Zackrissonb298f742020-10-06 11:40:30 +000025#include "rtc_base/third_party/sigslot/sigslot.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026
27namespace rtc {
28
29// Constants for SSL profile.
30const int TLS_NULL_WITH_NULL_NULL = 0;
Qingsi Wang7fc821d2018-07-12 12:54:53 -070031const int SSL_CIPHER_SUITE_MAX_VALUE = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032
33// Constants for SRTP profiles.
34const int SRTP_INVALID_CRYPTO_SUITE = 0;
35#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
Qingsi Wang7fc821d2018-07-12 12:54:53 -070047const int SRTP_CRYPTO_SUITE_MAX_VALUE = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048
49// Names of SRTP profiles listed above.
50// 128-bit AES with 80-bit SHA-1 HMAC.
51extern const char CS_AES_CM_128_HMAC_SHA1_80[];
52// 128-bit AES with 32-bit SHA-1 HMAC.
53extern const char CS_AES_CM_128_HMAC_SHA1_32[];
54// 128-bit AES GCM with 16 byte AEAD auth tag.
55extern const char CS_AEAD_AES_128_GCM[];
56// 256-bit AES GCM with 16 byte AEAD auth tag.
57extern const char CS_AEAD_AES_256_GCM[];
58
59// 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
64// The reverse of above conversion.
65int SrtpCryptoSuiteFromName(const std::string& crypto_suite);
66
67// Get key length and salt length for given crypto suite. Returns true for
68// valid suites, otherwise false.
Jian Cui0a8798b2017-11-16 16:58:02 -080069bool GetSrtpKeyAndSaltLengths(int crypto_suite,
70 int* key_length,
71 int* salt_length);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072
73// Returns true if the given crypto suite id uses a GCM cipher.
74bool IsGcmCryptoSuite(int crypto_suite);
75
76// Returns true if the given crypto suite name uses a GCM cipher.
77bool IsGcmCryptoSuiteName(const std::string& crypto_suite);
78
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
80// After SSL has been started, the stream will only open on successful
81// SSL verification of certificates, and the communication is
82// encrypted of course.
83//
84// This class was written with SSLAdapter as a starting point. It
85// offers a similar interface, with two differences: there is no
86// support for a restartable SSL connection, and this class has a
87// peer-to-peer mode.
88//
89// The SSL library requires initialization and cleanup. Static method
90// for doing this are in SSLAdapter. They should possibly be moved out
91// to a neutral class.
92
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093enum SSLRole { SSL_CLIENT, SSL_SERVER };
94enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Harald Alvestrand13799132020-03-09 19:39:36 +010095
96// Note: TLS_10, TLS_11, and DTLS_10 will all be ignored, and only
97// DTLS1_2 will be accepted, if the trial flag
98// WebRTC-LegacyTlsProtocols/Disabled/ is passed in. Support for these
99// protocol versions will be completely removed in M84 or later.
100// TODO(https://bugs.webrtc.org/10261).
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101enum SSLProtocolVersion {
Harald Alvestrand5cb78072019-10-28 09:51:17 +0100102 SSL_PROTOCOL_NOT_GIVEN = -1,
103 SSL_PROTOCOL_TLS_10 = 0,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104 SSL_PROTOCOL_TLS_11,
105 SSL_PROTOCOL_TLS_12,
106 SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11,
107 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
108};
109enum class SSLPeerCertificateDigestError {
110 NONE,
111 UNKNOWN_ALGORITHM,
112 INVALID_LENGTH,
113 VERIFICATION_FAILED,
114};
115
116// Errors for Read -- in the high range so no conflict with OpenSSL.
117enum { SSE_MSG_TRUNC = 0xff0001 };
118
119// Used to send back UMA histogram value. Logged when Dtls handshake fails.
120enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
121
122class SSLStreamAdapter : public StreamAdapterInterface {
123 public:
124 // Instantiate an SSLStreamAdapter wrapping the given stream,
125 // (using the selected implementation for the platform).
126 // Caller is responsible for freeing the returned object.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100127 static std::unique_ptr<SSLStreamAdapter> Create(
128 std::unique_ptr<StreamInterface> stream);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200129
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100130 explicit SSLStreamAdapter(std::unique_ptr<StreamInterface> stream);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131 ~SSLStreamAdapter() override;
132
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200133 // 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 22:51:32 +0100136 virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100137 virtual SSLIdentity* GetIdentityForTesting() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138
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
145 // Do DTLS or TLS.
146 virtual void SetMode(SSLMode mode) = 0;
147
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 //
185 // Returns true if successful.
186 // |error| is optional and provides more information about the failure.
187 virtual bool SetPeerCertificateDigest(
188 const std::string& digest_alg,
189 const unsigned char* digest_val,
190 size_t digest_len,
191 SSLPeerCertificateDigestError* error = nullptr) = 0;
192
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800193 // Retrieves the peer's certificate chain including leaf certificate, if a
Jian Cui0a8798b2017-11-16 16:58:02 -0800194 // connection has been established.
195 virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0;
196
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200197 // Retrieves the IANA registration id of the cipher suite used for the
198 // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA").
199 virtual bool GetSslCipherSuite(int* cipher_suite);
200
Harald Alvestrand5cb78072019-10-28 09:51:17 +0100201 // Retrieves the enum value for SSL version.
202 // Will return -1 until the version has been negotiated.
203 virtual SSLProtocolVersion GetSslVersion() const = 0;
204 // Retrieves the 2-byte version from the TLS protocol.
205 // Will return false until the version has been negotiated.
206 virtual bool GetSslVersionBytes(int* version) const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200207
208 // Key Exporter interface from RFC 5705
209 // Arguments are:
210 // label -- the exporter label.
211 // part of the RFC defining each exporter
212 // usage (IN)
213 // context/context_len -- a context to bind to for this connection;
214 // optional, can be null, 0 (IN)
215 // use_context -- whether to use the context value
216 // (needed to distinguish no context from
217 // zero-length ones).
218 // result -- where to put the computed value
219 // result_len -- the length of the computed value
220 virtual bool ExportKeyingMaterial(const std::string& label,
221 const uint8_t* context,
222 size_t context_len,
223 bool use_context,
224 uint8_t* result,
225 size_t result_len);
226
227 // DTLS-SRTP interface
228 virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites);
229 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite);
230
231 // Returns true if a TLS connection has been established.
232 // The only difference between this and "GetState() == SE_OPEN" is that if
233 // the peer certificate digest hasn't been verified, the state will still be
234 // SS_OPENING but IsTlsConnected should return true.
235 virtual bool IsTlsConnected() = 0;
236
237 // Capabilities testing.
238 // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now
239 // that's assumed.
240 static bool IsBoringSsl();
241
242 // Returns true iff the supplied cipher is deemed to be strong.
243 // TODO(torbjorng): Consider removing the KeyType argument.
244 static bool IsAcceptableCipher(int cipher, KeyType key_type);
245 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
246
247 // TODO(guoweis): Move this away from a static class method. Currently this is
248 // introduced such that any caller could depend on sslstreamadapter.h without
249 // depending on specific SSL implementation.
250 static std::string SslCipherSuiteToName(int cipher_suite);
251
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700252 ////////////////////////////////////////////////////////////////////////////
253 // Testing only member functions
254 ////////////////////////////////////////////////////////////////////////////
255
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200256 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
257 // using a fake clock.
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700258 static void EnableTimeCallbackForTesting();
259
260 // Deprecated. Do not use this API outside of testing.
261 // Do not set this to false outside of testing.
262 void SetClientAuthEnabledForTesting(bool enabled) {
263 client_auth_enabled_ = enabled;
264 }
265
266 // Deprecated. Do not use this API outside of testing.
267 // Returns true by default, else false if explicitly set to disable client
268 // authentication.
269 bool GetClientAuthEnabled() const { return client_auth_enabled_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200270
Sam Zackrissonb298f742020-10-06 11:40:30 +0000271 sigslot::signal1<SSLHandshakeError> SignalSSLHandshakeError;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200272
273 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200274 // If true (default), the client is required to provide a certificate during
275 // handshake. If no certificate is given, handshake fails. This applies to
276 // server mode only.
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700277 bool client_auth_enabled_ = true;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200278};
279
280} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000281
Steve Anton10542f22019-01-11 09:11:00 -0800282#endif // RTC_BASE_SSL_STREAM_ADAPTER_H_