ssl_context_with_default.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_SSL_CONTEXT_WITH_DEFAULT_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SSL_CONTEXT_WITH_DEFAULT_HPP
  9. #include <boost/asio/ssl/context.hpp>
  10. #include <boost/variant2/variant.hpp>
  11. namespace boost {
  12. namespace mysql {
  13. namespace detail {
  14. inline asio::ssl::context create_default_ssl_context()
  15. {
  16. // As of MySQL 5.7.35, support for previous TLS versions is deprecated,
  17. // so this is a secure default. User can override it if they want
  18. asio::ssl::context ctx(asio::ssl::context::tlsv12_client);
  19. return ctx;
  20. }
  21. inline asio::ssl::context& default_ssl_context()
  22. {
  23. static asio::ssl::context ctx = create_default_ssl_context();
  24. return ctx;
  25. }
  26. class ssl_context_with_default
  27. {
  28. asio::ssl::context* impl_;
  29. public:
  30. ssl_context_with_default(asio::ssl::context* ctx) noexcept : impl_(ctx) {}
  31. asio::ssl::context& get()
  32. {
  33. if (impl_ == nullptr)
  34. impl_ = &default_ssl_context();
  35. return *impl_;
  36. }
  37. // Exposed for the sake of testing
  38. const asio::ssl::context* get_ptr() const noexcept { return impl_; }
  39. };
  40. } // namespace detail
  41. } // namespace mysql
  42. } // namespace boost
  43. #endif