auth.ipp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_IMPL_INTERNAL_AUTH_AUTH_IPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_AUTH_AUTH_IPP
  9. #pragma once
  10. #include <boost/mysql/client_errc.hpp>
  11. #include <boost/mysql/string_view.hpp>
  12. #include <boost/mysql/detail/make_string_view.hpp>
  13. #include <boost/mysql/impl/internal/auth/auth.hpp>
  14. #include <boost/config.hpp>
  15. #include <algorithm>
  16. #include <cstring>
  17. #include <openssl/sha.h>
  18. namespace boost {
  19. namespace mysql {
  20. namespace detail {
  21. // mysql_native_password
  22. // Authorization for this plugin is always challenge (nonce) -> response
  23. // (hashed password).
  24. BOOST_INLINE_CONSTEXPR std::size_t mnp_challenge_length = 20;
  25. BOOST_INLINE_CONSTEXPR std::size_t mnp_response_length = 20;
  26. // challenge must point to challenge_length bytes of data
  27. // output must point to response_length bytes of data
  28. // SHA1( password ) XOR SHA1( "20-bytes random data from server" <concat> SHA1( SHA1( password ) ) )
  29. inline void mnp_compute_auth_string(string_view password, const void* challenge, void* output)
  30. {
  31. // SHA1 (password)
  32. using sha1_buffer = unsigned char[SHA_DIGEST_LENGTH];
  33. sha1_buffer password_sha1;
  34. SHA1(reinterpret_cast<const unsigned char*>(password.data()), password.size(), password_sha1);
  35. // Add server challenge (salt)
  36. unsigned char salted_buffer[mnp_challenge_length + SHA_DIGEST_LENGTH];
  37. memcpy(salted_buffer, challenge, mnp_challenge_length);
  38. SHA1(password_sha1, sizeof(password_sha1), salted_buffer + 20);
  39. sha1_buffer salted_sha1;
  40. SHA1(salted_buffer, sizeof(salted_buffer), salted_sha1);
  41. // XOR
  42. static_assert(mnp_response_length == SHA_DIGEST_LENGTH, "Buffer size mismatch");
  43. for (std::size_t i = 0; i < SHA_DIGEST_LENGTH; ++i)
  44. {
  45. static_cast<std::uint8_t*>(output)[i] = password_sha1[i] ^ salted_sha1[i];
  46. }
  47. }
  48. inline error_code mnp_compute_response(
  49. string_view password,
  50. boost::span<const std::uint8_t> challenge,
  51. bool, // secure_channel
  52. std::vector<std::uint8_t>& output
  53. )
  54. {
  55. // Check challenge size
  56. if (challenge.size() != mnp_challenge_length)
  57. {
  58. return make_error_code(client_errc::protocol_value_error);
  59. }
  60. // Do the calculation
  61. output.resize(mnp_response_length);
  62. mnp_compute_auth_string(password, challenge.data(), output.data());
  63. return error_code();
  64. }
  65. // caching_sha2_password
  66. // Authorization for this plugin may be cleartext password or challenge/response.
  67. // The server has a cache that uses when employing challenge/response. When
  68. // the server sends a challenge of challenge_length bytes, we should send
  69. // the password hashed with the challenge. The server may send a challenge
  70. // equals to perform_full_auth, meaning it could not use the cache to
  71. // complete the auth. In this case, we should just send the cleartext password.
  72. // Doing the latter requires a SSL connection. It is possible to perform full
  73. // auth without an SSL connection, but that requires the server public key,
  74. // and we do not implement that.
  75. BOOST_INLINE_CONSTEXPR std::size_t csha2p_challenge_length = 20;
  76. BOOST_INLINE_CONSTEXPR std::size_t csha2p_response_length = 32;
  77. // challenge must point to challenge_length bytes of data
  78. // output must point to response_length bytes of data
  79. inline void csha2p_compute_auth_string(string_view password, const void* challenge, void* output)
  80. {
  81. static_assert(csha2p_response_length == SHA256_DIGEST_LENGTH, "Buffer size mismatch");
  82. // SHA(SHA(password_sha) concat challenge) XOR password_sha
  83. // hash1 = SHA(pass)
  84. using sha_buffer = std::uint8_t[csha2p_response_length];
  85. sha_buffer password_sha;
  86. SHA256(reinterpret_cast<const unsigned char*>(password.data()), password.size(), password_sha);
  87. // SHA(password_sha) concat challenge = buffer
  88. std::uint8_t buffer[csha2p_response_length + csha2p_challenge_length];
  89. SHA256(password_sha, csha2p_response_length, buffer);
  90. std::memcpy(buffer + csha2p_response_length, challenge, csha2p_challenge_length);
  91. // SHA(SHA(password_sha) concat challenge) = SHA(buffer) = salted_password
  92. sha_buffer salted_password;
  93. SHA256(buffer, sizeof(buffer), salted_password);
  94. // salted_password XOR password_sha
  95. for (unsigned i = 0; i < csha2p_response_length; ++i)
  96. {
  97. static_cast<std::uint8_t*>(output)[i] = salted_password[i] ^ password_sha[i];
  98. }
  99. }
  100. inline bool should_perform_full_auth(boost::span<const std::uint8_t> challenge)
  101. {
  102. // A challenge of "\4" means "perform full auth"
  103. return challenge.size() == 1u && challenge[0] == 4;
  104. }
  105. inline error_code csha2p_compute_response(
  106. string_view password,
  107. boost::span<const std::uint8_t> challenge,
  108. bool secure_channel,
  109. std::vector<std::uint8_t>& output
  110. )
  111. {
  112. if (should_perform_full_auth(challenge))
  113. {
  114. if (!secure_channel)
  115. {
  116. return make_error_code(client_errc::auth_plugin_requires_ssl);
  117. }
  118. output.assign(password.begin(), password.end());
  119. output.push_back(0);
  120. return error_code();
  121. }
  122. else
  123. {
  124. // Check challenge size
  125. if (challenge.size() != csha2p_challenge_length)
  126. {
  127. return make_error_code(client_errc::protocol_value_error);
  128. }
  129. // Do the calculation
  130. output.resize(csha2p_response_length);
  131. csha2p_compute_auth_string(password, challenge.data(), output.data());
  132. return error_code();
  133. }
  134. }
  135. // top-level API
  136. struct authentication_plugin
  137. {
  138. using calculator_signature = error_code (*)(
  139. string_view password,
  140. boost::span<const std::uint8_t> challenge,
  141. bool secure_channel,
  142. std::vector<std::uint8_t>& output
  143. );
  144. string_view name;
  145. calculator_signature calculator;
  146. };
  147. BOOST_INLINE_CONSTEXPR authentication_plugin all_authentication_plugins[] = {
  148. {
  149. make_string_view("mysql_native_password"),
  150. &mnp_compute_response,
  151. },
  152. {
  153. make_string_view("caching_sha2_password"),
  154. &csha2p_compute_response,
  155. },
  156. };
  157. inline const authentication_plugin* find_plugin(string_view name)
  158. {
  159. auto it = std::find_if(
  160. std::begin(all_authentication_plugins),
  161. std::end(all_authentication_plugins),
  162. [name](const authentication_plugin& plugin) { return plugin.name == name; }
  163. );
  164. return it == std::end(all_authentication_plugins) ? nullptr : it;
  165. }
  166. } // namespace detail
  167. } // namespace mysql
  168. } // namespace boost
  169. boost::mysql::error_code boost::mysql::detail::compute_auth_response(
  170. string_view plugin_name,
  171. string_view password,
  172. span<const std::uint8_t> challenge,
  173. bool secure_channel,
  174. auth_response& output
  175. )
  176. {
  177. const auto* plugin = find_plugin(plugin_name);
  178. if (plugin)
  179. {
  180. output.plugin_name = plugin->name;
  181. if (password.empty())
  182. {
  183. // Blank password: we should just return an empty auth string
  184. output.data.clear();
  185. return error_code();
  186. }
  187. else
  188. {
  189. return plugin->calculator(password, challenge, secure_channel, output.data);
  190. }
  191. }
  192. else
  193. {
  194. return client_errc::unknown_auth_plugin;
  195. }
  196. }
  197. #endif