ssl_stream_adapter.h 12 KB

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