MySQL 9.1.0
Source Code Documentation
auth_digest.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2024, 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 MYSQLROUTER_ROUTING_AUTH_DIGEST_H
27#define MYSQLROUTER_ROUTING_AUTH_DIGEST_H
28
29#include <optional>
30#include <string_view>
31
32#include "openssl_digest.h"
33
34namespace routing::impl {
35
36/*
37 * scramble the password with the nonce using a digest function.
38 *
39 * @param nonce the use-once number
40 * @param password cleartext password to scramble
41 * @param digest_func function to use for scrambling
42 * @tparam Ret the inner return type.
43 * @tparam nonce_before_double_hashed_password if nonce or password should be
44 * hashed first.
45 */
46template <class Ret, bool nonce_before_double_hashed_password>
47inline std::optional<Ret> scramble(std::string_view nonce,
48 std::string_view password,
49 const EVP_MD *digest_func) {
50 using return_type = Ret;
51
52 // in case of empty password, the hash is empty too
53 if (password.size() == 0) return Ret{};
54
55 openssl::DigestFunc func(digest_func);
56
57 const int digest_size = func.size();
58
59 openssl::DigestCtx digest(func);
60
61 if (!digest.init() || !digest.update(password)) {
62 return std::nullopt;
63 }
64
65 return_type hashed_password;
66 hashed_password.resize(digest_size);
67
68 if (!digest.finalize(hashed_password) || !digest.init() ||
69 !digest.update(hashed_password)) {
70 return std::nullopt;
71 }
72
73 // digest2 (double-hashed password)
74 return_type digest2;
75 digest2.resize(digest_size);
76
77 if (!digest.finalize(digest2) || !digest.init()) {
78 return std::nullopt;
79 }
80
81 if (nonce_before_double_hashed_password) {
82 if (!digest.update(nonce) || !digest.update(digest2)) {
83 return std::nullopt;
84 }
85 } else {
86 if (!digest.update(digest2) || !digest.update(nonce)) {
87 return std::nullopt;
88 }
89 }
90
91 // overwrite the double-hashed password buffer as it isn't needed anymore
92 //
93 // hash(nonce + double-hashed)
94 if (!digest.finalize(digest2)) {
95 return std::nullopt;
96 }
97
98 // scramble the hashed password with the hash(nonce + double-hashed)
99 for (int i = 0; i < digest_size; ++i) {
100 hashed_password[i] ^= digest2[i];
101 }
102
103 return hashed_password;
104}
105} // namespace routing::impl
106
107template <class Ret>
108std::optional<Ret> mysql_native_password_scramble(std::string_view nonce,
109 std::string_view pwd) {
110 return routing::impl::scramble<Ret, true>(nonce, pwd, EVP_sha1());
111}
112
113template <class Ret>
114std::optional<Ret> caching_sha2_password_scramble(std::string_view nonce,
115 std::string_view pwd) {
116 return routing::impl::scramble<Ret, false>(nonce, pwd, EVP_sha256());
117}
118
119#endif
std::optional< Ret > mysql_native_password_scramble(std::string_view nonce, std::string_view pwd)
Definition: auth_digest.h:108
std::optional< Ret > caching_sha2_password_scramble(std::string_view nonce, std::string_view pwd)
Definition: auth_digest.h:114
Definition: openssl_digest.h:49
bool update(const T &data)
Definition: openssl_digest.h:81
bool init()
Definition: openssl_digest.h:66
bool finalize(T &out)
Definition: openssl_digest.h:86
Definition: openssl_digest.h:37
int size() const
Definition: openssl_digest.h:41
static char * password
Definition: mysql_secure_installation.cc:58
Definition: auth_digest.h:34
std::optional< Ret > scramble(std::string_view nonce, std::string_view password, const EVP_MD *digest_func)
Definition: auth_digest.h:47