engine.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // ssl/detail/engine.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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. // Move construct from another engine.
  52. ASIO_DECL engine(engine&& other) noexcept;
  53. // Destructor.
  54. ASIO_DECL ~engine();
  55. // Move assign from another engine.
  56. ASIO_DECL engine& operator=(engine&& other) noexcept;
  57. // Get the underlying implementation in the native type.
  58. ASIO_DECL SSL* native_handle();
  59. // Set the peer verification mode.
  60. ASIO_DECL asio::error_code set_verify_mode(
  61. verify_mode v, asio::error_code& ec);
  62. // Set the peer verification depth.
  63. ASIO_DECL asio::error_code set_verify_depth(
  64. int depth, asio::error_code& ec);
  65. // Set a peer certificate verification callback.
  66. ASIO_DECL asio::error_code set_verify_callback(
  67. verify_callback_base* callback, asio::error_code& ec);
  68. // Perform an SSL handshake using either SSL_connect (client-side) or
  69. // SSL_accept (server-side).
  70. ASIO_DECL want handshake(
  71. stream_base::handshake_type type, asio::error_code& ec);
  72. // Perform a graceful shutdown of the SSL session.
  73. ASIO_DECL want shutdown(asio::error_code& ec);
  74. // Write bytes to the SSL session.
  75. ASIO_DECL want write(const asio::const_buffer& data,
  76. asio::error_code& ec, std::size_t& bytes_transferred);
  77. // Read bytes from the SSL session.
  78. ASIO_DECL want read(const asio::mutable_buffer& data,
  79. asio::error_code& ec, std::size_t& bytes_transferred);
  80. // Get output data to be written to the transport.
  81. ASIO_DECL asio::mutable_buffer get_output(
  82. const asio::mutable_buffer& data);
  83. // Put input data that was read from the transport.
  84. ASIO_DECL asio::const_buffer put_input(
  85. const asio::const_buffer& data);
  86. // Map an error::eof code returned by the underlying transport according to
  87. // the type and state of the SSL session. Returns a const reference to the
  88. // error code object, suitable for passing to a completion handler.
  89. ASIO_DECL const asio::error_code& map_error_code(
  90. asio::error_code& ec) const;
  91. private:
  92. // Disallow copying and assignment.
  93. engine(const engine&);
  94. engine& operator=(const engine&);
  95. // Callback used when the SSL implementation wants to verify a certificate.
  96. ASIO_DECL static int verify_callback_function(
  97. int preverified, X509_STORE_CTX* ctx);
  98. #if (OPENSSL_VERSION_NUMBER < 0x10000000L)
  99. // The SSL_accept function may not be thread safe. This mutex is used to
  100. // protect all calls to the SSL_accept function.
  101. ASIO_DECL static asio::detail::static_mutex& accept_mutex();
  102. #endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
  103. // Perform one operation. Returns >= 0 on success or error, want_read if the
  104. // operation needs more input, or want_write if it needs to write some output
  105. // before the operation can complete.
  106. ASIO_DECL want perform(int (engine::* op)(void*, std::size_t),
  107. void* data, std::size_t length, asio::error_code& ec,
  108. std::size_t* bytes_transferred);
  109. // Adapt the SSL_accept function to the signature needed for perform().
  110. ASIO_DECL int do_accept(void*, std::size_t);
  111. // Adapt the SSL_connect function to the signature needed for perform().
  112. ASIO_DECL int do_connect(void*, std::size_t);
  113. // Adapt the SSL_shutdown function to the signature needed for perform().
  114. ASIO_DECL int do_shutdown(void*, std::size_t);
  115. // Adapt the SSL_read function to the signature needed for perform().
  116. ASIO_DECL int do_read(void* data, std::size_t length);
  117. // Adapt the SSL_write function to the signature needed for perform().
  118. ASIO_DECL int do_write(void* data, std::size_t length);
  119. SSL* ssl_;
  120. BIO* ext_bio_;
  121. };
  122. } // namespace detail
  123. } // namespace ssl
  124. } // namespace asio
  125. #include "asio/detail/pop_options.hpp"
  126. #if defined(ASIO_HEADER_ONLY)
  127. # include "asio/ssl/detail/impl/engine.ipp"
  128. #endif // defined(ASIO_HEADER_ONLY)
  129. #endif // ASIO_SSL_DETAIL_ENGINE_HPP