blob: 391019165ff9558efaf8e41ff85f4dc877f73d92 [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
41// Cipher suite to use for SRTP. Typically a 80-bit HMAC will be used, except
42// in applications (voice) where the additional bandwidth may be significant.
43// A 80-bit HMAC is always used for SRTCP.
44// 128-bit AES with 80-bit SHA-1 HMAC.
45extern const char CS_AES_CM_128_HMAC_SHA1_80[];
46// 128-bit AES with 32-bit SHA-1 HMAC.
47extern const char CS_AES_CM_128_HMAC_SHA1_32[];
jbauchcb560652016-08-04 05:20:32 -070048// 128-bit AES GCM with 16 byte AEAD auth tag.
49extern const char CS_AEAD_AES_128_GCM[];
50// 256-bit AES GCM with 16 byte AEAD auth tag.
51extern const char CS_AEAD_AES_256_GCM[];
Guo-wei Shieh456696a2015-09-30 21:48:54 -070052
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080053// Given the DTLS-SRTP protection profile ID, as defined in
54// https://tools.ietf.org/html/rfc4568#section-6.2 , return the SRTP profile
55// name, as defined in https://tools.ietf.org/html/rfc5764#section-4.1.2.
56std::string SrtpCryptoSuiteToName(int crypto_suite);
57
58// The reverse of above conversion.
59int SrtpCryptoSuiteFromName(const std::string& crypto_suite);
Guo-wei Shieh456696a2015-09-30 21:48:54 -070060
jbauchcb560652016-08-04 05:20:32 -070061// Get key length and salt length for given crypto suite. Returns true for
62// valid suites, otherwise false.
63bool GetSrtpKeyAndSaltLengths(int crypto_suite, int *key_length,
64 int *salt_length);
65
66// Returns true if the given crypto suite id uses a GCM cipher.
67bool IsGcmCryptoSuite(int crypto_suite);
68
69// Returns true if the given crypto suite name uses a GCM cipher.
70bool IsGcmCryptoSuiteName(const std::string& crypto_suite);
71
72struct CryptoOptions {
73 CryptoOptions() {}
74
75 // Helper method to return an instance of the CryptoOptions with GCM crypto
76 // suites disabled. This method should be used instead of depending on current
77 // default values set by the constructor.
78 static CryptoOptions NoGcm();
79
80 // Enable GCM crypto suites from RFC 7714 for SRTP. GCM will only be used
81 // if both sides enable it.
82 bool enable_gcm_crypto_suites = false;
83};
84
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085// SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
86// After SSL has been started, the stream will only open on successful
87// SSL verification of certificates, and the communication is
88// encrypted of course.
89//
90// This class was written with SSLAdapter as a starting point. It
91// offers a similar interface, with two differences: there is no
92// support for a restartable SSL connection, and this class has a
93// peer-to-peer mode.
94//
95// The SSL library requires initialization and cleanup. Static method
96// for doing this are in SSLAdapter. They should possibly be moved out
97// to a neutral class.
98
99
100enum SSLRole { SSL_CLIENT, SSL_SERVER };
101enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
Joachim Bauch831c5582015-05-20 12:48:41 +0200102enum SSLProtocolVersion {
103 SSL_PROTOCOL_TLS_10,
104 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};
deadbeef89824f62016-09-30 11:55:43 -0700109enum class SSLPeerCertificateDigestError {
110 NONE,
111 UNKNOWN_ALGORITHM,
112 INVALID_LENGTH,
113 VERIFICATION_FAILED,
114};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115
116// Errors for Read -- in the high range so no conflict with OpenSSL.
117enum { SSE_MSG_TRUNC = 0xff0001 };
118
zhihuangd82eee02016-08-26 11:25:05 -0700119// Used to send back UMA histogram value. Logged when Dtls handshake fails.
120enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE };
121
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122class 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.
127 static SSLStreamAdapter* Create(StreamInterface* stream);
128
zhihuangd82eee02016-08-26 11:25:05 -0700129 explicit SSLStreamAdapter(StreamInterface* stream);
130 ~SSLStreamAdapter() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131
132 void set_ignore_bad_cert(bool ignore) { ignore_bad_cert_ = ignore; }
133 bool ignore_bad_cert() const { return ignore_bad_cert_; }
134
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000135 void set_client_auth_enabled(bool enabled) { client_auth_enabled_ = enabled; }
136 bool client_auth_enabled() const { return client_auth_enabled_; }
137
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700138 // Specify our SSL identity: key and certificate. SSLStream takes ownership
139 // of the SSLIdentity object and will free it when appropriate. Should be
140 // called no more than once on a given SSLStream instance.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141 virtual void SetIdentity(SSLIdentity* identity) = 0;
142
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700143 // Call this to indicate that we are to play the server role (or client role,
144 // if the default argument is replaced by SSL_CLIENT).
145 // The default argument is for backward compatibility.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
147 virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
148
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700149 // Do DTLS or TLS.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150 virtual void SetMode(SSLMode mode) = 0;
151
Joachim Bauch831c5582015-05-20 12:48:41 +0200152 // Set maximum supported protocol version. The highest version supported by
153 // both ends will be used for the connection, i.e. if one party supports
154 // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used.
155 // If requested version is not supported by underlying crypto library, the
156 // next lower will be used.
157 virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0;
158
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700159 // 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.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164 //
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700165 // 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;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700177 // 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.
deadbeef89824f62016-09-30 11:55:43 -0700182 //
183 // Returns true if successful.
184 // |error| is optional and provides more information about the failure.
185 virtual bool SetPeerCertificateDigest(
186 const std::string& digest_alg,
187 const unsigned char* digest_val,
188 size_t digest_len,
189 SSLPeerCertificateDigestError* error = nullptr) = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190
191 // Retrieves the peer's X.509 certificate, if a connection has been
192 // established. It returns the transmitted over SSL, including the entire
kwibergb4d01c42016-04-06 05:15:06 -0700193 // chain.
jbauch555604a2016-04-26 03:13:22 -0700194 virtual std::unique_ptr<SSLCertificate> GetPeerCertificate() const = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700196 // 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").
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800198 virtual bool GetSslCipherSuite(int* cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000199
torbjorng43166b82016-03-11 00:06:47 -0800200 virtual int GetSslVersion() const = 0;
201
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202 // Key Exporter interface from RFC 5705
203 // Arguments are:
204 // label -- the exporter label.
205 // part of the RFC defining each exporter
206 // usage (IN)
207 // context/context_len -- a context to bind to for this connection;
208 // optional, can be NULL, 0 (IN)
209 // use_context -- whether to use the context value
210 // (needed to distinguish no context from
211 // zero-length ones).
212 // result -- where to put the computed value
213 // result_len -- the length of the computed value
214 virtual bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200215 const uint8_t* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216 size_t context_len,
217 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200218 uint8_t* result,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000219 size_t result_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220
221 // DTLS-SRTP interface
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800222 virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites);
223 virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224
deadbeef89824f62016-09-30 11:55:43 -0700225 // Returns true if a TLS connection has been established.
226 // The only difference between this and "GetState() == SE_OPEN" is that if
227 // the peer certificate digest hasn't been verified, the state will still be
228 // SS_OPENING but IsTlsConnected should return true.
229 virtual bool IsTlsConnected() = 0;
230
deadbeeff33491e2017-01-20 17:01:45 -0800231 // Capabilities testing
232 static bool HaveDtls();
233 static bool HaveDtlsSrtp();
234 static bool HaveExporter();
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700235 static bool IsBoringSsl();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236
torbjorng43166b82016-03-11 00:06:47 -0800237 // Returns true iff the supplied cipher is deemed to be strong.
238 // TODO(torbjorng): Consider removing the KeyType argument.
239 static bool IsAcceptableCipher(int cipher, KeyType key_type);
240 static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700241
242 // TODO(guoweis): Move this away from a static class method. Currently this is
243 // introduced such that any caller could depend on sslstreamadapter.h without
244 // depending on specific SSL implementation.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800245 static std::string SslCipherSuiteToName(int cipher_suite);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000246
deadbeef6cf94a02016-11-28 17:38:34 -0800247 // Use our timeutils.h source of timing in BoringSSL, allowing us to test
248 // using a fake clock.
249 static void enable_time_callback_for_testing();
250
zhihuangd82eee02016-08-26 11:25:05 -0700251 sigslot::signal1<SSLHandshakeError> SignalSSLHandshakeError;
252
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000253 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 // If true, the server certificate need not match the configured
255 // server_name, and in fact missing certificate authority and other
256 // verification errors are ignored.
257 bool ignore_bad_cert_;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000258
259 // If true (default), the client is required to provide a certificate during
260 // handshake. If no certificate is given, handshake fails. This applies to
261 // server mode only.
262 bool client_auth_enabled_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263};
264
265} // namespace rtc
266
267#endif // WEBRTC_BASE_SSLSTREAMADAPTER_H_