Skip to content

Commit 06a90a7

Browse files
committed
Support older openssl.
1 parent 2e321ba commit 06a90a7

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

src/plugins/input/tcp/README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ certificates (certificate authority) is relevant only if the option ``verifyPeer
9393

9494
:``caStore``:
9595
Path to certificate store with trusted certificates (certificate authorities). Default uses
96-
system defaults.
96+
system defaults. This is available only when using OpenSSL 3 and newer. When used with older
97+
OpenSSL, the plugin will return error.
9798

9899
Notes
99100
-----

src/plugins/input/tcp/src/Config.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cstring>
1919

2020
#include <libfds.h> // fds_*, FDS_*
21+
#include <openssl/opensslv.h>
2122

2223
#include <ipfixcol2.h> // ipx_ctx, IPX_CTX_WARNING
2324

@@ -199,6 +200,13 @@ void Config::parse_tls(ipx_ctx *ctx, fds_xml_ctx_t *params) {
199200
use_default_ca = !default_ca_file && ca_file.empty()
200201
&& !default_ca_dir && ca_dir.empty()
201202
&& !default_ca_store && ca_store.empty();
203+
204+
// Supported only from OpenSSL 3.0.0-0 release
205+
#if OPENSSL_VERSION_NUMBER < 0x03000000f
206+
if (default_ca_store || !ca_store.empty()) {
207+
throw std::invalid_argument("Certificate store is not supported before openssl 3.");
208+
}
209+
#endif // OPENSSL_VERSION_MAJOR < 0x03000000f
202210
}
203211

204212
} // namespace tcp_in

src/plugins/input/tcp/src/tls/DecoderFactory.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,33 @@ static void load_ca(const Config &conf, SslCtx &ctx) {
7373
return;
7474
}
7575

76+
const char *ca_file = nullptr;
77+
const char *ca_dir = nullptr;
78+
7679
if (conf.default_ca_file) {
7780
ctx.set_default_verify_file();
7881
} else if (!conf.ca_file.empty()) {
79-
ctx.load_verify_file(conf.ca_file.c_str());
82+
ca_file = conf.ca_file.c_str();
8083
}
8184

8285
if (conf.default_ca_dir) {
8386
ctx.set_default_verify_dir();
8487
} else if (!conf.ca_dir.empty()) {
85-
ctx.load_verify_dir(conf.ca_dir.c_str());
88+
ca_dir = conf.ca_dir.c_str();
89+
}
90+
91+
if (ca_file != nullptr || ca_dir != nullptr) {
92+
ctx.load_verify_locations(ca_file, ca_dir);
8693
}
8794

95+
// Supported only from OpenSSL 3.0.0-0 release
96+
#if OPENSSL_VERSION_NUMBER >= 0x03000000f
8897
if (conf.default_ca_store) {
8998
ctx.set_default_verify_store();
9099
} else if (!conf.ca_store.empty()) {
91100
ctx.load_verify_store(conf.ca_store.c_str());
92101
}
102+
#endif // OPENSSL_VERSION_NUMBER >= 0x03000000f
93103
}
94104

95105
} // namespace tls

src/plugins/input/tcp/src/tls/SslCtx.hpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ class SslCtx {
7474
}
7575
}
7676

77-
/**
78-
* @brief Sets the path to trusted certificate file.
79-
* @throws `std::runtime_error` on failure.
80-
*/
81-
void load_verify_file(const char *path) {
82-
if (!SSL_CTX_load_verify_file(m_ctx.get(), path)) {
83-
throw_ssl_err("Failed to set default trusted certificate file `", path, "`.");
84-
}
85-
}
86-
8777
/**
8878
* @brief Sets the path to trusted certificates directory.
8979
*
@@ -98,35 +88,50 @@ class SslCtx {
9888
}
9989

10090
/**
101-
* @brief Sets the path to trusted certificates directory.
102-
* @throws `std::runtime_error` on failure.
91+
* @brief Sets the path for trusted certificate file and directory.
92+
* @param file Path to trusted certificate file.
93+
* @param dir Path to trusted certificate directory.
10394
*/
104-
void load_verify_dir(const char *path) {
105-
if (!SSL_CTX_load_verify_dir(m_ctx.get(), path)) {
106-
throw_ssl_err("Failed to set default trusted certificate directory `", path, "`.");
95+
void load_verify_locations(const char *file, const char *dir) {
96+
if (!SSL_CTX_load_verify_locations(m_ctx.get(), file, dir)) {
97+
throw_ssl_err("Failed to set default trusted certificate locations.");
10798
}
10899
}
109100

110101
/**
111102
* @brief Sets the path to trusted certificates store.
112103
*
113104
* It will use the OS defaults.
105+
*
106+
* This is not available before openssl 3.
114107
* @throws `std::runtime_error` on failure.
115108
*/
116109
void set_default_verify_store() {
110+
// Available only from OpenSSL 3.0.0-0 release
111+
#if OPENSSL_VERSION_NUMBER >= 0x03000000f
117112
if (!SSL_CTX_set_default_verify_store(m_ctx.get())) {
118113
throw_ssl_err("Failed to set default trusted certificate store.");
119114
}
115+
#else // OPENSSL_VERSION_NUMBER >= 0x03000000f
116+
throw std::runtime_error("`set_default_verify_store` is not available before OpenSSL 3.");
117+
#endif // OPENSSL_VERSION_NUMBER >= 0x03000000f
120118
}
121119

122120
/**
123121
* @brief Sets the path to trusted certificates store.
122+
*
123+
* This is not available before openssl 3.
124124
* @throws `std::runtime_error` on failure.
125125
*/
126126
void load_verify_store(const char *path) {
127+
// Available only from OpenSSL 3.0.0-0 release
128+
#if OPENSSL_VERSION_NUMBER >= 0x03000000f
127129
if (!SSL_CTX_load_verify_store(m_ctx.get(), path)) {
128130
throw_ssl_err("Failed to set default trusted certificate store `", path, "`.");
129131
}
132+
#else // OPENSSL_VERSION_NUMBER >= 0x03000000f
133+
throw std::runtime_error("`load_verify_store` is not available before OpenSSL 3.");
134+
#endif // OPENSSL_VERSION_NUMBER >= 0x03000000f
130135
}
131136

132137
/**

0 commit comments

Comments
 (0)