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.

226 lines
8.0 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_OPENSSL_STREAM_ADAPTER_H_
  11. #define RTC_BASE_OPENSSL_STREAM_ADAPTER_H_
  12. #include <openssl/ossl_typ.h>
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "rtc_base/buffer.h"
  19. #include "rtc_base/message_queue.h"
  20. #include "rtc_base/openssl_identity.h"
  21. #include "rtc_base/ssl_identity.h"
  22. #include "rtc_base/ssl_stream_adapter.h"
  23. #include "rtc_base/stream.h"
  24. namespace rtc {
  25. // This class was written with OpenSSLAdapter (a socket adapter) as a
  26. // starting point. It has similar structure and functionality, but uses a
  27. // "peer-to-peer" mode, verifying the peer's certificate using a digest
  28. // sent over a secure signaling channel.
  29. //
  30. // Static methods to initialize and deinit the SSL library are in
  31. // OpenSSLAdapter. These should probably be moved out to a neutral class.
  32. //
  33. // In a few cases I have factored out some OpenSSLAdapter code into static
  34. // methods so it can be reused from this class. Eventually that code should
  35. // probably be moved to a common support class. Unfortunately there remain a
  36. // few duplicated sections of code. I have not done more restructuring because
  37. // I did not want to affect existing code that uses OpenSSLAdapter.
  38. //
  39. // This class does not support the SSL connection restart feature present in
  40. // OpenSSLAdapter. I am not entirely sure how the feature is useful and I am
  41. // not convinced that it works properly.
  42. //
  43. // This implementation is careful to disallow data exchange after an SSL error,
  44. // and it has an explicit SSL_CLOSED state. It should not be possible to send
  45. // any data in clear after one of the StartSSL methods has been called.
  46. // Look in sslstreamadapter.h for documentation of the methods.
  47. class SSLCertChain;
  48. ///////////////////////////////////////////////////////////////////////////////
  49. class OpenSSLStreamAdapter final : public SSLStreamAdapter {
  50. public:
  51. explicit OpenSSLStreamAdapter(StreamInterface* stream);
  52. ~OpenSSLStreamAdapter() override;
  53. void SetIdentity(SSLIdentity* identity) override;
  54. // Default argument is for compatibility
  55. void SetServerRole(SSLRole role = SSL_SERVER) override;
  56. bool SetPeerCertificateDigest(
  57. const std::string& digest_alg,
  58. const unsigned char* digest_val,
  59. size_t digest_len,
  60. SSLPeerCertificateDigestError* error = nullptr) override;
  61. std::unique_ptr<SSLCertChain> GetPeerSSLCertChain() const override;
  62. // Goes from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT, depending
  63. // on whether the underlying stream is already open or not.
  64. int StartSSL() override;
  65. void SetMode(SSLMode mode) override;
  66. void SetMaxProtocolVersion(SSLProtocolVersion version) override;
  67. void SetInitialRetransmissionTimeout(int timeout_ms) override;
  68. StreamResult Read(void* data,
  69. size_t data_len,
  70. size_t* read,
  71. int* error) override;
  72. StreamResult Write(const void* data,
  73. size_t data_len,
  74. size_t* written,
  75. int* error) override;
  76. void Close() override;
  77. StreamState GetState() const override;
  78. // TODO(guoweis): Move this away from a static class method.
  79. static std::string SslCipherSuiteToName(int crypto_suite);
  80. bool GetSslCipherSuite(int* cipher) override;
  81. int GetSslVersion() const override;
  82. // Key Extractor interface
  83. bool ExportKeyingMaterial(const std::string& label,
  84. const uint8_t* context,
  85. size_t context_len,
  86. bool use_context,
  87. uint8_t* result,
  88. size_t result_len) override;
  89. // DTLS-SRTP interface
  90. bool SetDtlsSrtpCryptoSuites(const std::vector<int>& crypto_suites) override;
  91. bool GetDtlsSrtpCryptoSuite(int* crypto_suite) override;
  92. bool IsTlsConnected() override;
  93. // Capabilities interfaces.
  94. static bool IsBoringSsl();
  95. static bool IsAcceptableCipher(int cipher, KeyType key_type);
  96. static bool IsAcceptableCipher(const std::string& cipher, KeyType key_type);
  97. // Use our timeutils.h source of timing in BoringSSL, allowing us to test
  98. // using a fake clock.
  99. static void EnableTimeCallbackForTesting();
  100. protected:
  101. void OnEvent(StreamInterface* stream, int events, int err) override;
  102. private:
  103. enum SSLState {
  104. // Before calling one of the StartSSL methods, data flows
  105. // in clear text.
  106. SSL_NONE,
  107. SSL_WAIT, // waiting for the stream to open to start SSL negotiation
  108. SSL_CONNECTING, // SSL negotiation in progress
  109. SSL_CONNECTED, // SSL stream successfully established
  110. SSL_ERROR, // some SSL error occurred, stream is closed
  111. SSL_CLOSED // Clean close
  112. };
  113. enum { MSG_TIMEOUT = MSG_MAX + 1 };
  114. // The following three methods return 0 on success and a negative
  115. // error code on failure. The error code may be from OpenSSL or -1
  116. // on some other error cases, so it can't really be interpreted
  117. // unfortunately.
  118. // Prepare SSL library, state is SSL_CONNECTING.
  119. int BeginSSL();
  120. // Perform SSL negotiation steps.
  121. int ContinueSSL();
  122. // Error handler helper. signal is given as true for errors in
  123. // asynchronous contexts (when an error method was not returned
  124. // through some other method), and in that case an SE_CLOSE event is
  125. // raised on the stream with the specified error.
  126. // A 0 error means a graceful close, otherwise there is not really enough
  127. // context to interpret the error code.
  128. // |alert| indicates an alert description (one of the SSL_AD constants) to
  129. // send to the remote endpoint when closing the association. If 0, a normal
  130. // shutdown will be performed.
  131. void Error(const char* context, int err, uint8_t alert, bool signal);
  132. void Cleanup(uint8_t alert);
  133. // Override MessageHandler
  134. void OnMessage(Message* msg) override;
  135. // Flush the input buffers by reading left bytes (for DTLS)
  136. void FlushInput(unsigned int left);
  137. // SSL library configuration
  138. SSL_CTX* SetupSSLContext();
  139. // Verify the peer certificate matches the signaled digest.
  140. bool VerifyPeerCertificate();
  141. // SSL certificate verification callback. See
  142. // SSL_CTX_set_cert_verify_callback.
  143. static int SSLVerifyCallback(X509_STORE_CTX* store, void* arg);
  144. bool WaitingToVerifyPeerCertificate() const {
  145. return GetClientAuthEnabled() && !peer_certificate_verified_;
  146. }
  147. bool HasPeerCertificateDigest() const {
  148. return !peer_certificate_digest_algorithm_.empty() &&
  149. !peer_certificate_digest_value_.empty();
  150. }
  151. SSLState state_;
  152. SSLRole role_;
  153. int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
  154. // Whether the SSL negotiation is blocked on needing to read or
  155. // write to the wrapped stream.
  156. bool ssl_read_needs_write_;
  157. bool ssl_write_needs_read_;
  158. SSL* ssl_;
  159. SSL_CTX* ssl_ctx_;
  160. // Our key and certificate.
  161. std::unique_ptr<OpenSSLIdentity> identity_;
  162. // The certificate chain that the peer presented. Initially null, until the
  163. // connection is established.
  164. std::unique_ptr<SSLCertChain> peer_cert_chain_;
  165. bool peer_certificate_verified_ = false;
  166. // The digest of the certificate that the peer must present.
  167. Buffer peer_certificate_digest_value_;
  168. std::string peer_certificate_digest_algorithm_;
  169. // The DtlsSrtp ciphers
  170. std::string srtp_ciphers_;
  171. // Do DTLS or not
  172. SSLMode ssl_mode_;
  173. // Max. allowed protocol version
  174. SSLProtocolVersion ssl_max_version_;
  175. // A 50-ms initial timeout ensures rapid setup on fast connections, but may
  176. // be too aggressive for low bandwidth links.
  177. int dtls_handshake_timeout_ms_ = 50;
  178. };
  179. /////////////////////////////////////////////////////////////////////////////
  180. } // namespace rtc
  181. #endif // RTC_BASE_OPENSSL_STREAM_ADAPTER_H_