Botan 3.6.1
Crypto and TLS for C&
asio_async_ops.h
Go to the documentation of this file.
1/*
2* Helpers for TLS ASIO Stream
3* (C) 2018-2020 Jack Lloyd
4* 2018-2020 Hannes Rantzsch, Tim Oesterreich, Rene Meusel
5* 2023 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#ifndef BOTAN_ASIO_ASYNC_OPS_H_
11#define BOTAN_ASIO_ASYNC_OPS_H_
12
13#include <botan/asio_compat.h>
14#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_ASIO_VERSION)
15
16 #include <botan/asio_error.h>
17
18 // We need to define BOOST_ASIO_DISABLE_SERIAL_PORT before any asio imports. Otherwise asio will include <termios.h>,
19 // which interferes with Botan's amalgamation by defining macros like 'B0' and 'FF1'.
20 #define BOOST_ASIO_DISABLE_SERIAL_PORT
21 #include <boost/asio.hpp>
22 #include <boost/asio/yield.hpp>
23
24namespace Botan::TLS::detail {
25
26/**
27 * Base class for asynchronous stream operations.
28 *
29 * Asynchronous operations, used for example to implement an interface for boost::asio::async_read_some and
30 * boost::asio::async_write_some, are based on boost::asio::coroutines.
31 * Derived operations should implement a call operator and invoke it with the correct parameters upon construction. The
32 * call operator needs to make sure that the user-provided handler is not called directly. Typically, yield / reenter is
33 * used for this in the following fashion:
34 *
35 * ```
36 * void operator()(boost::system::error_code ec, std::size_t bytes_transferred, bool isContinuation = true)
37 * {
38 * reenter(this)
39 * {
40 * // operation specific logic, repeatedly interacting with the stream_core and the next_layer (socket)
41 *
42 * // make sure intermediate initiating function is called
43 * if(!isContinuation)
44 * {
45 * yield next_layer.async_operation(empty_buffer, this);
46 * }
47 *
48 * // call the completion handler
49 * complete_now(error_code, bytes_transferred);
50 * }
51 * }
52 * ```
53 *
54 * Once the operation is completed and ready to call the completion handler it checks if an intermediate initiating
55 * function has been called using the `isContinuation` parameter. If not, it will call an asynchronous operation, such
56 * as `async_read_some`, with and empty buffer, set the object itself as the handler, and `yield`. As a result, the call
57 * operator will be invoked again, this time as a continuation, and will jump to the location where it yielded before
58 * using `reenter`. It is now safe to call the handler function via `complete_now`.
59 *
60 * \tparam Handler Type of the completion handler
61 * \tparam Executor1 Type of the asio executor (usually derived from the lower layer)
62 * \tparam Allocator Type of the allocator to be used
63 */
64template <class Handler, class Executor1, class Allocator>
65class AsyncBase : public boost::asio::coroutine {
66 public:
67 using allocator_type = boost::asio::associated_allocator_t<Handler, Allocator>;
68 using executor_type = boost::asio::associated_executor_t<Handler, Executor1>;
69
70 allocator_type get_allocator() const noexcept { return boost::asio::get_associated_allocator(m_handler); }
71
72 executor_type get_executor() const noexcept {
73 return boost::asio::get_associated_executor(m_handler, m_work_guard_1.get_executor());
74 }
75
76 protected:
77 template <class HandlerT>
78 AsyncBase(HandlerT&& handler, const Executor1& executor) :
79 m_handler(std::forward<HandlerT>(handler)), m_work_guard_1(executor) {}
80
81 /**
82 * Call the completion handler.
83 *
84 * This function should only be called after an intermediate initiating function has been called.
85 *
86 * @param args Arguments forwarded to the completion handler function.
87 */
88 template <class... Args>
89 void complete_now(Args&&... args) {
90 m_work_guard_1.reset();
91 m_handler(std::forward<Args>(args)...);
92 }
93
94 Handler m_handler;
95 boost::asio::executor_work_guard<Executor1> m_work_guard_1;
96};
97
98template <class Handler, class Stream, class MutableBufferSequence, class Allocator = std::allocator<void>>
99class AsyncReadOperation : public AsyncBase<Handler, typename Stream::executor_type, Allocator> {
100 public:
101 /**
102 * Construct and invoke an AsyncReadOperation.
103 *
104 * @param handler Handler function to be called upon completion.
105 * @param stream The stream from which the data will be read
106 * @param buffers The buffers into which the data will be read.
107 * @param ec Optional error code; used to report an error to the handler function.
108 */
109 template <class HandlerT>
110 AsyncReadOperation(HandlerT&& handler,
111 Stream& stream,
112 const MutableBufferSequence& buffers,
113 const boost::system::error_code& ec = {}) :
114 AsyncBase<Handler, typename Stream::executor_type, Allocator>(std::forward<HandlerT>(handler),
115 stream.get_executor()),
116 m_stream(stream),
117 m_buffers(buffers),
118 m_decodedBytes(0) {
119 this->operator()(ec, std::size_t(0), false);
120 }
121
122 AsyncReadOperation(AsyncReadOperation&&) = default;
123
124 /**
125 * Read and decrypt application data from the peer. Note, that this is
126 * unsuitable to receive initial handshake records! Post-handshake
127 * messages like KeyUpdate will transparently work, though.
128 *
129 * Depending on the situation, this handler will:
130 *
131 * 1. Process TLS data received from the peer, expecting to find:
132 * * a complete and decryptable application data record,
133 * * a TLS alert (e.g. close_notify)
134 * 2. Handle any pending TLS protocol errors and (if non were found) wait
135 * for more data from the peer, or
136 * 3. Copy any received application data out to the downstream application
137 * and finalize this operation.
138 */
139 void operator()(boost::system::error_code ec, std::size_t bytes_transferred, bool isContinuation = true) {
140 reenter(this) {
141 // Check whether we received a premature EOF from the next layer.
142 if(ec == boost::asio::error::eof && !m_stream.shutdown_received()) {
143 ec = StreamError::StreamTruncated;
144 }
145
146 // If we received data from the peer, we hand it to the native
147 // handle for processing. When enough bytes were received to decrypt
148 // an application data record, the "input buffer" will become
149 // non-empty.
150 if(!ec && bytes_transferred > 0) {
151 boost::asio::const_buffer read_buffer{m_stream.input_buffer().data(), bytes_transferred};
152 m_stream.process_encrypted_data(read_buffer);
153 }
154
155 // If the "input buffer" is still empty, indicating that we didn't
156 // successfully decrypt any application data from the peer, yet, ...
157 if(!ec && !m_stream.has_received_data()) {
158 // ... we first ensure that no TLS protocol error was detected until now.
159 // Otherwise the read operation is aborted with an error code.
160 m_stream.handle_tls_protocol_errors(ec);
161
162 if(!ec && boost::asio::buffer_size(m_buffers) > 0) {
163 // ... and wait for more incoming data.
164 m_stream.next_layer().async_read_some(m_stream.input_buffer(), std::move(*this));
165 return;
166 }
167 }
168
169 // If the "input buffer" holds successfully decrypted application
170 // data, we copy it to the "output buffers" and finalize the
171 // operation with the number of produced bytes.
172 if(!ec && m_stream.has_received_data()) {
173 m_decodedBytes = m_stream.copy_received_data(m_buffers);
174 }
175
176 if(!isContinuation) {
177 // Make sure the handler is not called without an intermediate initiating function.
178 // "Reading" into a zero-byte buffer will complete immediately.
179 m_ec = ec;
180 yield m_stream.next_layer().async_read_some(boost::asio::mutable_buffer(), std::move(*this));
181 ec = m_ec;
182 }
183
184 this->complete_now(ec, m_decodedBytes);
185 }
186 }
187
188 private:
189 Stream& m_stream;
190 MutableBufferSequence m_buffers;
191 std::size_t m_decodedBytes;
192 boost::system::error_code m_ec;
193};
194
195template <typename Handler, class Stream, class Allocator = std::allocator<void>>
196class AsyncWriteOperation : public AsyncBase<Handler, typename Stream::executor_type, Allocator> {
197 public:
198 /**
199 * Construct and invoke an AsyncWriteOperation.
200 *
201 * @param handler Handler function to be called upon completion.
202 * @param stream The stream from which the data will be read
203 * @param plainBytesTransferred Number of bytes to be reported to the user-provided handler function as
204 * bytes_transferred. This needs to be provided since the amount of plaintext data
205 * consumed from the input buffer can differ from the amount of encrypted data written
206 * to the next layer.
207 * @param ec Optional error code; used to report an error to the handler function.
208 */
209 template <class HandlerT>
210 AsyncWriteOperation(HandlerT&& handler,
211 Stream& stream,
212 std::size_t plainBytesTransferred,
213 const boost::system::error_code& ec = {}) :
214 AsyncBase<Handler, typename Stream::executor_type, Allocator>(std::forward<HandlerT>(handler),
215 stream.get_executor()),
216 m_stream(stream),
217 m_plainBytesTransferred(plainBytesTransferred) {
218 this->operator()(ec, std::size_t(0), false);
219 }
220
221 AsyncWriteOperation(AsyncWriteOperation&&) = default;
222
223 /**
224 * Write (encrypted) TLS record data generated by us and bound to the peer
225 * from the "send buffer" to the wire.
226 *
227 * Depending on the situation, this handler will:
228 *
229 * 1. Mark the successfully sent bytes as "consumed", or
230 * 2. Send remaining bytes to the peer,
231 *
232 * until the "send buffer" does not contain any more bytes to be sent.
233 */
234 void operator()(boost::system::error_code ec, std::size_t bytes_transferred, bool isContinuation = true) {
235 reenter(this) {
236 // Check whether we received a premature EOF from the next layer.
237 if(ec == boost::asio::error::eof && !m_stream.shutdown_received()) {
238 ec = StreamError::StreamTruncated;
239 }
240
241 // If we have successfully transferred some bytes to the peer, mark
242 // the number of sent bytes as "consumed".
243 if(!ec && bytes_transferred > 0) {
244 m_stream.consume_send_buffer(bytes_transferred);
245 }
246
247 // If we have more data to send, go for it...
248 if(!ec && m_stream.has_data_to_send()) {
249 m_stream.next_layer().async_write_some(m_stream.send_buffer(), std::move(*this));
250 return;
251 }
252
253 if(!isContinuation) {
254 // Make sure the handler is not called without an intermediate initiating function.
255 // "Writing" to a zero-byte buffer will complete immediately.
256 m_ec = ec;
257 yield m_stream.next_layer().async_write_some(boost::asio::const_buffer(), std::move(*this));
258 ec = m_ec;
259 }
260
261 // The size of the sent TLS record can differ from the size of the payload due to TLS encryption. We need to
262 // tell the handler how many bytes of the original data we already processed.
263 this->complete_now(ec, m_plainBytesTransferred);
264 }
265 }
266
267 private:
268 Stream& m_stream;
269 std::size_t m_plainBytesTransferred;
270 boost::system::error_code m_ec;
271};
272
273template <class Handler, class Stream, class Allocator = std::allocator<void>>
274class AsyncHandshakeOperation : public AsyncBase<Handler, typename Stream::executor_type, Allocator> {
275 public:
276 /**
277 * Construct and invoke an AsyncHandshakeOperation.
278 *
279 * @param handler Handler function to be called upon completion.
280 * @param stream The stream from which the data will be read
281 * @param ec Optional error code; used to report an error to the handler function.
282 */
283 template <class HandlerT>
284 AsyncHandshakeOperation(HandlerT&& handler, Stream& stream, const boost::system::error_code& ec = {}) :
285 AsyncBase<Handler, typename Stream::executor_type, Allocator>(std::forward<HandlerT>(handler),
286 stream.get_executor()),
287 m_stream(stream) {
288 this->operator()(ec, std::size_t(0), false);
289 }
290
291 AsyncHandshakeOperation(AsyncHandshakeOperation&&) = default;
292
293 /**
294 * Perform a TLS handshake with the peer.
295 *
296 * Depending on the situation, this handler will:
297 *
298 * 1. Process TLS data received from the peer, and potentially:
299 * * generate a response (another handshake flight, or an alert) or
300 * * finalize the handshake,
301 * 2. Send pending data to the peer, or
302 * 3. Handle any pending TLS protocol errors and (if none were found) wait
303 * for more data from the peer.
304 */
305 void operator()(boost::system::error_code ec, std::size_t bytesTransferred, bool isContinuation = true) {
306 reenter(this) {
307 // Check whether we received a premature EOF from the next layer.
308 // Note that the AsyncWriteOperation handles this internally; here
309 // we only have to handle reading.
310 if(ec == boost::asio::error::eof && !m_stream.shutdown_received()) {
311 ec = StreamError::StreamTruncated;
312 }
313
314 // If we received data from the peer, we hand it to the native
315 // handle for processing. When enough bytes were received this will
316 // result in the advancement of the handshake state and produce data
317 // in the output buffer.
318 if(!ec && bytesTransferred > 0) {
319 boost::asio::const_buffer read_buffer{m_stream.input_buffer().data(), bytesTransferred};
320 m_stream.process_encrypted_data(read_buffer);
321 }
322
323 // If we have data to be sent to the peer, we do that now. Note that
324 // this might either be a flight in our handshake, or a TLS alert
325 // record if we decided to abort due to some failure.
326 if(!ec && m_stream.has_data_to_send()) {
327 // Note: we construct `AsyncWriteOperation` with 0 as its last parameter (`plainBytesTransferred`). This
328 // operation will eventually call `*this` as its own handler, passing the 0 back to this call operator.
329 // This is necessary because the check of `bytesTransferred > 0` assumes that `bytesTransferred` bytes
330 // were just read and are available in input_buffer for further processing.
331 AsyncWriteOperation<AsyncHandshakeOperation<typename std::decay<Handler>::type, Stream, Allocator>,
332 Stream,
333 Allocator>
334 op{std::move(*this), m_stream, 0};
335 return;
336 }
337
338 // If we have no more data from the peer to process and no more data
339 // to be sent to the peer...
340 if(!ec && !m_stream.native_handle()->is_handshake_complete()) {
341 // ... we first ensure that no TLS protocol error was detected until now.
342 // Otherwise the handshake is aborted with an error code.
343 m_stream.handle_tls_protocol_errors(ec);
344
345 if(!ec) {
346 // The handshake is neither finished nor aborted. Wait for
347 // more data from the peer.
348 m_stream.next_layer().async_read_some(m_stream.input_buffer(), std::move(*this));
349 return;
350 }
351 }
352
353 if(!isContinuation) {
354 // Make sure the handler is not called without an intermediate initiating function.
355 // "Reading" into a zero-byte buffer will complete immediately.
356 m_ec = ec;
357 yield m_stream.next_layer().async_read_some(boost::asio::mutable_buffer(), std::move(*this));
358 ec = m_ec;
359 }
360
361 this->complete_now(ec);
362 }
363 }
364
365 private:
366 Stream& m_stream;
367 boost::system::error_code m_ec;
368 boost::system::error_code m_stashed_ec;
369};
370
371} // namespace Botan::TLS::detail
372
373 #include <boost/asio/unyield.hpp>
374
375#endif
376#endif // BOTAN_ASIO_ASYNC_OPS_H_