Botan 3.14.1
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/exceptn.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 if(!m_credentials_manager || !m_rng || !m_session_manager || !m_policy) {
77 throw Invalid_Argument("TLS::Context requires a credentials manager, RNG, session manager and policy");
78 }
79 }
80
81 virtual ~Context() = default;
82
83 Context(Context&&) = default;
84 Context(const Context&) = delete;
85 Context& operator=(const Context&) = delete;
86 Context& operator=(Context&&) = delete;
87
88 /**
89 * @brief Override the tls_verify_cert_chain callback
90 *
91 * This changes the verify_callback in the stream's TLS::Context, and hence the tls_verify_cert_chain callback
92 * used in the handshake.
93 * Using this function is equivalent to setting the callback via @see Botan::TLS::Stream::set_verify_callback
94 *
95 * @note This function should only be called before initiating the TLS handshake
96 */
97 void set_verify_callback(Verify_Callback callback) { m_verify_callback = std::move(callback); }
98
99 bool has_verify_callback() const { return static_cast<bool>(m_verify_callback); }
100
101 const Verify_Callback& get_verify_callback() const { return m_verify_callback; }
102
103 void set_server_info(Server_Information server_info) { m_server_info = std::move(server_info); }
104
105 void set_app_protocols(std::vector<std::string> app_protocols = {}) {
106 m_app_protocols = std::move(app_protocols);
107 }
108
109 protected:
110 template <class S, class C>
111 friend class Stream;
112 friend class StreamCallbacks;
113
114 // NOLINTBEGIN(*-non-private-member-variable*)
115 std::shared_ptr<Credentials_Manager> m_credentials_manager;
116 std::shared_ptr<RandomNumberGenerator> m_rng;
117 std::shared_ptr<Session_Manager> m_session_manager;
118 std::shared_ptr<const Policy> m_policy;
119
120 Server_Information m_server_info;
121 Verify_Callback m_verify_callback;
122 std::vector<std::string> m_app_protocols;
123 // NOLINTEND(*-non-private-member-variable*)
124};
125
126} // namespace Botan::TLS
127
128#endif
129#endif // BOTAN_ASIO_TLS_CONTEXT_H_
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52