Skip to content

Commit 2b53b76

Browse files
authored
Fix openssl3 deprecated functions (#376)
Fix OpenSSL 3.0 deprecated functions Co-authored-by: Gareth Sylvester-Bradley <[email protected]>
1 parent f936df5 commit 2b53b76

File tree

6 files changed

+237
-227
lines changed

6 files changed

+237
-227
lines changed

Development/boost/asio/ssl/use_tmp_ecdh.hpp

+36-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
# define BOOST_ASIO_SYNC_OP_VOID_RETURN(e) return
1717
#endif
1818

19+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
20+
#include <openssl/core_names.h>
21+
#include <openssl/evp.h>
22+
#endif
23+
1924
namespace boost {
2025
namespace asio {
2126
namespace ssl {
@@ -40,16 +45,19 @@ struct evp_pkey_cleanup
4045
~evp_pkey_cleanup() { if (p) ::EVP_PKEY_free(p); }
4146
};
4247

48+
#if OPENSSL_VERSION_NUMBER < 0x30000000L
4349
struct ec_key_cleanup
4450
{
4551
EC_KEY *p;
4652
~ec_key_cleanup() { if (p) ::EC_KEY_free(p); }
4753
};
54+
#endif
4855

4956
inline
5057
BOOST_ASIO_SYNC_OP_VOID do_use_tmp_ecdh(boost::asio::ssl::context& ctx,
5158
BIO* bio, boost::system::error_code& ec)
5259
{
60+
#if OPENSSL_VERSION_NUMBER < 0x30000000L
5361
::ERR_clear_error();
5462

5563
int nid = NID_undef;
@@ -63,7 +71,7 @@ BOOST_ASIO_SYNC_OP_VOID do_use_tmp_ecdh(boost::asio::ssl::context& ctx,
6371
ec_key_cleanup key = { ::EVP_PKEY_get1_EC_KEY(pkey.p) };
6472
if (key.p)
6573
{
66-
const EC_GROUP *group = EC_KEY_get0_group(key.p);
74+
const EC_GROUP* group = EC_KEY_get0_group(key.p);
6775
nid = EC_GROUP_get_curve_name(group);
6876
}
6977
}
@@ -83,6 +91,33 @@ BOOST_ASIO_SYNC_OP_VOID do_use_tmp_ecdh(boost::asio::ssl::context& ctx,
8391
static_cast<int>(::ERR_get_error()),
8492
boost::asio::error::get_ssl_category());
8593
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
94+
#else
95+
::ERR_clear_error();
96+
97+
x509_cleanup x509 = { ::PEM_read_bio_X509(bio, NULL, 0, NULL) };
98+
if (x509.p)
99+
{
100+
evp_pkey_cleanup pkey = { ::X509_get_pubkey(x509.p) };
101+
if (pkey.p)
102+
{
103+
char curve_name[64];
104+
size_t return_size{ 0 };
105+
if (::EVP_PKEY_get_utf8_string_param(pkey.p, OSSL_PKEY_PARAM_GROUP_NAME, curve_name, sizeof(curve_name), &return_size))
106+
{
107+
if (::SSL_CTX_set1_groups_list(ctx.native_handle(), curve_name) == 1)
108+
{
109+
ec = boost::system::error_code();
110+
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
111+
}
112+
}
113+
}
114+
}
115+
116+
ec = boost::system::error_code(
117+
static_cast<int>(::ERR_get_error()),
118+
boost::asio::error::get_ssl_category());
119+
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
120+
#endif
86121
}
87122

88123
inline

Development/nmos/authorization_operation.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,23 @@ namespace nmos
6969
// generate SHA256 with the given string
7070
std::vector<uint8_t> sha256(const std::string& text)
7171
{
72+
#if OPENSSL_VERSION_NUMBER < 0x30000000L
7273
uint8_t hash[SHA256_DIGEST_LENGTH];
7374
SHA256_CTX ctx;
7475
if (SHA256_Init(&ctx) && SHA256_Update(&ctx, text.c_str(), text.size()) && SHA256_Final(hash, &ctx))
7576
{
7677
return{ hash, hash + SHA256_DIGEST_LENGTH };
7778
}
79+
#else
80+
typedef std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> EVP_MD_CTX_ptr;
81+
uint8_t hash[EVP_MAX_MD_SIZE];
82+
uint32_t md_len{ 0 };
83+
EVP_MD_CTX_ptr mdctx(EVP_MD_CTX_new(), &EVP_MD_CTX_free);
84+
if (EVP_DigestInit_ex(mdctx.get(), EVP_sha256(), NULL) && EVP_DigestUpdate(mdctx.get(), text.c_str(), text.size()) && EVP_DigestFinal_ex(mdctx.get(), hash, &md_len))
85+
{
86+
return{ hash, hash + md_len };
87+
}
88+
#endif
7889
return{};
7990
}
8091

@@ -998,6 +1009,10 @@ namespace nmos
9981009
{
9991010
slog::log<slog::severities::error>(gate, SLOG_FLF) << "Authorization API Bearer token request OAuth 2.0 error: " << e.what();
10001011
}
1012+
catch (const nmos::experimental::jwk_exception& e)
1013+
{
1014+
slog::log<slog::severities::error>(gate, SLOG_FLF) << "Authorization API Bearer token request JWK error: " << e.what();
1015+
}
10011016
catch (const std::exception& e)
10021017
{
10031018
slog::log<slog::severities::error>(gate, SLOG_FLF) << "Authorization API Bearer token request error: " << e.what();
@@ -1058,7 +1073,7 @@ namespace nmos
10581073
{
10591074
try
10601075
{
1061-
const auto pem = jwk_to_public_key(jwk); // can throw jwk_exception
1076+
const auto pem = jwk_to_rsa_public_key(jwk); // can throw jwk_exception
10621077

10631078
web::json::push_back(pems, web::json::value_of({
10641079
{ U("jwk"), jwk },
@@ -1895,7 +1910,7 @@ namespace nmos
18951910
{
18961911
try
18971912
{
1898-
const auto& pem = jwk_to_public_key(jwk); // can throw jwk_exception
1913+
const auto& pem = jwk_to_rsa_public_key(jwk); // can throw jwk_exception
18991914

19001915
web::json::push_back(pems, web::json::value_of({
19011916
{ U("jwk"), jwk },

0 commit comments

Comments
 (0)