26#ifndef DH_KEYS_INCLUDED
27#define DH_KEYS_INCLUDED
33#include <openssl/dh.h>
34#include <openssl/err.h>
35#include <openssl/evp.h>
36#include <openssl/ssl.h>
45#if OPENSSL_VERSION_NUMBER >= 0x30500000L
61[[maybe_unused]]
bool set_dh(SSL_CTX *ctx) {
72#if OPENSSL_VERSION_NUMBER >= 0x30000000L
73 if (SSL_CTX_set_dh_auto(ctx, 1) == 0) {
83 dh = DH_new_by_nid(NID_ffdhe2048);
86 dh = DH_new_by_nid(NID_ffdhe3072);
89 dh = DH_new_by_nid(NID_ffdhe8192);
97 if (SSL_CTX_set_tmp_dh(ctx, dh) == 0) {
120 std::string *filtered) {
124 static constexpr const char *classic_groups[] = {
128 "secp384r1",
"secp256r1",
"secp521r1"};
129#if OPENSSL_VERSION_NUMBER >= 0x30500000L
130 static constexpr const char *pqc_groups[] = {
131 "X25519MLKEM768",
"secp384r1MLKEM1024",
"secp256r1MLKEM768",
"MLKEM512",
135 auto is_allowed = [](std::string_view group,
bool &is_pqc_capable) {
136#if OPENSSL_VERSION_NUMBER >= 0x30500000L
137 for (
const char *candidate : pqc_groups) {
138 if (group == candidate) {
139 is_pqc_capable =
true;
144 is_pqc_capable =
false;
145 for (
const char *candidate : classic_groups) {
146 if (group == candidate)
return true;
152 std::vector<std::string_view> seen_groups;
154 while (
begin <= groups.length()) {
155 const size_t end = groups.find(
':',
begin);
156 const std::string_view token{
157 groups.data() +
begin,
161 if (token.empty() || !is_allowed(token, is_pqc_capable) ||
165 if (
std::find(seen_groups.begin(), seen_groups.end(), token) ==
167 seen_groups.push_back(token);
168 if (!filtered->empty()) filtered->append(
":");
169 filtered->append(token);
172 if (
end == std::string::npos)
break;
190[[maybe_unused]]
bool set_ecdh(SSL_CTX *ctx,
bool force_pqc =
false,
191 const char *tls_kex =
nullptr) {
192 std::string filtered_tls_kex;
195 if (!filtered_tls_kex.empty()) {
196#if OPENSSL_VERSION_NUMBER >= 0x30500000L
197 if (SSL_CTX_set1_groups_list(
198 ctx,
const_cast<char *
>(filtered_tls_kex.c_str())) == 0)
201 auto group_name_to_nid = [](
const std::string &group) {
203 if (group ==
"X25519")
return NID_X25519;
205 if (group ==
"secp384r1")
return NID_secp384r1;
206 if (group ==
"secp256r1")
return NID_X9_62_prime256v1;
207 if (group ==
"secp521r1")
return NID_secp521r1;
211 std::vector<int> groups;
214 while (
begin <= filtered_tls_kex.length()) {
215 const size_t end = filtered_tls_kex.find(
':',
begin);
216 const std::string token =
217 end == std::string::npos
218 ? filtered_tls_kex.substr(
begin)
220 const int nid = group_name_to_nid(token);
221 if (nid != NID_undef) groups.push_back(nid);
222 if (
end == std::string::npos)
break;
226 if (groups.empty())
return true;
228 if (SSL_CTX_set1_groups(ctx, groups.data(),
229 static_cast<int>(groups.size())) == 0)
236#if OPENSSL_VERSION_NUMBER >= 0x30500000L
255 static constexpr char force_pqc_groups[] =
257 "secp384r1MLKEM1024:"
260 static constexpr char default_groups[] =
262 "?secp384r1MLKEM1024:"
263 "?secp256r1MLKEM768:"
264 "?MLKEM512:?MLKEM768/"
271 static constexpr char fallback_groups[] =
279 const char *
const selected_groups =
280 force_pqc ? force_pqc_groups : default_groups;
281 const char *
const selected_fallback_groups =
284 if (SSL_CTX_set1_groups_list(ctx,
const_cast<char *
>(selected_groups)) == 0) {
285 if (selected_fallback_groups !=
nullptr) {
287 if (SSL_CTX_set1_groups_list(
288 ctx,
const_cast<char *
>(selected_fallback_groups)) != 0)
305 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1};
306 int group_size =
sizeof(groups) /
sizeof(
int);
307 if (SSL_CTX_set1_groups(ctx, groups, group_size) == 0)
return true;
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:171
bool set_dh(SSL_CTX *ctx)
Set DH paramenter for given SSL context.
Definition: dh_ecdh_config.h:61
constexpr bool tls_pqc_sign_supported()
Definition: dh_ecdh_config.h:44
bool sanitize_tls_kex_list(const char *tls_kex, bool force_pqc, std::string *filtered)
Filter a user-configured TLS key exchange list against the internal allowlist and optional PQC-only r...
Definition: dh_ecdh_config.h:119
bool set_ecdh(SSL_CTX *ctx, bool force_pqc=false, const char *tls_kex=nullptr)
Set TLS supported group details.
Definition: dh_ecdh_config.h:190
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:40
const char * begin(const char *const c)
Definition: base64.h:44
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
int security_level(void)
Definition: sql_authentication.cc:1291