aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/asio/asio_stream.h
blob: ced9a65ef04661fee7025050fbec078e4b0a9819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef BOTAN_ASIO_STREAM_H_
#define BOTAN_ASIO_STREAM_H_

#include <botan/asio_async_handshake_op.h>
#include <botan/asio_async_read_op.h>
#include <botan/asio_async_write_op.h>
#include <botan/asio_convert_exceptions.h>
#include <botan/asio_includes.h>
#include <botan/asio_stream_base.h>
#include <botan/asio_stream_core.h>

#include <memory>
#include <thread>
#include <type_traits>

namespace Botan {

/**
 * boost::asio compatible SSL/TLS stream based on TLS::Client or TLS::Server.
 */
template <class StreamLayer, class Channel>
class Stream : public StreamBase<Channel>
   {
   public:
      using next_layer_type = typename std::remove_reference<StreamLayer>::type;

      using lowest_layer_type = typename next_layer_type::lowest_layer_type;

      using executor_type = typename next_layer_type::executor_type;

      template <typename... Args>
      Stream(StreamLayer nextLayer, Args&& ... args)
         : StreamBase<Channel>(std::forward<Args>(args)...),
           nextLayer_(std::forward<StreamLayer>(nextLayer)) {}

      executor_type get_executor() noexcept { return nextLayer_.get_executor(); }

      const next_layer_type& next_layer() const { return nextLayer_; }
      next_layer_type& next_layer() { return nextLayer_; }

      lowest_layer_type& lowest_layer() { return nextLayer_.lowest_layer(); }

      Channel& channel() { return this->channel_; }

      const Channel& channel() const { return this->channel_; }

      void handshake(boost::system::error_code& ec)
         {
         while(!channel().is_active())
            {
            writePendingTlsData(ec);
            if(ec)
               {
               return;
               }

            auto read_buffer = boost::asio::buffer(
                                  this->core_.input_buffer_,
                                  nextLayer_.read_some(this->core_.input_buffer_, ec));
            if(ec)
               {
               return;
               }

            try
               {
               channel().received_data(static_cast<const uint8_t*>(read_buffer.data()),
                                       read_buffer.size());
               }
            catch(...)
               {
               ec = Botan::convertException();
               return;
               }

            writePendingTlsData(ec);
            }
         }

      void handshake()
         {
         boost::system::error_code ec;
         handshake(ec);
         boost::asio::detail::throw_error(ec, "handshake");
         }

      template <typename HandshakeHandler>
      BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler,
                                    void(boost::system::error_code))
      async_handshake(HandshakeHandler&& handler)
         {
         // If you get an error on the following line it means that your handler does
         // not meet the documented type requirements for a HandshakeHandler.
         BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(HandshakeHandler, handler) type_check;

         boost::asio::async_completion<HandshakeHandler,
               void(boost::system::error_code)>
               init(handler);

         auto op = create_async_handshake_op(std::move(init.completion_handler));
         op(boost::system::error_code{}, 0, 1);

         return init.result.get();
         }

      void shutdown(boost::system::error_code& ec)
         {
         try
            {
            channel().close();
            }
         catch(...)
            {
            ec = Botan::convertException();
            return;
            }
         writePendingTlsData(ec);
         }

      void shutdown()
         {
         boost::system::error_code ec;
         shutdown(ec);
         boost::asio::detail::throw_error(ec, "shutdown");
         }

      template <typename MutableBufferSequence>
      std::size_t read_some(const MutableBufferSequence& buffers,
                            boost::system::error_code& ec)
         {
         if(this->core_.hasReceivedData())
            {
            return this->core_.copyReceivedData(buffers);
            }

         auto read_buffer = boost::asio::buffer(
                               this->core_.input_buffer_,
                               nextLayer_.read_some(this->core_.input_buffer_, ec));
         if(ec)
            {
            return 0;
            }

         try
            {
            channel().received_data(static_cast<const uint8_t*>(read_buffer.data()),
                                    read_buffer.size());
            }
         catch(...)
            {
            ec = Botan::convertException();
            return 0;
            }

         return this->core_.copyReceivedData(buffers);
         }

      template <typename MutableBufferSequence>
      std::size_t read_some(const MutableBufferSequence& buffers)
         {
         boost::system::error_code ec;
         auto const n = read_some(buffers, ec);
         boost::asio::detail::throw_error(ec, "read_some");
         return n;
         }

      template <typename ConstBufferSequence>
      std::size_t write_some(const ConstBufferSequence& buffers,
                             boost::system::error_code& ec)
         {
         boost::asio::const_buffer buffer =
            boost::asio::detail::buffer_sequence_adapter<
            boost::asio::const_buffer, ConstBufferSequence>::first(buffers);

         try
            {
            channel().send(static_cast<const uint8_t*>(buffer.data()), buffer.size());
            }
         catch(...)
            {
            ec = Botan::convertException();
            return 0;
            }

         writePendingTlsData(ec);
         if(ec)
            {
            return 0;
            }
         return buffer.size();
         }

      template <typename ConstBufferSequence>
      std::size_t write_some(const ConstBufferSequence& buffers)
         {
         boost::system::error_code ec;
         auto const n = write_some(buffers, ec);
         boost::asio::detail::throw_error(ec, "write_some");
         return n;
         }

      template <typename ConstBufferSequence, typename WriteHandler>
      BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
                                    void(boost::system::error_code, std::size_t))
      async_write_some(const ConstBufferSequence& buffers, WriteHandler&& handler)
         {
         BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;

         boost::asio::const_buffer buffer =
            boost::asio::detail::buffer_sequence_adapter<
            boost::asio::const_buffer, ConstBufferSequence>::first(buffers);

         try
            {
            channel().send(static_cast<const uint8_t*>(buffer.data()),
                           buffer.size());
            }
         catch(...)
            {
            // TODO: don't call directly
            handler(Botan::convertException(), 0);
            return;
            }

         boost::asio::async_completion<WriteHandler,
               void(boost::system::error_code, std::size_t)>
               init(handler);
         auto op = create_async_write_op(std::move(init.completion_handler),
                                         buffer.size());

         boost::asio::async_write(nextLayer_, this->core_.sendBuffer(),
                                  std::move(op));
         return init.result.get();
         }

      template <typename MutableBufferSequence, typename ReadHandler>
      BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
                                    void(boost::system::error_code, std::size_t))
      async_read_some(const MutableBufferSequence& buffers, ReadHandler&& handler)
         {
         BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;

         boost::asio::async_completion<ReadHandler,
               void(boost::system::error_code, std::size_t)>
               init(handler);

         auto op = create_async_read_op(std::move(init.completion_handler), buffers);
         op(boost::system::error_code{}, 0);
         return init.result.get();
         }

   protected:
      size_t writePendingTlsData(boost::system::error_code& ec)
         {
         auto writtenBytes =
            boost::asio::write(nextLayer_, this->core_.sendBuffer(), ec);

         this->core_.consumeSendBuffer(writtenBytes);
         return writtenBytes;
         }

      template <typename Handler>
      Botan::AsyncHandshakeOperation<Channel, StreamLayer, Handler>
      create_async_handshake_op(Handler&& handler)
         {
         return Botan::AsyncHandshakeOperation<Channel, StreamLayer, Handler>(
                   channel(), this->core_, nextLayer_, std::forward<Handler>(handler));
         }

      template <typename Handler, typename MutableBufferSequence>
      Botan::AsyncReadOperation<Channel, StreamLayer, Handler,
            MutableBufferSequence>
            create_async_read_op(Handler&& handler,
                                 const MutableBufferSequence& buffers)
         {
         return Botan::AsyncReadOperation<Channel, StreamLayer, Handler,
                MutableBufferSequence>(
                   channel(), this->core_, nextLayer_, std::forward<Handler>(handler),
                   buffers);
         }

      template <typename Handler>
      Botan::AsyncWriteOperation<Handler>
      create_async_write_op(Handler&& handler, std::size_t plainBytesTransferred)
         {
         return Botan::AsyncWriteOperation<Handler>(
                   this->core_, std::forward<Handler>(handler), plainBytesTransferred);
         }

      StreamLayer nextLayer_;
   };

} // namespace Botan

#endif