Botan 3.6.1
Crypto and TLS for C&
Botan::TLS::Client Class Referencefinal

#include <tls_client.h>

Inheritance diagram for Botan::TLS::Client:
Botan::TLS::Channel

Public Member Functions

std::string application_protocol () 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)
 
void close () override
 
std::optional< std::string > external_psk_identity () const override
 
size_t from_peer (std::span< const uint8_t > data) override
 
bool is_active () const override
 
bool is_closed () const override
 
bool is_closed_for_reading () const override
 
bool is_closed_for_writing () const override
 
bool is_handshake_complete () const override
 
SymmetricKey key_material_export (std::string_view label, std::string_view context, size_t length) const override
 
std::vector< X509_Certificatepeer_cert_chain () const override
 
std::shared_ptr< const Public_Keypeer_raw_public_key () const override
 
size_t received_data (const uint8_t buf[], size_t buf_size)
 
size_t received_data (std::span< const uint8_t > data)
 
void renegotiate (bool force_full_renegotiation=false) override
 
bool secure_renegotiation_supported () const override
 
void send (const uint8_t buf[], size_t buf_size)
 
void send (std::span< const uint8_t > data)
 
void send (std::string_view val)
 
void send_alert (const Alert &alert) override
 
void send_fatal_alert (Alert::Type type) override
 
void send_warning_alert (Alert::Type type) override
 
bool timeout_check () override
 
void to_peer (std::span< const uint8_t > data) override
 
void update_traffic_keys (bool request_peer_update=false) override
 
 ~Client () override
 

Static Public Attributes

static constexpr size_t IO_BUF_DEFAULT_SIZE = 10 * 1024
 

Detailed Description

SSL/TLS Client

Definition at line 28 of file tls_client.h.

Constructor & Destructor Documentation

◆ Client()

Botan::TLS::Client::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 )

Initialize a new TLS client. The constructor will immediately initiate a new session.

The callbacks parameter specifies the various application callbacks which pertain to this particular client connection.

The session_manager is an interface for storing TLS sessions, which allows for session resumption upon reconnecting to a server. In the absence of a need for persistent sessions, use TLS::Session_Manager_In_Memory which caches connections for the lifetime of a single process.

The credentials_manager is an interface that will be called to retrieve any certificates, private keys, or pre-shared keys.

Use the optional server_info to specify the DNS name of the server you are attempting to connect to, if you know it. This helps the server select what certificate to use and helps the client validate the connection.

Use the optional offer_version to control the version of TLS you wish the client to offer. Normally, you'll want to offer the most recent version of (D)TLS that is available, however some broken servers are intolerant of certain versions being offered, and for classes of applications that have to deal with such servers (typically web browsers) it may be necessary to implement a version backdown strategy if the initial attempt fails.

Warning
Implementing such a backdown strategy allows an attacker to downgrade your connection to the weakest protocol that both you and the server support.

Setting offer_version is also used to offer DTLS instead of TLS; use TLS::Protocol_Version::latest_dtls_version().

Optionally, the client will advertise next_protocols to the server using the ALPN extension.

The optional reserved_io_buffer_size specifies how many bytes to pre-allocate in the I/O buffers. Use this if you want to control how much memory the channel uses initially (the buffers will be resized as needed to process inputs). Otherwise some reasonable default is used. The TLS 1.3 implementation ignores this.

Definition at line 30 of file tls_client.cpp.

38 {
39 BOTAN_ARG_CHECK(policy->acceptable_protocol_version(offer_version),
40 "Policy does not allow to offer requested protocol version");
41
42#if defined(BOTAN_HAS_TLS_13)
43 if(offer_version == Protocol_Version::TLS_V13) {
44 m_impl = std::make_unique<Client_Impl_13>(
45 callbacks, session_manager, creds, policy, rng, std::move(info), next_protocols);
46
47 if(m_impl->expects_downgrade()) {
48 m_impl->set_io_buffer_size(io_buf_sz);
49 }
50
51 if(m_impl->is_downgrading()) {
52 // TLS 1.3 implementation found a resumable TLS 1.2 session and
53 // requested a downgrade right away.
54 downgrade();
55 }
56
57 return;
58 }
59#endif
60
61 m_impl = std::make_unique<Client_Impl_12>(callbacks,
62 session_manager,
63 creds,
64 policy,
65 rng,
66 std::move(info),
67 offer_version.is_datagram_protocol(),
68 next_protocols,
69 io_buf_sz);
70}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29

References BOTAN_ARG_CHECK, and Botan::TLS::Protocol_Version::is_datagram_protocol().

◆ ~Client()

Botan::TLS::Client::~Client ( )
overridedefault

Member Function Documentation

◆ application_protocol()

std::string Botan::TLS::Client::application_protocol ( ) const
overridevirtual
Returns
network protocol as advertised by the TLS server, if server sent the ALPN extension

Implements Botan::TLS::Channel.

Definition at line 172 of file tls_client.cpp.

172 {
173 return m_impl->application_protocol();
174}

◆ close()

void Botan::TLS::Client::close ( )
overridevirtual

Send a close notification alert

Implements Botan::TLS::Channel.

Definition at line 164 of file tls_client.cpp.

164 {
165 m_impl->close();
166}

◆ external_psk_identity()

std::optional< std::string > Botan::TLS::Client::external_psk_identity ( ) const
overridevirtual
Returns
identity of the PSK used for this connection or std::nullopt if no PSK was used.

Implements Botan::TLS::Channel.

Definition at line 128 of file tls_client.cpp.

128 {
129 return m_impl->external_psk_identity();
130}

◆ from_peer()

size_t Botan::TLS::Client::from_peer ( std::span< const uint8_t > data)
overridevirtual

Implements Botan::TLS::Channel.

Definition at line 90 of file tls_client.cpp.

90 {
91 auto read = m_impl->from_peer(data);
92
93 if(m_impl->is_downgrading()) {
94 read = downgrade();
95 }
96
97 return read;
98}

◆ is_active()

bool Botan::TLS::Client::is_active ( ) const
overridevirtual

Check whether the connection is ready to send application data. Note that a TLS 1.3 server MAY send data before receiving the client's Finished message. Only after receiving the client's Finished, can the server be sure about the client's liveness and (optional) identity.

Consider using is_handshake_complete() if you need to wait until the handshake if fully complete.

Returns
true iff the connection is active for sending application data

Implements Botan::TLS::Channel.

Definition at line 104 of file tls_client.cpp.

104 {
105 return m_impl->is_active();
106}

◆ is_closed()

bool Botan::TLS::Client::is_closed ( ) const
overridevirtual

Note: For TLS 1.3 a connection is closed only after both peers have signaled a "close_notify". While TLS 1.2 automatically responded in suit once the peer had sent "close_notify", TLS 1.3 allows to continue transmitting data even if the peer closed their writing end.

Returns
true iff the connection has been definitely closed

Implements Botan::TLS::Channel.

Definition at line 108 of file tls_client.cpp.

108 {
109 return m_impl->is_closed();
110}

◆ is_closed_for_reading()

bool Botan::TLS::Client::is_closed_for_reading ( ) const
overridevirtual
Returns
true iff the peer closed their channel (i.e. no more incoming data expected)

Implements Botan::TLS::Channel.

Definition at line 112 of file tls_client.cpp.

112 {
113 return m_impl->is_closed_for_reading();
114}

◆ is_closed_for_writing()

bool Botan::TLS::Client::is_closed_for_writing ( ) const
overridevirtual
Returns
true iff we closed our channel (i.e. no more outgoing data allowed)

Implements Botan::TLS::Channel.

Definition at line 116 of file tls_client.cpp.

116 {
117 return m_impl->is_closed_for_writing();
118}

◆ is_handshake_complete()

bool Botan::TLS::Client::is_handshake_complete ( ) const
overridevirtual

Becomes true as soon as the TLS handshake is fully complete and all security assurances TLS provides can be guaranteed.

Returns
true once the TLS handshake has finished successfully

Implements Botan::TLS::Channel.

Definition at line 100 of file tls_client.cpp.

100 {
101 return m_impl->is_handshake_complete();
102}

◆ key_material_export()

SymmetricKey Botan::TLS::Client::key_material_export ( std::string_view label,
std::string_view context,
size_t length ) const
overridevirtual

Key material export (RFC 5705)

Parameters
labela disambiguating label string
contexta per-association context value
lengththe length of the desired key in bytes
Returns
key of length bytes

Implements Botan::TLS::Channel.

Definition at line 132 of file tls_client.cpp.

132 {
133 return m_impl->key_material_export(label, context, length);
134}

◆ peer_cert_chain()

std::vector< X509_Certificate > Botan::TLS::Client::peer_cert_chain ( ) const
overridevirtual
Returns
certificate chain of the peer (may be empty)

Implements Botan::TLS::Channel.

Definition at line 120 of file tls_client.cpp.

120 {
121 return m_impl->peer_cert_chain();
122}

◆ peer_raw_public_key()

std::shared_ptr< const Public_Key > Botan::TLS::Client::peer_raw_public_key ( ) const
overridevirtual
Returns
raw public key of the peer (may be nullptr)

Implements Botan::TLS::Channel.

Definition at line 124 of file tls_client.cpp.

124 {
125 return m_impl->peer_raw_public_key();
126}

◆ received_data() [1/2]

size_t Botan::TLS::Channel::received_data ( const uint8_t buf[],
size_t buf_size )
inlineinherited

Definition at line 48 of file tls_channel.h.

48{ return this->from_peer(std::span(buf, buf_size)); }
virtual size_t from_peer(std::span< const uint8_t > data)=0

◆ received_data() [2/2]

size_t Botan::TLS::Channel::received_data ( std::span< const uint8_t > data)
inlineinherited

Inject TLS traffic received from counterparty

Returns
a hint as to how many more bytes we need to process the current record (this may be 0 if on a record boundary)

Definition at line 46 of file tls_channel.h.

46{ return this->from_peer(data); }

◆ renegotiate()

void Botan::TLS::Client::renegotiate ( bool force_full_renegotiation = false)
overridevirtual

Attempt to renegotiate the session

Parameters
force_full_renegotiationif true, require a full renegotiation, otherwise allow session resumption

Implements Botan::TLS::Channel.

Definition at line 136 of file tls_client.cpp.

136 {
137 m_impl->renegotiate(force_full_renegotiation);
138}

◆ secure_renegotiation_supported()

bool Botan::TLS::Client::secure_renegotiation_supported ( ) const
overridevirtual
Returns
true iff the counterparty supports the secure renegotiation extensions.

Implements Botan::TLS::Channel.

Definition at line 144 of file tls_client.cpp.

144 {
145 return m_impl->secure_renegotiation_supported();
146}

◆ send() [1/3]

void Botan::TLS::Channel::send ( const uint8_t buf[],
size_t buf_size )
inlineinherited

Definition at line 56 of file tls_channel.h.

56{ this->to_peer(std::span(buf, buf_size)); }
virtual void to_peer(std::span< const uint8_t > data)=0

◆ send() [2/3]

void Botan::TLS::Channel::send ( std::span< const uint8_t > data)
inlineinherited

Inject plaintext intended for counterparty Throws an exception if is_active() is false

Definition at line 54 of file tls_channel.h.

54{ this->to_peer(data); }

◆ send() [3/3]

void Botan::TLS::Channel::send ( std::string_view val)
inlineinherited

Inject plaintext intended for counterparty Throws an exception if is_active() is false

Definition at line 62 of file tls_channel.h.

62{ this->send(std::span(cast_char_ptr_to_uint8(val.data()), val.size())); }
void send(std::span< const uint8_t > data)
Definition tls_channel.h:54
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273

References Botan::cast_char_ptr_to_uint8(), and Botan::TLS::Channel::send().

Referenced by Botan::TLS::Channel::send().

◆ send_alert()

void Botan::TLS::Client::send_alert ( const Alert & alert)
overridevirtual

Inject plaintext intended for counterparty Throws an exception if is_active() is false Send a TLS alert message. If the alert is fatal, the internal state (keys, etc) will be reset.

Parameters
alertthe Alert to send

Implements Botan::TLS::Channel.

Definition at line 152 of file tls_client.cpp.

152 {
153 m_impl->send_alert(alert);
154}

◆ send_fatal_alert()

void Botan::TLS::Client::send_fatal_alert ( Alert::Type type)
overridevirtual

Send a fatal alert

Implements Botan::TLS::Channel.

Definition at line 160 of file tls_client.cpp.

160 {
161 m_impl->send_fatal_alert(type);
162}

◆ send_warning_alert()

void Botan::TLS::Client::send_warning_alert ( Alert::Type type)
overridevirtual

Send a warning alert

Implements Botan::TLS::Channel.

Definition at line 156 of file tls_client.cpp.

156 {
157 m_impl->send_warning_alert(type);
158}

◆ timeout_check()

bool Botan::TLS::Client::timeout_check ( )
overridevirtual

Perform a handshake timeout check.

This function does nothing unless the channel represents a DTLS connection and a handshake is actively in progress. In this case it will check the current timeout state and potentially initiate retransmission of handshake packets.

Returns
true if a timeout condition occurred

Implements Botan::TLS::Channel.

Definition at line 168 of file tls_client.cpp.

168 {
169 return m_impl->timeout_check();
170}

◆ to_peer()

void Botan::TLS::Client::to_peer ( std::span< const uint8_t > data)
overridevirtual

Implements Botan::TLS::Channel.

Definition at line 148 of file tls_client.cpp.

148 {
149 m_impl->to_peer(data);
150}

◆ update_traffic_keys()

void Botan::TLS::Client::update_traffic_keys ( bool request_peer_update = false)
overridevirtual

Attempt to update the session's traffic key material Note that this is possible with a TLS 1.3 channel, only.

Parameters
request_peer_updateif true, require a reciprocal key update

Implements Botan::TLS::Channel.

Definition at line 140 of file tls_client.cpp.

140 {
141 m_impl->update_traffic_keys(request_peer_update);
142}

Member Data Documentation

◆ IO_BUF_DEFAULT_SIZE

size_t Botan::TLS::Channel::IO_BUF_DEFAULT_SIZE = 10 * 1024
staticconstexprinherited

Definition at line 32 of file tls_channel.h.

Referenced by Botan::TLS::Channel_Impl_13::expect_downgrade().


The documentation for this class was generated from the following files: