blob: 62a724996ef621cc7ea368c30a1589a28ee07bb8 [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
11#ifndef WEBRTC_BASE_SSLSTREAMADAPTER_H_
12#define WEBRTC_BASE_SSLSTREAMADAPTER_H_
13
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <string>
16#include <vector>
17
18#include "webrtc/base/stream.h"
19#include "webrtc/base/sslidentity.h"
20
21namespace rtc {
22
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080023// Constants for SSL profile.
24const int TLS_NULL_WITH_NULL_NULL = 0;
25
Guo-wei Shieh456696a2015-09-30 21:48:54 -070026// Constants for SRTP profiles.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080027const int SRTP_INVALID_CRYPTO_SUITE = 0;
torbjorng4b9d1de2016-04-09 11:35:29 -070028#ifndef SRTP_AES128_CM_SHA1_80
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -070029const int SRTP_AES128_CM_SHA1_80 = 0x0001;
torbjorng4b9d1de2016-04-09 11:35:29 -070030#endif
31#ifndef SRTP_AES128_CM_SHA1_32
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -070032const int SRTP_AES128_CM_SHA1_32 = 0x0002;
torbjorng4b9d1de2016-04-09 11:35:29 -070033#endif
jbauchcb560652016-08-04 05:20:32 -070034#ifndef SRTP_AEAD_AES_128_GCM
35const int SRTP_AEAD_AES_128_GCM = 0x0007;
36#endif
37#ifndef SRTP_AEAD_AES_256_GCM
38const int SRTP_AEAD_AES_256_GCM = 0x0008;
39#endif
Guo-wei Shieh456696a2015-09-30 21:48:54 -070040
deadbeef7914b8c2017-04-21 03:23:33 -070041// Names of SRTP profiles listed above.
Guo-wei Shieh456696a2015-09-30 21:48:54 -070042// 128-bit AES with 80-bit SHA-1 HMAC.
43extern const char CS_AES_CM_128_HMAC_SHA1_80[];
44// 128-bit AES with 32-bit SHA-1 HMAC.
45extern const char CS_AES_CM_128_HMAC_SHA1_32[];
jbauchcb560652016-08-04 05:20:32 -070046// 128-bit AES GCM with 16 byte AEAD auth tag.
47extern const char CS_AEAD_AES_128_GCM[];
48// 256-bit AES GCM with 16 byte AEAD auth tag.
49extern const char CS_AEAD_AES_256_GCM[];
Guo-wei Shieh456696a2015-09-30 21:48:54 -070050
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080051// Given the DTLS-SRTP protection profile ID, as defined in
52// https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile
53// name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2.
54std::string SrtpCryptoSuiteToName(int crypto_suite);
55
56// The reverse of above conversion.
57int SrtpCryptoSuiteFromName(const std::string& crypto_suite);
Guo-wei Shieh456696a2015-09-30 21:48:54 -070058
jbauchcb560652016-08-04 05:20:32 -070059// Get key length and salt length for given crypto suite. Returns true for
60// valid suites, otherwise false.
61bool GetSrtpKeyAndSaltLengths(int crypto_suite, int *key_length,
62 int *salt_length);
63
64// Returns true if the given crypto suite id uses a GCM cipher.
65bool IsGcmCryptoSuite(int crypto_suite);
66
67// Returns true if the given crypto suite name uses a GCM cipher.
68bool IsGcmCryptoSuiteName(const std::string& crypto_suite);
69
70struct CryptoOptions {
71 CryptoOptions() {}
72
73 // Helper method to return an instance of the CryptoOptions with GCM crypto
74 // suites disabled. This method should be used instead of depending on current
75 // default values set by the constructor.
76 static CryptoOptions NoGcm();
77
78 // Enable GCM crypto suites from RFC 7714 for SRTP. GCM will only be used
79 // if both sides enable it.
80 bool enable_gcm_crypto_suites = false;
81};
82
deadbeef7914b8c2017-04-21 03:23:33 -070083// Returns supported crypto suites, given |crypto_options|.
84// CS_AES_CM_128_HMAC_SHA1_32 will be preferred by default.
85std::vector<int> GetSupportedDtlsSrtpCryptoSuites(
86 const rtc::CryptoOptions& crypto_options);
87
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
89// After SSL has been started, the stream will only open on successful
90// SSL verification of certificates, and the communication is
91// encrypted of course.
92//
93// This class was written with SSLAdapter as a starting point. It
94// offers a similar interface, with two differences: there is no
95// support for a restartable SSL connection, and this class has a
96// peer-to-peer mode.
97//
98// The SSL library requires initialization and cleanup. Static method
99// for doing this are in SSLAdapter. They should possibly be moved out
100// to a neutral class.
101
102
103enum SSLRole { SSL_CLIENT, SSL_SERVER };
104enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Joachim Bauch831c5582015-05-20 12:48:41 +0200105enum SSLProtocolVersion {
106 SSL_PROTOCOL_TLS_10,
107 SSL_PROTOCOL_TLS_11,
108 SSL_PROTOCOL_TLS_12,
109 SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11,
110 SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12,
111};
deadbeef89824f62016-09-30 11:55:43 -0700112enum class SSLPeerCertificateDigestError {
113 NONE,
114 UNKNOWN_ALGORITHM,
115 INVALID_LENGTH,
116 VERIFICATION_FAILED,
117};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118
119// Errors for Read -- in the high range so no conflict with OpenSSL.
120enum { SSE_MSG_TRUNC = 0xff0001 };
121
zhihuangd82eee02016-08-26 11:25:05 -0700122// Used to send back UMA histogram value. Logged when Dtls handshake fails.
123enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
124
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125class SSLStreamAdapter : public StreamAdapterInterface {
126 public:
127 // Instantiate an SSLStreamAdapter wrapping the given stream,
128 // (using the selected implementation for the platform).
129 // Caller is responsible for freeing the returned object.
130 static SSLStreamAdapter* Create(StreamInterface* stream);
131
zhihuangd82eee02016-08-26 11:25:05 -0700132 explicit SSLStreamAdapter(StreamInterface* stream);
133 ~SSLStreamAdapter() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134
135 void set_ignore_bad_cert(bool ignore) { ignore_bad_cert_ = ignore; }
136 bool ignore_bad_cert() const { return ignore_bad_cert_; }
137
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000138 void set_client_auth_enabled(bool enabled) { client_auth_enabled_ = enabled; }
139 bool client_auth_enabled() const { return client_auth_enabled_; }
140
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700141 // Specify our SSL identity: key and certificate. SSLStream takes ownership
142 // of the SSLIdentity object and will free it when appropriate. Should be
143 // called no more than once on a given SSLStream instance.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144 virtual void SetIdentity(SSLIdentity* identity) = 0;
145
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700146 // Call this to indicate that we are to play the server role (or client role,
147 // if the default argument is replaced by SSL_CLIENT).
148 // The default argument is for backward compatibility.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
150 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
151
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700152 // Do DTLS or TLS.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153 virtual void SetMode(SSLMode mode) = 0;
154
Joachim Bauch831c5582015-05-20 12:48:41 +0200155 // Set maximum supported protocol version. The highest version supported by
156 // both ends will be used for the connection, i.e. if one party supports
157 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
158 // If requested version is not supported by underlying crypto library, the
159 // next lower will be used.
160 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
161
skvladd0309122017-02-02 17:18:37 -0800162 // Set the initial retransmission timeout for DTLS messages. When the timeout
163 // expires, the message gets retransmitted and the timeout is exponentially
164 // increased.
165 // This should only be called before StartSSL().
166 virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0;
167
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700168 // StartSSL starts negotiation with a peer, whose certificate is verified
169 // using the certificate digest. Generally, SetIdentity() and possibly
170 // SetServerRole() should have been called before this.
171 // SetPeerCertificateDigest() must also be called. It may be called after
172 // StartSSLWithPeer() but must be called before the underlying stream opens.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173 //
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700174 // Use of the stream prior to calling StartSSL will pass data in clear text.
175 // Calling StartSSL causes SSL negotiation to begin as soon as possible: right
176 // away if the underlying wrapped stream is already opened, or else as soon as
177 // it opens.
178 //
179 // StartSSL returns a negative error code on failure. Returning 0 means
180 // success so far, but negotiation is probably not complete and will continue
181 // asynchronously. In that case, the exposed stream will open after
182 // successful negotiation and verification, or an SE_CLOSE event will be
183 // raised if negotiation fails.
184 virtual int StartSSL() = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700186 // Specify the digest of the certificate that our peer is expected to use.
187 // Only this certificate will be accepted during SSL verification. The
188 // certificate is assumed to have been obtained through some other secure
189 // channel (such as the signaling channel). This must specify the terminal
190 // certificate, not just a CA. SSLStream makes a copy of the digest value.
deadbeef89824f62016-09-30 11:55:43 -0700191 //
192 // Returns true if successful.
193 // |error| is optional and provides more information about the failure.
194 virtual bool SetPeerCertificateDigest(
195 const std::string& digest_alg,
196 const unsigned char* digest_val,
197 size_t digest_len,
198 SSLPeerCertificateDigestError* error = nullptr) = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199
200 // Retrieves the peer's X.509 certificate, if a connection has been
201 // established. It returns the transmitted over SSL, including the entire
kwibergb4d01c42016-04-06 05:15:06 -0700202 // chain.
jbauch555604a2016-04-26 03:13:22 -0700203 virtual std::unique_ptr<SSLCertificate> GetPeerCertificate() const = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000204
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700205 // Retrieves the IANA registration id of the cipher suite used for the
206 // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA").
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800207 virtual bool GetSslCipherSuite(int* cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000208
torbjorng43166b82016-03-11 00:06:47 -0800209 virtual int GetSslVersion() const = 0;
210
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 // Key Exporter interface from RFC 5705
212 // Arguments are:
213 // label -- the exporter label.
214 // part of the RFC defining each exporter
215 // usage (IN)
216 // context/context_len -- a context to bind to for this connection;
deadbeef37f5ecf2017-02-27 14:06:41 -0800217 // optional, can be null, 0 (IN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218 // use_context -- whether to use the context value
219 // (needed to distinguish no context from
220 // zero-length ones).
221 // result -- where to put the computed value
222 // result_len -- the length of the computed value
223 virtual bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200224 const uint8_t* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225 size_t context_len,
226 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200227 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000228 size_t result_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229
230 // DTLS-SRTP interface
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800231 virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites);
232 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233
deadbeef89824f62016-09-30 11:55:43 -0700234 // Returns true if a TLS connection has been established.
235 // The only difference between this and "GetState() == SE_OPEN" is that if
236 // the peer certificate digest hasn't been verified, the state will still be
237 // SS_OPENING but IsTlsConnected should return true.
238 virtual bool IsTlsConnected() = 0;
239
deadbeef1b54a5f2017-01-23 19:39:57 -0800240 // Capabilities testing.
241 // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now
242 // that's assumed.
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700243 static bool IsBoringSsl();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244
torbjorng43166b82016-03-11 00:06:47 -0800245 // Returns true iff the supplied cipher is deemed to be strong.
246 // TODO(torbjorng): Consider removing the KeyType argument.
247 static bool IsAcceptableCipher(int cipher, KeyType key_type);
248 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700249
250 // TODO(guoweis): Move this away from a static class method. Currently this is
251 // introduced such that any caller could depend on sslstreamadapter.h without
252 // depending on specific SSL implementation.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800253 static std::string SslCipherSuiteToName(int cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000254
deadbeef6cf94a02016-11-28 17:38:34 -0800255 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
256 // using a fake clock.
257 static void enable_time_callback_for_testing();
258
zhihuangd82eee02016-08-26 11:25:05 -0700259 sigslot::signal1<SSLHandshakeError> SignalSSLHandshakeError;
260
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000261 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 // If true, the server certificate need not match the configured
263 // server_name, and in fact missing certificate authority and other
264 // verification errors are ignored.
265 bool ignore_bad_cert_;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000266
267 // If true (default), the client is required to provide a certificate during
268 // handshake. If no certificate is given, handshake fails. This applies to
269 // server mode only.
270 bool client_auth_enabled_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000271};
272
273} // namespace rtc
274
275#endif // WEBRTC_BASE_SSLSTREAMADAPTER_H_