Botan 3.11.0
Crypto and TLS for C&
tls_client.h
Go to the documentation of this file.
1/*
2* TLS Client
3* (C) 2004-2011 Jack Lloyd
4* 2016 Matthias Gierlings
5* 2021 Elektrobit Automotive GmbH
6* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#ifndef BOTAN_TLS_CLIENT_H_
12#define BOTAN_TLS_CLIENT_H_
13
14#include <botan/credentials_manager.h>
15#include <botan/tls_callbacks.h> // TODO(Botan4) not necessary here, remove
16#include <botan/tls_channel.h>
17#include <botan/tls_policy.h> // TODO(Botan4) not necessary here, remove
18#include <botan/tls_server_info.h>
19#include <botan/tls_version.h>
20#include <memory>
21#include <vector>
22
23namespace Botan::TLS {
24
25class Callbacks;
26class Session_Manager;
27class Channel_Impl;
28class Policy;
29
30/**
31* SSL/TLS Client
32*/
33class BOTAN_PUBLIC_API(2, 0) Client final : public Channel {
34 public:
35 /**
36 * Initialize a new TLS client. The constructor will immediately initiate a
37 * new session.
38 *
39 * The @p callbacks parameter specifies the various application callbacks
40 * which pertain to this particular client connection.
41 *
42 * The @p session_manager is an interface for storing TLS sessions, which
43 * allows for session resumption upon reconnecting to a server. In the
44 * absence of a need for persistent sessions, use
45 * TLS::Session_Manager_In_Memory which caches connections for the lifetime
46 * of a single process.
47 *
48 * The @p credentials_manager is an interface that will be called to
49 * retrieve any certificates, private keys, or pre-shared keys.
50 *
51 * Use the optional @p server_info to specify the DNS name of the server
52 * you are attempting to connect to, if you know it. This helps the server
53 * select what certificate to use and helps the client validate the
54 * connection.
55 *
56 * Use the optional @p offer_version to control the version of TLS you wish
57 * the client to offer. Normally, you'll want to offer the most recent
58 * version of (D)TLS that is available, however some broken servers are
59 * intolerant of certain versions being offered, and for classes of
60 * applications that have to deal with such servers (typically web
61 * browsers) it may be necessary to implement a version backdown strategy
62 * if the initial attempt fails.
63 *
64 * @warning Implementing such a backdown strategy allows an attacker to
65 * downgrade your connection to the weakest protocol that both you
66 * and the server support.
67 *
68 * Setting @p offer_version is also used to offer DTLS instead of TLS; use
69 * TLS::Protocol_Version::latest_dtls_version().
70 *
71 * Optionally, the client will advertise @p next_protocols to the server
72 * using the ALPN extension.
73 *
74 * The optional @p reserved_io_buffer_size specifies how many bytes to
75 * pre-allocate in the I/O buffers. Use this if you want to control how
76 * much memory the channel uses initially (the buffers will be resized as
77 * needed to process inputs). Otherwise some reasonable default is used.
78 * The TLS 1.3 implementation ignores this.
79 */
80 Client(const std::shared_ptr<Callbacks>& callbacks,
81 const std::shared_ptr<Session_Manager>& session_manager,
82 const std::shared_ptr<Credentials_Manager>& creds,
83 const std::shared_ptr<const Policy>& policy,
84 const std::shared_ptr<RandomNumberGenerator>& rng,
87 const std::vector<std::string>& next_protocols = {},
88 size_t reserved_io_buffer_size = TLS::Client::IO_BUF_DEFAULT_SIZE);
89
90 ~Client() override;
91
92 /**
93 * @return network protocol as advertised by the TLS server, if server sent the ALPN extension
94 */
95 std::string application_protocol() const override;
96
97 size_t from_peer(std::span<const uint8_t> data) override;
98
99 bool is_handshake_complete() const override;
100
101 bool is_active() const override;
102
103 bool is_closed() const override;
104
105 bool is_closed_for_reading() const override;
106 bool is_closed_for_writing() const override;
107
108 std::vector<X509_Certificate> peer_cert_chain() const override;
109 std::shared_ptr<const Public_Key> peer_raw_public_key() const override;
110 std::optional<std::string> external_psk_identity() const override;
111
112 SymmetricKey key_material_export(std::string_view label, std::string_view context, size_t length) const override;
113
114 void renegotiate(bool force_full_renegotiation = false) override;
115
116 void update_traffic_keys(bool request_peer_update = false) override;
117
118 bool secure_renegotiation_supported() const override;
119
120 void to_peer(std::span<const uint8_t> data) override;
121
122 void send_alert(const Alert& alert) override;
123
124 void send_warning_alert(Alert::Type type) override;
125
126 void send_fatal_alert(Alert::Type type) override;
127
128 void close() override;
129
130 bool timeout_check() override;
131
132 Client(const Client& other) = delete;
133 Client(Client&& other) = default;
134 Client& operator=(const Client& other) = delete;
135 Client& operator=(Client&& other) = delete;
136
137 private:
138 size_t downgrade();
139
140 private:
141 std::unique_ptr<Channel_Impl> m_impl;
142};
143} // namespace Botan::TLS
144
145#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
AlertType Type
Definition tls_alert.h:72
Channel(const Channel &other)=delete
static constexpr size_t IO_BUF_DEFAULT_SIZE
Definition tls_channel.h:37
bool is_closed_for_reading() const override
Client(const Client &other)=delete
bool is_handshake_complete() const override
void renegotiate(bool force_full_renegotiation=false) override
void close() override
std::string application_protocol() const override
Client(Client &&other)=default
std::shared_ptr< const Public_Key > peer_raw_public_key() const override
Client(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &creds, const std::shared_ptr< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng, Server_Information server_info=Server_Information(), Protocol_Version offer_version=Protocol_Version::latest_tls_version(), const std::vector< std::string > &next_protocols={}, size_t reserved_io_buffer_size=TLS::Client::IO_BUF_DEFAULT_SIZE)
bool secure_renegotiation_supported() const override
bool is_active() const override
SymmetricKey key_material_export(std::string_view label, std::string_view context, size_t length) const override
bool is_closed_for_writing() const override
void send_fatal_alert(Alert::Type type) override
bool timeout_check() override
void send_warning_alert(Alert::Type type) override
Client & operator=(Client &&other)=delete
void to_peer(std::span< const uint8_t > data) override
~Client() override
std::vector< X509_Certificate > peer_cert_chain() const override
bool is_closed() const override
void update_traffic_keys(bool request_peer_update=false) override
std::optional< std::string > external_psk_identity() const override
Client & operator=(const Client &other)=delete
void send_alert(const Alert &alert) override
size_t from_peer(std::span< const uint8_t > data) override
static Protocol_Version latest_tls_version()
OctetString SymmetricKey
Definition symkey.h:140