engine.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ssl/detail/engine.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_SSL_DETAIL_ENGINE_HPP
  11. #define ASIO_SSL_DETAIL_ENGINE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/buffer.hpp"
  17. #include "asio/detail/static_mutex.hpp"
  18. #include "asio/ssl/detail/openssl_types.hpp"
  19. #include "asio/ssl/detail/verify_callback.hpp"
  20. #include "asio/ssl/stream_base.hpp"
  21. #include "asio/ssl/verify_mode.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace ssl {
  25. namespace detail {
  26. class engine
  27. {
  28. public:
  29. enum want
  30. {
  31. // Returned by functions to indicate that the engine wants input. The input
  32. // buffer should be updated to point to the data. The engine then needs to
  33. // be called again to retry the operation.
  34. want_input_and_retry = -2,
  35. // Returned by functions to indicate that the engine wants to write output.
  36. // The output buffer points to the data to be written. The engine then
  37. // needs to be called again to retry the operation.
  38. want_output_and_retry = -1,
  39. // Returned by functions to indicate that the engine doesn't need input or
  40. // output.
  41. want_nothing = 0,
  42. // Returned by functions to indicate that the engine wants to write output.
  43. // The output buffer points to the data to be written. After that the
  44. // operation is complete, and the engine does not need to be called again.
  45. want_output = 1
  46. };
  47. // Construct a new engine for the specified context.
  48. ASIO_DECL explicit engine(SSL_CTX* context);
  49. // Construct a new engine for an existing native SSL implementation.
  50. ASIO_DECL explicit engine(SSL* ssl_impl);
  51. #if defined(ASIO_HAS_MOVE)
  52. // Move construct from another engine.
  53. ASIO_DECL engine(engine&& other) ASIO_NOEXCEPT;
  54. #endif // defined(ASIO_HAS_MOVE)
  55. // Destructor.
  56. ASIO_DECL ~engine();
  57. #if defined(ASIO_HAS_MOVE)
  58. // Move assign from another engine.
  59. ASIO_DECL engine& operator=(engine&& other) ASIO_NOEXCEPT;
  60. #endif // defined(ASIO_HAS_MOVE)
  61. // Get the underlying implementation in the native type.
  62. ASIO_DECL SSL* native_handle();
  63. // Set the peer verification mode.
  64. ASIO_DECL asio::error_code set_verify_mode(
  65. verify_mode v, asio::error_code& ec);
  66. // Set the peer verification depth.
  67. ASIO_DECL asio::error_code set_verify_depth(
  68. int depth, asio::error_code& ec);
  69. // Set a peer certificate verification callback.
  70. ASIO_DECL asio::error_code set_verify_callback(
  71. verify_callback_base* callback, asio::error_code& ec);
  72. // Perform an SSL handshake using either SSL_connect (client-side) or
  73. // SSL_accept (server-side).
  74. ASIO_DECL want handshake(
  75. stream_base::handshake_type type, asio::error_code& ec);
  76. // Perform a graceful shutdown of the SSL session.
  77. ASIO_DECL want shutdown(asio::error_code& ec);
  78. // Write bytes to the SSL session.
  79. ASIO_DECL want write(const asio::const_buffer& data,
  80. asio::error_code& ec, std::size_t& bytes_transferred);
  81. // Read bytes from the SSL session.
  82. ASIO_DECL want read(const asio::mutable_buffer& data,
  83. asio::error_code& ec, std::size_t& bytes_transferred);
  84. // Get output data to be written to the transport.
  85. ASIO_DECL asio::mutable_buffer get_output(
  86. const asio::mutable_buffer& data);
  87. // Put input data that was read from the transport.
  88. ASIO_DECL asio::const_buffer put_input(
  89. const asio::const_buffer& data);
  90. // Map an error::eof code returned by the underlying transport according to
  91. // the type and state of the SSL session. Returns a const reference to the
  92. // error code object, suitable for passing to a completion handler.
  93. ASIO_DECL const asio::error_code& map_error_code(
  94. asio::error_code& ec) const;
  95. private:
  96. // Disallow copying and assignment.
  97. engine(const engine&);
  98. engine& operator=(const engine&);
  99. // Callback used when the SSL implementation wants to verify a certificate.
  100. ASIO_DECL static int verify_callback_function(
  101. int preverified, X509_STORE_CTX* ctx);
  102. #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
  103. // The SSL_accept function may not be thread safe. This mutex is used to
  104. // protect all calls to the SSL_accept function.
  105. ASIO_DECL static asio::detail::static_mutex& accept_mutex();
  106. #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
  107. // Perform one operation. Returns >= 0 on success or error, want_read if the
  108. // operation needs more input, or want_write if it needs to write some output
  109. // before the operation can complete.
  110. ASIO_DECL want perform(int (engine::* op)(void*, std::size_t),
  111. void* data, std::size_t length, asio::error_code& ec,
  112. std::size_t* bytes_transferred);
  113. // Adapt the SSL_accept function to the signature needed for perform().
  114. ASIO_DECL int do_accept(void*, std::size_t);
  115. // Adapt the SSL_connect function to the signature needed for perform().
  116. ASIO_DECL int do_connect(void*, std::size_t);
  117. // Adapt the SSL_shutdown function to the signature needed for perform().
  118. ASIO_DECL int do_shutdown(void*, std::size_t);
  119. // Adapt the SSL_read function to the signature needed for perform().
  120. ASIO_DECL int do_read(void* data, std::size_t length);
  121. // Adapt the SSL_write function to the signature needed for perform().
  122. ASIO_DECL int do_write(void* data, std::size_t length);
  123. SSL* ssl_;
  124. BIO* ext_bio_;
  125. };
  126. } // namespace detail
  127. } // namespace ssl
  128. } // namespace asio
  129. #include "asio/detail/pop_options.hpp"
  130. #if defined(ASIO_HEADER_ONLY)
  131. # include "asio/ssl/detail/impl/engine.ipp"
  132. #endif // defined(ASIO_HEADER_ONLY)
  133. #endif // ASIO_SSL_DETAIL_ENGINE_HPP