Botan 2.19.5 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/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 =
54 detail::fn_signature_helper<decltype(&Callbacks::tls_verify_cert_chain)>::type;
55
56 Context(Credentials_Manager& credentials_manager,
57 RandomNumberGenerator& rng,
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() {}
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 */
84 void set_verify_callback(Verify_Callback callback)
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
94 const Verify_Callback& get_verify_callback() const
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
107 Credentials_Manager& m_credentials_manager;
108 RandomNumberGenerator& m_rng;
109 Session_Manager& m_session_manager;
110 Policy& m_policy;
111
112 Server_Information m_server_info;
113 Verify_Callback m_verify_callback;
114 };
115
116} // namespace TLS
117} // namespace Botan
118
119#endif // BOOST_VERSION
120#endif // BOTAN_ASIO_TLS_CONTEXT_H_
Botan::Credentials_Manager
Definition: credentials_manager.h:31
Botan::RandomNumberGenerator
Definition: rng.h:26
Botan::TLS::Callbacks::tls_verify_cert_chain
virtual void tls_verify_cert_chain(const std::vector< X509_Certificate > &cert_chain, const std::vector< std::shared_ptr< const OCSP::Response > > &ocsp_responses, const std::vector< Certificate_Store * > &trusted_roots, Usage_Type usage, const std::string &hostname, const TLS::Policy &policy)
Definition: tls_callbacks.cpp:53
Botan::TLS::Context
Definition: asio_context.h:45
Botan::TLS::Context::set_server_info
void set_server_info(const Server_Information &server_info)
Definition: asio_context.h:99
Botan::TLS::Context::~Context
virtual ~Context()
Definition: asio_context.h:68
Botan::TLS::Context::Context
Context(const Context &)=delete
Botan::TLS::Context::m_credentials_manager
Credentials_Manager & m_credentials_manager
Definition: asio_context.h:107
Botan::TLS::Context::m_verify_callback
Verify_Callback m_verify_callback
Definition: asio_context.h:113
Botan::TLS::Context::operator=
Context & operator=(Context &&)=delete
Botan::TLS::Context::has_verify_callback
bool has_verify_callback() const
Definition: asio_context.h:89
Botan::TLS::Context::m_server_info
Server_Information m_server_info
Definition: asio_context.h:112
Botan::TLS::Context::m_rng
RandomNumberGenerator & m_rng
Definition: asio_context.h:108
Botan::TLS::Context::set_verify_callback
void set_verify_callback(Verify_Callback callback)
Override the tls_verify_cert_chain callback.
Definition: asio_context.h:84
Botan::TLS::Context::operator=
Context & operator=(const Context &)=delete
Botan::TLS::Context::get_verify_callback
const Verify_Callback & get_verify_callback() const
Definition: asio_context.h:94
Botan::TLS::Context::m_policy
Policy & m_policy
Definition: asio_context.h:110
Botan::TLS::Context::Context
Context(Context &&)=default
Botan::TLS::Context::m_session_manager
Session_Manager & m_session_manager
Definition: asio_context.h:109
Botan::TLS::Context::Verify_Callback
detail::fn_signature_helper< decltype(&Callbacks::tls_verify_cert_chain)>::type Verify_Callback
Definition: asio_context.h:54
Botan::TLS::Context::Context
Context(Credentials_Manager &credentials_manager, RandomNumberGenerator &rng, Session_Manager &session_manager, Policy &policy, Server_Information server_info=Server_Information())
Definition: asio_context.h:56
Botan::TLS::Policy
Definition: tls_policy.h:29
Botan::TLS::Server_Information
Definition: tls_server_info.h:22
Botan::TLS::Session_Manager
Definition: tls_session_manager.h:30
Botan::TLS::Stream
boost::asio compatible SSL/TLS stream
Definition: asio_stream.h:49
Botan
Definition: alg_id.cpp:13
type
MechanismType type
Definition: p11_mechanism.cpp:44
Botan::TLS::detail::fn_signature_helper< R(D::*)(Args...)>::type
std::function< R(Args...)> type
Definition: asio_context.h:37
Botan::TLS::detail::fn_signature_helper
Definition: asio_context.h:32