henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_SSL_STREAM_ADAPTER_H_ |
| 12 | #define RTC_BASE_SSL_STREAM_ADAPTER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 19 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/ssl_certificate.h" |
| 21 | #include "rtc_base/ssl_identity.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "rtc_base/stream.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 23 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 24 | |
| 25 | namespace rtc { |
| 26 | |
| 27 | // Constants for SSL profile. |
| 28 | const int TLS_NULL_WITH_NULL_NULL = 0; |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 29 | const int SSL_CIPHER_SUITE_MAX_VALUE = 0xFFFF; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 30 | |
| 31 | // Constants for SRTP profiles. |
| 32 | const int SRTP_INVALID_CRYPTO_SUITE = 0; |
| 33 | #ifndef SRTP_AES128_CM_SHA1_80 |
| 34 | const int SRTP_AES128_CM_SHA1_80 = 0x0001; |
| 35 | #endif |
| 36 | #ifndef SRTP_AES128_CM_SHA1_32 |
| 37 | const int SRTP_AES128_CM_SHA1_32 = 0x0002; |
| 38 | #endif |
| 39 | #ifndef SRTP_AEAD_AES_128_GCM |
| 40 | const int SRTP_AEAD_AES_128_GCM = 0x0007; |
| 41 | #endif |
| 42 | #ifndef SRTP_AEAD_AES_256_GCM |
| 43 | const int SRTP_AEAD_AES_256_GCM = 0x0008; |
| 44 | #endif |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 45 | const int SRTP_CRYPTO_SUITE_MAX_VALUE = 0xFFFF; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 46 | |
| 47 | // Names of SRTP profiles listed above. |
| 48 | // 128-bit AES with 80-bit SHA-1 HMAC. |
| 49 | extern const char CS_AES_CM_128_HMAC_SHA1_80[]; |
| 50 | // 128-bit AES with 32-bit SHA-1 HMAC. |
| 51 | extern const char CS_AES_CM_128_HMAC_SHA1_32[]; |
| 52 | // 128-bit AES GCM with 16 byte AEAD auth tag. |
| 53 | extern const char CS_AEAD_AES_128_GCM[]; |
| 54 | // 256-bit AES GCM with 16 byte AEAD auth tag. |
| 55 | extern 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. |
| 60 | std::string SrtpCryptoSuiteToName(int crypto_suite); |
| 61 | |
| 62 | // The reverse of above conversion. |
| 63 | int 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 Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 67 | bool GetSrtpKeyAndSaltLengths(int crypto_suite, |
| 68 | int* key_length, |
| 69 | int* salt_length); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 70 | |
| 71 | // Returns true if the given crypto suite id uses a GCM cipher. |
| 72 | bool IsGcmCryptoSuite(int crypto_suite); |
| 73 | |
| 74 | // Returns true if the given crypto suite name uses a GCM cipher. |
| 75 | bool IsGcmCryptoSuiteName(const std::string& crypto_suite); |
| 76 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 77 | // 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 Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 91 | enum SSLRole { SSL_CLIENT, SSL_SERVER }; |
| 92 | enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS }; |
| 93 | enum SSLProtocolVersion { |
Harald Alvestrand | 5cb7807 | 2019-10-28 08:51:17 | [diff] [blame] | 94 | SSL_PROTOCOL_NOT_GIVEN = -1, |
| 95 | SSL_PROTOCOL_TLS_10 = 0, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 96 | SSL_PROTOCOL_TLS_11, |
| 97 | SSL_PROTOCOL_TLS_12, |
| 98 | SSL_PROTOCOL_DTLS_10 = SSL_PROTOCOL_TLS_11, |
| 99 | SSL_PROTOCOL_DTLS_12 = SSL_PROTOCOL_TLS_12, |
| 100 | }; |
| 101 | enum class SSLPeerCertificateDigestError { |
| 102 | NONE, |
| 103 | UNKNOWN_ALGORITHM, |
| 104 | INVALID_LENGTH, |
| 105 | VERIFICATION_FAILED, |
| 106 | }; |
| 107 | |
| 108 | // Errors for Read -- in the high range so no conflict with OpenSSL. |
| 109 | enum { SSE_MSG_TRUNC = 0xff0001 }; |
| 110 | |
| 111 | // Used to send back UMA histogram value. Logged when Dtls handshake fails. |
| 112 | enum class SSLHandshakeError { UNKNOWN, INCOMPATIBLE_CIPHERSUITE, MAX_VALUE }; |
| 113 | |
| 114 | class SSLStreamAdapter : public StreamAdapterInterface { |
| 115 | public: |
| 116 | // Instantiate an SSLStreamAdapter wrapping the given stream, |
| 117 | // (using the selected implementation for the platform). |
| 118 | // Caller is responsible for freeing the returned object. |
| 119 | static SSLStreamAdapter* Create(StreamInterface* stream); |
| 120 | |
| 121 | explicit SSLStreamAdapter(StreamInterface* stream); |
| 122 | ~SSLStreamAdapter() override; |
| 123 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 124 | // Specify our SSL identity: key and certificate. SSLStream takes ownership |
| 125 | // of the SSLIdentity object and will free it when appropriate. Should be |
| 126 | // called no more than once on a given SSLStream instance. |
| 127 | virtual void SetIdentity(SSLIdentity* identity) = 0; |
| 128 | |
| 129 | // Call this to indicate that we are to play the server role (or client role, |
| 130 | // if the default argument is replaced by SSL_CLIENT). |
| 131 | // The default argument is for backward compatibility. |
| 132 | // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function |
| 133 | virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0; |
| 134 | |
| 135 | // Do DTLS or TLS. |
| 136 | virtual void SetMode(SSLMode mode) = 0; |
| 137 | |
| 138 | // Set maximum supported protocol version. The highest version supported by |
| 139 | // both ends will be used for the connection, i.e. if one party supports |
| 140 | // DTLS 1.0 and the other DTLS 1.2, DTLS 1.0 will be used. |
| 141 | // If requested version is not supported by underlying crypto library, the |
| 142 | // next lower will be used. |
| 143 | virtual void SetMaxProtocolVersion(SSLProtocolVersion version) = 0; |
| 144 | |
| 145 | // Set the initial retransmission timeout for DTLS messages. When the timeout |
| 146 | // expires, the message gets retransmitted and the timeout is exponentially |
| 147 | // increased. |
| 148 | // This should only be called before StartSSL(). |
| 149 | virtual void SetInitialRetransmissionTimeout(int timeout_ms) = 0; |
| 150 | |
| 151 | // StartSSL starts negotiation with a peer, whose certificate is verified |
| 152 | // using the certificate digest. Generally, SetIdentity() and possibly |
| 153 | // SetServerRole() should have been called before this. |
| 154 | // SetPeerCertificateDigest() must also be called. It may be called after |
| 155 | // StartSSLWithPeer() but must be called before the underlying stream opens. |
| 156 | // |
| 157 | // Use of the stream prior to calling StartSSL will pass data in clear text. |
| 158 | // Calling StartSSL causes SSL negotiation to begin as soon as possible: right |
| 159 | // away if the underlying wrapped stream is already opened, or else as soon as |
| 160 | // it opens. |
| 161 | // |
| 162 | // StartSSL returns a negative error code on failure. Returning 0 means |
| 163 | // success so far, but negotiation is probably not complete and will continue |
| 164 | // asynchronously. In that case, the exposed stream will open after |
| 165 | // successful negotiation and verification, or an SE_CLOSE event will be |
| 166 | // raised if negotiation fails. |
| 167 | virtual int StartSSL() = 0; |
| 168 | |
| 169 | // Specify the digest of the certificate that our peer is expected to use. |
| 170 | // Only this certificate will be accepted during SSL verification. The |
| 171 | // certificate is assumed to have been obtained through some other secure |
| 172 | // channel (such as the signaling channel). This must specify the terminal |
| 173 | // certificate, not just a CA. SSLStream makes a copy of the digest value. |
| 174 | // |
| 175 | // Returns true if successful. |
| 176 | // |error| is optional and provides more information about the failure. |
| 177 | virtual bool SetPeerCertificateDigest( |
| 178 | const std::string& digest_alg, |
| 179 | const unsigned char* digest_val, |
| 180 | size_t digest_len, |
| 181 | SSLPeerCertificateDigestError* error = nullptr) = 0; |
| 182 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 183 | // Retrieves the peer's certificate chain including leaf certificate, if a |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 184 | // connection has been established. |
| 185 | virtual std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const = 0; |
| 186 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 187 | // Retrieves the IANA registration id of the cipher suite used for the |
| 188 | // connection (e.g. 0x2F for "TLS_RSA_WITH_AES_128_CBC_SHA"). |
| 189 | virtual bool GetSslCipherSuite(int* cipher_suite); |
| 190 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 08:51:17 | [diff] [blame] | 191 | // Retrieves the enum value for SSL version. |
| 192 | // Will return -1 until the version has been negotiated. |
| 193 | virtual SSLProtocolVersion GetSslVersion() const = 0; |
| 194 | // Retrieves the 2-byte version from the TLS protocol. |
| 195 | // Will return false until the version has been negotiated. |
| 196 | virtual bool GetSslVersionBytes(int* version) const = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 197 | |
| 198 | // Key Exporter interface from RFC 5705 |
| 199 | // Arguments are: |
| 200 | // label -- the exporter label. |
| 201 | // part of the RFC defining each exporter |
| 202 | // usage (IN) |
| 203 | // context/context_len -- a context to bind to for this connection; |
| 204 | // optional, can be null, 0 (IN) |
| 205 | // use_context -- whether to use the context value |
| 206 | // (needed to distinguish no context from |
| 207 | // zero-length ones). |
| 208 | // result -- where to put the computed value |
| 209 | // result_len -- the length of the computed value |
| 210 | virtual bool ExportKeyingMaterial(const std::string& label, |
| 211 | const uint8_t* context, |
| 212 | size_t context_len, |
| 213 | bool use_context, |
| 214 | uint8_t* result, |
| 215 | size_t result_len); |
| 216 | |
| 217 | // DTLS-SRTP interface |
| 218 | virtual bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites); |
| 219 | virtual bool GetDtlsSrtpCryptoSuite(int* crypto_suite); |
| 220 | |
| 221 | // Returns true if a TLS connection has been established. |
| 222 | // The only difference between this and "GetState() == SE_OPEN" is that if |
| 223 | // the peer certificate digest hasn't been verified, the state will still be |
| 224 | // SS_OPENING but IsTlsConnected should return true. |
| 225 | virtual bool IsTlsConnected() = 0; |
| 226 | |
| 227 | // Capabilities testing. |
| 228 | // Used to have "DTLS supported", "DTLS-SRTP supported" etc. methods, but now |
| 229 | // that's assumed. |
| 230 | static bool IsBoringSsl(); |
| 231 | |
| 232 | // Returns true iff the supplied cipher is deemed to be strong. |
| 233 | // TODO(torbjorng): Consider removing the KeyType argument. |
| 234 | static bool IsAcceptableCipher(int cipher, KeyType key_type); |
| 235 | static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type); |
| 236 | |
| 237 | // TODO(guoweis): Move this away from a static class method. Currently this is |
| 238 | // introduced such that any caller could depend on sslstreamadapter.h without |
| 239 | // depending on specific SSL implementation. |
| 240 | static std::string SslCipherSuiteToName(int cipher_suite); |
| 241 | |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 242 | //////////////////////////////////////////////////////////////////////////// |
| 243 | // Testing only member functions |
| 244 | //////////////////////////////////////////////////////////////////////////// |
| 245 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 246 | // Use our timeutils.h source of timing in BoringSSL, allowing us to test |
| 247 | // using a fake clock. |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 248 | static void EnableTimeCallbackForTesting(); |
| 249 | |
| 250 | // Deprecated. Do not use this API outside of testing. |
| 251 | // Do not set this to false outside of testing. |
| 252 | void SetClientAuthEnabledForTesting(bool enabled) { |
| 253 | client_auth_enabled_ = enabled; |
| 254 | } |
| 255 | |
| 256 | // Deprecated. Do not use this API outside of testing. |
| 257 | // Returns true by default, else false if explicitly set to disable client |
| 258 | // authentication. |
| 259 | bool GetClientAuthEnabled() const { return client_auth_enabled_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 260 | |
| 261 | sigslot::signal1<SSLHandshakeError> SignalSSLHandshakeError; |
| 262 | |
| 263 | private: |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 264 | // If true (default), the client is required to provide a certificate during |
| 265 | // handshake. If no certificate is given, handshake fails. This applies to |
| 266 | // server mode only. |
Benjamin Wright | b19b497 | 2018-10-25 17:46:49 | [diff] [blame] | 267 | bool client_auth_enabled_ = true; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 271 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 272 | #endif // RTC_BASE_SSL_STREAM_ADAPTER_H_ |