Botan 3.4.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
25namespace Botan::TLS {
26
27namespace detail {
28template <typename FunT>
29struct fn_signature_helper : public std::false_type {};
30
31template <typename R, typename D, typename... Args>
32struct fn_signature_helper<R (D::*)(Args...)> {
33 using type = std::function<R(Args...)>;
34};
35} // namespace detail
36
37/**
38 * A helper class to initialize and configure Botan::TLS::Stream
39 */
40class Context {
41 public:
42 // statically extract the function signature type from Callbacks::tls_verify_cert_chain
43 // and reuse it as an std::function<> for the verify callback signature
44 /**
45 * The signature of the callback function should correspond to the signature of
46 * Callbacks::tls_verify_cert_chain
47 */
48 using Verify_Callback = detail::fn_signature_helper<decltype(&Callbacks::tls_verify_cert_chain)>::type;
49
50 Context(std::shared_ptr<Credentials_Manager> credentials_manager,
51 std::shared_ptr<RandomNumberGenerator> rng,
52 std::shared_ptr<Session_Manager> session_manager,
53 std::shared_ptr<const Policy> policy,
54 Server_Information server_info = Server_Information()) :
55 m_credentials_manager(credentials_manager),
56 m_rng(rng),
57 m_session_manager(session_manager),
58 m_policy(policy),
59 m_server_info(std::move(server_info)) {}
60
61 virtual ~Context() = default;
62
63 Context(Context&&) = default;
64 Context(const Context&) = delete;
65 Context& operator=(const Context&) = delete;
66 Context& operator=(Context&&) = delete;
67
68 /**
69 * @brief Override the tls_verify_cert_chain callback
70 *
71 * This changes the verify_callback in the stream's TLS::Context, and hence the tls_verify_cert_chain callback
72 * used in the handshake.
73 * Using this function is equivalent to setting the callback via @see Botan::TLS::Stream::set_verify_callback
74 *
75 * @note This function should only be called before initiating the TLS handshake
76 */
77 void set_verify_callback(Verify_Callback callback) { m_verify_callback = std::move(callback); }
78
79 bool has_verify_callback() const { return static_cast<bool>(m_verify_callback); }
80
81 const Verify_Callback& get_verify_callback() const { return m_verify_callback; }
82
83 void set_server_info(Server_Information server_info) { m_server_info = std::move(server_info); }
84
85 protected:
86 template <class S, class C>
87 friend class Stream;
88
89 std::shared_ptr<Credentials_Manager> m_credentials_manager;
90 std::shared_ptr<RandomNumberGenerator> m_rng;
91 std::shared_ptr<Session_Manager> m_session_manager;
92 std::shared_ptr<const Policy> m_policy;
93
94 Server_Information m_server_info;
95 Verify_Callback m_verify_callback;
96};
97
98} // namespace Botan::TLS
99
100#endif
101#endif // BOTAN_ASIO_TLS_CONTEXT_H_