blob: 04d0fc5dd48345b0cbbf4cbc54e9ba4287071d8a [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>
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <memory>
17#include <string>
18#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:2619
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/ssl_certificate.h"
21#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "rtc_base/stream.h"
Yves Gerey3e707812018-11-28 15:47:4923#include "rtc_base/third_party/sigslot/sigslot.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5024
25namespace rtc {
26
27// Constants for SSL profile.
28const int TLS_NULL_WITH_NULL_NULL = 0;
Qingsi Wang7fc821d2018-07-12 19:54:5329const int SSL_CIPHER_SUITE_MAX_VALUE = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 05:52:5030
31// Constants for SRTP profiles.
32const int SRTP_INVALID_CRYPTO_SUITE = 0;
33#ifndef SRTP_AES128_CM_SHA1_80
34const int SRTP_AES128_CM_SHA1_80 = 0x0001;
35#endif
36#ifndef SRTP_AES128_CM_SHA1_32
37const int SRTP_AES128_CM_SHA1_32 = 0x0002;
38#endif
39#ifndef SRTP_AEAD_AES_128_GCM
40const int SRTP_AEAD_AES_128_GCM = 0x0007;
41#endif
42#ifndef SRTP_AEAD_AES_256_GCM
43const int SRTP_AEAD_AES_256_GCM = 0x0008;
44#endif
Qingsi Wang7fc821d2018-07-12 19:54:5345const int SRTP_CRYPTO_SUITE_MAX_VALUE = 0xFFFF;
Henrik Kjellanderec78f1c2017-06-29 05:52:5046
47// 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[];
56
57// Given the DTLS-SRTP protection profile ID, as defined in
58// https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile
59// name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2.
60std::string SrtpCryptoSuiteToName(int crypto_suite);
61
62// The reverse of above conversion.
63int SrtpCryptoSuiteFromName(const std::string& crypto_suite);
64
65// Get key length and salt length for given crypto suite. Returns true for
66// valid suites, otherwise false.
Jian Cui0a8798b2017-11-17 00:58:0267bool GetSrtpKeyAndSaltLengths(int crypto_suite,
68 int* key_length,
69 int* salt_length);
Henrik Kjellanderec78f1c2017-06-29 05:52:5070
71// Returns true if the given crypto suite id uses a GCM cipher.
72bool IsGcmCryptoSuite(int crypto_suite);
73
74// Returns true if the given crypto suite name uses a GCM cipher.
75bool IsGcmCryptoSuiteName(const std::string& crypto_suite);
76
Henrik Kjellanderec78f1c2017-06-29 05:52:5077// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
78// After SSL has been started, the stream will only open on successful
79// SSL verification of certificates, and the communication is
80// encrypted of course.
81//
82// This class was written with SSLAdapter as a starting point. It
83// offers a similar interface, with two differences: there is no
84// support for a restartable SSL connection, and this class has a
85// peer-to-peer mode.
86//
87// The SSL library requires initialization and cleanup. Static method
88// for doing this are in SSLAdapter. They should possibly be moved out
89// to a neutral class.
90
Henrik Kjellanderec78f1c2017-06-29 05:52:5091enum SSLRole { SSL_CLIENT, SSL_SERVER };
92enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
93enum SSLProtocolVersion {
94 SSL_PROTOCOL_TLS_10,
95 SSL_PROTOCOL_TLS_11,
96 SSL_PROTOCOL_TLS_12,
97 SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11,
98 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
99};
100enum class SSLPeerCertificateDigestError {
101 NONE,
102 UNKNOWN_ALGORITHM,
103 INVALID_LENGTH,
104 VERIFICATION_FAILED,
105};
106
107// Errors for Read -- in the high range so no conflict with OpenSSL.
108enum { SSE_MSG_TRUNC = 0xff0001 };
109
110// Used to send back UMA histogram value. Logged when Dtls handshake fails.
111enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
112
113class SSLStreamAdapter : public StreamAdapterInterface {
114 public:
115 // Instantiate an SSLStreamAdapter wrapping the given stream,
116 // (using the selected implementation for the platform).
117 // Caller is responsible for freeing the returned object.
118 static SSLStreamAdapter* Create(StreamInterface* stream);
119
120 explicit SSLStreamAdapter(StreamInterface* stream);
121 ~SSLStreamAdapter() override;
122
Henrik Kjellanderec78f1c2017-06-29 05:52:50123 // Specify our SSL identity: key and certificate. SSLStream takes ownership
124 // of the SSLIdentity object and will free it when appropriate. Should be
125 // called no more than once on a given SSLStream instance.
126 virtual void SetIdentity(SSLIdentity* identity) = 0;
127
128 // Call this to indicate that we are to play the server role (or client role,
129 // if the default argument is replaced by SSL_CLIENT).
130 // The default argument is for backward compatibility.
131 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
132 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
133
134 // Do DTLS or TLS.
135 virtual void SetMode(SSLMode mode) = 0;
136
137 // Set maximum supported protocol version. The highest version supported by
138 // both ends will be used for the connection, i.e. if one party supports
139 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
140 // If requested version is not supported by underlying crypto library, the
141 // next lower will be used.
142 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
143
144 // Set the initial retransmission timeout for DTLS messages. When the timeout
145 // expires, the message gets retransmitted and the timeout is exponentially
146 // increased.
147 // This should only be called before StartSSL().
148 virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0;
149
150 // StartSSL starts negotiation with a peer, whose certificate is verified
151 // using the certificate digest. Generally, SetIdentity() and possibly
152 // SetServerRole() should have been called before this.
153 // SetPeerCertificateDigest() must also be called. It may be called after
154 // StartSSLWithPeer() but must be called before the underlying stream opens.
155 //
156 // Use of the stream prior to calling StartSSL will pass data in clear text.
157 // Calling StartSSL causes SSL negotiation to begin as soon as possible: right
158 // away if the underlying wrapped stream is already opened, or else as soon as
159 // it opens.
160 //
161 // StartSSL returns a negative error code on failure. Returning 0 means
162 // success so far, but negotiation is probably not complete and will continue
163 // asynchronously. In that case, the exposed stream will open after
164 // successful negotiation and verification, or an SE_CLOSE event will be
165 // raised if negotiation fails.
166 virtual int StartSSL() = 0;
167
168 // Specify the digest of the certificate that our peer is expected to use.
169 // Only this certificate will be accepted during SSL verification. The
170 // certificate is assumed to have been obtained through some other secure
171 // channel (such as the signaling channel). This must specify the terminal
172 // certificate, not just a CA. SSLStream makes a copy of the digest value.
173 //
174 // Returns true if successful.
175 // |error| is optional and provides more information about the failure.
176 virtual bool SetPeerCertificateDigest(
177 const std::string& digest_alg,
178 const unsigned char* digest_val,
179 size_t digest_len,
180 SSLPeerCertificateDigestError* error = nullptr) = 0;
181
Taylor Brandstetterc3928662018-02-23 21:04:51182 // Retrieves the peer's certificate chain including leaf certificate, if a
Jian Cui0a8798b2017-11-17 00:58:02183 // connection has been established.
184 virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0;
185
Henrik Kjellanderec78f1c2017-06-29 05:52:50186 // Retrieves the IANA registration id of the cipher suite used for the
187 // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA").
188 virtual bool GetSslCipherSuite(int* cipher_suite);
189
190 virtual int GetSslVersion() const = 0;
191
192 // Key Exporter interface from RFC 5705
193 // Arguments are:
194 // label -- the exporter label.
195 // part of the RFC defining each exporter
196 // usage (IN)
197 // context/context_len -- a context to bind to for this connection;
198 // optional, can be null, 0 (IN)
199 // use_context -- whether to use the context value
200 // (needed to distinguish no context from
201 // zero-length ones).
202 // result -- where to put the computed value
203 // result_len -- the length of the computed value
204 virtual bool ExportKeyingMaterial(const std::string& label,
205 const uint8_t* context,
206 size_t context_len,
207 bool use_context,
208 uint8_t* result,
209 size_t result_len);
210
211 // DTLS-SRTP interface
212 virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites);
213 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite);
214
215 // Returns true if a TLS connection has been established.
216 // The only difference between this and "GetState() == SE_OPEN" is that if
217 // the peer certificate digest hasn't been verified, the state will still be
218 // SS_OPENING but IsTlsConnected should return true.
219 virtual bool IsTlsConnected() = 0;
220
221 // Capabilities testing.
222 // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now
223 // that's assumed.
224 static bool IsBoringSsl();
225
226 // Returns true iff the supplied cipher is deemed to be strong.
227 // TODO(torbjorng): Consider removing the KeyType argument.
228 static bool IsAcceptableCipher(int cipher, KeyType key_type);
229 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
230
231 // TODO(guoweis): Move this away from a static class method. Currently this is
232 // introduced such that any caller could depend on sslstreamadapter.h without
233 // depending on specific SSL implementation.
234 static std::string SslCipherSuiteToName(int cipher_suite);
235
Benjamin Wrightb19b4972018-10-25 17:46:49236 ////////////////////////////////////////////////////////////////////////////
237 // Testing only member functions
238 ////////////////////////////////////////////////////////////////////////////
239
Henrik Kjellanderec78f1c2017-06-29 05:52:50240 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
241 // using a fake clock.
Benjamin Wrightb19b4972018-10-25 17:46:49242 static void EnableTimeCallbackForTesting();
243
244 // Deprecated. Do not use this API outside of testing.
245 // Do not set this to false outside of testing.
246 void SetClientAuthEnabledForTesting(bool enabled) {
247 client_auth_enabled_ = enabled;
248 }
249
250 // Deprecated. Do not use this API outside of testing.
251 // Returns true by default, else false if explicitly set to disable client
252 // authentication.
253 bool GetClientAuthEnabled() const { return client_auth_enabled_; }
Henrik Kjellanderec78f1c2017-06-29 05:52:50254
255 sigslot::signal1<SSLHandshakeError> SignalSSLHandshakeError;
256
257 private:
Henrik Kjellanderec78f1c2017-06-29 05:52:50258 // If true (default), the client is required to provide a certificate during
259 // handshake. If no certificate is given, handshake fails. This applies to
260 // server mode only.
Benjamin Wrightb19b4972018-10-25 17:46:49261 bool client_auth_enabled_ = true;
Henrik Kjellanderec78f1c2017-06-29 05:52:50262};
263
264} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26265
Steve Anton10542f22019-01-11 17:11:00266#endif // RTC_BASE_SSL_STREAM_ADAPTER_H_