-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathopenssl_stream.hpp
More file actions
202 lines (152 loc) · 5.95 KB
/
openssl_stream.hpp
File metadata and controls
202 lines (152 loc) · 5.95 KB
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
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/corosio
//
#ifndef BOOST_COROSIO_OPENSSL_STREAM_HPP
#define BOOST_COROSIO_OPENSSL_STREAM_HPP
#include <boost/corosio/detail/config.hpp>
#include <boost/corosio/tls_context.hpp>
#include <boost/corosio/tls_stream.hpp>
#include <boost/capy/buffers/buffer_array.hpp>
#include <boost/capy/concept/stream.hpp>
#include <boost/capy/io/any_stream.hpp>
#include <boost/capy/io_task.hpp>
#include <concepts>
namespace boost::corosio {
/** A TLS stream using OpenSSL.
This class wraps an underlying stream satisfying `capy::Stream`
and provides TLS encryption using the OpenSSL library.
Derives from @ref tls_stream to provide a runtime-polymorphic
interface. The TLS operations are implemented as coroutines
that orchestrate reads and writes on the underlying stream.
@par Construction Modes
Two construction modes are supported:
- **Owning**: Pass stream by value. The openssl_stream takes
ownership and the stream is moved into internal storage.
- **Reference**: Pass stream by pointer. The openssl_stream
does not own the stream; the caller must ensure the stream
outlives this object.
@par Thread Safety
Distinct objects: Safe.@n
Shared objects: Unsafe.
@par Example
@code
tls_context ctx;
ctx.set_hostname("example.com");
ctx.set_verify_mode(tls_verify_mode::peer);
corosio::tcp_socket sock(ioc);
co_await sock.connect(endpoint);
// Reference mode - sock must outlive tls
corosio::openssl_stream tls(&sock, ctx);
auto [ec] = co_await tls.handshake(openssl_stream::client);
// Or owning mode - tls owns the socket
corosio::openssl_stream tls2(std::move(sock), ctx);
@endcode
@see tls_stream, wolfssl_stream
*/
class BOOST_COROSIO_DECL openssl_stream final : public tls_stream
{
struct impl;
capy::any_stream stream_; // must be first - impl_ holds reference
impl* impl_;
public:
/** Construct an OpenSSL stream (owning mode).
Takes ownership of the underlying stream by moving it into
internal storage. The stream will be destroyed when this
openssl_stream is destroyed.
@param stream The stream to take ownership of. Must satisfy
`capy::Stream`.
@param ctx The TLS context containing configuration.
*/
template<capy::Stream S>
requires(!std::same_as<std::decay_t<S>, openssl_stream>)
openssl_stream(S stream, tls_context const& ctx)
: stream_(std::move(stream))
, impl_(make_impl(stream_, ctx))
{
}
/** Construct an OpenSSL stream (reference mode).
Wraps the underlying stream without taking ownership. The
caller must ensure the stream remains valid for the lifetime
of this openssl_stream.
@param stream Pointer to the stream to wrap. Must satisfy
`capy::Stream`.
@param ctx The TLS context containing configuration.
*/
template<capy::Stream S>
openssl_stream(S* stream, tls_context const& ctx)
: stream_(stream)
, impl_(make_impl(stream_, ctx))
{
}
/** Destructor.
Releases the underlying OpenSSL resources. If constructed
in owning mode, also destroys the underlying stream.
*/
~openssl_stream() override;
/** Move construct from another OpenSSL stream.
@param other The source stream. After the move,
@p other is in a valid but unspecified state.
*/
openssl_stream(openssl_stream&& other) noexcept;
/** Move assign from another OpenSSL stream.
@param other The source stream. After the move,
@p other is in a valid but unspecified state.
@return `*this`.
*/
openssl_stream& operator=(openssl_stream&& other) noexcept;
/** Perform the TLS handshake asynchronously.
Suspends the calling coroutine until the handshake
completes, an error occurs, or the operation is
cancelled via stop token.
@par Preconditions
The underlying stream must be connected. No other
TLS operation may be in progress on this stream.
@param type The handshake role (client or server).
@return An awaitable yielding `(error_code)`.
*/
capy::io_task<> handshake(handshake_type type) override;
/** Shut down the TLS session asynchronously.
Sends a close_notify alert and waits for the peer's
close_notify response. Supports cancellation via
stop token.
@par Preconditions
A handshake must have completed successfully. No
other TLS operation may be in progress on this stream.
@return An awaitable yielding `(error_code)`.
*/
capy::io_task<> shutdown() override;
/** Reset TLS session state for reuse.
Clears internal buffers and session data so the stream
can perform a new handshake on the same underlying
connection.
@par Preconditions
No TLS operation may be in progress on this stream.
*/
void reset() override;
/// Return the underlying stream.
capy::any_stream& next_layer() noexcept override
{
return stream_;
}
/// Return the underlying stream.
capy::any_stream const& next_layer() const noexcept override
{
return stream_;
}
/// Return the TLS backend name ("openssl").
std::string_view name() const noexcept override;
protected:
capy::io_task<std::size_t> do_read_some(
capy::mutable_buffer_array<capy::detail::max_iovec_> buffers) override;
capy::io_task<std::size_t> do_write_some(
capy::const_buffer_array<capy::detail::max_iovec_> buffers) override;
private:
static impl* make_impl(capy::any_stream& stream, tls_context const& ctx);
};
} // namespace boost::corosio
#endif