Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 08:39:20

0001 //
0002 // Copyright (c) 2025 Klemens Morgenstern (klemens.morgenstern@gmx.net)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 
0009 #ifndef BOOST_COBALT_IO_SSL_HPP
0010 #define BOOST_COBALT_IO_SSL_HPP
0011 
0012 
0013 #if !defined(BOOST_COBALT_SSL_SOURCE) && !defined(BOOST_ALL_NO_LIB)  \
0014  && !defined(BOOST_COBALT_NO_LIB) && !defined(BOOST_COBALT_IO_SSL_NO_LIB)
0015 #define BOOST_LIB_NAME boost_cobalt_io_ssl
0016 #if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_COBALT_DYN_LINK)
0017 #define BOOST_DYN_LINK
0018 #endif
0019 #include <boost/config/auto_link.hpp>
0020 #endif
0021 
0022 #include <boost/cobalt/io/socket.hpp>
0023 #include <boost/cobalt/io/stream.hpp>
0024 
0025 #include <boost/asio/generic/datagram_protocol.hpp>
0026 #include <boost/asio/basic_stream_socket.hpp>
0027 #include <boost/asio/ssl/stream.hpp>
0028 
0029 
0030 namespace boost::cobalt::io::ssl
0031 {
0032 
0033 enum class verify
0034 {
0035   none = asio::ssl::verify_none,
0036   peer = asio::ssl::verify_peer,
0037   fail_if_no_peer_cert = asio::ssl::verify_fail_if_no_peer_cert,
0038   client_once = asio::ssl::verify_client_once
0039 };
0040 
0041 using context = asio::ssl::context;
0042 using verify_mode = asio::ssl::verify_mode;
0043 
0044 namespace detail
0045 {
0046 
0047 struct stream_impl
0048 {
0049   asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> stream_socket_;
0050 };
0051 
0052 }
0053 
0054 struct BOOST_SYMBOL_VISIBLE stream final : private detail::stream_impl, socket, cobalt::io::stream, asio::ssl::stream_base
0055 {
0056   BOOST_COBALT_SSL_DECL stream(context & ctx, const cobalt::executor & executor = this_thread::get_executor());
0057   BOOST_COBALT_SSL_DECL stream(context & ctx, stream_socket && sock);
0058   BOOST_COBALT_SSL_DECL stream(context & ctx, native_handle_type h, protocol_type protocol = protocol_type(),
0059                                      const cobalt::executor & executor = this_thread::get_executor());
0060   BOOST_COBALT_SSL_DECL stream(context & ctx, endpoint ep,
0061                                      const cobalt::executor & executor = this_thread::get_executor());
0062 
0063   [[nodiscard]] write_op write_some(const_buffer_sequence buffer) override
0064   {
0065     return {buffer, this, initiate_write_some_};
0066   }
0067   [[nodiscard]] read_op read_some(mutable_buffer_sequence buffer) override
0068   {
0069     return {buffer, this, initiate_read_some_};
0070   }
0071 
0072   [[nodiscard]] bool secure() const {return upgraded_;}
0073 
0074   template<typename VerifyCallback>
0075     requires requires (const VerifyCallback & cb, context & ctx) {{cb(true, ctx)} -> std::same_as<bool>;}
0076   system::result<void> set_verify_callback(VerifyCallback vc)
0077   {
0078     system::error_code ec;
0079     stream_socket_.set_verify_callback(std::move(vc), ec);
0080     return ec ? ec : system::result<void>();
0081   }
0082 
0083   BOOST_COBALT_SSL_DECL
0084   system::result<void> set_verify_depth(int depth);
0085 
0086 
0087   BOOST_COBALT_SSL_DECL
0088   system::result<void> set_verify_mode(verify depth);
0089 
0090  private:
0091 
0092   struct BOOST_COBALT_SSL_DECL handshake_op_ final : cobalt::op<system::error_code>
0093   {
0094     void ready(handler<system::error_code> h) final;
0095     void initiate(completion_handler<system::error_code> h) final;
0096     handshake_op_(handshake_type type, bool upgraded, asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>>  & stream_socket)
0097         : type_(type), upgraded_(upgraded), stream_socket_(stream_socket) {}
0098     ~handshake_op_() = default;
0099    private:
0100     handshake_type type_;
0101     bool upgraded_;
0102     asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> &stream_socket_;
0103   };
0104 
0105 
0106   struct BOOST_COBALT_SSL_DECL handshake_buffer_op_ final : cobalt::op<system::error_code, std::size_t>
0107   {
0108     void ready(handler<system::error_code, std::size_t> h) final;
0109     void initiate(completion_handler<system::error_code, std::size_t> h) final;
0110     handshake_buffer_op_(handshake_type type, bool upgraded, const_buffer_sequence buffer_,
0111                          asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>>  & stream_socket)
0112         : type_(type), upgraded_(upgraded), buffer_(buffer_), stream_socket_(stream_socket) {}
0113     ~handshake_buffer_op_() = default;
0114    private:
0115     handshake_type type_;
0116     bool upgraded_;
0117     const_buffer_sequence buffer_;
0118     asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> &stream_socket_;
0119   };
0120 
0121 
0122   struct BOOST_COBALT_SSL_DECL shutdown_op_ final : cobalt::op<system::error_code>
0123   {
0124     void ready(handler<system::error_code> h) final;
0125     void initiate(completion_handler<system::error_code> h) final;
0126     shutdown_op_(bool upgraded, asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>>  & stream_socket)
0127         : upgraded_(upgraded), stream_socket_(stream_socket) {}
0128     ~shutdown_op_() = default;
0129    private:
0130     bool upgraded_;
0131     asio::ssl::stream<asio::basic_stream_socket<protocol_type, executor>> &stream_socket_;
0132   };
0133  public:
0134   [[nodiscard]] auto handshake(handshake_type type) { return handshake_op_{type, upgraded_, stream_socket_}; }
0135   [[nodiscard]] auto handshake(handshake_type type, const_buffer_sequence buffer)
0136   {
0137     return handshake_buffer_op_{type, upgraded_, buffer, stream_socket_};
0138   }
0139   [[nodiscard]] auto shutdown() { return shutdown_op_{upgraded_, stream_socket_}; }
0140  private:
0141 
0142   BOOST_COBALT_SSL_DECL void adopt_endpoint_(endpoint & ep) override;
0143 
0144   BOOST_COBALT_SSL_DECL static void initiate_read_some_ (void *, mutable_buffer_sequence, cobalt::completion_handler<system::error_code, std::size_t>);
0145   BOOST_COBALT_SSL_DECL static void initiate_write_some_(void *,   const_buffer_sequence, cobalt::completion_handler<system::error_code, std::size_t>);
0146 
0147 
0148   bool upgraded_ = false;
0149 };
0150 
0151 }
0152 
0153 #endif //BOOST_COBALT_IO_SSL_HPP
0154