MySQL 26.10.0
Source Code Documentation
classic_auth.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTING_CLASSIC_AUTH_INCLUDED
27#define ROUTING_CLASSIC_AUTH_INCLUDED
28
29#include <memory> // unique_ptr
30#include <string_view>
31#include <system_error>
32
33#include <openssl/ssl.h>
34
38
39template <class T>
41
42template <>
43struct OsslDeleter<EVP_PKEY> {
44 void operator()(EVP_PKEY *k) { EVP_PKEY_free(k); }
45};
46
47using EvpPkey = std::unique_ptr<EVP_PKEY, OsslDeleter<EVP_PKEY>>;
48
49class AuthBase {
50 public:
52 public_key_from_ssl_ctx_as_pem(SSL_CTX *ssl_ctx);
53
55 std::string_view pubkey);
56
58 std::string plaintext, EVP_PKEY *pkey);
59
61 std::string_view ciphertext, EVP_PKEY *priv);
62
64 SSL_CTX *ssl_ctx, std::string_view encrypted, std::string_view nonce);
65
67 const EvpPkey &pkey, std::string_view password, std::string_view nonce);
68
69 static std::string_view strip_trailing_null(std::string_view s);
70
71 static bool connection_has_public_key(
73};
74
75/** Pins one TLS generation for an RSA password exchange. */
77 public:
78 void reset(std::shared_ptr<TlsServerContext> tls_context = {}) {
79 tls_context_ = std::move(tls_context);
80 }
81
82 explicit operator bool() const { return tls_context_ != nullptr; }
83
85
87 std::string_view encrypted, std::string_view nonce) const;
88
89 private:
90 std::shared_ptr<TlsServerContext> tls_context_;
91};
92
93#endif
Definition: classic_auth.h:49
static stdx::expected< std::string, std::error_code > public_key_encrypt(std::string plaintext, EVP_PKEY *pkey)
Definition: classic_auth.cc:101
static stdx::expected< std::string, std::error_code > private_key_decrypt(std::string_view ciphertext, EVP_PKEY *priv)
Definition: classic_auth.cc:141
static stdx::expected< std::string, std::error_code > rsa_encrypt_password(const EvpPkey &pkey, std::string_view password, std::string_view nonce)
Definition: classic_auth.cc:235
static stdx::expected< std::string, std::error_code > public_key_from_ssl_ctx_as_pem(SSL_CTX *ssl_ctx)
Definition: classic_auth.cc:76
static bool connection_has_public_key(MysqlRoutingClassicConnectionBase *connection)
Definition: classic_auth.cc:267
static std::string_view strip_trailing_null(std::string_view s)
remove trailing \0 in a string_view.
Definition: classic_auth.cc:45
static stdx::expected< std::string, std::error_code > rsa_decrypt_password(SSL_CTX *ssl_ctx, std::string_view encrypted, std::string_view nonce)
Definition: classic_auth.cc:214
static stdx::expected< EvpPkey, std::error_code > public_key_from_pem(std::string_view pubkey)
Definition: classic_auth.cc:89
Definition: classic_connection_base.h:57
Pins one TLS generation for an RSA password exchange.
Definition: classic_auth.h:76
std::shared_ptr< TlsServerContext > tls_context_
Definition: classic_auth.h:90
void reset(std::shared_ptr< TlsServerContext > tls_context={})
Definition: classic_auth.h:78
stdx::expected< std::string, std::error_code > public_key() const
Definition: classic_auth.cc:252
stdx::expected< std::string, std::error_code > decrypt_password(std::string_view encrypted, std::string_view nonce) const
Definition: classic_auth.cc:259
std::unique_ptr< EVP_PKEY, OsslDeleter< EVP_PKEY > > EvpPkey
Definition: classic_auth.cc:69
Definition: expected.h:286
static char * password
Definition: mysql_secure_installation.cc:58
void operator()(EVP_PKEY *k)
Definition: classic_auth.h:44
Definition: classic_auth.h:40