blob: 701cc4437ba94eeb99200e3ba5bdbaa5edad009b [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>
18#include <string>
19#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:2620
Tommi59574ca2023-09-05 07:21:5721#include "absl/functional/any_invocable.h"
Harald Alvestrand8515d5a2020-03-20 21:51:3222#include "absl/memory/memory.h"
Ali Tofigh7fa90572022-03-17 14:47:4923#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/ssl_certificate.h"
25#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "rtc_base/stream.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5027
28namespace rtc {
29
30// Constants for SSL profile.
Mirko Bonadei7750d802021-07-26 15:27:4231constexpr int kTlsNullWithNullNull = 0;
32constexpr int kSslCipherSuiteMaxValue = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 05:52:5033
Mirko Bonadei7750d802021-07-26 15:27:4234// Constants for SRTP profiles.
35constexpr int kSrtpInvalidCryptoSuite = 0;
36constexpr int kSrtpAes128CmSha1_80 = 0x0001;
37constexpr int kSrtpAes128CmSha1_32 = 0x0002;
38constexpr int kSrtpAeadAes128Gcm = 0x0007;
39constexpr int kSrtpAeadAes256Gcm = 0x0008;
40constexpr int kSrtpCryptoSuiteMaxValue = 0xFFFF;
41
Philipp Hancke36e4dd22023-09-28 09:02:2142// Constants for SSL signature algorithms.
43constexpr int kSslSignatureAlgorithmUnknown = 0;
44constexpr int kSslSignatureAlgorithmMaxValue = 0xFFFF;
45
Mirko Bonadei7750d802021-07-26 15:27:4246// Names of SRTP profiles listed above.
47// 128-bit AES with 80-bit SHA-1 HMAC.
48extern const char kCsAesCm128HmacSha1_80[];
49// 128-bit AES with 32-bit SHA-1 HMAC.
50extern const char kCsAesCm128HmacSha1_32[];
51// 128-bit AES GCM with 16 byte AEAD auth tag.
52extern const char kCsAeadAes128Gcm[];
53// 256-bit AES GCM with 16 byte AEAD auth tag.
54extern const char kCsAeadAes256Gcm[];
Henrik Kjellanderec78f1c2017-06-29 05:52:5055
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.
59std::string SrtpCryptoSuiteToName(int crypto_suite);
60
61// The reverse of above conversion.
Ali Tofigh7fa90572022-03-17 14:47:4962int SrtpCryptoSuiteFromName(absl::string_view crypto_suite);
Henrik Kjellanderec78f1c2017-06-29 05:52:5063
64// 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
73// Returns true if the given crypto suite name uses a GCM cipher.
Ali Tofigh7fa90572022-03-17 14:47:4974bool IsGcmCryptoSuiteName(absl::string_view crypto_suite);
Henrik Kjellanderec78f1c2017-06-29 05:52:5075
Henrik Kjellanderec78f1c2017-06-29 05:52:5076// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
77// After SSL has been started, the stream will only open on successful
78// SSL verification of certificates, and the communication is
79// encrypted of course.
80//
81// This class was written with SSLAdapter as a starting point. It
82// offers a similar interface, with two differences: there is no
83// support for a restartable SSL connection, and this class has a
84// peer-to-peer mode.
85//
86// The SSL library requires initialization and cleanup. Static method
87// for doing this are in SSLAdapter. They should possibly be moved out
88// to a neutral class.
89
Henrik Kjellanderec78f1c2017-06-29 05:52:5090enum SSLRole { SSL_CLIENT, SSL_SERVER };
91enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Harald Alvestrand13799132020-03-09 18:39:3692
Guido Urdanetaae2e8642020-10-26 08:55:2693// Note: TLS_10, TLS_11, and DTLS_10 will all be ignored, and only DTLS1_2 will
94// be accepted unless the trial flag WebRTC-LegacyTlsProtocols/Enabled/ is
95// passed in or an explicit override is used. Support for the legacy protocol
96// versions will be completely removed in the future.
97// See https://bugs.webrtc.org/10261.
Henrik Kjellanderec78f1c2017-06-29 05:52:5098enum SSLProtocolVersion {
Harald Alvestrand5cb78072019-10-28 08:51:1799 SSL_PROTOCOL_NOT_GIVEN = -1,
100 SSL_PROTOCOL_TLS_10 = 0,
Henrik Kjellanderec78f1c2017-06-29 05:52:50101 SSL_PROTOCOL_TLS_11,
102 SSL_PROTOCOL_TLS_12,
103 SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11,
104 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
105};
106enum class SSLPeerCertificateDigestError {
107 NONE,
108 UNKNOWN_ALGORITHM,
109 INVALID_LENGTH,
110 VERIFICATION_FAILED,
111};
112
113// Errors for Read -- in the high range so no conflict with OpenSSL.
114enum { SSE_MSG_TRUNC = 0xff0001 };
115
116// Used to send back UMA histogram value. Logged when Dtls handshake fails.
117enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
118
Tommi48df56e2023-09-05 11:16:47119class SSLStreamAdapter : public StreamInterface {
Henrik Kjellanderec78f1c2017-06-29 05:52:50120 public:
121 // Instantiate an SSLStreamAdapter wrapping the given stream,
122 // (using the selected implementation for the platform).
123 // Caller is responsible for freeing the returned object.
Harald Alvestrand8515d5a2020-03-20 21:51:32124 static std::unique_ptr<SSLStreamAdapter> Create(
Tommi59574ca2023-09-05 07:21:57125 std::unique_ptr<StreamInterface> stream,
126 absl::AnyInvocable<void(SSLHandshakeError)> handshake_error = nullptr);
Henrik Kjellanderec78f1c2017-06-29 05:52:50127
Niels Möller0131a4d2021-04-16 07:16:21128 SSLStreamAdapter() = default;
129 ~SSLStreamAdapter() override = default;
Henrik Kjellanderec78f1c2017-06-29 05:52:50130
Henrik Kjellanderec78f1c2017-06-29 05:52:50131 // Specify our SSL identity: key and certificate. SSLStream takes ownership
132 // of the SSLIdentity object and will free it when appropriate. Should be
133 // called no more than once on a given SSLStream instance.
Harald Alvestrand8515d5a2020-03-20 21:51:32134 virtual void SetIdentity(std::unique_ptr<SSLIdentity> identity) = 0;
Harald Alvestrand8515d5a2020-03-20 21:51:32135 virtual SSLIdentity* GetIdentityForTesting() const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50136
137 // Call this to indicate that we are to play the server role (or client role,
138 // if the default argument is replaced by SSL_CLIENT).
139 // The default argument is for backward compatibility.
140 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
141 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
142
143 // Do DTLS or TLS.
144 virtual void SetMode(SSLMode mode) = 0;
145
146 // Set maximum supported protocol version. The highest version supported by
147 // both ends will be used for the connection, i.e. if one party supports
148 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
149 // If requested version is not supported by underlying crypto library, the
150 // next lower will be used.
151 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
152
153 // Set the initial retransmission timeout for DTLS messages. When the timeout
154 // expires, the message gets retransmitted and the timeout is exponentially
155 // increased.
156 // This should only be called before StartSSL().
157 virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0;
158
159 // StartSSL starts negotiation with a peer, whose certificate is verified
160 // using the certificate digest. Generally, SetIdentity() and possibly
161 // SetServerRole() should have been called before this.
162 // SetPeerCertificateDigest() must also be called. It may be called after
163 // StartSSLWithPeer() but must be called before the underlying stream opens.
164 //
165 // Use of the stream prior to calling StartSSL will pass data in clear text.
166 // Calling StartSSL causes SSL negotiation to begin as soon as possible: right
167 // away if the underlying wrapped stream is already opened, or else as soon as
168 // it opens.
169 //
170 // StartSSL returns a negative error code on failure. Returning 0 means
171 // success so far, but negotiation is probably not complete and will continue
172 // asynchronously. In that case, the exposed stream will open after
173 // successful negotiation and verification, or an SE_CLOSE event will be
174 // raised if negotiation fails.
175 virtual int StartSSL() = 0;
176
177 // Specify the digest of the certificate that our peer is expected to use.
178 // Only this certificate will be accepted during SSL verification. The
179 // certificate is assumed to have been obtained through some other secure
180 // channel (such as the signaling channel). This must specify the terminal
181 // certificate, not just a CA. SSLStream makes a copy of the digest value.
182 //
183 // Returns true if successful.
Artem Titov96e3b992021-07-26 14:03:14184 // `error` is optional and provides more information about the failure.
Henrik Kjellanderec78f1c2017-06-29 05:52:50185 virtual bool SetPeerCertificateDigest(
Ali Tofigh7fa90572022-03-17 14:47:49186 absl::string_view digest_alg,
Henrik Kjellanderec78f1c2017-06-29 05:52:50187 const unsigned char* digest_val,
188 size_t digest_len,
189 SSLPeerCertificateDigestError* error = nullptr) = 0;
190
Taylor Brandstetterc3928662018-02-23 21:04:51191 // Retrieves the peer's certificate chain including leaf certificate, if a
Jian Cui0a8798b2017-11-17 00:58:02192 // connection has been established.
193 virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0;
194
Henrik Kjellanderec78f1c2017-06-29 05:52:50195 // Retrieves the IANA registration id of the cipher suite used for the
196 // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA").
197 virtual bool GetSslCipherSuite(int* cipher_suite);
198
Harald Alvestrand5cb78072019-10-28 08:51:17199 // Retrieves the enum value for SSL version.
200 // Will return -1 until the version has been negotiated.
201 virtual SSLProtocolVersion GetSslVersion() const = 0;
202 // Retrieves the 2-byte version from the TLS protocol.
203 // Will return false until the version has been negotiated.
204 virtual bool GetSslVersionBytes(int* version) const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50205
206 // Key Exporter interface from RFC 5705
207 // Arguments are:
208 // label -- the exporter label.
209 // part of the RFC defining each exporter
210 // usage (IN)
211 // context/context_len -- a context to bind to for this connection;
212 // optional, can be null, 0 (IN)
213 // use_context -- whether to use the context value
214 // (needed to distinguish no context from
215 // zero-length ones).
216 // result -- where to put the computed value
217 // result_len -- the length of the computed value
Ali Tofigh7fa90572022-03-17 14:47:49218 virtual bool ExportKeyingMaterial(absl::string_view label,
Henrik Kjellanderec78f1c2017-06-29 05:52:50219 const uint8_t* context,
220 size_t context_len,
221 bool use_context,
222 uint8_t* result,
223 size_t result_len);
224
Philipp Hancke36e4dd22023-09-28 09:02:21225 // Returns the signature algorithm or 0 if not applicable.
226 virtual uint16_t GetPeerSignatureAlgorithm() const = 0;
227
Henrik Kjellanderec78f1c2017-06-29 05:52:50228 // DTLS-SRTP interface
229 virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites);
230 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite);
231
232 // Returns true if a TLS connection has been established.
233 // The only difference between this and "GetState() == SE_OPEN" is that if
234 // the peer certificate digest hasn't been verified, the state will still be
235 // SS_OPENING but IsTlsConnected should return true.
236 virtual bool IsTlsConnected() = 0;
237
238 // Capabilities testing.
239 // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now
240 // that's assumed.
241 static bool IsBoringSsl();
242
243 // Returns true iff the supplied cipher is deemed to be strong.
244 // TODO(torbjorng): Consider removing the KeyType argument.
245 static bool IsAcceptableCipher(int cipher, KeyType key_type);
Ali Tofigh7fa90572022-03-17 14:47:49246 static bool IsAcceptableCipher(absl::string_view cipher, KeyType key_type);
Henrik Kjellanderec78f1c2017-06-29 05:52:50247
248 // TODO(guoweis): Move this away from a static class method. Currently this is
249 // introduced such that any caller could depend on sslstreamadapter.h without
250 // depending on specific SSL implementation.
251 static std::string SslCipherSuiteToName(int cipher_suite);
252
Benjamin Wrightb19b4972018-10-25 17:46:49253 ////////////////////////////////////////////////////////////////////////////
254 // Testing only member functions
255 ////////////////////////////////////////////////////////////////////////////
256
Henrik Kjellanderec78f1c2017-06-29 05:52:50257 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
258 // using a fake clock.
Benjamin Wrightb19b4972018-10-25 17:46:49259 static void EnableTimeCallbackForTesting();
260
261 // Deprecated. Do not use this API outside of testing.
262 // Do not set this to false outside of testing.
263 void SetClientAuthEnabledForTesting(bool enabled) {
264 client_auth_enabled_ = enabled;
265 }
266
267 // Deprecated. Do not use this API outside of testing.
268 // Returns true by default, else false if explicitly set to disable client
269 // authentication.
270 bool GetClientAuthEnabled() const { return client_auth_enabled_; }
Henrik Kjellanderec78f1c2017-06-29 05:52:50271
Henrik Kjellanderec78f1c2017-06-29 05:52:50272 private:
Henrik Kjellanderec78f1c2017-06-29 05:52:50273 // 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 17:46:49276 bool client_auth_enabled_ = true;
Henrik Kjellanderec78f1c2017-06-29 05:52:50277};
278
279} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26280
Steve Anton10542f22019-01-11 17:11:00281#endif // RTC_BASE_SSL_STREAM_ADAPTER_H_