Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GnuTLS #663

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
23fed28
Cmake changes
Sep 15, 2020
49561d2
ifdef changes in files
Sep 15, 2020
ad9271b
Replaced all
Sep 15, 2020
7444c04
Cmake fixes
Sep 15, 2020
9d94ec5
Final working changes
Sep 16, 2020
6ef17e8
Using nested namespaces and redefining as::ssl
Sep 16, 2020
cc9d4d8
Add ssl_implementation.hpp
Sep 16, 2020
1367a85
Working changes
Sep 18, 2020
8b9ee52
Remove not condition from ssl_implementation
Sep 18, 2020
d3778b3
remove all not conditions
Sep 18, 2020
6eb8d09
Rename to TLS from SSL
Sep 18, 2020
52ef72d
Rename file to tls_implementation.hpp
Sep 18, 2020
10e6871
Update yaml files + cmake module
Sep 18, 2020
0aa3288
Update gitignore
Sep 18, 2020
abf1536
cosmetic fixes
Sep 18, 2020
5746ffd
Remove find boost asio gnu tls cmake module
Sep 18, 2020
a9da0f2
Use conditional expression in include/Cmakelists.txt
Sep 18, 2020
ac1ecbb
Install GnuTLS packages
Sep 18, 2020
195e1f9
Update matrix pattern numbers:
Sep 18, 2020
50688b7
Update matrix strategy pattern list
Sep 18, 2020
d5a3ef2
Install boost-asio-gnutls
Sep 18, 2020
dc98256
Rename use_verify_file to load_verify_file
Sep 18, 2020
c0ea73c
Create a function to check for short reads
Sep 18, 2020
11bc857
Use sudo for copying files
Sep 18, 2020
9867f96
Reenable all tests
Sep 18, 2020
3a4a8e0
Fix MacOs copy command
Sep 18, 2020
a6b4579
Add default return statement to is_tls_short_read
Sep 18, 2020
a018a30
Debug macos
Sep 18, 2020
c7d4bb6
Debug macos
Sep 18, 2020
bf54b28
Add new patterns to pipeline steps
Sep 18, 2020
98c658f
Mac os debug
Sep 18, 2020
36fc136
Redefine MQTT_USE_TLS to take type of TLS library as value:
Oct 2, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/gha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
- '-DMQTT_TEST_1=ON -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_USE_TLS=ON -DMQTT_BUILD_EXAMPLES=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=ON'
- '-DMQTT_TEST_1=OFF -DMQTT_TEST_2=ON -DMQTT_TEST_3=ON -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_USE_TLS=ON -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF'
- '-DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=ON -DMQTT_TEST_7=ON -DMQTT_USE_TLS=ON -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF'
- '-DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=ON -DMQTT_TEST_5=ON -DMQTT_TEST_6=ON -DMQTT_TEST_7=OFF -DMQTT_USE_TLS=ON -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=OFF -DMQTT_USE_GNU_TLS=ON'
- '-DMQTT_TEST_1=ON -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_USE_TLS=ON -DMQTT_BUILD_EXAMPLES=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=ON -DMQTT_USE_GNU_TLS=ON'
- '-DMQTT_TEST_1=OFF -DMQTT_TEST_2=ON -DMQTT_TEST_3=ON -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_USE_TLS=ON -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_USE_GNU_TLS=ON'
- '-DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=ON -DMQTT_TEST_7=ON -DMQTT_USE_TLS=ON -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_USE_GNU_TLS=ON'
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -66,6 +70,11 @@ jobs:
[ ${{ matrix.pattern }} == 5 ] && FLAGS="-DCMAKE_CXX_COMPILER=g++ -DMQTT_CODECOV=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=ON -DMQTT_TEST_4=ON -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_TLS=OFF -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_STD_ANY=ON -DMQTT_STD_OPTIONAL=ON -DMQTT_STD_VARIANT=ON -DMQTT_STD_STRING_VIEW=ON -DMQTT_STD_SHARED_PTR_ARRAY=OFF"
[ ${{ matrix.pattern }} == 6 ] && FLAGS="-DCMAKE_CXX_COMPILER=g++ -DMQTT_CODECOV=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=ON -DMQTT_TEST_6=ON -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_STD_ANY=ON -DMQTT_STD_OPTIONAL=ON -DMQTT_STD_VARIANT=ON -DMQTT_STD_STRING_VIEW=ON -DMQTT_STD_SHARED_PTR_ARRAY=OFF"
[ ${{ matrix.pattern }} == 7 ] && FLAGS="-DCMAKE_CXX_COMPILER=g++ -DMQTT_CODECOV=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=ON -DMQTT_BUILD_EXAMPLES=ON -DMQTT_USE_TLS=ON -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF"
[ ${{ matrix.pattern }} == 1 ] && FLAGS="-DCMAKE_CXX_COMPILER=clang++ -DMQTT_TEST_1=ON -DMQTT_TEST_2=ON -DMQTT_TEST_3=ON -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF -DMQTT_USE_GNU_TLS=ON"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't right

You're overwriting the flags that are already selected for the "1" case.

If you want to add the GNUTLS implementation to the unit tests, you'll need to add more items to the list "matrix.pattern", and not reuse them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recommendation is to shrink the text matrix a bit.

Right now we have "MQTT_TEST_1" through MQTT_TEST_7". Most likely these can be merged down into a smaller list, perhaps 1 though 5 or something. That'll keep the combinatorial explosion slightly under control.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reduce the test list but I wasn't sure which ones are okay to remove. If you and @redboltz both agree to keep it 1-5 only, I can easily update it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might have misunderstood.

My recommendation was to re-arrange the MQTT_TEST_1 and so on binaries so that the code for each of them is preserved, and all the tests continue working, but there are fewer build varients.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not sure I'm fully following your recommendation.

Did you mean rearranging the patterns such that they are ordered in a way where all patterns that have MQTT_TEST_1
enabled are one after another?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically I meant changing text/CMakeLists.txt to have the different MQTT_TEST_# entries have different cpp files included in them.

[ ${{ matrix.pattern }} == 2 ] && FLAGS="-DCMAKE_CXX_COMPILER=clang++ -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=ON -DMQTT_TEST_5=ON -DMQTT_TEST_6=ON -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_STD_ANY=ON -DMQTT_STD_OPTIONAL=ON -DMQTT_STD_VARIANT=ON -DMQTT_STD_STRING_VIEW=ON -DMQTT_STD_SHARED_PTR_ARRAY=OFF -DMQTT_USE_GNU_TLS=ON"
[ ${{ matrix.pattern }} == 3 ] && FLAGS="-DCMAKE_CXX_COMPILER=clang++ -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=ON -DMQTT_BUILD_EXAMPLES=ON -DMQTT_USE_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_STD_ANY=ON -DMQTT_STD_OPTIONAL=ON -DMQTT_STD_VARIANT=ON -DMQTT_STD_STRING_VIEW=ON -DMQTT_STD_SHARED_PTR_ARRAY=OFF -DMQTT_USE_GNU_TLS=ON"
[ ${{ matrix.pattern }} == 6 ] && FLAGS="-DCMAKE_CXX_COMPILER=g++ -DMQTT_CODECOV=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=ON -DMQTT_TEST_6=ON -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=OFF -DMQTT_USE_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON -DMQTT_USE_LOG=OFF -DMQTT_STD_ANY=ON -DMQTT_STD_OPTIONAL=ON -DMQTT_STD_VARIANT=ON -DMQTT_STD_STRING_VIEW=ON -DMQTT_STD_SHARED_PTR_ARRAY=OFF -DMQTT_USE_GNU_TLS=ON"
[ ${{ matrix.pattern }} == 7 ] && FLAGS="-DCMAKE_CXX_COMPILER=g++ -DMQTT_CODECOV=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=ON -DMQTT_BUILD_EXAMPLES=ON -DMQTT_USE_TLS=ON -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF -DMQTT_USE_GNU_TLS=ON"

cmake -S ${{ github.workspace }} -B ${{ runner.temp }} ${FLAGS} -DBOOSTROOT=/usr
- name: Check Header Dependencies
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
CMakeLists.txt.user*

# Build artifacts
build/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, don't add this. Builds should not happen inside the source tree.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repository's ReadMe suggests doing just that. I've seen several other projects follow the same format.

What's so wrong with adding the build/ directory to this file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least, if it's going to be added to the repository, it needs to happen in it's own pull request.

There are a variety of reasons why builds should not be done inside the source directory, but one specific one that I'll share to help you research the topic, is that cmake natively understands multiple configurations. If you put the build inside the source directory, you pre-clude the ability to have multiple build configurations at the same time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove it. It's just that it was making my life easier as I've always built libraries this way.


# VS code settings
.vscode/
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ OPTION(MQTT_ALWAYS_SEND_REASON_CODE "Always send a reason code, even if the stan
OPTION(MQTT_USE_STATIC_BOOST "Statically link with boost libraries" OFF)
OPTION(MQTT_USE_STATIC_OPENSSL "Statically link with openssl libraries" OFF)
OPTION(MQTT_USE_TLS "Enable building TLS code" OFF)
OPTION(MQTT_USE_GNU_TLS "Enable the use of GnuTLS instead of OpenSSL" OFF)
OPTION(MQTT_USE_WS "Enable building WebSockets code" OFF)
OPTION(MQTT_USE_STR_CHECK "Enable UTF8 String check" ON)
OPTION(MQTT_USE_LOG "Enable building logging code" OFF)
Expand All @@ -27,12 +28,20 @@ IF (POLICY CMP0074)
CMAKE_POLICY(SET CMP0074 NEW)
ENDIF ()

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake ${CMAKE_MODULE_PATH})

IF (MQTT_USE_TLS)
MESSAGE (STATUS "TLS enabled")
ELSE ()
MESSAGE (STATUS "TLS disabled")
ENDIF ()

IF (MQTT_USE_GNU_TLS)
MESSAGE (STATUS "GNU TLS enabled")
ELSE ()
MESSAGE (STATUS "GNU TLS disabled")
ENDIF ()

IF (MQTT_USE_WS)
MESSAGE (STATUS "WebSocket enabled")
ELSE ()
Expand Down Expand Up @@ -130,9 +139,13 @@ ELSE ()
ENDIF ()

IF (MQTT_USE_TLS)
FIND_PACKAGE (OpenSSL REQUIRED)
IF (MQTT_USE_STATIC_OPENSSL)
FIND_PACKAGE (ZLIB REQUIRED)
IF (MQTT_USE_GNU_TLS)
FIND_PACKAGE (GnuTLS REQUIRED)
ELSE ()
FIND_PACKAGE (OpenSSL REQUIRED)
IF (MQTT_USE_STATIC_OPENSSL)
FIND_PACKAGE (ZLIB REQUIRED)
ENDIF ()
ENDIF ()
ENDIF ()

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ g++ -std=c++14 -Ipath_to_mqtt_cpp/include your_program.cpp -pthread additional_o

|what you want|required option|
|---|---|
|TLS support|`-DMQTT_USE_TLS -pthread -lssl -lcrypto`|
|TLS support with OpenSSL|`-DMQTT_USE_TLS -pthread -lssl -lcrypto`|
|TLS support with GnuTLS|`-DMQTT_USE_TLS -DMQTT_USE_GNU_TLS -pthread -lssl -lcrypto`|
|Logging support|`-DMQTT_USE_LOG -DBOOST_LOG_DYN_LINK -lboost_log -lboost_filesystem -lboost_thread`|
|WebSocket support|`-DMQTT_USE_WS`|

Expand Down
50 changes: 49 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ strategy:
MQTT_TEST_5: OFF
MQTT_TEST_6: OFF
MQTT_BUILD_EXAMPLES: ON
ExamplesGnuTls:
MQTT_USE_TLS: ON
MQTT_USE_GNU_TLS: ON
MQTT_USE_WS: OFF
MQTT_USE_STR_CHECK: ON
MQTT_TEST_1: OFF
MQTT_TEST_2: OFF
MQTT_TEST_3: OFF
MQTT_TEST_4: OFF
MQTT_TEST_5: OFF
MQTT_TEST_6: OFF
MQTT_BUILD_EXAMPLES: ON
ExamplesWs:
MQTT_USE_TLS: OFF
MQTT_USE_WS: ON
Expand Down Expand Up @@ -116,6 +128,42 @@ strategy:
MQTT_TEST_5: OFF
MQTT_TEST_6: ON
MQTT_BUILD_EXAMPLES: OFF
TestGroup8:
MQTT_USE_TLS: ON
MQTT_USE_GNU_TLS: ON
MQTT_USE_WS: OFF
MQTT_USE_STR_CHECK: OFF
MQTT_TEST_1: ON
MQTT_TEST_2: OFF
MQTT_TEST_3: OFF
MQTT_TEST_4: OFF
MQTT_TEST_5: OFF
MQTT_TEST_6: OFF
MQTT_BUILD_EXAMPLES: OFF
TestGroup9:
MQTT_USE_TLS: ON
MQTT_USE_GNU_TLS: ON
MQTT_USE_WS: OFF
MQTT_USE_STR_CHECK: ON
MQTT_TEST_1: OFF
MQTT_TEST_2: OFF
MQTT_TEST_3: ON
MQTT_TEST_4: OFF
MQTT_TEST_5: OFF
MQTT_TEST_6: OFF
MQTT_BUILD_EXAMPLES: OFF
TestGroup10:
MQTT_USE_TLS: ON
MQTT_USE_GNU_TLS: ON
MQTT_USE_WS: OFF
MQTT_USE_STR_CHECK: ON
MQTT_TEST_1: OFF
MQTT_TEST_2: OFF
MQTT_TEST_3: OFF
MQTT_TEST_4: OFF
MQTT_TEST_5: OFF
MQTT_TEST_6: ON
MQTT_BUILD_EXAMPLES: OFF

steps:
- script: |
Expand All @@ -127,7 +175,7 @@ steps:
mkdir build
cd build
set CL=/D_WIN32_WINNT#0x0601
cmake -A x64 -DMQTT_USE_TLS=%MQTT_USE_TLS% -DMQTT_USE_WS=%MQTT_USE_WS% -DMQTT_USE_STR_CHECK=%MQTT_USE_STR_CHECK% -DMQTT_TEST_1=%MQTT_TEST_1% -DMQTT_TEST_2=%MQTT_TEST_2% -DMQTT_TEST_3=%MQTT_TEST_3% -DMQTT_TEST_4=%MQTT_TEST_4% -DMQTT_TEST_5=%MQTT_TEST_5% -DMQTT_TEST_6=%MQTT_TEST_6% -DMQTT_BUILD_EXAMPLES=%MQTT_BUILD_EXAMPLES% ..
cmake -A x64 -DMQTT_USE_TLS=%MQTT_USE_TLS% -DMQTT_USE_GNU_TLS=%MQTT_USE_GNU_TLS% -DMQTT_USE_WS=%MQTT_USE_WS% -DMQTT_USE_STR_CHECK=%MQTT_USE_STR_CHECK% -DMQTT_TEST_1=%MQTT_TEST_1% -DMQTT_TEST_2=%MQTT_TEST_2% -DMQTT_TEST_3=%MQTT_TEST_3% -DMQTT_TEST_4=%MQTT_TEST_4% -DMQTT_TEST_5=%MQTT_TEST_5% -DMQTT_TEST_6=%MQTT_TEST_6% -DMQTT_BUILD_EXAMPLES=%MQTT_BUILD_EXAMPLES% ..
cmake --build . --config Release -- -verbosity:minimal
cd test
set PATH=%BOOST_ROOT%\lib;%PATH%
Expand Down
17 changes: 12 additions & 5 deletions example/tls_both.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,12 @@ int main(int argc, char** argv) {
std::uint16_t port = boost::lexical_cast<std::uint16_t>(argv[1]);

// server
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
tls::context ctx(tls::context::tlsv12);
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
ctx.use_certificate_file(base + "server.crt.pem", boost::asio::ssl::context::pem);
ctx.use_private_key_file(base + "server.key.pem", boost::asio::ssl::context::pem);
tls::context::default_workarounds |
tls::context::single_dh_use);
ctx.use_certificate_file(base + "server.crt.pem", tls::context::pem);
ctx.use_private_key_file(base + "server.key.pem", tls::context::pem);

boost::asio::io_context iocs;
auto s = MQTT_NS::server_tls<>(
Expand All @@ -371,7 +371,14 @@ int main(int argc, char** argv) {
std::uint16_t pid_sub2;

auto c = MQTT_NS::make_tls_sync_client(ioc, "localhost", port);

#if defined(MQTT_USE_TLS)
#if defined(MQTT_USE_GNU_TLS)
c->get_ssl_context().use_verify_file(base + "cacert.pem", tls::context::pem);
#else
c->get_ssl_context().load_verify_file(base + "cacert.pem");
#endif // defined(MQTT_USE_GNU_TLS)
#endif // defined(MQTT_USE_TLS)

int count = 0;
auto disconnect = [&] {
Expand Down
7 changes: 7 additions & 0 deletions example/tls_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ int main(int argc, char** argv) {
// Setup client
c->set_client_id("cid1");
c->set_clean_session(true);

#if defined(MQTT_USE_TLS)
#if defined(MQTT_USE_GNU_TLS)
c->get_ssl_context().use_verify_file(cacert, tls::context::pem);
shantanu1singh marked this conversation as resolved.
Show resolved Hide resolved
#else
c->get_ssl_context().load_verify_file(cacert);
#endif // defined(MQTT_USE_GNU_TLS)
#endif // defined(MQTT_USE_TLS)

#if OPENSSL_VERSION_NUMBER >= 0x10101000L

Expand Down
10 changes: 5 additions & 5 deletions example/tls_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ int main(int argc, char** argv) {
std::string cert = argv[2];
std::string key = argv[3];

boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
tls::context ctx(tls::context::tlsv12);
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
ctx.use_certificate_file(cert, boost::asio::ssl::context::pem);
ctx.use_private_key_file(key, boost::asio::ssl::context::pem);
tls::context::default_workarounds |
tls::context::single_dh_use);
ctx.use_certificate_file(cert, tls::context::pem);
ctx.use_private_key_file(key, tls::context::pem);

auto s = MQTT_NS::server_tls<>(
boost::asio::ip::tcp::endpoint(
Expand Down
17 changes: 12 additions & 5 deletions example/tls_ws_both.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ int main(int argc, char** argv) {
std::uint16_t port = boost::lexical_cast<std::uint16_t>(argv[1]);

// server
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
tls::context ctx(tls::context::tlsv12);
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
ctx.use_certificate_file(base + "server.crt.pem", boost::asio::ssl::context::pem);
ctx.use_private_key_file(base + "server.key.pem", boost::asio::ssl::context::pem);
tls::context::default_workarounds |
tls::context::single_dh_use);
ctx.use_certificate_file(base + "server.crt.pem", tls::context::pem);
ctx.use_private_key_file(base + "server.key.pem", tls::context::pem);

boost::asio::io_context iocs;
auto s = MQTT_NS::server_tls_ws<>(
Expand All @@ -370,7 +370,14 @@ int main(int argc, char** argv) {
std::uint16_t pid_sub2;

auto c = MQTT_NS::make_tls_sync_client_ws(ioc, "localhost", port);

#if defined(MQTT_USE_TLS)
#if defined(MQTT_USE_GNU_TLS)
c->get_ssl_context().use_verify_file(base + "cacert.pem", tls::context::pem);
#else
c->get_ssl_context().load_verify_file(base + "cacert.pem");
#endif // defined(MQTT_USE_GNU_TLS)
#endif // defined(MQTT_USE_TLS)

int count = 0;
auto disconnect = [&] {
Expand Down
7 changes: 7 additions & 0 deletions example/tls_ws_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ int main(int argc, char** argv) {
// Setup client
c->set_client_id("cid1");
c->set_clean_session(true);

#if defined(MQTT_USE_TLS)
#if defined(MQTT_USE_GNU_TLS)
c->get_ssl_context().use_verify_file(cacert, tls::context::pem);
#else
c->get_ssl_context().load_verify_file(cacert);
#endif // defined(MQTT_USE_GNU_TLS)
#endif // defined(MQTT_USE_TLS)

// Setup handlers
c->set_connack_handler(
Expand Down
10 changes: 5 additions & 5 deletions example/tls_ws_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ int main(int argc, char** argv) {
std::string cert = argv[2];
std::string key = argv[3];

boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
tls::context ctx(tls::context::tlsv12);
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
ctx.use_certificate_file(cert, boost::asio::ssl::context::pem);
ctx.use_private_key_file(key, boost::asio::ssl::context::pem);
tls::context::default_workarounds |
tls::context::single_dh_use);
ctx.use_certificate_file(cert, tls::context::pem);
ctx.use_private_key_file(key, tls::context::pem);

auto s = MQTT_NS::server_tls_ws<>(
boost::asio::ip::tcp::endpoint(
Expand Down
5 changes: 4 additions & 1 deletion include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ IF(MQTT_USE_STATIC_OPENSSL)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_TLS}>:${CMAKE_DL_LIBS} ZLIB::ZLIB>)
ENDIF()

TARGET_LINK_LIBRARIES(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_TLS}>:OpenSSL::SSL>)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} INTERFACE $<$<AND:$<BOOL:${MQTT_USE_TLS}>,$<NOT:$<BOOL:${MQTT_USE_GNU_TLS}>>>:OpenSSL::SSL>)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_GNU_TLS}>:${GNUTLS_LIBRARIES}>)

TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME}
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${ROOT_INCLUDE_TARGET}>
$<INSTALL_INTERFACE:${ROOT_MQTT_TARGET}>
$<$<BOOL:${MQTT_USE_GNU_TLS}>:${GNUTLS_INCLUDE_DIRS}>
)

TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_TLS}>:MQTT_USE_TLS>)
TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_GNU_TLS}>:MQTT_USE_GNU_TLS>)
TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_WS}>:MQTT_USE_WS>)
TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_STR_CHECK}>:MQTT_USE_STR_CHECK>)
TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} INTERFACE $<$<BOOL:${MQTT_USE_LOG}>:MQTT_USE_LOG>)
Expand Down
Loading