You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
11 KiB

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