Botan  2.15.0
Crypto and TLS for C++11
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/build.h>
13 
14 #include <boost/version.hpp>
15 #if BOOST_VERSION >= 106600
16 
17 #include <functional>
18 
19 #include <botan/credentials_manager.h>
20 #include <botan/ocsp.h>
21 #include <botan/rng.h>
22 #include <botan/tls_callbacks.h>
23 #include <botan/tls_policy.h>
24 #include <botan/tls_server_info.h>
25 #include <botan/tls_session_manager.h>
26 
27 namespace Botan {
28 namespace TLS {
29 
30 namespace detail {
31 template <typename FunT>
32 struct fn_signature_helper : public std::false_type {};
33 
34 template <typename R, typename D, typename... Args>
35 struct fn_signature_helper<R(D::*)(Args...)>
36  {
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  */
44 class Context
45  {
46  public:
47  // statically extract the function signature type from Callbacks::tls_verify_cert_chain
48  // and reuse it as an std::function<> for the verify callback signature
49  /**
50  * The signature of the callback function should correspond to the signature of
51  * Callbacks::tls_verify_cert_chain
52  */
53  using Verify_Callback =
55 
56  Context(Credentials_Manager& credentials_manager,
58  Session_Manager& session_manager,
59  Policy& policy,
60  Server_Information server_info = Server_Information()) :
61  m_credentials_manager(credentials_manager),
62  m_rng(rng),
63  m_session_manager(session_manager),
64  m_policy(policy),
65  m_server_info(server_info)
66  {}
67 
68  virtual ~Context() = default;
69 
70  Context(Context&&) = default;
71  Context(const Context&) = delete;
72  Context& operator=(const Context&) = delete;
73  Context& operator=(Context&&) = delete;
74 
75  /**
76  * @brief Override the tls_verify_cert_chain callback
77  *
78  * This changes the verify_callback in the stream's TLS::Context, and hence the tls_verify_cert_chain callback
79  * used in the handshake.
80  * Using this function is equivalent to setting the callback via @see Botan::TLS::Stream::set_verify_callback
81  *
82  * @note This function should only be called before initiating the TLS handshake
83  */
85  {
86  m_verify_callback = std::move(callback);
87  }
88 
89  bool has_verify_callback() const
90  {
91  return static_cast<bool>(m_verify_callback);
92  }
93 
95  {
96  return m_verify_callback;
97  }
98 
99  void set_server_info(const Server_Information& server_info)
100  {
101  m_server_info = server_info;
102  }
103 
104  protected:
105  template <class S, class C> friend class Stream;
106 
111 
114  };
115 
116 } // namespace TLS
117 } // namespace Botan
118 
119 #endif // BOOST_VERSION
120 #endif // BOTAN_ASIO_TLS_CONTEXT_H_
RandomNumberGenerator & m_rng
Definition: asio_context.h:108
void set_verify_callback(Verify_Callback callback)
Override the tls_verify_cert_chain callback.
Definition: asio_context.h:84
void set_server_info(const Server_Information &server_info)
Definition: asio_context.h:99
Credentials_Manager & m_credentials_manager
Definition: asio_context.h:107
Context(Credentials_Manager &credentials_manager, RandomNumberGenerator &rng, Session_Manager &session_manager, Policy &policy, Server_Information server_info=Server_Information())
Definition: asio_context.h:56
Server_Information m_server_info
Definition: asio_context.h:112
bool has_verify_callback() const
Definition: asio_context.h:89
boost::asio compatible SSL/TLS stream
Definition: asio_stream.h:48
Definition: alg_id.cpp:13
virtual ~Context()=default
Context & operator=(const Context &)=delete
Session_Manager & m_session_manager
Definition: asio_context.h:109
const Verify_Callback & get_verify_callback() const
Definition: asio_context.h:94
Verify_Callback m_verify_callback
Definition: asio_context.h:113
detail::fn_signature_helper< decltype(&Callbacks::tls_verify_cert_chain)>::type Verify_Callback
Definition: asio_context.h:54