engine.hpp 5.4 KB

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