Botan 3.14.1
Crypto and TLS for C&
tls_client.cpp
Go to the documentation of this file.
1/*
2* TLS Client
3* (C) 2004-2011,2012,2015,2016 Jack Lloyd
4* 2016 Matthias Gierlings
5* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
6* 2021 Elektrobit Automotive GmbH
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#include <botan/tls_client.h>
12
13#include <botan/tls_policy.h>
14#include <botan/x509cert.h>
15#include <botan/internal/tls_channel_impl.h>
16
17#if defined(BOTAN_HAS_TLS_12)
18 #include <botan/internal/tls_client_impl_12.h>
19#endif
20
21#if defined(BOTAN_HAS_TLS_13)
22 #include <botan/internal/tls_client_impl_13.h>
23#endif
24
25namespace Botan::TLS {
26
27/*
28* TLS Client Constructor
29*/
30Client::Client(const std::shared_ptr<Callbacks>& callbacks,
31 const std::shared_ptr<Session_Manager>& session_manager,
32 const std::shared_ptr<Credentials_Manager>& creds,
33 const std::shared_ptr<const Policy>& policy,
34 const std::shared_ptr<RandomNumberGenerator>& rng,
36 Protocol_Version offer_version,
37 const std::vector<std::string>& next_protocols,
38 size_t io_buf_sz) {
39 BOTAN_ARG_CHECK(policy != nullptr, "Policy must not be null");
40 BOTAN_ARG_CHECK(policy->acceptable_protocol_version(offer_version),
41 "Policy does not allow to offer requested protocol version");
42
43#if defined(BOTAN_HAS_TLS_13)
44 if(offer_version == Protocol_Version::TLS_V13) {
45 m_impl = Client_Impl_13::create(callbacks, session_manager, creds, policy, rng, std::move(info), next_protocols);
46
47 #if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
48 if(m_impl->expects_downgrade()) {
49 m_impl->set_io_buffer_size(io_buf_sz);
50 }
51
52 if(m_impl->is_downgrading()) {
53 // TLS 1.3 implementation found a resumable TLS 1.2 session and
54 // requested a downgrade right away.
55 downgrade();
56 }
57 #endif
58
59 return;
60 }
61#endif
62
63#if defined(BOTAN_HAS_TLS_12)
64 // Older versions are not implemented, and would otherwise be offered as (D)TLS 1.2
65 if(offer_version == Protocol_Version::TLS_V12 || offer_version == Protocol_Version::DTLS_V12) {
66 m_impl = Client_Impl_12::create(callbacks,
67 session_manager,
68 creds,
69 policy,
70 rng,
71 std::move(info),
73 next_protocols,
74 io_buf_sz);
75 return;
76 }
77#endif
78
79 BOTAN_UNUSED(callbacks, session_manager, creds, policy, rng, info, offer_version, next_protocols, io_buf_sz);
80 throw Not_Implemented("Requested TLS version to be offered is not available in this build");
81}
82
83Client::~Client() = default;
84
85#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
86
87size_t Client::downgrade() {
88 BOTAN_ASSERT_NOMSG(m_impl->is_downgrading());
89
90 auto info = m_impl->extract_downgrade_info();
91 m_impl = Client_Impl_12::create_for_downgrade(*info);
92
93 if(!info->peer_transcript.empty()) {
94 // replay peer data received so far
95 return m_impl->from_peer(info->peer_transcript);
96 } else {
97 // the downgrade happened due to a resumable TLS 1.2 session
98 // before any data was transferred
99 return 0;
100 }
101}
102
103#endif
104
105size_t Client::from_peer(std::span<const uint8_t> data) {
106 auto read = m_impl->from_peer(data);
107
108#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
109 if(m_impl->is_downgrading()) {
110 read = downgrade();
111 }
112#endif
113
114 return read;
115}
116
118 return m_impl->is_handshake_complete();
119}
120
121bool Client::is_active() const {
122 return m_impl->is_active();
123}
124
125std::optional<std::chrono::milliseconds> Client::next_retransmission_timeout() const {
126 return m_impl->next_retransmission_timeout();
127}
128
129bool Client::is_closed() const {
130 return m_impl->is_closed();
131}
132
134 return m_impl->is_closed_for_reading();
135}
136
138 return m_impl->is_closed_for_writing();
139}
140
141std::vector<X509_Certificate> Client::peer_cert_chain() const {
142 return m_impl->peer_cert_chain();
143}
144
145std::shared_ptr<const Public_Key> Client::peer_raw_public_key() const {
146 return m_impl->peer_raw_public_key();
147}
148
149std::optional<std::string> Client::external_psk_identity() const {
150 return m_impl->external_psk_identity();
151}
152
153SymmetricKey Client::key_material_export(std::string_view label, std::string_view context, size_t length) const {
154 return m_impl->key_material_export(label, context, length);
155}
156
157void Client::renegotiate(bool force_full_renegotiation) {
158 m_impl->renegotiate(force_full_renegotiation);
159}
160
161void Client::update_traffic_keys(bool request_peer_update) {
162 m_impl->update_traffic_keys(request_peer_update);
163}
164
166 return m_impl->secure_renegotiation_supported();
167}
168
169void Client::to_peer(std::span<const uint8_t> data) {
170 m_impl->to_peer(data);
171}
172
173void Client::send_alert(const Alert& alert) {
174 m_impl->send_alert(alert);
175}
176
178 m_impl->send_warning_alert(type);
179}
180
182 m_impl->send_fatal_alert(type);
183}
184
186 m_impl->close();
187}
188
190 return m_impl->timeout_check();
191}
192
193std::string Client::application_protocol() const {
194 return m_impl->application_protocol();
195}
196
197} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
AlertType Type
Definition tls_alert.h:73
static std::shared_ptr< Client_Impl_12 > create(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(), TLS_Flavor flavor=TLS_Flavor::TLS, const std::vector< std::string > &next_protocols={}, size_t reserved_io_buffer_size=TLS::Channel::IO_BUF_DEFAULT_SIZE)
static std::shared_ptr< Client_Impl_13 > create(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(), const std::vector< std::string > &next_protocols={})
bool is_closed_for_reading() const override
bool is_handshake_complete() const override
void renegotiate(bool force_full_renegotiation=false) override
void close() override
std::string application_protocol() const override
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
std::optional< std::chrono::milliseconds > next_retransmission_timeout() const override
void send_fatal_alert(Alert::Type type) override
bool timeout_check() override
void send_warning_alert(Alert::Type type) override
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
void send_alert(const Alert &alert) override
size_t from_peer(std::span< const uint8_t > data) override
OctetString SymmetricKey
Definition symkey.h:153