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