Skip to content

Commit

Permalink
Fix build on Debian Bullseye
Browse files Browse the repository at this point in the history
  • Loading branch information
Benno Evers committed Feb 17, 2023
1 parent 445d3aa commit 1804776
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions libcaf_openssl/src/openssl/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include "caf/openssl/session.hpp"

#include <optional>

#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/decoder.h>
#endif

CAF_PUSH_WARNINGS
#include <openssl/err.h>
Expand Down Expand Up @@ -79,6 +82,7 @@ std::optional<std::string> contents_from_indirect_envvar(const std::string& str)
// If the input is a string like `env:SOME_VARIABLE` and the environment variable
// `SOME_VARIABLE` exists, returns a string with the value of `SOME_VARIABLE`.
std::optional<std::string> tmpfile_from_string(const std::string& content) {
std::cout << "content:\n" << content << std::endl;
auto filename = std::string{"/tmp/caf-openssl.XXXXXX"};
::mkstemp(&filename[0]);
std::ofstream ofs(filename);
Expand Down Expand Up @@ -262,9 +266,10 @@ SSL_CTX* session::create_ssl_context() {
// memory and the implementation of `SSL_CTX_use_certificate_chain_file`
// is huge, so we write into a temporary file.
if (auto filename = pem_file_from_envvar(cfg.openssl_certificate)) {
std::cout << *filename << std::endl;
if (SSL_CTX_use_certificate_chain_file(ctx, filename->c_str())
!= 1)
CAF_RAISE_ERROR("cannot load certificate from environt variable");
CAF_RAISE_ERROR("cannot load certificate from environment variable");
} else if (!cfg.openssl_certificate.empty()
&& SSL_CTX_use_certificate_chain_file(ctx,
cfg.openssl_certificate.c_str())
Expand All @@ -277,20 +282,20 @@ SSL_CTX* session::create_ssl_context() {
SSL_CTX_set_default_passwd_cb_userdata(ctx, this);
}
if (auto var = pem_string_from_envvar(cfg.openssl_key)) {
#if OPENSSL_VERSION_NUMBER < 0x10101000L
#if OPENSSL_VERSION_NUMBER < 0x30000000L
// BIO_new_mem_buf just creates a read-only view, so we don't need
// to free it afterwards.
kbio = BIO_new_mem_buf(var->data(), -1);
auto* kbio = BIO_new_mem_buf(var->data(), -1);
if (!kbio)
CAF_RAISE_ERROR("");
CAF_RAISE_ERROR("failed to construct OpenSSL BIO");
// This function was deprecated in favor of the OSSL_DECODER API
// introduced in OpenSSL 3.0, but that is not available on
// Debian Bullseye.
rsa = PEM_read_bio_RSAPrivateKey(kbio, NULL, 0, NULL);
auto* rsa = PEM_read_bio_RSAPrivateKey(kbio, NULL, 0, NULL);
if (rsa != NULL)
CAF_RAISE_CAF_RAISE_ERROR("invalid private key");
CAF_RAISE_ERROR("invalid private key");
SSL_CTX_use_RSAPrivateKey(ctx, rsa);
#else /* OPENSSL_VERSION_NUMBER < 0x10101000L */
#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
EVP_PKEY *pkey = nullptr;
const char *format = "PEM"; // NULL for any format
const char *structure = nullptr; // Any structure
Expand All @@ -311,7 +316,7 @@ SSL_CTX* session::create_ssl_context() {
// Use the private key.
SSL_CTX_use_PrivateKey(ctx, pkey);
OSSL_DECODER_CTX_free(dctx);
#endif /* OPENSSL_VERSION_NUMBER < 0x10101000L */
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
}
else if (!cfg.openssl_key.empty()
&& SSL_CTX_use_PrivateKey_file(ctx, cfg.openssl_key.c_str(),
Expand Down

0 comments on commit 1804776

Please sign in to comment.