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