Skip to content

Commit fa143ad

Browse files
authored
Merge pull request #3765 from randombit/fix/asio_compat
Introduce <botan/boost_compat.h> as feature flag
2 parents 4fbff62 + bdb44d8 commit fa143ad

10 files changed

Lines changed: 103 additions & 42 deletions

File tree

doc/api_ref/tls.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,13 @@ TLS Stream
11911191

11921192
:cpp:class:`TLS::Stream` offers a Boost.Asio compatible wrapper around :cpp:class:`TLS::Client` and :cpp:class:`TLS::Server`.
11931193
It can be used as an alternative to Boost.Asio's `ssl::stream <https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/ssl__stream.html>`_ with minor adjustments to the using code.
1194-
It offers the following interface:
1194+
1195+
To use the asio stream wrapper, a relatively recent version of boost is required.
1196+
Include ``botan/boost_compat.h`` and check that ``BOTAN_FOUND_COMPATIBLE_BOOST_VERSION``
1197+
is defined before including ``botan/asio_stream.h`` to be ensure compatibility at
1198+
compile time of your application.
1199+
1200+
The asio Stream offers the following interface:
11951201

11961202
.. cpp:class:: template <class StreamLayer, class ChannelT> TLS::Stream
11971203

src/examples/tls_stream_client.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#include <iostream>
22

3-
#include <botan/asio_stream.h>
4-
#include <botan/auto_rng.h>
5-
#include <botan/certstor_system.h>
6-
#include <botan/tls.h>
3+
#include <botan/boost_compat.h>
4+
#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_VERSION)
75

8-
#include <boost/asio.hpp>
9-
#include <boost/beast.hpp>
10-
#include <boost/bind.hpp>
11-
#include <utility>
6+
#include <botan/asio_stream.h>
7+
#include <botan/auto_rng.h>
8+
#include <botan/certstor_system.h>
9+
#include <botan/tls.h>
10+
11+
#include <boost/asio.hpp>
12+
#include <boost/beast.hpp>
13+
#include <boost/bind.hpp>
14+
#include <utility>
1215

1316
namespace http = boost::beast::http;
1417
namespace ap = boost::asio::placeholders;
@@ -113,3 +116,12 @@ int main() {
113116

114117
return 0;
115118
}
119+
120+
#else
121+
122+
int main() {
123+
std::cout << "Your boost version is too old, sorry.\n";
124+
return 1;
125+
}
126+
127+
#endif

src/lib/tls/asio/asio_async_ops.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
#ifndef BOTAN_ASIO_ASYNC_OPS_H_
1010
#define BOTAN_ASIO_ASYNC_OPS_H_
1111

12-
#include <botan/types.h>
13-
14-
#include <boost/version.hpp>
15-
#if BOOST_VERSION >= 107300
12+
#include <botan/boost_compat.h>
13+
#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_VERSION)
1614

1715
#include <botan/asio_error.h>
1816

@@ -305,5 +303,5 @@ class AsyncHandshakeOperation : public AsyncBase<Handler, typename Stream::execu
305303

306304
#include <boost/asio/unyield.hpp>
307305

308-
#endif // BOOST_VERSION
306+
#endif
309307
#endif // BOTAN_ASIO_ASYNC_OPS_H_

src/lib/tls/asio/asio_context.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
#ifndef BOTAN_ASIO_TLS_CONTEXT_H_
1010
#define BOTAN_ASIO_TLS_CONTEXT_H_
1111

12-
#include <botan/types.h>
13-
14-
#include <boost/version.hpp>
15-
#if BOOST_VERSION >= 107300
12+
#include <botan/boost_compat.h>
13+
#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_VERSION)
1614

1715
#include <functional>
1816

@@ -99,5 +97,5 @@ class Context {
9997

10098
} // namespace Botan::TLS
10199

102-
#endif // BOOST_VERSION
100+
#endif
103101
#endif // BOTAN_ASIO_TLS_CONTEXT_H_

src/lib/tls/asio/asio_error.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
#ifndef BOTAN_ASIO_ERROR_H_
1010
#define BOTAN_ASIO_ERROR_H_
1111

12-
#include <botan/types.h>
13-
14-
#include <boost/version.hpp>
15-
#if BOOST_VERSION >= 107300
12+
#include <botan/boost_compat.h>
13+
#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_VERSION)
1614

1715
#include <boost/system/system_error.hpp>
1816

@@ -122,5 +120,5 @@ struct is_error_code_enum<Botan::ErrorType> {
122120

123121
} // namespace boost::system
124122

125-
#endif // BOOST_VERSION
123+
#endif
126124
#endif // BOTAN_ASIO_ERROR_H_

src/lib/tls/asio/asio_stream.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
#ifndef BOTAN_ASIO_STREAM_H_
1111
#define BOTAN_ASIO_STREAM_H_
1212

13-
#include <botan/types.h>
14-
15-
#include <boost/version.hpp>
16-
17-
// First version of boost asio that is prepared to use C++20 concepts
18-
#if BOOST_VERSION >= 107300
13+
#include <botan/boost_compat.h>
14+
#if !defined(BOTAN_FOUND_COMPATIBLE_BOOST_VERSION)
15+
#error Available boost headers are too old for the boost asio stream.
16+
#else
1917

2018
#include <botan/asio_async_ops.h>
2119
#include <botan/asio_context.h>
@@ -800,5 +798,5 @@ class Stream {
800798

801799
} // namespace Botan::TLS
802800

803-
#endif // BOOST_VERSION
801+
#endif
804802
#endif // BOTAN_ASIO_STREAM_H_

src/lib/utils/boost/boost_compat.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Checks compatibility between the existing headers of Botan and boost
3+
* (C) 2023 Jack Lloyd
4+
* 2023 René Meusel - Rohde & Schwarz Cybersecurity
5+
*
6+
* Botan is released under the Simplified BSD License (see license.txt)
7+
*/
8+
9+
#ifndef BOTAN_BOOST_COMPAT_H_
10+
#define BOTAN_BOOST_COMPAT_H_
11+
12+
#include <botan/build.h>
13+
14+
#if defined(BOTAN_HAS_BOOST_ASIO)
15+
16+
#include <boost/version.hpp>
17+
18+
/** @brief minimum supported boost version for the TLS ASIO wrapper
19+
*
20+
* BOOST_VERSION % 100 is the patch level
21+
* BOOST_VERSION / 100 % 1000 is the minor version
22+
* BOOST_VERSION / 100000 is the major version
23+
*
24+
* Botan may still work with older versions of boost. Though, the asio TLS
25+
* wrapper won't work with versions older than the one specified below.
26+
*
27+
* Also note the changelog with rationales for the required versions:
28+
*
29+
* until Botan 3.2.0
30+
* 1.66.0 - first version to be compatible with Networking TS (N4656) and boost::beast
31+
*
32+
* as of Botan 3.3.0
33+
* 1.73.0 - first version supporting the C++20 concepts syntax
34+
*/
35+
#define BOTAN_MINIMUM_SUPPORTED_BOOST_VERSION 107300
36+
37+
#if BOOST_VERSION >= BOTAN_MINIMUM_SUPPORTED_BOOST_VERSION
38+
39+
/**
40+
* Indicates that the local boost and botan headers are compatible.
41+
*/
42+
#define BOTAN_FOUND_COMPATIBLE_BOOST_VERSION 1
43+
44+
#endif
45+
46+
#endif
47+
#endif

src/lib/utils/boost/info.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ name -> "Boost"
88

99
load_on vendor
1010

11+
<header:public>
12+
boost_compat.h
13+
</header:public>
14+
1115
<libs>
1216
linux -> rt
1317
mingw -> ws2_32

src/tests/test_tls_stream_integration.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_HAS_TLS_ASIO_STREAM) && defined(BOTAN_TARGET_OS_HAS_THREADS)
1313

14-
#include <boost/version.hpp>
15-
#if BOOST_VERSION >= 107300
14+
#include <botan/boost_compat.h>
15+
#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_VERSION)
1616

1717
#include <functional>
1818
#include <memory>
@@ -835,5 +835,5 @@ BOTAN_REGISTER_TEST("tls", "tls_stream_integration", Tls_Stream_Integration_Test
835835

836836
} // namespace Botan_Tests
837837

838-
#endif // BOOST_VERSION
839-
#endif // BOTAN_HAS_TLS && BOTAN_HAS_BOOST_ASIO
838+
#endif
839+
#endif // BOTAN_HAS_TLS && BOTAN_HAS_BOOST_ASIO

src/tests/unit_asio_stream.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_HAS_TLS_ASIO_STREAM)
1212

13-
#include <botan/asio_stream.h>
14-
#include <botan/tls_callbacks.h>
15-
#include <botan/tls_session_manager_noop.h>
13+
#include <botan/boost_compat.h>
14+
#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_VERSION)
1615

17-
#include <boost/version.hpp>
18-
#if BOOST_VERSION >= 107300
16+
#include <botan/asio_stream.h>
17+
#include <botan/tls_callbacks.h>
18+
#include <botan/tls_session_manager_noop.h>
1919

2020
#include <boost/beast/_experimental/test/stream.hpp>
2121
#include <boost/bind.hpp>
@@ -736,5 +736,5 @@ BOTAN_REGISTER_TEST("tls", "tls_asio_stream", Asio_Stream_Tests);
736736

737737
} // namespace Botan_Tests
738738

739-
#endif // BOOST_VERSION
740-
#endif // BOTAN_HAS_TLS && BOTAN_HAS_BOOST_ASIO
739+
#endif
740+
#endif // BOTAN_HAS_TLS && BOTAN_HAS_TLS_ASIO_STREAM

0 commit comments

Comments
 (0)