ssl.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Copyright (c) 2025 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_IO_SSL_HPP
  8. #define BOOST_COBALT_IO_SSL_HPP
  9. #if !defined(BOOST_COBALT_SSL_SOURCE) && !defined(BOOST_ALL_NO_LIB) \
  10. && !defined(BOOST_COBALT_NO_LIB) && !defined(BOOST_COBALT_IO_SSL_NO_LIB)
  11. #define BOOST_LIB_NAME boost_cobalt_io_ssl
  12. #if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_COBALT_DYN_LINK)
  13. #define BOOST_DYN_LINK
  14. #endif
  15. #include <boost/config/auto_link.hpp>
  16. #endif
  17. #include <boost/cobalt/io/socket.hpp>
  18. #include <boost/cobalt/io/stream.hpp>
  19. #include <boost/asio/generic/datagram_protocol.hpp>
  20. #include <boost/asio/basic_stream_socket.hpp>
  21. #include <boost/asio/ssl/stream.hpp>
  22. namespace boost::cobalt::io::ssl
  23. {
  24. enum class verify
  25. {
  26. none = asio::ssl::verify_none,
  27. peer = asio::ssl::verify_peer,
  28. fail_if_no_peer_cert = asio::ssl::verify_fail_if_no_peer_cert,
  29. client_once = asio::ssl::verify_client_once
  30. };
  31. using context = asio::ssl::context;
  32. using verify_mode = asio::ssl::verify_mode;
  33. namespace detail
  34. {
  35. struct stream_impl
  36. {
  37. asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> stream_socket_;
  38. };
  39. }
  40. struct BOOST_SYMBOL_VISIBLE stream final : private detail::stream_impl, socket, cobalt::io::stream, asio::ssl::stream_base
  41. {
  42. BOOST_COBALT_SSL_DECL stream(context & ctx, const cobalt::executor & executor = this_thread::get_executor());
  43. BOOST_COBALT_SSL_DECL stream(context & ctx, stream_socket && sock);
  44. BOOST_COBALT_SSL_DECL stream(context & ctx, native_handle_type h, protocol_type protocol = protocol_type(),
  45. const cobalt::executor & executor = this_thread::get_executor());
  46. BOOST_COBALT_SSL_DECL stream(context & ctx, endpoint ep,
  47. const cobalt::executor & executor = this_thread::get_executor());
  48. [[nodiscard]] write_op write_some(const_buffer_sequence buffer) override
  49. {
  50. return {buffer, this, initiate_write_some_};
  51. }
  52. [[nodiscard]] read_op read_some(mutable_buffer_sequence buffer) override
  53. {
  54. return {buffer, this, initiate_read_some_};
  55. }
  56. [[nodiscard]] bool secure() const {return upgraded_;}
  57. template<typename VerifyCallback>
  58. requires requires (const VerifyCallback & cb, context & ctx) {{cb(true, ctx)} -> std::same_as<bool>;}
  59. system::result<void> set_verify_callback(VerifyCallback vc)
  60. {
  61. system::error_code ec;
  62. stream_socket_.set_verify_callback(std::move(vc), ec);
  63. return ec ? ec : system::result<void>();
  64. }
  65. BOOST_COBALT_SSL_DECL
  66. system::result<void> set_verify_depth(int depth);
  67. BOOST_COBALT_SSL_DECL
  68. system::result<void> set_verify_mode(verify depth);
  69. private:
  70. struct BOOST_COBALT_SSL_DECL handshake_op_ final : cobalt::op<system::error_code>
  71. {
  72. void ready(handler<system::error_code> h) final;
  73. void initiate(completion_handler<system::error_code> h) final;
  74. handshake_op_(handshake_type type, bool upgraded, asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> & stream_socket)
  75. : type_(type), upgraded_(upgraded), stream_socket_(stream_socket) {}
  76. ~handshake_op_() = default;
  77. private:
  78. handshake_type type_;
  79. bool upgraded_;
  80. asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> &stream_socket_;
  81. };
  82. struct BOOST_COBALT_SSL_DECL handshake_buffer_op_ final : cobalt::op<system::error_code, std::size_t>
  83. {
  84. void ready(handler<system::error_code, std::size_t> h) final;
  85. void initiate(completion_handler<system::error_code, std::size_t> h) final;
  86. handshake_buffer_op_(handshake_type type, bool upgraded, const_buffer_sequence buffer_,
  87. asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> & stream_socket)
  88. : type_(type), upgraded_(upgraded), buffer_(buffer_), stream_socket_(stream_socket) {}
  89. ~handshake_buffer_op_() = default;
  90. private:
  91. handshake_type type_;
  92. bool upgraded_;
  93. const_buffer_sequence buffer_;
  94. asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> &stream_socket_;
  95. };
  96. struct BOOST_COBALT_SSL_DECL shutdown_op_ final : cobalt::op<system::error_code>
  97. {
  98. void ready(handler<system::error_code> h) final;
  99. void initiate(completion_handler<system::error_code> h) final;
  100. shutdown_op_(bool upgraded, asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> & stream_socket)
  101. : upgraded_(upgraded), stream_socket_(stream_socket) {}
  102. ~shutdown_op_() = default;
  103. private:
  104. bool upgraded_;
  105. asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> &stream_socket_;
  106. };
  107. public:
  108. [[nodiscard]] auto handshake(handshake_type type) { return handshake_op_{type, upgraded_, stream_socket_}; }
  109. [[nodiscard]] auto handshake(handshake_type type, const_buffer_sequence buffer)
  110. {
  111. return handshake_buffer_op_{type, upgraded_, buffer, stream_socket_};
  112. }
  113. [[nodiscard]] auto shutdown() { return shutdown_op_{upgraded_, stream_socket_}; }
  114. private:
  115. BOOST_COBALT_SSL_DECL void adopt_endpoint_(endpoint & ep) override;
  116. BOOST_COBALT_SSL_DECL static void initiate_read_some_ (void *, mutable_buffer_sequence, cobalt::completion_handler<system::error_code, std::size_t>);
  117. BOOST_COBALT_SSL_DECL static void initiate_write_some_(void *, const_buffer_sequence, cobalt::completion_handler<system::error_code, std::size_t>);
  118. bool upgraded_ = false;
  119. };
  120. }
  121. #endif //BOOST_COBALT_IO_SSL_HPP