1 //
2 // ssl/detail/stream_core.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 
11 #ifndef BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP
12 #define BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
21 # include <boost/asio/deadline_timer.hpp>
22 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
23 # include <boost/asio/steady_timer.hpp>
24 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
25 #include <boost/asio/ssl/detail/engine.hpp>
26 #include <boost/asio/buffer.hpp>
27 
28 #include <boost/asio/detail/push_options.hpp>
29 
30 namespace boost {
31 namespace asio {
32 namespace ssl {
33 namespace detail {
34 
35 struct stream_core
36 {
37   // According to the OpenSSL documentation, this is the buffer size that is
38   // sufficient to hold the largest possible TLS record.
39   enum { max_tls_record_size = 17 * 1024 };
40 
41   template <typename Executor>
stream_coreboost::asio::ssl::detail::stream_core42   stream_core(SSL_CTX* context, const Executor& ex)
43     : engine_(context),
44       pending_read_(ex),
45       pending_write_(ex),
46       output_buffer_space_(max_tls_record_size),
47       output_buffer_(boost::asio::buffer(output_buffer_space_)),
48       input_buffer_space_(max_tls_record_size),
49       input_buffer_(boost::asio::buffer(input_buffer_space_))
50   {
51     pending_read_.expires_at(neg_infin());
52     pending_write_.expires_at(neg_infin());
53   }
54 
55 #if defined(BOOST_ASIO_HAS_MOVE)
stream_coreboost::asio::ssl::detail::stream_core56   stream_core(stream_core&& other)
57     : engine_(BOOST_ASIO_MOVE_CAST(engine)(other.engine_)),
58 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
59       pending_read_(
60          BOOST_ASIO_MOVE_CAST(boost::asio::deadline_timer)(
61            other.pending_read_)),
62       pending_write_(
63          BOOST_ASIO_MOVE_CAST(boost::asio::deadline_timer)(
64            other.pending_write_)),
65 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
66       pending_read_(
67          BOOST_ASIO_MOVE_CAST(boost::asio::steady_timer)(
68            other.pending_read_)),
69       pending_write_(
70          BOOST_ASIO_MOVE_CAST(boost::asio::steady_timer)(
71            other.pending_write_)),
72 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
73       output_buffer_space_(
74           BOOST_ASIO_MOVE_CAST(std::vector<unsigned char>)(
75             other.output_buffer_space_)),
76       output_buffer_(other.output_buffer_),
77       input_buffer_space_(
78           BOOST_ASIO_MOVE_CAST(std::vector<unsigned char>)(
79             other.input_buffer_space_)),
80       input_buffer_(other.input_buffer_),
81       input_(other.input_)
82   {
83     other.output_buffer_ = boost::asio::mutable_buffer(0, 0);
84     other.input_buffer_ = boost::asio::mutable_buffer(0, 0);
85     other.input_ = boost::asio::const_buffer(0, 0);
86   }
87 #endif // defined(BOOST_ASIO_HAS_MOVE)
88 
~stream_coreboost::asio::ssl::detail::stream_core89   ~stream_core()
90   {
91   }
92 
93   // The SSL engine.
94   engine engine_;
95 
96 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
97   // Timer used for storing queued read operations.
98   boost::asio::deadline_timer pending_read_;
99 
100   // Timer used for storing queued write operations.
101   boost::asio::deadline_timer pending_write_;
102 
103   // Helper function for obtaining a time value that always fires.
neg_infinboost::asio::ssl::detail::stream_core104   static boost::asio::deadline_timer::time_type neg_infin()
105   {
106     return boost::posix_time::neg_infin;
107   }
108 
109   // Helper function for obtaining a time value that never fires.
pos_infinboost::asio::ssl::detail::stream_core110   static boost::asio::deadline_timer::time_type pos_infin()
111   {
112     return boost::posix_time::pos_infin;
113   }
114 
115   // Helper function to get a timer's expiry time.
expiryboost::asio::ssl::detail::stream_core116   static boost::asio::deadline_timer::time_type expiry(
117       const boost::asio::deadline_timer& timer)
118   {
119     return timer.expires_at();
120   }
121 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
122   // Timer used for storing queued read operations.
123   boost::asio::steady_timer pending_read_;
124 
125   // Timer used for storing queued write operations.
126   boost::asio::steady_timer pending_write_;
127 
128   // Helper function for obtaining a time value that always fires.
neg_infinboost::asio::ssl::detail::stream_core129   static boost::asio::steady_timer::time_point neg_infin()
130   {
131     return (boost::asio::steady_timer::time_point::min)();
132   }
133 
134   // Helper function for obtaining a time value that never fires.
pos_infinboost::asio::ssl::detail::stream_core135   static boost::asio::steady_timer::time_point pos_infin()
136   {
137     return (boost::asio::steady_timer::time_point::max)();
138   }
139 
140   // Helper function to get a timer's expiry time.
expiryboost::asio::ssl::detail::stream_core141   static boost::asio::steady_timer::time_point expiry(
142       const boost::asio::steady_timer& timer)
143   {
144     return timer.expiry();
145   }
146 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
147 
148   // Buffer space used to prepare output intended for the transport.
149   std::vector<unsigned char> output_buffer_space_;
150 
151   // A buffer that may be used to prepare output intended for the transport.
152   boost::asio::mutable_buffer output_buffer_;
153 
154   // Buffer space used to read input intended for the engine.
155   std::vector<unsigned char> input_buffer_space_;
156 
157   // A buffer that may be used to read input intended for the engine.
158   boost::asio::mutable_buffer input_buffer_;
159 
160   // The buffer pointing to the engine's unconsumed input.
161   boost::asio::const_buffer input_;
162 };
163 
164 } // namespace detail
165 } // namespace ssl
166 } // namespace asio
167 } // namespace boost
168 
169 #include <boost/asio/detail/pop_options.hpp>
170 
171 #endif // BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP
172