Botan 3.12.0
Crypto and TLS for C&
asio_context.h
Go to the documentation of this file.
1/*
2 * TLS Context
3 * (C) 2018-2020 Jack Lloyd
4 * 2018-2020 Hannes Rantzsch, Tim Oesterreich, Rene Meusel
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_ASIO_TLS_CONTEXT_H_
10#define BOTAN_ASIO_TLS_CONTEXT_H_
11
12#include <botan/asio_compat.h>
13#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_ASIO_VERSION)
14
15 #include <functional>
16
17 #include <botan/credentials_manager.h>
18 #include <botan/ocsp.h>
19 #include <botan/rng.h>
20 #include <botan/tls_callbacks.h>
21 #include <botan/tls_policy.h>
22 #include <botan/tls_server_info.h>
23 #include <botan/tls_session_manager.h>
24
25 #if defined(BOTAN_HAS_AUTO_SEEDING_RNG) && defined(BOTAN_HAS_CERTSTOR_SYSTEM)
26 #define BOTAN_HAS_DEFAULT_TLS_CONTEXT
27
28 // TODO(Botan4) remove this
29 #define BOTAN_HAS_HAS_DEFAULT_TLS_CONTEXT
30 #endif
31
32namespace Botan::TLS {
33
34namespace detail {
35template <typename FunT>
36struct fn_signature_helper : public std::false_type {};
37
38template <typename R, typename D, typename... Args>
39struct fn_signature_helper<R (D::*)(Args...)> {
40 using type = std::function<R(Args...)>;
41};
42} // namespace detail
43
44/**
45 * A helper class to initialize and configure Botan::TLS::Stream
46 */
47class BOTAN_PUBLIC_API(2, 11) Context {
48 public:
49 // statically extract the function signature type from Callbacks::tls_verify_cert_chain
50 // and reuse it as an std::function<> for the verify callback signature
51 /**
52 * The signature of the callback function should correspond to the signature of
53 * Callbacks::tls_verify_cert_chain
54 */
55 using Verify_Callback = detail::fn_signature_helper<decltype(&Callbacks::tls_verify_cert_chain)>::type;
56
57 #if defined(BOTAN_HAS_DEFAULT_TLS_CONTEXT)
58 /**
59 * @brief Construct a TLS stream context with typical defaults
60 *
61 * @param server_info Basic information about the host to connect to (SNI)
62 */
63 BOTAN_FUTURE_EXPLICIT Context(Server_Information server_info = Server_Information());
64 #endif
65
66 Context(std::shared_ptr<Credentials_Manager> credentials_manager,
67 std::shared_ptr<RandomNumberGenerator> rng,
68 std::shared_ptr<Session_Manager> session_manager,
69 std::shared_ptr<const Policy> policy,
70 Server_Information server_info = Server_Information()) :
71 m_credentials_manager(std::move(credentials_manager)),
72 m_rng(std::move(rng)),
73 m_session_manager(std::move(session_manager)),
74 m_policy(std::move(policy)),
75 m_server_info(std::move(server_info)) {}
76
77 virtual ~Context() = default;
78
79 Context(Context&&) = default;
80 Context(const Context&) = delete;
81 Context& operator=(const Context&) = delete;
82 Context& operator=(Context&&) = delete;
83
84 /**
85 * @brief Override the tls_verify_cert_chain callback
86 *
87 * This changes the verify_callback in the stream's TLS::Context, and hence the tls_verify_cert_chain callback
88 * used in the handshake.
89 * Using this function is equivalent to setting the callback via @see Botan::TLS::Stream::set_verify_callback
90 *
91 * @note This function should only be called before initiating the TLS handshake
92 */
93 void set_verify_callback(Verify_Callback callback) { m_verify_callback = std::move(callback); }
94
95 bool has_verify_callback() const { return static_cast<bool>(m_verify_callback); }
96
97 const Verify_Callback& get_verify_callback() const { return m_verify_callback; }
98
99 void set_server_info(Server_Information server_info) { m_server_info = std::move(server_info); }
100
101 void set_app_protocols(std::vector<std::string> app_protocols = {}) {
102 m_app_protocols = std::move(app_protocols);
103 }
104
105 protected:
106 template <class S, class C>
107 friend class Stream;
108 friend class StreamCallbacks;
109
110 // NOLINTBEGIN(*-non-private-member-variable*)
111 std::shared_ptr<Credentials_Manager> m_credentials_manager;
112 std::shared_ptr<RandomNumberGenerator> m_rng;
113 std::shared_ptr<Session_Manager> m_session_manager;
114 std::shared_ptr<const Policy> m_policy;
115
116 Server_Information m_server_info;
117 Verify_Callback m_verify_callback;
118 std::vector<std::string> m_app_protocols;
119 // NOLINTEND(*-non-private-member-variable*)
120};
121
122} // namespace Botan::TLS
123
124#endif
125#endif // BOTAN_ASIO_TLS_CONTEXT_H_
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52